bevy_fleet/
metrics.rs

1use bevy::diagnostic::DiagnosticsStore;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Metric data extracted from Bevy diagnostics
6#[derive(Clone, Debug, Serialize, Deserialize)]
7pub struct FleetMetric {
8    pub name: String,
9    pub value: f64,
10    pub tags: HashMap<String, String>,
11}
12
13impl FleetMetric {
14    pub fn new(name: impl Into<String>, value: f64) -> Self {
15        Self {
16            name: name.into(),
17            value,
18            tags: HashMap::new(),
19        }
20    }
21
22    pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
23        self.tags.insert(key.into(), value.into());
24        self
25    }
26}
27
28/// Extracts metrics from Bevy's DiagnosticsStore
29pub fn extract_metrics_from_diagnostics(diagnostics: &DiagnosticsStore) -> Vec<FleetMetric> {
30    let mut metrics = Vec::new();
31
32    for diagnostic in diagnostics.iter() {
33        let path = diagnostic.path();
34
35        // Extract current value
36        if let Some(value) = diagnostic.value() {
37            metrics.push(FleetMetric::new(format!("{}.value", path), value));
38        }
39
40        // Extract average if available
41        if let Some(average) = diagnostic.average() {
42            metrics.push(FleetMetric::new(format!("{}.average", path), average));
43        }
44    }
45
46    metrics
47}