cc_switch_lib/database/dao/
stream_check.rs1use crate::database::{lock_conn, Database};
4use crate::error::AppError;
5use crate::services::stream_check::{StreamCheckConfig, StreamCheckResult};
6
7impl Database {
8 pub fn save_stream_check_log(
10 &self,
11 provider_id: &str,
12 provider_name: &str,
13 app_type: &str,
14 result: &StreamCheckResult,
15 ) -> Result<i64, AppError> {
16 let conn = lock_conn!(self.conn);
17
18 conn.execute(
19 "INSERT INTO stream_check_logs
20 (provider_id, provider_name, app_type, status, success, message,
21 response_time_ms, http_status, model_used, retry_count, tested_at)
22 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
23 rusqlite::params![
24 provider_id,
25 provider_name,
26 app_type,
27 format!("{:?}", result.status).to_lowercase(),
28 result.success,
29 result.message,
30 result.response_time_ms.map(|t| t as i64),
31 result.http_status.map(|s| s as i64),
32 result.model_used,
33 result.retry_count as i64,
34 result.tested_at,
35 ],
36 )
37 .map_err(|e| AppError::Database(e.to_string()))?;
38
39 Ok(conn.last_insert_rowid())
40 }
41
42 pub fn get_stream_check_config(&self) -> Result<StreamCheckConfig, AppError> {
44 match self.get_setting("stream_check_config")? {
45 Some(json) => serde_json::from_str(&json)
46 .map_err(|e| AppError::Message(format!("解析配置失败: {e}"))),
47 None => Ok(StreamCheckConfig::default()),
48 }
49 }
50
51 pub fn save_stream_check_config(&self, config: &StreamCheckConfig) -> Result<(), AppError> {
53 let json = serde_json::to_string(config)
54 .map_err(|e| AppError::Message(format!("序列化配置失败: {e}")))?;
55 self.set_setting("stream_check_config", &json)
56 }
57}