bevy_fleet 0.1.0

bevy swarm diagnostic, event, metric, and telemetry client
Documentation
use bevy::diagnostic::DiagnosticsStore;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Metric data extracted from Bevy diagnostics
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FleetMetric {
    pub name: String,
    pub value: f64,
    pub tags: HashMap<String, String>,
}

impl FleetMetric {
    pub fn new(name: impl Into<String>, value: f64) -> Self {
        Self {
            name: name.into(),
            value,
            tags: HashMap::new(),
        }
    }

    pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.tags.insert(key.into(), value.into());
        self
    }
}

/// Extracts metrics from Bevy's DiagnosticsStore
pub fn extract_metrics_from_diagnostics(diagnostics: &DiagnosticsStore) -> Vec<FleetMetric> {
    let mut metrics = Vec::new();

    for diagnostic in diagnostics.iter() {
        let path = diagnostic.path();

        // Extract current value
        if let Some(value) = diagnostic.value() {
            metrics.push(FleetMetric::new(format!("{}.value", path), value));
        }

        // Extract average if available
        if let Some(average) = diagnostic.average() {
            metrics.push(FleetMetric::new(format!("{}.average", path), average));
        }
    }

    metrics
}