Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/api/handlers/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn performance_summary(
};

let latency = start.elapsed().as_millis() as u64;
let circuit_breaker_state = if verifier_available { "closed" } else { "open" };
let circuit_breaker_state = derive_circuit_breaker_state(verifier_reachable);
let attestation_rate = agent_count as f64 / 30.0;
let utilization = compute_utilization_pct(agent_count as u64, 1000);

Expand Down Expand Up @@ -144,6 +144,14 @@ pub async fn capacity_planning(
}))))
}

pub(crate) fn derive_circuit_breaker_state(reachable: bool) -> &'static str {
if reachable {
"closed"
} else {
"open"
}
}

pub(crate) fn compute_utilization_pct(active: u64, capacity: u64) -> f64 {
if capacity > 0 {
(active as f64 / capacity as f64) * 100.0
Expand All @@ -156,6 +164,16 @@ pub(crate) fn compute_utilization_pct(active: u64, capacity: u64) -> f64 {
mod tests {
use super::*;

#[test]
fn circuit_breaker_closed_when_reachable() {
assert_eq!(derive_circuit_breaker_state(true), "closed");
}

#[test]
fn circuit_breaker_open_when_unreachable() {
assert_eq!(derive_circuit_breaker_state(false), "open");
}

#[test]
fn utilization_zero_capacity() {
assert_eq!(compute_utilization_pct(10, 0), 0.0);
Expand Down