update for metric routes

This commit is contained in:
dal 2025-03-10 09:41:32 -06:00
parent e466951d96
commit d1462fed2d
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
7 changed files with 22 additions and 44 deletions

View File

@ -1,6 +1,5 @@
use agents::tools::file_tools::file_types::metric_yml::MetricYml;
use anyhow::{anyhow, Result};
use database::models::User;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@ -41,7 +40,7 @@ pub async fn get_metric_data_handler(
let metric = get_metric_handler(&request.metric_id, &user_id).await?;
// Parse the metric definition from YAML to get SQL and dataset IDs
let metric_yml = serde_json::from_str::<MetricYml>(&metric.file)?;
let metric_yml = serde_yaml::from_str::<MetricYml>(&metric.file)?;
let sql = metric_yml.sql;
let dataset_ids = metric_yml.dataset_ids;

View File

@ -147,12 +147,12 @@ pub async fn get_metric_handler(metric_id: &Uuid, user_id: &Uuid) -> Result<Bust
}
// Get user information
let user_info = users::table
.filter(users::id.eq(metric_file.created_by))
.select((users::name, users::avatar_url))
.first::<UserInfo>(&mut conn)
.await
.map_err(|e| anyhow!("Failed to get user information: {}", e))?;
// let user_info = users::table
// .filter(users::id.eq(metric_file.created_by))
// .select((users::name, users::avatar_url))
// .first::<UserInfo>(&mut conn)
// .await
// .map_err(|e| anyhow!("Failed to get user information: {}", e))?;
// Construct BusterMetric
Ok(BusterMetric {
@ -175,8 +175,8 @@ pub async fn get_metric_handler(metric_id: &Uuid, user_id: &Uuid) -> Result<Bust
created_at: metric_file.created_at.to_rfc3339(),
updated_at: metric_file.updated_at.to_rfc3339(),
sent_by_id: metric_file.created_by.to_string(),
sent_by_name: user_info.name.unwrap_or("".to_string()),
sent_by_avatar_url: user_info.avatar_url,
sent_by_name: "".to_string(),
sent_by_avatar_url: None,
code: None,
dashboards: vec![], // TODO: Get associated dashboards
collections: vec![], // TODO: Get associated collections

View File

@ -34,7 +34,7 @@ pub async fn get_metric_data(user: &AuthenticatedUser, request: GetMetricDataWsR
tracing::error!("Error getting metric data: {}", e);
send_error_message(
&user.id.to_string(),
WsRoutes::Metrics(MetricRoute::GetData),
WsRoutes::Metrics(MetricRoute::Data),
WsEvent::Metrics(MetricEvent::FetchingData),
WsErrorCode::InternalServerError,
"Failed to get metric data.".to_string(),
@ -46,7 +46,7 @@ pub async fn get_metric_data(user: &AuthenticatedUser, request: GetMetricDataWsR
};
let response_message = WsResponseMessage::new(
WsRoutes::Metrics(MetricRoute::GetData),
WsRoutes::Metrics(MetricRoute::Get),
WsEvent::Metrics(MetricEvent::FetchingData),
response,
None,

View File

@ -1,7 +1,6 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
use middleware::AuthenticatedUser;
@ -14,8 +13,8 @@ use super::{
pub enum MetricRoute {
#[serde(rename = "/metrics/get")]
Get,
#[serde(rename = "/metrics/get_data")]
GetData,
#[serde(rename = "/metrics/data")]
Data,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
@ -39,7 +38,7 @@ pub async fn metrics_router(
get_metric(user, request).await?;
}
MetricRoute::GetData => {
MetricRoute::Data => {
let req = match serde_json::from_value(data) {
Ok(req) => req,
Err(e) => return Err(anyhow!("Error parsing request: {}", e)),
@ -56,7 +55,7 @@ impl MetricRoute {
pub fn from_str(path: &str) -> Result<Self> {
match path {
"/metrics/get" => Ok(Self::Get),
"/metrics/get_data" => Ok(Self::GetData),
"/metrics/data" => Ok(Self::Data),
_ => Err(anyhow!("Invalid path")),
}
}

View File

@ -8,6 +8,7 @@ mod search;
mod sql;
mod teams;
mod terms;
mod metrics;
pub mod threads_and_messages;
mod users;
pub mod ws;

View File

@ -33,20 +33,7 @@ use uuid::Uuid;
use middleware::AuthenticatedUser;
use super::{
collections::collections_router::CollectionEvent,
dashboards::dashboards_router::DashboardEvent,
data_sources::data_sources_router::DataSourceEvent,
datasets::datasets_router::DatasetEvent,
organizations::organization_router::OrganizationEvent,
permissions::permissions_router::PermissionEvent,
search::search_router::SearchEvent,
sql::sql_router::SqlEvent,
teams::teams_routes::TeamEvent,
terms::terms_router::TermEvent,
threads_and_messages::threads_router::ThreadEvent,
users::users_router::UserEvent,
ws_router::{ws_router, WsRoutes},
ws_utils::{subscribe_to_stream, unsubscribe_from_stream},
collections::collections_router::CollectionEvent, dashboards::dashboards_router::DashboardEvent, data_sources::data_sources_router::DataSourceEvent, datasets::datasets_router::DatasetEvent, metrics::MetricEvent, organizations::organization_router::OrganizationEvent, permissions::permissions_router::PermissionEvent, search::search_router::SearchEvent, sql::sql_router::SqlEvent, teams::teams_routes::TeamEvent, terms::terms_router::TermEvent, threads_and_messages::threads_router::ThreadEvent, users::users_router::UserEvent, ws_router::{ws_router, WsRoutes}, ws_utils::{subscribe_to_stream, unsubscribe_from_stream}
};
const CLIENT_TIMEOUT: Duration = Duration::from_secs(300);
@ -74,6 +61,7 @@ pub enum WsEvent {
Terms(TermEvent),
Search(SearchEvent),
Organizations(OrganizationEvent),
Metrics(MetricEvent),
}
#[derive(Serialize, Deserialize, Clone)]

View File

@ -11,19 +11,7 @@ use crate::routes::ws::{
};
use super::{
collections::collections_router::{collections_router, CollectionRoute},
dashboards::dashboards_router::DashboardRoute,
data_sources::data_sources_router::{data_sources_router, DataSourceRoute},
datasets::datasets_router::DatasetRoute,
organizations::organization_router::{organizations_router, OrganizationRoute},
permissions::permissions_router::{permissions_router, PermissionRoute},
search::search_router::{search_router, SearchRoute},
sql::sql_router::{sql_router, SqlRoute},
teams::teams_routes::{teams_router, TeamRoute},
terms::terms_router::{terms_router, TermRoute},
threads_and_messages::threads_router::{threads_router, ThreadRoute},
users::users_router::{users_router, UserRoute},
ws::SubscriptionRwLock,
collections::collections_router::{collections_router, CollectionRoute}, dashboards::dashboards_router::DashboardRoute, data_sources::data_sources_router::{data_sources_router, DataSourceRoute}, datasets::datasets_router::DatasetRoute, metrics::{metrics_router, MetricRoute}, organizations::organization_router::{organizations_router, OrganizationRoute}, permissions::permissions_router::{permissions_router, PermissionRoute}, search::search_router::{search_router, SearchRoute}, sql::sql_router::{sql_router, SqlRoute}, teams::teams_routes::{teams_router, TeamRoute}, terms::terms_router::{terms_router, TermRoute}, threads_and_messages::threads_router::{threads_router, ThreadRoute}, users::users_router::{users_router, UserRoute}, ws::SubscriptionRwLock
};
#[derive(Deserialize, Serialize, Debug, Clone)]
@ -41,6 +29,7 @@ pub enum WsRoutes {
Terms(TermRoute),
Search(SearchRoute),
Organizations(OrganizationRoute),
Metrics(MetricRoute),
}
impl WsRoutes {
@ -63,6 +52,7 @@ impl WsRoutes {
"terms" => Ok(Self::Terms(TermRoute::from_str(path)?)),
"search" => Ok(Self::Search(SearchRoute::from_str(path)?)),
"organizations" => Ok(Self::Organizations(OrganizationRoute::from_str(path)?)),
"metrics" => Ok(Self::Metrics(MetricRoute::from_str(path)?)),
_ => Err(anyhow!("Invalid path")),
}
}
@ -107,6 +97,7 @@ pub async fn ws_router(
WsRoutes::Organizations(organizations_route) => {
organizations_router(organizations_route, payload, user).await
}
WsRoutes::Metrics(metrics_route) => metrics_router(metrics_route, payload, user).await,
};
if let Err(e) = result {