From a665648308be9e071347bd75bb00936280ae6b7f Mon Sep 17 00:00:00 2001 From: dal Date: Mon, 3 Feb 2025 12:51:55 -0700 Subject: [PATCH] feat: Implement file creation for metric and dashboard files - Added implementation for creating metric files with database insertion - Introduced `create_metric_file()` and `create_dashboard_file()` functions - Updated `CreateFilesTool` to handle different file types - Added `data_metadata` field to `MetricFile` struct - Implemented basic file type validation and creation logic --- .../utils/tools/file_tools/create_files.rs | 51 ++++++++++++++++++- .../file_tools/file_types/metric_file.rs | 8 ++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/api/src/utils/tools/file_tools/create_files.rs b/api/src/utils/tools/file_tools/create_files.rs index 402176563..95434a67c 100644 --- a/api/src/utils/tools/file_tools/create_files.rs +++ b/api/src/utils/tools/file_tools/create_files.rs @@ -1,9 +1,15 @@ use anyhow::Result; use async_trait::async_trait; +use diesel::insert_into; use serde::{Deserialize, Serialize}; use serde_json::Value; -use crate::utils::{clients::ai::litellm::ToolCall, tools::ToolExecutor}; +use crate::{ + database::{lib::get_pg_pool, schema::metric_files}, + utils::{clients::ai::litellm::ToolCall, tools::ToolExecutor}, +}; + +use super::file_types::{dashboard_file::DashboardFile, metric_file::MetricFile}; #[derive(Debug, Serialize, Deserialize)] struct FileParams { @@ -39,7 +45,13 @@ impl ToolExecutor for CreateFilesTool { let files = params.files; - for file in files {} + for file in files { + match file.file_type.as_str() { + "metric" => create_metric_file(file)?, + "dashboard" => create_dashboard_file(file)?, + _ => return Err(anyhow::anyhow!("Invalid file type: {}. Currently only `metric` and `dashboard` types are supported.", file.file_type)), + } + } Ok(Value::Array(vec![])) } @@ -82,3 +94,38 @@ impl ToolExecutor for CreateFilesTool { }) } } + +async fn create_metric_file(file: FileParams) -> Result<()> { + let metric_file = match MetricFile::new(file.yml_content) { + Ok(metric_file) => metric_file, + Err(e) => return Err(e), + }; + + let mut conn = match get_pg_pool().get().await { + Ok(conn) => conn, + Err(e) => return Err(e), + }; + + // TODO: Add Metric File Record here. + + let metric_file_record = match insert_into(metric_files::table) + .values(metric_file) + .returning(metric_files::all_columns) + .execute(&mut conn) + .await + { + Ok(metric_file_record) => metric_file_record, + Err(e) => return Err(e), + }; + + Ok(()) +} + +fn create_dashboard_file(file: FileParams) -> Result<()> { + let dashboard_file = match DashboardFile::new(file.yml_content) { + Ok(dashboard_file) => dashboard_file, + Err(e) => return Err(e), + }; + + Ok(()) +} diff --git a/api/src/utils/tools/file_tools/file_types/metric_file.rs b/api/src/utils/tools/file_tools/file_types/metric_file.rs index bc0d9280a..639bafa4e 100644 --- a/api/src/utils/tools/file_tools/file_types/metric_file.rs +++ b/api/src/utils/tools/file_tools/file_types/metric_file.rs @@ -9,6 +9,13 @@ pub struct MetricFile { pub description: Option, pub sql: String, pub chart_config: ChartConfig, + pub data_metadata: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct DataMetadata { + pub name: String, + pub data_type: String, } #[derive(Debug, Serialize, Deserialize)] @@ -287,7 +294,6 @@ impl MetricFile { } } - // TODO: Add validation logic here pub fn validate(&self) -> Result<()> { Ok(()) }