Compress impls

This commit is contained in:
Hugh Kaznowski
2026-09-07 02:04:36 +01:00
parent 55820982b1
commit f96de8f38d
3 changed files with 22 additions and 92 deletions
+2 -59
View File
@@ -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<PanelDto>,
}
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<PanelDto>,
pub panels: Vec<Panel>,
}
+6 -9
View File
@@ -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<DashboardDto> = 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(),
}
}