mirror of https://github.com/buster-so/buster.git
start to implement camelCase on dashboard drop debugs in prod
This commit is contained in:
parent
9dd4355380
commit
cef57963f7
|
@ -1,5 +1,4 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use diesel::{
|
use diesel::{
|
||||||
deserialize::FromSql,
|
deserialize::FromSql,
|
||||||
pg::Pg,
|
pg::Pg,
|
||||||
|
@ -14,24 +13,40 @@ use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, FromSqlRow, AsExpression)]
|
#[derive(Debug, Serialize, Deserialize, Clone, FromSqlRow, AsExpression)]
|
||||||
#[diesel(sql_type = Jsonb)]
|
#[diesel(sql_type = Jsonb)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct DashboardYml {
|
pub struct DashboardYml {
|
||||||
|
#[serde(alias = "name")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
|
#[serde(alias = "description")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
|
#[serde(alias = "rows")]
|
||||||
pub rows: Vec<Row>,
|
pub rows: Vec<Row>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Row {
|
pub struct Row {
|
||||||
pub items: Vec<RowItem>, // max number of items in a row is 4, min is 1
|
pub items: Vec<RowItem>, // max number of items in a row is 4, min is 1
|
||||||
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
#[serde(alias = "row_height")]
|
||||||
pub row_height: Option<u32>, // max is 550, min is 320
|
pub row_height: Option<u32>, // max is 550, min is 320
|
||||||
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
#[serde(alias = "column_sizes")]
|
||||||
pub column_sizes: Option<Vec<u32>>, // max sum of elements is 12 min is 3
|
pub column_sizes: Option<Vec<u32>>, // max sum of elements is 12 min is 3
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none", default)]
|
||||||
|
pub id: Option<u32>, // incremental id for rows
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct RowItem {
|
pub struct RowItem {
|
||||||
// This id is the id of the metric or item reference that goes here in the dashboard.
|
// This id is the id of the metric or item reference that goes here in the dashboard.
|
||||||
|
#[serde(alias = "id")]
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +63,13 @@ impl DashboardYml {
|
||||||
file.name = String::from("New Dashboard");
|
file.name = String::from("New Dashboard");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add row IDs if they don't exist
|
||||||
|
for (index, row) in file.rows.iter_mut().enumerate() {
|
||||||
|
if row.id.is_none() {
|
||||||
|
row.id = Some((index as u32) + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Validate the file
|
// Validate the file
|
||||||
match file.validate() {
|
match file.validate() {
|
||||||
Ok(_) => Ok(file),
|
Ok(_) => Ok(file),
|
||||||
|
@ -66,7 +88,7 @@ impl DashboardYml {
|
||||||
for row in &self.rows {
|
for row in &self.rows {
|
||||||
// Check row height constraints
|
// Check row height constraints
|
||||||
if let Some(row_height) = row.row_height {
|
if let Some(row_height) = row.row_height {
|
||||||
if row_height < 320 || row_height > 550 {
|
if !(320..=550).contains(&row_height) {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Row height must be between 320 and 550, got {}",
|
"Row height must be between 320 and 550, got {}",
|
||||||
row_height
|
row_height
|
||||||
|
@ -75,7 +97,7 @@ impl DashboardYml {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check number of items constraint
|
// Check number of items constraint
|
||||||
if row.items.len() < 1 || row.items.len() > 4 {
|
if row.items.is_empty() || row.items.len() > 4 {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Number of items in row must be between 1 and 4, got {}",
|
"Number of items in row must be between 1 and 4, got {}",
|
||||||
row.items.len()
|
row.items.len()
|
||||||
|
@ -85,7 +107,7 @@ impl DashboardYml {
|
||||||
// Check column sizes sum to valid amount
|
// Check column sizes sum to valid amount
|
||||||
if let Some(column_sizes) = &row.column_sizes {
|
if let Some(column_sizes) = &row.column_sizes {
|
||||||
let sum: u32 = column_sizes.iter().sum();
|
let sum: u32 = column_sizes.iter().sum();
|
||||||
if sum < 3 || sum > 12 {
|
if !(3..=12).contains(&sum) {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Sum of column sizes must be between 3 and 12, got {}",
|
"Sum of column sizes must be between 3 and 12, got {}",
|
||||||
sum
|
sum
|
||||||
|
@ -110,6 +132,26 @@ impl DashboardYml {
|
||||||
serde_json::to_value(self)
|
serde_json::to_value(self)
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to serialize dashboard yml: {}", e))
|
.map_err(|e| anyhow::anyhow!("Failed to serialize dashboard yml: {}", e))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the next available row ID based on existing rows
|
||||||
|
pub fn get_next_row_id(&self) -> u32 {
|
||||||
|
self.rows
|
||||||
|
.iter()
|
||||||
|
.filter_map(|row| row.id)
|
||||||
|
.max()
|
||||||
|
.map_or(1, |max_id| max_id + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a new row with provided items and auto-generates the row ID
|
||||||
|
pub fn add_row(&mut self, items: Vec<RowItem>, row_height: Option<u32>, column_sizes: Option<Vec<u32>>) {
|
||||||
|
let next_id = self.get_next_row_id();
|
||||||
|
self.rows.push(Row {
|
||||||
|
items,
|
||||||
|
row_height,
|
||||||
|
column_sizes,
|
||||||
|
id: Some(next_id),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromSql<Jsonb, Pg> for DashboardYml {
|
impl FromSql<Jsonb, Pg> for DashboardYml {
|
||||||
|
|
|
@ -165,12 +165,17 @@ pub async fn get_metric_handler(metric_id: &Uuid, user_id: &Uuid) -> Result<Bust
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// Get the latest version number from version history
|
||||||
|
let latest_version = metric_file.version_history.get_latest_version()
|
||||||
|
.map(|v| v.version_number)
|
||||||
|
.unwrap_or(1);
|
||||||
|
|
||||||
// Construct BusterMetric
|
// Construct BusterMetric
|
||||||
Ok(BusterMetric {
|
Ok(BusterMetric {
|
||||||
id: metric_file.id,
|
id: metric_file.id,
|
||||||
metric_type: "metric".to_string(),
|
metric_type: "metric".to_string(),
|
||||||
title: metric_file.name,
|
title: metric_file.name,
|
||||||
version_number: 1,
|
version_number: latest_version,
|
||||||
description: metric_yml.description,
|
description: metric_yml.description,
|
||||||
file_name: metric_file.file_name,
|
file_name: metric_file.file_name,
|
||||||
time_frame: metric_yml.time_frame,
|
time_frame: metric_yml.time_frame,
|
||||||
|
|
|
@ -39,7 +39,12 @@ async fn main() {
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
EnvFilter::try_from_default_env()
|
EnvFilter::try_from_default_env()
|
||||||
.unwrap_or_else(|_| EnvFilter::new(tracing::Level::DEBUG.to_string())),
|
.unwrap_or_else(|_| {
|
||||||
|
let log_level = env::var("LOG_LEVEL")
|
||||||
|
.unwrap_or_else(|_| "warn".to_string())
|
||||||
|
.to_uppercase();
|
||||||
|
EnvFilter::new(log_level)
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.init();
|
.init();
|
||||||
|
|
Loading…
Reference in New Issue