alec 1.3.5

Adaptive Lazy Evolving Compression - Smart codec for IoT sensor data with 90% compression ratio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Exemple — Refactoring

## Contexte

Cet exemple montre comment refactoriser du code ALEC en préservant le comportement existant.

**Cible** : Simplifier la fonction `classify()` qui est devenue trop complexe.

---

## Étape 1 : Demande de refactoring

```
REFACTORING: Simplifier la fonction classify()

Zone : src/classifier.rs, lignes 45-145
Type : Simplification + Extraction

Problème actuel : 
- Fonction de 100 lignes
- Complexité cyclomatique de 18
- 7 niveaux d'indentation max
- Difficile à tester unitairement

Objectif :
- Maximum 25 lignes par fonction
- Complexité < 8
- Maximum 3 niveaux d'indentation
- Fonctions auxiliaires testables individuellement

Invariants :
- [x] API publique inchangée
- [x] Même classification pour mêmes entrées
```

---

## Étape 2 : Code original (avant)

```rust
// src/classifier.rs - AVANT REFACTORING

impl Classifier {
    pub fn classify(&self, data: &RawData, context: &Context) -> Classification {
        let prediction = context.predict(data.source_id);
        
        if let Some(pred) = prediction {
            let delta = (data.value - pred.value).abs();
            let relative_delta = if pred.value != 0.0 {
                delta / pred.value.abs()
            } else {
                delta
            };
            
            // Vérifier les seuils critiques
            if let Some(critical_thresholds) = self.config.critical_thresholds.get(&data.source_id) {
                if data.value < critical_thresholds.min {
                    return Classification {
                        priority: Priority::P1Critical,
                        reason: ClassificationReason::ThresholdExceeded {
                            threshold: critical_thresholds.min,
                            actual: data.value,
                        },
                        delta: relative_delta,
                        confidence: 1.0,
                    };
                }
                if data.value > critical_thresholds.max {
                    return Classification {
                        priority: Priority::P1Critical,
                        reason: ClassificationReason::ThresholdExceeded {
                            threshold: critical_thresholds.max,
                            actual: data.value,
                        },
                        delta: relative_delta,
                        confidence: 1.0,
                    };
                }
            }
            
            // Vérifier les anomalies statistiques
            if relative_delta > self.config.anomaly_threshold {
                if relative_delta > self.config.critical_anomaly_threshold {
                    return Classification {
                        priority: Priority::P1Critical,
                        reason: ClassificationReason::AnomalyDetected {
                            anomaly_type: AnomalyType::ExtremeDeviation,
                        },
                        delta: relative_delta,
                        confidence: pred.confidence,
                    };
                } else {
                    return Classification {
                        priority: Priority::P2Important,
                        reason: ClassificationReason::AnomalyDetected {
                            anomaly_type: AnomalyType::SignificantDeviation,
                        },
                        delta: relative_delta,
                        confidence: pred.confidence,
                    };
                }
            }
            
            // Vérifier si c'est une transmission programmée
            if self.is_scheduled_transmission(data.timestamp) {
                return Classification {
                    priority: Priority::P3Normal,
                    reason: ClassificationReason::ScheduledTransmission,
                    delta: relative_delta,
                    confidence: pred.confidence,
                };
            }
            
            // Valeur normale, peut être différée
            if relative_delta < self.config.minimum_delta_threshold {
                return Classification {
                    priority: Priority::P5Disposable,
                    reason: ClassificationReason::BelowMinimumDelta,
                    delta: relative_delta,
                    confidence: pred.confidence,
                };
            }
            
            Classification {
                priority: Priority::P4Deferred,
                reason: ClassificationReason::NormalValue,
                delta: relative_delta,
                confidence: pred.confidence,
            }
        } else {
            // Pas de prédiction disponible, envoyer comme normal
            Classification {
                priority: Priority::P3Normal,
                reason: ClassificationReason::NoPrediction,
                delta: 0.0,
                confidence: 0.0,
            }
        }
    }
}
```

---

## Étape 3 : Diagnostic

### Code smells identifiés

1. **Fonction trop longue** (100 lignes)
2. **Niveaux d'imbrication excessifs** (7 niveaux)
3. **Logique mélangée** (seuils, anomalies, scheduling)
4. **Répétition** (construction de Classification)
5. **Difficile à tester** (pas de points d'entrée intermédiaires)

### Métriques

| Métrique | Avant | Objectif |
|----------|-------|----------|
| Lignes | 100 | < 25 par fonction |
| Complexité cyclomatique | 18 | < 8 |
| Niveaux d'indentation | 7 | < 4 |
| Fonctions auxiliaires | 0 | 4-5 |

---

## Étape 4 : Plan de refactoring

### Étape 4.1 : Extraire le calcul du delta

```rust
fn calculate_delta(value: f64, prediction: &Prediction) -> DeltaInfo {
    let absolute = (value - prediction.value).abs();
    let relative = if prediction.value != 0.0 {
        absolute / prediction.value.abs()
    } else {
        absolute
    };
    DeltaInfo { absolute, relative }
}
```

### Étape 4.2 : Extraire la vérification des seuils critiques

```rust
fn check_critical_thresholds(
    &self,
    value: f64,
    source_id: u32,
    delta: &DeltaInfo,
) -> Option<Classification> {
    // ...
}
```

### Étape 4.3 : Extraire la détection d'anomalies

```rust
fn check_anomaly(
    &self,
    delta: &DeltaInfo,
    confidence: f32,
) -> Option<Classification> {
    // ...
}
```

### Étape 4.4 : Extraire la classification normale

```rust
fn classify_normal(
    &self,
    timestamp: u64,
    delta: &DeltaInfo,
    confidence: f32,
) -> Classification {
    // ...
}
```

---

## Étape 5 : Code refactorisé (après)

```rust
// src/classifier.rs - APRÈS REFACTORING

/// Informations sur l'écart entre valeur et prédiction
struct DeltaInfo {
    absolute: f64,
    relative: f64,
}

impl Classifier {
    /// Point d'entrée principal - délègue aux fonctions spécialisées
    pub fn classify(&self, data: &RawData, context: &Context) -> Classification {
        match context.predict(data.source_id) {
            Some(prediction) => self.classify_with_prediction(data, &prediction),
            None => Classification::no_prediction(),
        }
    }
    
    fn classify_with_prediction(
        &self,
        data: &RawData,
        prediction: &Prediction,
    ) -> Classification {
        let delta = Self::calculate_delta(data.value, prediction);
        
        // Chaîne de responsabilité : première classification qui matche
        self.check_critical_thresholds(data.value, data.source_id, &delta)
            .or_else(|| self.check_anomaly(&delta, prediction.confidence))
            .unwrap_or_else(|| self.classify_normal(data.timestamp, &delta, prediction.confidence))
    }
    
    /// Calcule l'écart absolu et relatif
    fn calculate_delta(value: f64, prediction: &Prediction) -> DeltaInfo {
        let absolute = (value - prediction.value).abs();
        let relative = if prediction.value.abs() > f64::EPSILON {
            absolute / prediction.value.abs()
        } else {
            absolute
        };
        DeltaInfo { absolute, relative }
    }
    
    /// Vérifie les seuils critiques (min/max absolus)
    fn check_critical_thresholds(
        &self,
        value: f64,
        source_id: u32,
        delta: &DeltaInfo,
    ) -> Option<Classification> {
        let thresholds = self.config.critical_thresholds.get(&source_id)?;
        
        let violated_threshold = if value < thresholds.min {
            Some(thresholds.min)
        } else if value > thresholds.max {
            Some(thresholds.max)
        } else {
            None
        }?;
        
        Some(Classification::critical_threshold(violated_threshold, value, delta.relative))
    }
    
    /// Détecte les anomalies statistiques
    fn check_anomaly(&self, delta: &DeltaInfo, confidence: f32) -> Option<Classification> {
        if delta.relative <= self.config.anomaly_threshold {
            return None;
        }
        
        let (priority, anomaly_type) = if delta.relative > self.config.critical_anomaly_threshold {
            (Priority::P1Critical, AnomalyType::ExtremeDeviation)
        } else {
            (Priority::P2Important, AnomalyType::SignificantDeviation)
        };
        
        Some(Classification::anomaly(priority, anomaly_type, delta.relative, confidence))
    }
    
    /// Classifie une valeur normale (pas d'alerte)
    fn classify_normal(&self, timestamp: u64, delta: &DeltaInfo, confidence: f32) -> Classification {
        if self.is_scheduled_transmission(timestamp) {
            Classification::scheduled(delta.relative, confidence)
        } else if delta.relative < self.config.minimum_delta_threshold {
            Classification::disposable(delta.relative, confidence)
        } else {
            Classification::deferred(delta.relative, confidence)
        }
    }
}

// Constructeurs pour Classification (builder pattern simplifié)
impl Classification {
    fn no_prediction() -> Self {
        Self {
            priority: Priority::P3Normal,
            reason: ClassificationReason::NoPrediction,
            delta: 0.0,
            confidence: 0.0,
        }
    }
    
    fn critical_threshold(threshold: f64, actual: f64, delta: f64) -> Self {
        Self {
            priority: Priority::P1Critical,
            reason: ClassificationReason::ThresholdExceeded { threshold, actual },
            delta,
            confidence: 1.0,
        }
    }
    
    fn anomaly(priority: Priority, anomaly_type: AnomalyType, delta: f64, confidence: f32) -> Self {
        Self {
            priority,
            reason: ClassificationReason::AnomalyDetected { anomaly_type },
            delta,
            confidence,
        }
    }
    
    fn scheduled(delta: f64, confidence: f32) -> Self {
        Self {
            priority: Priority::P3Normal,
            reason: ClassificationReason::ScheduledTransmission,
            delta,
            confidence,
        }
    }
    
    fn disposable(delta: f64, confidence: f32) -> Self {
        Self {
            priority: Priority::P5Disposable,
            reason: ClassificationReason::BelowMinimumDelta,
            delta,
            confidence,
        }
    }
    
    fn deferred(delta: f64, confidence: f32) -> Self {
        Self {
            priority: Priority::P4Deferred,
            reason: ClassificationReason::NormalValue,
            delta,
            confidence,
        }
    }
}
```

---

## Étape 6 : Validation

### Tests existants

```bash
$ cargo test classifier
running 12 tests
test classifier::test_classify_no_prediction ... ok
test classifier::test_classify_critical_min ... ok
test classifier::test_classify_critical_max ... ok
test classifier::test_classify_extreme_anomaly ... ok
test classifier::test_classify_significant_anomaly ... ok
test classifier::test_classify_scheduled ... ok
test classifier::test_classify_disposable ... ok
test classifier::test_classify_deferred ... ok
test classifier::test_classify_edge_at_threshold ... ok
test classifier::test_classify_zero_prediction ... ok
test classifier::test_classify_negative_values ... ok
test classifier::test_classify_integration ... ok

test result: ok. 12 passed; 0 failed
```

### Nouveaux tests unitaires (pour les fonctions extraites)

```rust
#[test]
fn test_calculate_delta_normal() {
    let prediction = Prediction { value: 100.0, confidence: 0.9 };
    let delta = Classifier::calculate_delta(110.0, &prediction);
    
    assert!((delta.absolute - 10.0).abs() < 0.001);
    assert!((delta.relative - 0.1).abs() < 0.001);
}

#[test]
fn test_calculate_delta_zero_prediction() {
    let prediction = Prediction { value: 0.0, confidence: 0.9 };
    let delta = Classifier::calculate_delta(5.0, &prediction);
    
    assert!((delta.absolute - 5.0).abs() < 0.001);
    assert!((delta.relative - 5.0).abs() < 0.001);  // Fallback to absolute
}

#[test]
fn test_check_critical_thresholds_below_min() {
    let classifier = Classifier::with_thresholds(0.0, 100.0);
    let delta = DeltaInfo { absolute: 5.0, relative: 0.1 };
    
    let result = classifier.check_critical_thresholds(-5.0, 1, &delta);
    
    assert!(result.is_some());
    assert_eq!(result.unwrap().priority, Priority::P1Critical);
}
```

### Métriques après refactoring

| Métrique | Avant | Après | Objectif |
|----------|-------|-------|----------|
| Lignes (fonction principale) | 100 | 8 |< 25 |
| Complexité cyclomatique | 18 | 6 |< 8 |
| Niveaux d'indentation max | 7 | 2 |< 4 |
| Fonctions | 1 | 6 | ✅ Testables |

---

## Leçons apprises

1. **Extraire tôt** : Dès qu'une fonction dépasse 30 lignes, envisager l'extraction
2. **Nommer clairement** : `check_critical_thresholds` est plus clair que le code inline
3. **Chaîne de responsabilité** : `or_else` simplifie la logique de fallback
4. **Builder pattern** : Les constructeurs nommés (`Classification::anomaly`) documentent l'intention
5. **Tests préservés** : Le refactoring n'a cassé aucun test existant