erpc_analysis/models/
metrics.rs

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
use std::collections::HashMap;

/// Represents metrics for an individual node (relay) in the graph
#[derive(Debug, Clone, PartialEq)]
pub struct NodeMetrics {
    pub fingerprint: String,
    pub in_degree: i64,
    pub out_degree: i64,
    pub total_degree: i64,
}

/// Represents basic metrics for a graph or a GDS graph projection,
/// such as node and relationship counts.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct GraphMetrics {
    pub node_count: Option<i64>,
    pub relationship_count: Option<i64>,
    // degree -> count
    pub degree_distribution: Option<HashMap<i64, i64>>,
    pub average_degree: Option<f64>,
    pub max_degree: Option<i64>,
    pub min_degree: Option<i64>,
}

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

    #[test]
    fn test_node_metrics_creation() {
        let node = NodeMetrics {
            fingerprint: "RELAY001".to_string(),
            in_degree: 5,
            out_degree: 3,
            total_degree: 8,
        };

        assert_eq!(node.fingerprint, "RELAY001");
        assert_eq!(node.in_degree, 5);
        assert_eq!(node.out_degree, 3);
        assert_eq!(node.total_degree, 8);
    }

    #[test]
    fn test_node_metrics_total_degree_calculation() {
        let node = NodeMetrics {
            fingerprint: "TEST".to_string(),
            in_degree: 10,
            out_degree: 15,
            total_degree: 25,
        };

        assert_eq!(node.total_degree, 25);
        assert_eq!(node.in_degree + node.out_degree, node.total_degree);
    }

    #[test]
    fn test_graph_metrics_default() {
        let metrics = GraphMetrics::default();

        assert!(metrics.node_count.is_none());
        assert!(metrics.relationship_count.is_none());
        assert!(metrics.degree_distribution.is_none());
        assert!(metrics.average_degree.is_none());
        assert!(metrics.max_degree.is_none());
        assert!(metrics.min_degree.is_none());
        assert!(metrics.node_count.is_none());
        assert!(metrics.relationship_count.is_none());
    }

    #[test]
    fn test_graph_metrics_fields_access() {
        let metrics = GraphMetrics {
            node_count: Some(10),
            relationship_count: Some(25),
            degree_distribution: Some(HashMap::new()),
            average_degree: Some(2.5),
            max_degree: Some(5),
            min_degree: Some(1),
        };

        assert_eq!(metrics.node_count.unwrap_or(0), 10);
        assert_eq!(metrics.relationship_count.unwrap_or(0), 25);
        assert_eq!(metrics.average_degree.unwrap_or(0.0), 2.5);
        assert_eq!(metrics.max_degree.unwrap_or(0), 5);
        assert_eq!(metrics.min_degree.unwrap_or(0), 1);
    }

    #[test]
    fn test_degree_distribution() {
        let mut degree_dist = HashMap::new();
        degree_dist.insert(1, 5); // 5 nodes with degree 1
        degree_dist.insert(2, 3); // 3 nodes with degree 2
        degree_dist.insert(5, 1); // 1 node with degree 5

        let metrics = GraphMetrics {
            node_count: Some(9),          // 5 + 3 + 1
            relationship_count: Some(16), // (1*5 + 2*3 + 5*1) = 16
            degree_distribution: Some(degree_dist.clone()),
            average_degree: Some(16.0 / 9.0),
            max_degree: Some(5),
            min_degree: Some(1),
        };

        assert!(metrics.node_count.is_some());
        assert!(metrics.relationship_count.is_some());
        assert_eq!(metrics.degree_distribution.unwrap(), degree_dist);
    }

    #[test]
    fn test_node_metrics_equality() {
        let node1 = NodeMetrics {
            fingerprint: "RELAY001".to_string(),
            in_degree: 5,
            out_degree: 3,
            total_degree: 8,
        };
        let node2 = NodeMetrics {
            fingerprint: "RELAY001".to_string(),
            in_degree: 5,
            out_degree: 3,
            total_degree: 8,
        };
        let node3 = NodeMetrics {
            fingerprint: "RELAY002".to_string(),
            in_degree: 5,
            out_degree: 3,
            total_degree: 8,
        };

        assert_eq!(node1, node2);
        assert_ne!(node1, node3);
    }

    #[test]
    fn test_graph_metrics_equality() {
        let metrics1 = GraphMetrics {
            node_count: Some(10),
            relationship_count: Some(20),
            degree_distribution: None,
            average_degree: Some(2.0),
            max_degree: Some(5),
            min_degree: Some(1),
        };

        let metrics2 = GraphMetrics {
            node_count: Some(10),
            relationship_count: Some(20),
            degree_distribution: None,
            average_degree: Some(2.0),
            max_degree: Some(5),
            min_degree: Some(1),
        };

        assert_eq!(metrics1, metrics2);
    }
}