rustf 1.0.0

A simple MVC web framework inspired by Total.js
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/// Cache statistics and monitoring utilities
///
/// Provides comprehensive statistics collection and reporting for all cache types:
/// - Hit/miss rates and trends
/// - Memory usage and capacity utilization
/// - Cache entry lifecycle metrics
/// - Performance impact measurements
/// - Export capabilities for monitoring systems
use super::CacheStats;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Comprehensive cache statistics across all cache types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalCacheStats {
    /// Template cache statistics
    pub template_cache: Option<CacheTypeStats>,
    /// Response cache statistics
    pub response_cache: Option<CacheTypeStats>,
    /// Query cache statistics
    pub query_cache: Option<CacheTypeStats>,
    /// Memory cache statistics (generic)
    pub memory_caches: Vec<NamedCacheStats>,
    /// Overall statistics across all caches
    pub overall: OverallCacheStats,
    /// Statistics collection timestamp
    pub collected_at: u64,
}

/// Statistics for a specific cache type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheTypeStats {
    /// Base cache statistics
    pub base: CacheStats,
    /// Cache type specific metrics
    pub specific_metrics: HashMap<String, f64>,
    /// Recent performance trends
    pub trends: CacheTrends,
}

/// Named cache statistics for identification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamedCacheStats {
    /// Cache identifier
    pub name: String,
    /// Cache statistics
    pub stats: CacheTypeStats,
}

/// Overall statistics across all cache types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OverallCacheStats {
    /// Total hits across all caches
    pub total_hits: u64,
    /// Total misses across all caches
    pub total_misses: u64,
    /// Total entries across all caches
    pub total_entries: u64,
    /// Total maximum capacity across all caches
    pub total_max_entries: u64,
    /// Overall hit rate
    pub overall_hit_rate: f64,
    /// Overall capacity utilization
    pub overall_utilization: f64,
    /// Estimated memory usage in bytes
    pub estimated_memory_bytes: u64,
}

/// Cache performance trends over time
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheTrends {
    /// Hit rate trend (positive = improving, negative = declining)
    pub hit_rate_trend: f64,
    /// Entry count trend
    pub entry_count_trend: f64,
    /// Average response time trend (for applicable caches)
    pub response_time_trend_ms: Option<f64>,
    /// Data points used for trend calculation
    pub data_points: usize,
}

/// Cache statistics collector and manager
pub struct CacheStatsCollector {
    /// Historical data points for trend analysis
    history: Vec<CacheStatsSnapshot>,
    /// Maximum history size
    max_history_size: usize,
    /// Collection interval
    _collection_interval: Duration,
    /// Last collection timestamp
    last_collection: u64,
}

/// Point-in-time cache statistics snapshot
#[derive(Debug, Clone)]
struct CacheStatsSnapshot {
    _timestamp: u64,
    stats: GlobalCacheStats,
}

impl CacheStatsCollector {
    /// Create new cache statistics collector
    pub fn new() -> Self {
        Self {
            history: Vec::new(),
            max_history_size: 100, // Keep last 100 snapshots
            _collection_interval: Duration::from_secs(60), // Collect every minute
            last_collection: 0,
        }
    }

    /// Create collector with custom configuration
    pub fn with_config(max_history_size: usize, _collection_interval: Duration) -> Self {
        Self {
            history: Vec::new(),
            max_history_size,
            _collection_interval,
            last_collection: 0,
        }
    }

    /// Collect current cache statistics
    pub fn collect_stats(&mut self, cache_sources: &CacheStatsSources) -> GlobalCacheStats {
        let now = current_timestamp();

        // Collect individual cache statistics
        let template_cache = cache_sources.template_cache.as_ref().map(|cache| {
            let base_stats = cache();
            CacheTypeStats {
                base: base_stats.clone(),
                specific_metrics: HashMap::new(),
                trends: self.calculate_trends("template", &base_stats),
            }
        });

        let response_cache = cache_sources.response_cache.as_ref().map(|cache| {
            let base_stats = cache();
            let mut specific_metrics = HashMap::new();
            specific_metrics.insert("conditional_requests".to_string(), 0.0); // Placeholder

            CacheTypeStats {
                base: base_stats.clone(),
                specific_metrics,
                trends: self.calculate_trends("response", &base_stats),
            }
        });

        let query_cache = cache_sources.query_cache.as_ref().map(|cache| {
            let base_stats = cache();
            let mut specific_metrics = HashMap::new();
            specific_metrics.insert("cached_tables".to_string(), 0.0); // Placeholder

            CacheTypeStats {
                base: base_stats.clone(),
                specific_metrics,
                trends: self.calculate_trends("query", &base_stats),
            }
        });

        // Collect named memory caches
        let memory_caches: Vec<NamedCacheStats> = cache_sources
            .memory_caches
            .iter()
            .map(|(name, cache_fn)| {
                let base_stats = cache_fn();
                NamedCacheStats {
                    name: name.clone(),
                    stats: CacheTypeStats {
                        base: base_stats.clone(),
                        specific_metrics: HashMap::new(),
                        trends: self.calculate_trends(name, &base_stats),
                    },
                }
            })
            .collect();

        // Calculate overall statistics
        let overall = self.calculate_overall_stats(
            &template_cache,
            &response_cache,
            &query_cache,
            &memory_caches,
        );

        let global_stats = GlobalCacheStats {
            template_cache,
            response_cache,
            query_cache,
            memory_caches,
            overall,
            collected_at: now,
        };

        // Add to history for trend analysis
        self.add_to_history(global_stats.clone());
        self.last_collection = now;

        global_stats
    }

    /// Calculate overall statistics across all cache types
    fn calculate_overall_stats(
        &self,
        template_cache: &Option<CacheTypeStats>,
        response_cache: &Option<CacheTypeStats>,
        query_cache: &Option<CacheTypeStats>,
        memory_caches: &[NamedCacheStats],
    ) -> OverallCacheStats {
        let mut total_hits = 0;
        let mut total_misses = 0;
        let mut total_entries = 0;
        let mut total_max_entries = 0;
        let mut estimated_memory_bytes = 0;

        // Aggregate template cache stats
        if let Some(stats) = template_cache {
            total_hits += stats.base.hits;
            total_misses += stats.base.misses;
            total_entries += stats.base.entries;
            total_max_entries += stats.base.max_entries;
            estimated_memory_bytes += self.estimate_cache_memory(&stats.base);
        }

        // Aggregate response cache stats
        if let Some(stats) = response_cache {
            total_hits += stats.base.hits;
            total_misses += stats.base.misses;
            total_entries += stats.base.entries;
            total_max_entries += stats.base.max_entries;
            estimated_memory_bytes += self.estimate_cache_memory(&stats.base);
        }

        // Aggregate query cache stats
        if let Some(stats) = query_cache {
            total_hits += stats.base.hits;
            total_misses += stats.base.misses;
            total_entries += stats.base.entries;
            total_max_entries += stats.base.max_entries;
            estimated_memory_bytes += self.estimate_cache_memory(&stats.base);
        }

        // Aggregate memory cache stats
        for named_cache in memory_caches {
            total_hits += named_cache.stats.base.hits;
            total_misses += named_cache.stats.base.misses;
            total_entries += named_cache.stats.base.entries;
            total_max_entries += named_cache.stats.base.max_entries;
            estimated_memory_bytes += self.estimate_cache_memory(&named_cache.stats.base);
        }

        let overall_hit_rate = if total_hits + total_misses > 0 {
            total_hits as f64 / (total_hits + total_misses) as f64
        } else {
            0.0
        };

        let overall_utilization = if total_max_entries > 0 {
            total_entries as f64 / total_max_entries as f64
        } else {
            0.0
        };

        OverallCacheStats {
            total_hits,
            total_misses,
            total_entries,
            total_max_entries,
            overall_hit_rate,
            overall_utilization,
            estimated_memory_bytes,
        }
    }

    /// Estimate memory usage for a cache (rough approximation)
    fn estimate_cache_memory(&self, stats: &CacheStats) -> u64 {
        // Very rough estimation: assume average 1KB per entry
        // In production, this would be more sophisticated
        stats.entries * 1024
    }

    /// Calculate performance trends for a specific cache
    fn calculate_trends(&self, cache_name: &str, _current_stats: &CacheStats) -> CacheTrends {
        if self.history.len() < 2 {
            return CacheTrends {
                hit_rate_trend: 0.0,
                entry_count_trend: 0.0,
                response_time_trend_ms: None,
                data_points: 0,
            };
        }

        // Get historical hit rates for this cache type
        let historical_hit_rates: Vec<f64> = self
            .history
            .iter()
            .filter_map(|snapshot| self.extract_cache_hit_rate(&snapshot.stats, cache_name))
            .collect();

        let historical_entry_counts: Vec<u64> = self
            .history
            .iter()
            .filter_map(|snapshot| self.extract_cache_entry_count(&snapshot.stats, cache_name))
            .collect();

        let hit_rate_trend = self.calculate_linear_trend(&historical_hit_rates);
        let entry_count_trend = self.calculate_linear_trend(
            &historical_entry_counts
                .iter()
                .map(|&x| x as f64)
                .collect::<Vec<_>>(),
        );

        CacheTrends {
            hit_rate_trend,
            entry_count_trend,
            response_time_trend_ms: None, // NOTE: Response time tracking planned for future release
            data_points: historical_hit_rates.len(),
        }
    }

    /// Extract hit rate for specific cache from global stats
    fn extract_cache_hit_rate(
        &self,
        global_stats: &GlobalCacheStats,
        cache_name: &str,
    ) -> Option<f64> {
        match cache_name {
            "template" => global_stats
                .template_cache
                .as_ref()
                .map(|s| s.base.hit_rate()),
            "response" => global_stats
                .response_cache
                .as_ref()
                .map(|s| s.base.hit_rate()),
            "query" => global_stats.query_cache.as_ref().map(|s| s.base.hit_rate()),
            _ => {
                // Look in memory caches
                global_stats
                    .memory_caches
                    .iter()
                    .find(|mc| mc.name == cache_name)
                    .map(|mc| mc.stats.base.hit_rate())
            }
        }
    }

    /// Extract entry count for specific cache from global stats
    fn extract_cache_entry_count(
        &self,
        global_stats: &GlobalCacheStats,
        cache_name: &str,
    ) -> Option<u64> {
        match cache_name {
            "template" => global_stats.template_cache.as_ref().map(|s| s.base.entries),
            "response" => global_stats.response_cache.as_ref().map(|s| s.base.entries),
            "query" => global_stats.query_cache.as_ref().map(|s| s.base.entries),
            _ => {
                // Look in memory caches
                global_stats
                    .memory_caches
                    .iter()
                    .find(|mc| mc.name == cache_name)
                    .map(|mc| mc.stats.base.entries)
            }
        }
    }

    /// Calculate linear trend (slope) for a series of values
    fn calculate_linear_trend(&self, values: &[f64]) -> f64 {
        if values.len() < 2 {
            return 0.0;
        }

        let n = values.len() as f64;
        let x_sum: f64 = (0..values.len()).map(|i| i as f64).sum();
        let y_sum: f64 = values.iter().sum();
        let xy_sum: f64 = values.iter().enumerate().map(|(i, &y)| i as f64 * y).sum();
        let x_squared_sum: f64 = (0..values.len()).map(|i| (i as f64).powi(2)).sum();

        let denominator = n * x_squared_sum - x_sum.powi(2);
        if denominator.abs() < f64::EPSILON {
            return 0.0;
        }

        (n * xy_sum - x_sum * y_sum) / denominator
    }

    /// Add statistics snapshot to history
    fn add_to_history(&mut self, stats: GlobalCacheStats) {
        let snapshot = CacheStatsSnapshot {
            _timestamp: stats.collected_at,
            stats,
        };

        self.history.push(snapshot);

        // Trim history if it exceeds maximum size
        if self.history.len() > self.max_history_size {
            self.history.remove(0);
        }
    }

    /// Export statistics in various formats
    pub fn export_stats(&self, format: ExportFormat) -> Result<String, Box<dyn std::error::Error>> {
        if self.history.is_empty() {
            return Ok(String::new());
        }

        let latest_stats = &self.history.last().unwrap().stats;

        match format {
            ExportFormat::Json => Ok(serde_json::to_string_pretty(latest_stats)?),
            ExportFormat::Prometheus => Ok(self.to_prometheus_format(latest_stats)),
            ExportFormat::Csv => Ok(self.to_csv_format(latest_stats)),
        }
    }

    /// Convert to Prometheus metrics format
    fn to_prometheus_format(&self, stats: &GlobalCacheStats) -> String {
        let mut output = String::new();

        // Overall metrics
        output.push_str("# HELP rustf_cache_hits_total Total cache hits across all caches\n");
        output.push_str("# TYPE rustf_cache_hits_total counter\n");
        output.push_str(&format!(
            "rustf_cache_hits_total {}\n",
            stats.overall.total_hits
        ));

        output.push_str("# HELP rustf_cache_misses_total Total cache misses across all caches\n");
        output.push_str("# TYPE rustf_cache_misses_total counter\n");
        output.push_str(&format!(
            "rustf_cache_misses_total {}\n",
            stats.overall.total_misses
        ));

        output.push_str("# HELP rustf_cache_hit_rate Overall cache hit rate\n");
        output.push_str("# TYPE rustf_cache_hit_rate gauge\n");
        output.push_str(&format!(
            "rustf_cache_hit_rate {:.4}\n",
            stats.overall.overall_hit_rate
        ));

        output.push_str("# HELP rustf_cache_utilization Overall cache capacity utilization\n");
        output.push_str("# TYPE rustf_cache_utilization gauge\n");
        output.push_str(&format!(
            "rustf_cache_utilization {:.4}\n",
            stats.overall.overall_utilization
        ));

        // Per-cache type metrics
        if let Some(template_stats) = &stats.template_cache {
            self.add_cache_metrics(&mut output, "template", &template_stats.base);
        }

        if let Some(response_stats) = &stats.response_cache {
            self.add_cache_metrics(&mut output, "response", &response_stats.base);
        }

        if let Some(query_stats) = &stats.query_cache {
            self.add_cache_metrics(&mut output, "query", &query_stats.base);
        }

        output
    }

    /// Add cache-specific metrics to Prometheus output
    fn add_cache_metrics(&self, output: &mut String, cache_type: &str, stats: &CacheStats) {
        output.push_str(&format!(
            "rustf_cache_hits_total{{cache_type=\"{}\"}} {}\n",
            cache_type, stats.hits
        ));
        output.push_str(&format!(
            "rustf_cache_misses_total{{cache_type=\"{}\"}} {}\n",
            cache_type, stats.misses
        ));
        output.push_str(&format!(
            "rustf_cache_entries{{cache_type=\"{}\"}} {}\n",
            cache_type, stats.entries
        ));
        output.push_str(&format!(
            "rustf_cache_hit_rate{{cache_type=\"{}\"}} {:.4}\n",
            cache_type,
            stats.hit_rate()
        ));
        output.push_str(&format!(
            "rustf_cache_utilization{{cache_type=\"{}\"}} {:.4}\n",
            cache_type,
            stats.utilization()
        ));
    }

    /// Convert to CSV format
    fn to_csv_format(&self, stats: &GlobalCacheStats) -> String {
        let mut output = String::new();
        output.push_str(
            "timestamp,cache_type,hits,misses,entries,max_entries,hit_rate,utilization\n",
        );

        let timestamp = stats.collected_at;

        if let Some(template_stats) = &stats.template_cache {
            output.push_str(&format!(
                "{},template,{},{},{},{},{:.4},{:.4}\n",
                timestamp,
                template_stats.base.hits,
                template_stats.base.misses,
                template_stats.base.entries,
                template_stats.base.max_entries,
                template_stats.base.hit_rate(),
                template_stats.base.utilization()
            ));
        }

        if let Some(response_stats) = &stats.response_cache {
            output.push_str(&format!(
                "{},response,{},{},{},{},{:.4},{:.4}\n",
                timestamp,
                response_stats.base.hits,
                response_stats.base.misses,
                response_stats.base.entries,
                response_stats.base.max_entries,
                response_stats.base.hit_rate(),
                response_stats.base.utilization()
            ));
        }

        if let Some(query_stats) = &stats.query_cache {
            output.push_str(&format!(
                "{},query,{},{},{},{},{:.4},{:.4}\n",
                timestamp,
                query_stats.base.hits,
                query_stats.base.misses,
                query_stats.base.entries,
                query_stats.base.max_entries,
                query_stats.base.hit_rate(),
                query_stats.base.utilization()
            ));
        }

        output
    }
}

/// Export format options
#[derive(Debug, Clone)]
pub enum ExportFormat {
    Json,
    Prometheus,
    Csv,
}

/// Cache statistics sources for collection
pub struct CacheStatsSources {
    /// Function to get template cache stats
    pub template_cache: Option<Box<dyn Fn() -> CacheStats>>,
    /// Function to get response cache stats
    pub response_cache: Option<Box<dyn Fn() -> CacheStats>>,
    /// Function to get query cache stats
    pub query_cache: Option<Box<dyn Fn() -> CacheStats>>,
    /// Named memory caches with their stats functions
    pub memory_caches: Vec<(String, Box<dyn Fn() -> CacheStats>)>,
}

/// Get current timestamp
fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

impl Default for CacheStatsCollector {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_linear_trend_calculation() {
        let collector = CacheStatsCollector::new();

        // Test increasing trend
        let increasing = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let trend = collector.calculate_linear_trend(&increasing);
        assert!(trend > 0.0);

        // Test decreasing trend
        let decreasing = vec![5.0, 4.0, 3.0, 2.0, 1.0];
        let trend = collector.calculate_linear_trend(&decreasing);
        assert!(trend < 0.0);

        // Test flat trend
        let flat = vec![3.0, 3.0, 3.0, 3.0, 3.0];
        let trend = collector.calculate_linear_trend(&flat);
        assert_eq!(trend, 0.0);
    }

    #[test]
    fn test_overall_stats_calculation() {
        let collector = CacheStatsCollector::new();

        let template_stats = Some(CacheTypeStats {
            base: CacheStats {
                hits: 100,
                misses: 20,
                entries: 50,
                max_entries: 100,
                evictions: 0,
                expired_cleanups: 0,
                total_access_count: 120,
                average_entry_age: 60.0,
            },
            specific_metrics: HashMap::new(),
            trends: CacheTrends {
                hit_rate_trend: 0.0,
                entry_count_trend: 0.0,
                response_time_trend_ms: None,
                data_points: 0,
            },
        });

        let overall = collector.calculate_overall_stats(&template_stats, &None, &None, &[]);

        assert_eq!(overall.total_hits, 100);
        assert_eq!(overall.total_misses, 20);
        assert_eq!(overall.total_entries, 50);
        assert!((overall.overall_hit_rate - 100.0 / 120.0).abs() < f64::EPSILON);
    }
}