use super::{
worker::{Endpoint, StatsForPeriod},
utility::serialize_status_codes,
};
use axum::http::StatusCode;
use chrono::NaiveDateTime;
use indexmap::IndexMap;
use serde::Serialize;
use std::collections::HashMap;
#[cfg(feature = "utoipa")]
use utoipa::ToSchema;
#[derive(Clone, Debug, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[non_exhaustive]
pub struct StatsResponse {
pub started_at: NaiveDateTime,
pub last_second: NaiveDateTime,
pub uptime: u64,
pub active: u64,
pub requests: u64,
#[serde(serialize_with = "serialize_status_codes")]
#[cfg_attr(feature = "utoipa", schema(value_type = HashMap<String, u64>))]
pub codes: HashMap<StatusCode, u64>,
pub times: IndexMap<String, StatsResponseForPeriod>,
#[cfg_attr(feature = "utoipa", schema(value_type = HashMap<String, StatsResponseForPeriod>))]
pub endpoints: HashMap<Endpoint, StatsResponseForPeriod>,
pub connections: IndexMap<String, StatsResponseForPeriod>,
pub memory: IndexMap<String, StatsResponseForPeriod>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[non_exhaustive]
pub struct StatsHistoryResponse {
pub last_second: NaiveDateTime,
pub times: Vec<StatsResponseForPeriod>,
pub connections: Vec<StatsResponseForPeriod>,
pub memory: Vec<StatsResponseForPeriod>,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[non_exhaustive]
pub struct StatsResponseForPeriod {
pub average: f64,
pub maximum: u64,
pub minimum: u64,
pub count: u64,
}
impl From<&StatsForPeriod> for StatsResponseForPeriod {
fn from(stats: &StatsForPeriod) -> Self {
Self {
average: stats.average,
maximum: stats.maximum,
minimum: stats.minimum,
count: stats.count,
}
}
}