use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sea_orm::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use crate::schemas::consumer_handler_performance::{ActiveModel, Entity};
#[derive(Deserialize, Serialize, Debug)]
pub struct PerformanceRecord {
pub handled_at: DateTime<Utc>,
pub subject: String,
pub duration_ms: i64,
pub status: String, pub handler_name: String,
pub error_message: Option<String>,
}
#[async_trait]
pub trait ConsumerPerformanceRepository: Send + Sync {
async fn insert(&self, record: PerformanceRecord)
-> Result<InsertResult<ActiveModel>, DbErr>;
}
pub struct SeaOrmConsumerPerformanceRepository {
db_conn: Arc<DatabaseConnection>,
}
impl SeaOrmConsumerPerformanceRepository {
pub fn new(db_conn: Arc<DatabaseConnection>) -> Self {
Self { db_conn }
}
}
#[async_trait]
impl ConsumerPerformanceRepository for SeaOrmConsumerPerformanceRepository {
async fn insert(
&self,
record: PerformanceRecord,
) -> Result<InsertResult<ActiveModel>, DbErr> {
let active_model = ActiveModel {
handled_at: Set(record.handled_at),
subject: Set(record.subject),
duration_ms: Set(record.duration_ms),
status: Set(record.status),
handler_name: Set(record.handler_name),
error_message: Set(record.error_message),
..Default::default()
};
let insert_result = Entity::insert(active_model).exec(&*self.db_conn).await?;
Ok(insert_result)
}
}