use std::time::Duration;
use crate::model::error::{ErrorCode, ObzError};
#[derive(Debug, Clone)]
pub struct MetricQueryParams {
pub query: String,
pub is_range: bool,
pub start: i64,
pub end: i64,
pub step: Option<u64>,
pub limit: Option<usize>,
pub timeout: Option<Duration>,
}
impl MetricQueryParams {
pub const DEFAULT_TARGET_POINTS: i64 = 100;
pub fn step_or_auto(&self) -> u64 {
self.step.unwrap_or_else(|| {
let duration = (self.end - self.start).max(0);
std::cmp::max(1, (duration / Self::DEFAULT_TARGET_POINTS) as u64)
})
}
}
#[derive(Debug, Clone)]
pub struct MetricMetadataParams {
pub match_expr: Option<String>,
pub match_exprs: Vec<String>,
pub start: Option<i64>,
pub end: Option<i64>,
pub limit: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct MetricInfoParams {
pub metric_name: String,
}
#[derive(Debug, Clone)]
pub struct LabelValuesParams {
pub label_name: String,
pub match_expr: Option<String>,
pub start: Option<i64>,
pub end: Option<i64>,
pub limit: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct LogSearchParams {
pub query: String,
pub start: i64,
pub end: i64,
pub limit: usize,
}
#[derive(Debug, Clone)]
pub struct TraceSearchParams {
pub query: String,
pub start: i64,
pub end: i64,
pub limit: usize,
}
#[derive(Debug, Clone)]
pub struct TraceGetParams {
pub trace_id: String,
pub start: i64,
pub end: i64,
}
#[derive(Debug, Clone)]
pub struct ExtensionParams {
pub start: Option<i64>,
pub end: Option<i64>,
pub signal: String,
pub args: Vec<(String, String)>,
}
impl ExtensionParams {
pub fn get(&self, name: &str) -> Option<&str> {
self.args
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.as_str())
}
pub fn require(&self, name: &str) -> Result<&str, ObzError> {
self.get(name).ok_or_else(|| ObzError::InvalidArgument {
code: ErrorCode::MissingRequired,
message: format!("missing required argument: --{name}"),
suggestion: None,
})
}
pub fn get_all(&self, name: &str) -> Vec<&str> {
self.args
.iter()
.filter(|(k, _)| k == name)
.map(|(_, v)| v.as_str())
.collect()
}
}