Skip to content

Commit c7d08c7

Browse files
authored
Merge pull request #32 from RGGH/31-change-appstatedb_pool-from-arcoption
change-appstatedb_pool-from-arcoption
2 parents e62b745 + 3e4ee91 commit c7d08c7

3 files changed

Lines changed: 31 additions & 26 deletions

File tree

src/api/handlers/evals.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// src/api/handlers/evals.rs - Updated to pass db_pool to runner
1+
// src/api/handlers/evals.rs - Complete fixed version
22
use actix_web::{web, HttpResponse, Result};
33
use serde::{Deserialize, Serialize};
44
use uuid::Uuid;
@@ -56,10 +56,10 @@ pub async fn run_eval(
5656
metadata: None,
5757
};
5858

59-
// 🆕 Pass db_pool to runner for dynamic judge prompt loading
60-
let db_pool_ref = state.db_pool.as_ref();
59+
// Extract the pool reference properly for the new Option<Arc<SqlitePool>> structure
60+
let db_pool_ref = state.db_pool.as_ref().map(|arc| arc.as_ref());
6161

62-
match runner::run_eval_with_pool(&state.config, &eval_config, &state.client, db_pool_ref.as_ref()).await {
62+
match runner::run_eval_with_pool(&state.config, &eval_config, &state.client, db_pool_ref).await {
6363
Ok(result) => {
6464
let status = if let Some(judge) = &result.judge_result {
6565
match judge.verdict {
@@ -88,14 +88,14 @@ pub async fn run_eval(
8888
};
8989

9090
// Save to database
91-
if let Some(pool) = state.db_pool.as_ref() {
91+
if let Some(pool_arc) = state.db_pool.as_ref() {
9292
println!("💾 Saving successful evaluation to database: {}", eval_id);
9393
let api_response = crate::models::ApiResponse {
9494
id: eval_id.clone(),
9595
status: status.to_string(),
9696
result: crate::models::EvalResult::Success(result),
9797
};
98-
match crate::database::save_evaluation(pool, &api_response).await {
98+
match crate::database::save_evaluation(pool_arc, &api_response).await {
9999
Ok(_) => println!("✅ Successfully saved evaluation {} to database", eval_id),
100100
Err(e) => {
101101
eprintln!("❌ Failed to save evaluation to database: {}", e);
@@ -134,7 +134,7 @@ pub async fn run_eval(
134134
};
135135

136136
// Save error to database
137-
if let Some(pool) = state.db_pool.as_ref() {
137+
if let Some(pool_arc) = state.db_pool.as_ref() {
138138
println!("💾 Saving error evaluation to database: {}", eval_id);
139139
let api_response = crate::models::ApiResponse {
140140
id: eval_id.clone(),
@@ -143,7 +143,7 @@ pub async fn run_eval(
143143
message: error_string.clone(),
144144
}),
145145
};
146-
match crate::database::save_evaluation(pool, &api_response).await {
146+
match crate::database::save_evaluation(pool_arc, &api_response).await {
147147
Ok(_) => println!("✅ Successfully saved error evaluation {} to database", eval_id),
148148
Err(e) => {
149149
eprintln!("❌ Failed to save error evaluation to database: {}", e);
@@ -170,14 +170,14 @@ pub async fn run_batch(
170170
let batch_id = Uuid::new_v4().to_string();
171171
let total = eval_configs.len();
172172

173-
// 🆕 Pass db_pool to runner for dynamic judge prompt loading
174-
let db_pool_ref = state.db_pool.as_ref();
173+
// Extract the pool reference properly for the new Option<Arc<SqlitePool>> structure
174+
let db_pool_ref = state.db_pool.as_ref().map(|arc| arc.as_ref());
175175

176176
let results = runner::run_batch_evals_with_pool(
177177
&state.config,
178178
eval_configs.into_inner(),
179179
&state.client,
180-
db_pool_ref.as_ref(),
180+
db_pool_ref,
181181
).await;
182182

183183
let mut responses = Vec::new();
@@ -233,13 +233,13 @@ pub async fn run_batch(
233233
error: None,
234234
};
235235

236-
if let Some(pool) = state.db_pool.as_ref() {
236+
if let Some(pool_arc) = state.db_pool.as_ref() {
237237
let api_response = crate::models::ApiResponse {
238238
id: eval_id,
239239
status: status.to_string(),
240240
result: crate::models::EvalResult::Success(eval_result),
241241
};
242-
if let Err(e) = crate::database::save_evaluation(pool, &api_response).await {
242+
if let Err(e) = crate::database::save_evaluation(pool_arc, &api_response).await {
243243
log::error!("Failed to save batch evaluation to database: {}", e);
244244
}
245245
}
@@ -264,15 +264,15 @@ pub async fn run_batch(
264264
error: Some(error_string.clone()),
265265
};
266266

267-
if let Some(pool) = state.db_pool.as_ref() {
267+
if let Some(pool_arc) = state.db_pool.as_ref() {
268268
let api_response = crate::models::ApiResponse {
269269
id: eval_id,
270270
status: "error".to_string(),
271271
result: crate::models::EvalResult::Error(crate::models::ApiError {
272272
message: error_string,
273273
}),
274274
};
275-
if let Err(e) = crate::database::save_evaluation(pool, &api_response).await {
275+
if let Err(e) = crate::database::save_evaluation(pool_arc, &api_response).await {
276276
log::error!("Failed to save batch error to database: {}", e);
277277
}
278278
}
@@ -324,8 +324,8 @@ pub struct HistoryResponse {
324324
}
325325

326326
pub async fn get_history(state: web::Data<AppState>) -> Result<HttpResponse> {
327-
if let Some(pool) = state.db_pool.as_ref() {
328-
match crate::database::get_all_evaluations(pool).await {
327+
if let Some(pool_arc) = state.db_pool.as_ref() {
328+
match crate::database::get_all_evaluations(pool_arc).await {
329329
Ok(history) => Ok(HttpResponse::Ok().json(HistoryResponse { results: history })),
330330
Err(e) => {
331331
log::error!("Failed to fetch evaluation history: {}", e);
@@ -345,4 +345,4 @@ pub struct ModelsResponse {
345345

346346
pub async fn get_models(state: web::Data<AppState>) -> Result<HttpResponse> {
347347
Ok(HttpResponse::Ok().json(ModelsResponse { models: state.config.models.clone() }))
348-
}
348+
}

src/api/state.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ use std::sync::Arc;
77
pub struct AppState {
88
pub config: Arc<AppConfig>,
99
pub client: Client,
10-
pub db_pool: Arc<Option<SqlitePool>>,
10+
pub db_pool: Option<Arc<SqlitePool>>,
1111
}
1212

1313
impl AppState {
1414
pub async fn new(config: AppConfig) -> Self {
15-
let db_pool = crate::database::init_db()
16-
.await
17-
.ok();
15+
// Get the pool, convert Result to Option, then wrap in Arc
16+
let db_pool = match crate::database::init_db().await {
17+
Ok(pool) => Some(Arc::new(pool)),
18+
Err(e) => {
19+
eprintln!("⚠️ Failed to initialize database: {}", e);
20+
None
21+
}
22+
};
1823

1924
Self {
2025
config: Arc::new(config),
2126
client: Client::new(),
22-
db_pool: Arc::new(db_pool),
27+
db_pool, // Now it's Option<Arc<SqlitePool>>
2328
}
2429
}
25-
}
30+
}

src/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use sqlx::{
77
Row, SqlitePool,
88
};
99
use std::{
10-
path::{Path, PathBuf},
10+
path::PathBuf,
1111
str::FromStr,
1212
};
1313
use chrono::Utc; // Import chrono::Utc for use in structs and functions
@@ -380,4 +380,4 @@ pub async fn set_active_judge_prompt(pool: &SqlitePool, version: i64) -> Result<
380380
tx.commit().await?;
381381

382382
Ok(())
383-
}
383+
}

0 commit comments

Comments
 (0)