lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0

//! Telemetry type definitions

use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

/// Telemetry error types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TelemetryError {
    /// Telemetry not initialized
    NotInitialized,
    /// Metric not found
    MetricNotFound(MetricId),
    /// Metric already registered
    MetricExists(String),
    /// Invalid metric name
    InvalidName(String),
    /// Label count mismatch
    LabelMismatch {
        /// Expected label count
        expected: usize,
        /// Got label count
        got: usize,
    },
    /// Invalid bucket configuration
    InvalidBuckets,
}

impl fmt::Display for TelemetryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotInitialized => write!(f, "Telemetry not initialized"),
            Self::MetricNotFound(id) => write!(f, "Metric not found: {:?}", id),
            Self::MetricExists(name) => write!(f, "Metric already exists: {}", name),
            Self::InvalidName(name) => write!(f, "Invalid metric name: {}", name),
            Self::LabelMismatch { expected, got } => {
                write!(
                    f,
                    "Label count mismatch: expected {}, got {}",
                    expected, got
                )
            }
            Self::InvalidBuckets => write!(f, "Invalid bucket configuration"),
        }
    }
}

/// Metric identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MetricId(pub u64);

impl MetricId {
    /// Create a new metric ID
    pub fn new(id: u64) -> Self {
        Self(id)
    }
}

/// Metric type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetricType {
    /// Counter: monotonically increasing value
    Counter,
    /// Gauge: value that can go up or down
    Gauge,
    /// Histogram: bucketed observations
    Histogram,
    /// Summary: quantile observations
    Summary,
}

impl MetricType {
    /// Get type name for Prometheus export
    pub fn prometheus_type(&self) -> &'static str {
        match self {
            Self::Counter => "counter",
            Self::Gauge => "gauge",
            Self::Histogram => "histogram",
            Self::Summary => "summary",
        }
    }
}

/// Metric information
#[derive(Debug, Clone)]
pub struct MetricInfo {
    /// Metric ID
    pub id: MetricId,
    /// Metric name
    pub name: String,
    /// Help text
    pub help: String,
    /// Metric type
    pub metric_type: MetricType,
    /// Label names
    pub labels: Vec<String>,
}

/// Histogram bucket
#[derive(Debug, Clone)]
pub struct HistogramBucket {
    /// Upper bound
    pub le: f64,
    /// Cumulative count
    pub count: u64,
}

/// Histogram data
#[derive(Debug, Clone, Default)]
pub struct HistogramData {
    /// Buckets
    pub buckets: Vec<HistogramBucket>,
    /// Total sum of observations
    pub sum: f64,
    /// Total count of observations
    pub count: u64,
}

impl HistogramData {
    /// Create new histogram with given bucket boundaries
    pub fn new(bucket_bounds: &[f64]) -> Self {
        let buckets = bucket_bounds
            .iter()
            .map(|&le| HistogramBucket { le, count: 0 })
            .collect();

        Self {
            buckets,
            sum: 0.0,
            count: 0,
        }
    }

    /// Observe a value
    pub fn observe(&mut self, value: f64) {
        self.sum += value;
        self.count += 1;

        for bucket in &mut self.buckets {
            if value <= bucket.le {
                bucket.count += 1;
            }
        }
    }

    /// Reset histogram
    pub fn reset(&mut self) {
        self.sum = 0.0;
        self.count = 0;
        for bucket in &mut self.buckets {
            bucket.count = 0;
        }
    }
}

/// Summary quantile
#[derive(Debug, Clone)]
pub struct SummaryQuantile {
    /// Quantile (0.0 - 1.0)
    pub quantile: f64,
    /// Value at quantile
    pub value: f64,
}

/// Summary data
#[derive(Debug, Clone, Default)]
pub struct SummaryData {
    /// Quantiles
    pub quantiles: Vec<SummaryQuantile>,
    /// Total sum
    pub sum: f64,
    /// Total count
    pub count: u64,
    /// Raw observations (for calculating quantiles)
    observations: Vec<f64>,
    /// Max observations to keep
    max_observations: usize,
}

impl SummaryData {
    /// Create new summary
    pub fn new(max_observations: usize) -> Self {
        Self {
            quantiles: Vec::new(),
            sum: 0.0,
            count: 0,
            observations: Vec::new(),
            max_observations,
        }
    }

    /// Observe a value
    pub fn observe(&mut self, value: f64) {
        self.sum += value;
        self.count += 1;

        if self.observations.len() < self.max_observations {
            self.observations.push(value);
        }
    }

    /// Calculate quantiles
    pub fn calculate_quantiles(&mut self, quantiles: &[f64]) {
        if self.observations.is_empty() {
            return;
        }

        // Sort observations
        self.observations
            .sort_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal));

        self.quantiles.clear();
        for &q in quantiles {
            let idx =
                ((q * self.observations.len() as f64) as usize).min(self.observations.len() - 1);
            self.quantiles.push(SummaryQuantile {
                quantile: q,
                value: self.observations[idx],
            });
        }
    }

    /// Reset summary
    pub fn reset(&mut self) {
        self.sum = 0.0;
        self.count = 0;
        self.observations.clear();
        self.quantiles.clear();
    }
}

/// Label value pair
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LabelPair {
    /// Label name
    pub name: String,
    /// Label value
    pub value: String,
}

impl LabelPair {
    /// Create a new label pair
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
        }
    }
}

/// Default histogram buckets (in seconds, suitable for latency)
pub const DEFAULT_BUCKETS: &[f64] = &[
    0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
];

/// Bytes histogram buckets
pub const BYTES_BUCKETS: &[f64] = &[
    1024.0,       // 1 KB
    4096.0,       // 4 KB
    16384.0,      // 16 KB
    65536.0,      // 64 KB
    262144.0,     // 256 KB
    1048576.0,    // 1 MB
    4194304.0,    // 4 MB
    16777216.0,   // 16 MB
    67108864.0,   // 64 MB
    268435456.0,  // 256 MB
    1073741824.0, // 1 GB
];

/// Validate metric name
pub fn validate_metric_name(name: &str) -> Result<(), TelemetryError> {
    if name.is_empty() {
        return Err(TelemetryError::InvalidName("Empty name".to_string()));
    }

    // First character must be letter or underscore
    let first = name.chars().next().unwrap();
    if !first.is_ascii_alphabetic() && first != '_' {
        return Err(TelemetryError::InvalidName(alloc::format!(
            "Must start with letter or underscore: {}",
            name
        )));
    }

    // Rest must be alphanumeric or underscore
    for c in name.chars() {
        if !c.is_ascii_alphanumeric() && c != '_' {
            return Err(TelemetryError::InvalidName(alloc::format!(
                "Invalid character '{}' in: {}",
                c,
                name
            )));
        }
    }

    Ok(())
}

use alloc::string::ToString;

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

    #[test]
    fn test_validate_metric_name() {
        assert!(validate_metric_name("lcpfs_read_ops").is_ok());
        assert!(validate_metric_name("_private_metric").is_ok());
        assert!(validate_metric_name("metric123").is_ok());

        assert!(validate_metric_name("").is_err());
        assert!(validate_metric_name("123metric").is_err());
        assert!(validate_metric_name("metric-name").is_err());
    }

    #[test]
    fn test_histogram_observe() {
        let mut hist = HistogramData::new(&[1.0, 5.0, 10.0]);

        hist.observe(0.5);
        hist.observe(3.0);
        hist.observe(7.0);

        assert_eq!(hist.count, 3);
        assert!((hist.sum - 10.5).abs() < 0.001);

        // 0.5 <= 1.0, 5.0, 10.0
        assert_eq!(hist.buckets[0].count, 1);
        // 0.5 and 3.0 <= 5.0
        assert_eq!(hist.buckets[1].count, 2);
        // all <= 10.0
        assert_eq!(hist.buckets[2].count, 3);
    }

    #[test]
    fn test_metric_type() {
        assert_eq!(MetricType::Counter.prometheus_type(), "counter");
        assert_eq!(MetricType::Histogram.prometheus_type(), "histogram");
    }
}