From f96de8f38d4253af26f35650fb399b6ef08cd2dd Mon Sep 17 00:00:00 2001 From: Hugh Kaznowski Date: Mon, 7 Sep 2026 02:04:36 +0100 Subject: [PATCH] Compress impls --- src/api/dto.rs | 61 ++------------------------------------ src/api/mod.rs | 15 ++++------ src/engine/snapshot/mod.rs | 38 +++++++++--------------- 3 files changed, 22 insertions(+), 92 deletions(-) diff --git a/src/api/dto.rs b/src/api/dto.rs index c999c10..9831293 100644 --- a/src/api/dto.rs +++ b/src/api/dto.rs @@ -1,8 +1,8 @@ use crate::engine::alerts::AlertCondition; use crate::engine::data_value::MetricPrimitiveKind; use crate::engine::metric_primitive::MetricPrimitive; -use crate::config::profile::{ChartType, Dashboard, Panel, Profile}; use crate::config::Settings; +use crate::config::profile::{Panel, Profile}; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; @@ -192,63 +192,6 @@ pub struct CreateProfileRequest { pub name: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct PanelDto { - pub id: String, - pub title: String, - pub promql: String, - pub chart_type: ChartType, -} - -impl From<&Panel> for PanelDto { - fn from(panel: &Panel) -> Self { - PanelDto { - id: panel.id.clone(), - title: panel.title.clone(), - promql: panel.promql.clone(), - chart_type: panel.chart_type, - } - } -} - -impl From<&PanelDto> for Panel { - fn from(dto: &PanelDto) -> Self { - Panel { - id: dto.id.clone(), - title: dto.title.clone(), - promql: dto.promql.clone(), - chart_type: dto.chart_type, - } - } -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DashboardDto { - pub id: String, - pub name: String, - pub panels: Vec, -} - -impl From<&Dashboard> for DashboardDto { - fn from(dashboard: &Dashboard) -> Self { - DashboardDto { - id: dashboard.id.clone(), - name: dashboard.name.clone(), - panels: dashboard.panels.iter().map(PanelDto::from).collect(), - } - } -} - -impl From<&DashboardDto> for Dashboard { - fn from(dto: &DashboardDto) -> Self { - Dashboard { - id: dto.id.clone(), - name: dto.name.clone(), - panels: dto.panels.iter().map(Panel::from).collect(), - } - } -} - #[derive(Debug, Deserialize)] pub struct CreateDashboardRequest { pub name: String, @@ -258,5 +201,5 @@ pub struct CreateDashboardRequest { pub struct SaveDashboardRequest { pub name: String, #[serde(default)] - pub panels: Vec, + pub panels: Vec, } diff --git a/src/api/mod.rs b/src/api/mod.rs index 16f97c5..8c331ef 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use crate::api::dto::{ BucketValuesParams, CreateAlertRuleRequest, CreateDashboardRequest, CreateProfileRequest, - DashboardDto, ErrorDto, MetricDeclarationDto, + ErrorDto, MetricDeclarationDto, ParamDeclarationDto, LogsParams, ProfileSummaryDto, PromqlParams, SaveDashboardRequest, SettingsDto, TestAlertNotificationRequest, TimelineParams, UpdateAlertRuleRequest, UpdateSettingsRequest, }; @@ -773,10 +773,7 @@ async fn list_dashboards( ) -> impl IntoResponse { let settings = state.settings.lock().unwrap(); match settings.list_dashboards(&name) { - Ok(dashboards) => { - let dtos: Vec = dashboards.iter().map(DashboardDto::from).collect(); - Json(dtos).into_response() - } + Ok(dashboards) => Json(dashboards).into_response(), Err(err) => err.into_response(), } } @@ -788,7 +785,7 @@ async fn create_dashboard( ) -> impl IntoResponse { let mut settings = state.settings.lock().unwrap(); match settings.create_dashboard(&state.settings_path, &name, body.name) { - Ok(dashboard) => (StatusCode::CREATED, Json(DashboardDto::from(&dashboard))).into_response(), + Ok(dashboard) => (StatusCode::CREATED, Json(dashboard)).into_response(), Err(err) => err.into_response(), } } @@ -800,7 +797,7 @@ async fn get_dashboard( let settings = state.settings.lock().unwrap(); match settings.list_dashboards(&name) { Ok(dashboards) => match dashboards.iter().find(|d| d.id == dashboard_id) { - Some(dashboard) => Json(DashboardDto::from(dashboard)).into_response(), + Some(dashboard) => Json(dashboard).into_response(), None => ProfileError::NotFound.into_response(), }, Err(err) => err.into_response(), @@ -816,10 +813,10 @@ async fn save_dashboard( let dashboard = Dashboard { id: dashboard_id.clone(), name: body.name, - panels: body.panels.iter().map(|p| p.into()).collect(), + panels: body.panels, }; match settings.save_dashboard(&state.settings_path, &name, &dashboard_id, dashboard) { - Ok(dashboard) => Json(DashboardDto::from(&dashboard)).into_response(), + Ok(dashboard) => Json(dashboard).into_response(), Err(err) => err.into_response(), } } diff --git a/src/engine/snapshot/mod.rs b/src/engine/snapshot/mod.rs index a9c6941..85d06c1 100644 --- a/src/engine/snapshot/mod.rs +++ b/src/engine/snapshot/mod.rs @@ -54,6 +54,7 @@ pub(crate) struct EngineSnapshot { pub(crate) ingest_lag_high_watermark_ms: i64, } +// --- core: snapshot construction and log range access --- impl EngineSnapshot { pub(crate) fn metric_declarations_range( &self, @@ -218,16 +219,7 @@ impl EngineSnapshot { } } -impl EngineSnapshot { - pub(crate) fn handle_query_ro(&self, query: EngineQuery) -> EngineResponse { - handle_query_inner(self, query, SortHelperAccess::Immutable(&self.sort_helper_indexes)) - } - - pub(crate) fn has_sort_helper_index(&self, sort_by: &str) -> bool { - self.sort_helper_indexes.contains_key(sort_by) - } -} - +// --- filter: compiling search-bar filter expressions --- impl EngineSnapshot { pub(in crate::engine::snapshot) fn compile_filter_query(&self, filter: &str, now: Timestamp) -> Result { let expr = filter_parser::parse(filter).map_err(|err| err.to_string())?; @@ -308,6 +300,7 @@ impl EngineSnapshot { } } +// --- metrics: declaring, applying, and querying metric buckets --- impl EngineSnapshot { pub(crate) fn validate_metric_request(&self, request: &DeclareMetricRequest) -> Result { let re = Regex::new(&request.message_match_regex) @@ -424,9 +417,7 @@ impl EngineSnapshot { self.clear_metric_data(name); existed } -} -impl EngineSnapshot { #[allow(clippy::too_many_arguments)] fn apply_metric_to_log( &mut self, @@ -597,9 +588,7 @@ impl EngineSnapshot { }) .flatten() } -} -impl EngineSnapshot { pub(crate) fn declared_value_kind(&self, metric_name: &str) -> Option { match self.index.get(&IndexKey::MetricDeclaration { name: metric_name.to_string() }) { Some(DataValue::MetricDeclaration { value_param, .. }) => Some( @@ -619,6 +608,7 @@ impl EngineSnapshot { } } +// --- params: declaring and resolving param values --- impl EngineSnapshot { pub(crate) fn validate_param_declaration(&self, request: &DeclareParamRequest) -> Result<(), String> { if OTTYL_RESERVED_PARAM_NAMES.contains(&request.name.as_str()) { @@ -666,9 +656,7 @@ impl EngineSnapshot { DataValue::ParamDeclaration(ParamDeclaration { auto_derived: false, ..request }), ); } -} -impl EngineSnapshot { fn auto_derived_param_count(&self) -> usize { self.param_declarations_range().filter(|declaration| declaration.6).count() } @@ -734,9 +722,7 @@ impl EngineSnapshot { total += 1; } } -} -impl EngineSnapshot { pub(crate) fn derive_log_stream_params_from_row( &mut self, header: &crate::engine::logfmt::LogStreamHeader, @@ -795,9 +781,7 @@ impl EngineSnapshot { } } } -} -impl EngineSnapshot { pub(crate) fn clear_all_param_declarations(&mut self) -> usize { let names: Vec = self.param_declarations_range().map(|(name, ..)| name.to_string()).collect(); @@ -818,9 +802,7 @@ impl EngineSnapshot { let reextracted = self.param_declarations_range().count(); (reextracted, logs_considered) } -} -impl EngineSnapshot { pub(in crate::engine::snapshot) fn resolved_param_declarations(&self) -> HashMap { self.param_declarations_range() .filter_map(|(name, _, capture_group, json_field_path, kind, elapsed_since_last, _, log_stream_gated)| { @@ -840,9 +822,7 @@ impl EngineSnapshot { }) .collect() } -} -impl EngineSnapshot { pub(in crate::engine::snapshot) fn resolve_param_value( &mut self, metric_name: &str, @@ -876,7 +856,16 @@ impl EngineSnapshot { } } +// --- query: EngineQuery/PromQL evaluation --- impl EngineSnapshot { + pub(crate) fn handle_query_ro(&self, query: EngineQuery) -> EngineResponse { + handle_query_inner(self, query, SortHelperAccess::Immutable(&self.sort_helper_indexes)) + } + + pub(crate) fn has_sort_helper_index(&self, sort_by: &str) -> bool { + self.sort_helper_indexes.contains_key(sort_by) + } + pub(crate) fn evaluate_promql( &self, parsed: &PromqlQuery, @@ -1050,6 +1039,7 @@ impl EngineSnapshot { } } +// --- sort: sort-helper index maintenance --- impl EngineSnapshot { pub(super) fn build_sort_helper_index(&self, sort_by: &str) -> BTreeMap { let compiled_params = self.compiled_param_declarations();