fob_graph/analysis/
cache.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Serialize, Deserialize, Default)]
5pub struct CacheAnalysis {
6    pub hit_rate: f64,
7    pub hits: u64,
8    pub misses: u64,
9    pub total_requests: u64,
10    pub time_saved_ms: u64,
11}
12
13impl CacheAnalysis {
14    pub fn new(hits: u64, misses: u64, time_saved_ms: u64) -> Self {
15        let total = hits + misses;
16        let hit_rate = if total == 0 {
17            0.0
18        } else {
19            hits as f64 / total as f64
20        };
21        Self {
22            hit_rate,
23            hits,
24            misses,
25            total_requests: total,
26            time_saved_ms,
27        }
28    }
29
30    pub fn effectiveness(&self) -> CacheEffectiveness {
31        match self.hit_rate {
32            r if r >= 0.9 => CacheEffectiveness::Excellent,
33            r if r >= 0.7 => CacheEffectiveness::Good,
34            r if r >= 0.5 => CacheEffectiveness::Fair,
35            r if r >= 0.3 => CacheEffectiveness::Poor,
36            _ => CacheEffectiveness::VeryPoor,
37        }
38    }
39
40    pub fn is_healthy(&self) -> bool {
41        self.hit_rate > 0.5
42    }
43}
44
45impl fmt::Display for CacheAnalysis {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        writeln!(f, "Cache Analysis:")?;
48        writeln!(f, "  Hit rate: {:.1}%", self.hit_rate * 100.0)?;
49        writeln!(f, "  Hits: {}", self.hits)?;
50        writeln!(f, "  Misses: {}", self.misses)?;
51        writeln!(f, "  Requests: {}", self.total_requests)?;
52        writeln!(f, "  Time saved: {}ms", self.time_saved_ms)?;
53        writeln!(f, "  Effectiveness: {:?}", self.effectiveness())
54    }
55}
56
57#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
58pub enum CacheEffectiveness {
59    Excellent,
60    Good,
61    Fair,
62    Poor,
63    VeryPoor,
64}