|
| 1 | +/** |
| 2 | + * General Optimization System |
| 3 | + * Calculated opt and general opt light situation for all subpackages |
| 4 | + * |
| 5 | + * @version 1.0.0 |
| 6 | + * @author xaoex |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * OptimizationCalculator - Calculates optimization levels |
| 11 | + */ |
| 12 | +class OptimizationCalculator { |
| 13 | + constructor() { |
| 14 | + this.maxLevel = 100; |
| 15 | + this.lightLevel = 50; |
| 16 | + this.standardLevel = 75; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Calculate optimization level based on input |
| 21 | + */ |
| 22 | + calculateOptLevel(data) { |
| 23 | + if (!data || typeof data !== 'object') { |
| 24 | + return this.lightLevel; |
| 25 | + } |
| 26 | + |
| 27 | + let score = 0; |
| 28 | + let factors = 0; |
| 29 | + |
| 30 | + // Factor 1: Complexity |
| 31 | + if (data.complexity) { |
| 32 | + score += data.complexity * 20; |
| 33 | + factors++; |
| 34 | + } |
| 35 | + |
| 36 | + // Factor 2: Priority |
| 37 | + if (data.priority) { |
| 38 | + const priorityMap = { low: 25, medium: 50, high: 75, max: 100 }; |
| 39 | + score += priorityMap[data.priority] || 50; |
| 40 | + factors++; |
| 41 | + } |
| 42 | + |
| 43 | + // Factor 3: Size |
| 44 | + if (data.size) { |
| 45 | + score += Math.min(data.size / 10, 30); |
| 46 | + factors++; |
| 47 | + } |
| 48 | + |
| 49 | + if (factors === 0) { |
| 50 | + return this.standardLevel; |
| 51 | + } |
| 52 | + |
| 53 | + const calculated = Math.min(score / factors, this.maxLevel); |
| 54 | + return Math.round(calculated); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get maxopt (100%) |
| 59 | + */ |
| 60 | + maxopt() { |
| 61 | + return this.maxLevel; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get light optimization (50%) |
| 66 | + */ |
| 67 | + lightOpt() { |
| 68 | + return this.lightLevel; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get standard optimization (75%) |
| 73 | + */ |
| 74 | + standardOpt() { |
| 75 | + return this.standardLevel; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Apply optimization to data |
| 80 | + */ |
| 81 | + applyOptimization(data, level) { |
| 82 | + if (!data) return data; |
| 83 | + |
| 84 | + const optimized = { ...data }; |
| 85 | + optimized._optimized = true; |
| 86 | + optimized._optLevel = level; |
| 87 | + optimized._optTimestamp = new Date().toISOString(); |
| 88 | + |
| 89 | + // Apply optimization transformations based on level |
| 90 | + if (level >= 75) { |
| 91 | + optimized._maxopt = true; |
| 92 | + } else if (level >= 50) { |
| 93 | + optimized._lightopt = true; |
| 94 | + } |
| 95 | + |
| 96 | + return optimized; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * GeneralOptSituation - General optimization situation handler |
| 102 | + */ |
| 103 | +class GeneralOptSituation { |
| 104 | + constructor(options = {}) { |
| 105 | + this.calculator = new OptimizationCalculator(); |
| 106 | + this.situations = new Map(); |
| 107 | + this.verbose = options.verbose || false; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Create an optimization situation |
| 112 | + */ |
| 113 | + createSituation(name, data) { |
| 114 | + const optLevel = this.calculator.calculateOptLevel(data); |
| 115 | + const situation = { |
| 116 | + name, |
| 117 | + data, |
| 118 | + optLevel, |
| 119 | + created: new Date().toISOString(), |
| 120 | + status: 'created' |
| 121 | + }; |
| 122 | + |
| 123 | + this.situations.set(name, situation); |
| 124 | + |
| 125 | + if (this.verbose) { |
| 126 | + console.log(`[OptSituation] Created: ${name} (level: ${optLevel}%)`); |
| 127 | + } |
| 128 | + |
| 129 | + return situation; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Optimize a situation |
| 134 | + */ |
| 135 | + optimizeSituation(name) { |
| 136 | + const situation = this.situations.get(name); |
| 137 | + if (!situation) { |
| 138 | + throw new Error(`Situation not found: ${name}`); |
| 139 | + } |
| 140 | + |
| 141 | + const optimized = this.calculator.applyOptimization( |
| 142 | + situation.data, |
| 143 | + situation.optLevel |
| 144 | + ); |
| 145 | + |
| 146 | + situation.optimizedData = optimized; |
| 147 | + situation.status = 'optimized'; |
| 148 | + situation.optimizedAt = new Date().toISOString(); |
| 149 | + |
| 150 | + if (this.verbose) { |
| 151 | + console.log(`[OptSituation] Optimized: ${name} (${situation.optLevel}%)`); |
| 152 | + } |
| 153 | + |
| 154 | + return situation; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get maxopt situation (100%) |
| 159 | + */ |
| 160 | + maxoptSituation(name, data) { |
| 161 | + const situation = this.createSituation(name, data); |
| 162 | + situation.optLevel = this.calculator.maxopt(); |
| 163 | + return this.optimizeSituation(name); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get light opt situation (50%) |
| 168 | + */ |
| 169 | + lightOptSituation(name, data) { |
| 170 | + const situation = this.createSituation(name, data); |
| 171 | + situation.optLevel = this.calculator.lightOpt(); |
| 172 | + return this.optimizeSituation(name); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get all situations |
| 177 | + */ |
| 178 | + getSituations() { |
| 179 | + return Array.from(this.situations.values()); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Get optimization statistics |
| 184 | + */ |
| 185 | + getStats() { |
| 186 | + const situations = this.getSituations(); |
| 187 | + const total = situations.length; |
| 188 | + const optimized = situations.filter(s => s.status === 'optimized').length; |
| 189 | + const avgLevel = situations.reduce((sum, s) => sum + s.optLevel, 0) / total || 0; |
| 190 | + |
| 191 | + return { |
| 192 | + total, |
| 193 | + optimized, |
| 194 | + pending: total - optimized, |
| 195 | + avgLevel: Math.round(avgLevel) |
| 196 | + }; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// Export |
| 201 | +module.exports = { |
| 202 | + OptimizationCalculator, |
| 203 | + GeneralOptSituation |
| 204 | +}; |
0 commit comments