create metrics associating the user

This commit is contained in:
dal 2025-03-19 14:53:22 -06:00
parent e12fe28b04
commit f7b9e685f0
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
2 changed files with 106 additions and 1 deletions

View File

@ -4,7 +4,12 @@ use anyhow::{anyhow, Result};
use async_trait::async_trait;
use braintrust::{get_prompt_system_message, BraintrustClient};
use chrono::Utc;
use database::{pool::get_pg_pool, schema::metric_files};
use database::{
pool::get_pg_pool,
schema::{metric_files, asset_permissions},
models::AssetPermission,
enums::{AssetType, IdentityType, AssetPermissionRole},
};
use diesel::insert_into;
use diesel_async::RunQueryDsl;
use indexmap::IndexMap;
@ -120,6 +125,43 @@ impl ToolExecutor for CreateMetricFilesTool {
.await
{
Ok(_) => {
// Get the user ID from the agent state
let user_id = self.agent.get_user_id();
// Create asset permissions for each metric file
let now = Utc::now();
let asset_permissions: Vec<AssetPermission> = metric_records
.iter()
.map(|record| AssetPermission {
identity_id: user_id,
identity_type: IdentityType::User,
asset_id: record.id,
asset_type: AssetType::MetricFile,
role: AssetPermissionRole::Owner,
created_at: now,
updated_at: now,
deleted_at: None,
created_by: user_id,
updated_by: user_id,
})
.collect();
// Insert asset permissions
match insert_into(asset_permissions::table)
.values(&asset_permissions)
.execute(&mut conn)
.await
{
Ok(_) => {
tracing::debug!("Successfully inserted asset permissions for {} metric files", asset_permissions.len());
},
Err(e) => {
tracing::error!("Error inserting asset permissions: {}", e);
// Continue with the process even if permissions failed
// We'll still return the created files
}
}
for (i, yml) in metric_ymls.into_iter().enumerate() {
created_files.push(FileWithId {
id: metric_records[i].id,

View File

@ -0,0 +1,63 @@
use axum::{
extract::Path,
http::StatusCode,
Extension,
};
use handlers::collections::sharing::list_collection_sharing_handler;
use middleware::AuthenticatedUser;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::routes::rest::ApiResponse;
/// Response type for sharing permissions
#[derive(Debug, Serialize)]
pub struct SharingResponse {
pub permissions: Vec<SharingPermission>,
}
/// Single sharing permission entry
#[derive(Debug, Serialize)]
pub struct SharingPermission {
pub user_id: Uuid,
pub email: String,
pub name: Option<String>,
pub avatar_url: Option<String>,
pub role: database::enums::AssetPermissionRole,
}
/// REST handler for listing sharing permissions for a collection
pub async fn list_collection_sharing_rest_handler(
Extension(user): Extension<AuthenticatedUser>,
Path(id): Path<Uuid>,
) -> Result<ApiResponse<SharingResponse>, (StatusCode, String)> {
tracing::info!("Processing GET request for collection sharing with ID: {}, user_id: {}", id, user.id);
match list_collection_sharing_handler(&id, &user.id).await {
Ok(permissions) => {
let response = SharingResponse {
permissions: permissions.into_iter().map(|p| SharingPermission {
user_id: p.user.as_ref().map(|u| u.id).unwrap_or_default(),
email: p.user.as_ref().map(|u| u.email.clone()).unwrap_or_default(),
name: p.user.as_ref().and_then(|u| u.name.clone()),
avatar_url: p.user.as_ref().and_then(|u| u.avatar_url.clone()),
role: p.permission.role,
}).collect(),
};
Ok(ApiResponse::JsonData(response))
},
Err(e) => {
tracing::error!("Error listing sharing permissions: {}", e);
let error_message = e.to_string();
// Return appropriate status code based on error message
if error_message.contains("not found") {
return Err((StatusCode::NOT_FOUND, format!("Collection not found: {}", e)));
} else if error_message.contains("permission") {
return Err((StatusCode::FORBIDDEN, format!("Permission denied: {}", e)));
} else {
return Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to list sharing permissions: {}", e)));
}
}
}
}