burncloud_database_models/
models.rs

1use serde::{Deserialize, Serialize};
2use sqlx::{FromRow, Type};
3use uuid::Uuid;
4use chrono::{DateTime, Utc};
5use std::collections::HashMap;
6
7/// 数据库模型表 - 对应 burncloud_service_models::Model
8#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
9pub struct DbModel {
10    pub id: Uuid,
11    pub name: String,
12    pub display_name: String,
13    pub description: Option<String>,
14    pub version: String,
15    pub model_type: String, // JSON string of ModelType
16    pub size_category: String, // JSON string of ModelSize
17    pub file_size: i64,
18    pub provider: String,
19    pub license: Option<String>,
20    pub tags: sqlx::types::Json<Vec<String>>,
21    pub languages: sqlx::types::Json<Vec<String>>,
22    pub created_at: DateTime<Utc>,
23    pub updated_at: DateTime<Utc>,
24    pub file_path: Option<String>,
25    pub checksum: Option<String>,
26    pub download_url: Option<String>,
27    pub config: sqlx::types::Json<HashMap<String, serde_json::Value>>,
28    pub rating: Option<f32>,
29    pub download_count: i64,
30    pub is_official: bool,
31}
32
33/// 数据库已安装模型表 - 对应 burncloud_service_models::InstalledModel
34#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
35pub struct DbInstalledModel {
36    pub id: Uuid,
37    pub model_id: Uuid,
38    pub install_path: String,
39    pub installed_at: DateTime<Utc>,
40    pub status: String, // JSON string of ModelStatus
41    pub port: Option<i32>,
42    pub process_id: Option<i32>,
43    pub last_used: Option<DateTime<Utc>>,
44    pub usage_count: i64,
45}
46
47/// 数据库可用模型表 - 对应 burncloud_service_models::AvailableModel
48#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
49pub struct DbAvailableModel {
50    pub id: Uuid,
51    pub model_id: Uuid,
52    pub is_installed: bool,
53    pub published_at: DateTime<Utc>,
54    pub last_updated: DateTime<Utc>,
55    pub system_requirements: sqlx::types::Json<DbSystemRequirements>,
56}
57
58/// 数据库系统要求 - 对应 burncloud_service_models::SystemRequirements
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct DbSystemRequirements {
61    pub min_memory_gb: f32,
62    pub recommended_memory_gb: f32,
63    pub min_disk_space_gb: f32,
64    pub requires_gpu: bool,
65    pub supported_os: Vec<String>,
66    pub supported_architectures: Vec<String>,
67}
68
69/// 数据库运行时配置表 - 对应 burncloud_service_models::RuntimeConfig
70#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
71pub struct DbRuntimeConfig {
72    pub id: Uuid,
73    pub name: String,
74    pub max_context_length: Option<i32>,
75    pub temperature: Option<f32>,
76    pub top_p: Option<f32>,
77    pub top_k: Option<i32>,
78    pub max_tokens: Option<i32>,
79    pub stop_sequences: sqlx::types::Json<Vec<String>>,
80    pub batch_size: Option<i32>,
81    pub max_concurrent_requests: Option<i32>,
82    pub gpu_device_ids: sqlx::types::Json<Vec<i32>>,
83    pub memory_limit_mb: Option<i64>,
84    pub enable_streaming: bool,
85    pub custom_params: sqlx::types::Json<HashMap<String, serde_json::Value>>,
86    pub created_at: DateTime<Utc>,
87    pub updated_at: DateTime<Utc>,
88}
89
90/// 数据库模型运行时表 - 对应 burncloud_service_models::ModelRuntime
91#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
92pub struct DbModelRuntime {
93    pub id: Uuid,
94    pub model_id: Uuid,
95    pub runtime_config_id: Uuid,
96    pub name: String,
97    pub port: i32,
98    pub process_id: Option<i32>,
99    pub started_at: Option<DateTime<Utc>>,
100    pub stopped_at: Option<DateTime<Utc>>,
101    pub status: String, // JSON string of ModelStatus
102    pub health_endpoint: String,
103    pub api_endpoint: String,
104    pub log_file: Option<String>,
105    pub environment: sqlx::types::Json<HashMap<String, String>>,
106    pub created_at: DateTime<Utc>,
107    pub updated_at: DateTime<Utc>,
108}
109
110/// 数据库运行时指标表 - 对应 burncloud_service_models::RuntimeMetrics
111#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
112pub struct DbRuntimeMetrics {
113    pub id: Uuid,
114    pub runtime_id: Uuid,
115    pub timestamp: DateTime<Utc>,
116    pub cpu_usage_percent: f32,
117    pub memory_usage_mb: i64,
118    pub gpu_usage_percent: Option<f32>,
119    pub gpu_memory_usage_mb: Option<i64>,
120    pub active_connections: i32,
121    pub total_requests: i64,
122    pub successful_requests: i64,
123    pub failed_requests: i64,
124    pub avg_response_time_ms: f32,
125    pub throughput_rps: f32,
126    pub queue_length: i32,
127}
128
129/// 数据库运行时事件表 - 对应 burncloud_service_models::RuntimeEvent
130#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
131pub struct DbRuntimeEvent {
132    pub id: Uuid,
133    pub runtime_id: Uuid,
134    pub event_type: String, // JSON string of RuntimeEventType
135    pub timestamp: DateTime<Utc>,
136    pub message: String,
137    pub details: Option<sqlx::types::Json<serde_json::Value>>,
138    pub severity: String, // JSON string of EventSeverity
139}
140
141/// 数据库模型仓库表 - 对应 burncloud_service_models::ModelRepository
142#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
143pub struct DbModelRepository {
144    pub id: Uuid,
145    pub name: String,
146    pub url: String,
147    pub repo_type: String, // JSON string of RepositoryType
148    pub enabled: bool,
149    pub auth_config: Option<sqlx::types::Json<DbRepositoryAuth>>,
150    pub last_sync: Option<DateTime<Utc>>,
151    pub sync_status: String, // JSON string of SyncStatus
152    pub description: Option<String>,
153    pub tags: sqlx::types::Json<Vec<String>>,
154    pub priority: i32,
155    pub created_at: DateTime<Utc>,
156    pub updated_at: DateTime<Utc>,
157}
158
159/// 数据库仓库认证信息 - 对应 burncloud_service_models::RepositoryAuth
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct DbRepositoryAuth {
162    pub auth_type: String, // JSON string of AuthType
163    pub username: Option<String>,
164    pub token: Option<String>,
165    pub api_key: Option<String>,
166    pub extra_params: HashMap<String, String>,
167}
168
169/// 数据库仓库索引表 - 对应 burncloud_service_models::RepositoryIndex
170#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
171pub struct DbRepositoryIndex {
172    pub id: Uuid,
173    pub repository_id: Uuid,
174    pub version: String,
175    pub updated_at: DateTime<Utc>,
176    pub checksum: Option<String>,
177    pub metadata: sqlx::types::Json<HashMap<String, serde_json::Value>>,
178}
179
180/// 数据库仓库模型表 - 对应 burncloud_service_models::RepositoryModel
181#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
182pub struct DbRepositoryModel {
183    pub id: Uuid,
184    pub repository_id: Uuid,
185    pub model_id: Uuid,
186    pub repo_model_id: String,
187    pub repo_path: String,
188    pub download_urls: sqlx::types::Json<Vec<DbDownloadUrl>>,
189    pub files: sqlx::types::Json<Vec<DbModelFile>>,
190    pub dependencies: sqlx::types::Json<Vec<String>>,
191    pub installation_notes: Option<String>,
192    pub usage_examples: sqlx::types::Json<Vec<String>>,
193    pub license_text: Option<String>,
194    pub model_card: Option<String>,
195    pub created_at: DateTime<Utc>,
196    pub updated_at: DateTime<Utc>,
197}
198
199/// 数据库下载链接 - 对应 burncloud_service_models::DownloadUrl
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct DbDownloadUrl {
202    pub filename: String,
203    pub url: String,
204    pub size: i64,
205    pub checksum: Option<String>,
206    pub checksum_algorithm: Option<String>,
207    pub is_primary: bool,
208}
209
210/// 数据库模型文件 - 对应 burncloud_service_models::ModelFile
211#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct DbModelFile {
213    pub filename: String,
214    pub size: i64,
215    pub file_type: String, // JSON string of ModelFileType
216    pub checksum: Option<String>,
217    pub required: bool,
218    pub description: Option<String>,
219}
220
221/// 数据库全局配置表 - 对应 burncloud_service_models::GlobalConfig
222#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
223pub struct DbGlobalConfig {
224    pub id: Uuid,
225    pub version: String,
226    pub config_data: sqlx::types::Json<serde_json::Value>, // 整个配置作为JSON存储
227    pub created_at: DateTime<Utc>,
228    pub updated_at: DateTime<Utc>,
229}
230
231/// 数据库系统指标表 - 对应 burncloud_service_models::SystemMetrics
232#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
233pub struct DbSystemMetrics {
234    pub id: Uuid,
235    pub timestamp: DateTime<Utc>,
236    pub cpu_usage_percent: f32,
237    pub cpu_cores: i32,
238    pub memory_total_bytes: i64,
239    pub memory_used_bytes: i64,
240    pub memory_usage_percent: f32,
241    pub disk_total_bytes: i64,
242    pub disk_used_bytes: i64,
243    pub disk_usage_percent: f32,
244    pub network_rx_bytes_per_sec: i64,
245    pub network_tx_bytes_per_sec: i64,
246    pub gpu_usage_percent: Option<f32>,
247    pub gpu_memory_usage_mb: Option<i64>,
248    pub load_1m: f32,
249    pub load_5m: f32,
250    pub load_15m: f32,
251}
252
253/// 数据库应用指标表 - 对应 burncloud_service_models::ApplicationMetrics
254#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
255pub struct DbApplicationMetrics {
256    pub id: Uuid,
257    pub timestamp: DateTime<Utc>,
258    pub uptime_seconds: i64,
259    pub total_requests: i64,
260    pub successful_requests: i64,
261    pub failed_requests: i64,
262    pub active_connections: i32,
263    pub avg_response_time_ms: f32,
264    pub p95_response_time_ms: f32,
265    pub p99_response_time_ms: f32,
266    pub current_qps: f32,
267    pub peak_qps: f32,
268    pub error_rate_percent: f32,
269    pub health_status: String, // JSON string of HealthStatus
270}
271
272/// 数据库模型指标表 - 对应 burncloud_service_models::ModelMetrics
273#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
274pub struct DbModelMetrics {
275    pub id: Uuid,
276    pub model_id: Uuid,
277    pub runtime_id: Option<Uuid>,
278    pub timestamp: DateTime<Utc>,
279    pub status: String, // JSON string of ModelStatus
280    pub total_requests: i64,
281    pub successful_requests: i64,
282    pub failed_requests: i64,
283    pub avg_inference_time_ms: f32,
284    pub tokens_per_second: f32,
285    pub memory_usage_bytes: i64,
286    pub gpu_memory_usage_bytes: Option<i64>,
287    pub cpu_usage_percent: f32,
288    pub gpu_usage_percent: Option<f32>,
289    pub queue_length: i32,
290    pub last_request_time: Option<DateTime<Utc>>,
291}
292
293/// 数据库告警事件表 - 对应 burncloud_service_models::AlertEvent
294#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
295pub struct DbAlertEvent {
296    pub id: Uuid,
297    pub alert_type: String, // JSON string of AlertType
298    pub severity: String, // JSON string of AlertSeverity
299    pub title: String,
300    pub description: String,
301    pub triggered_at: DateTime<Utc>,
302    pub resolved_at: Option<DateTime<Utc>>,
303    pub status: String, // JSON string of AlertStatus
304    pub resource_type: String,
305    pub resource_id: String,
306    pub resource_name: String,
307    pub value: f32,
308    pub threshold: f32,
309    pub labels: sqlx::types::Json<HashMap<String, String>>,
310    pub metadata: sqlx::types::Json<HashMap<String, String>>,
311}
312
313/// 数据库同步结果表 - 对应 burncloud_service_models::SyncResult
314#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
315pub struct DbSyncResult {
316    pub id: Uuid,
317    pub repository_id: Uuid,
318    pub started_at: DateTime<Utc>,
319    pub completed_at: Option<DateTime<Utc>>,
320    pub status: String, // JSON string of SyncStatus
321    pub models_added: i32,
322    pub models_updated: i32,
323    pub models_removed: i32,
324    pub error_message: Option<String>,
325    pub log_entries: sqlx::types::Json<Vec<String>>,
326}
327
328/// 数据库用户会话表
329#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
330pub struct DbUserSession {
331    pub id: Uuid,
332    pub user_id: String,
333    pub session_token: String,
334    pub created_at: DateTime<Utc>,
335    pub expires_at: DateTime<Utc>,
336    pub last_accessed: DateTime<Utc>,
337    pub ip_address: String,
338    pub user_agent: Option<String>,
339    pub is_active: bool,
340}
341
342/// 数据库API使用统计表
343#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
344pub struct DbApiUsage {
345    pub id: Uuid,
346    pub api_key_id: Option<Uuid>,
347    pub endpoint: String,
348    pub method: String,
349    pub timestamp: DateTime<Utc>,
350    pub response_time_ms: i32,
351    pub status_code: i32,
352    pub request_size_bytes: i64,
353    pub response_size_bytes: i64,
354    pub ip_address: String,
355    pub user_agent: Option<String>,
356}
357
358/// 数据库任务队列表
359#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
360pub struct DbTask {
361    pub id: Uuid,
362    pub task_type: String,
363    pub payload: sqlx::types::Json<serde_json::Value>,
364    pub status: String, // pending, running, completed, failed
365    pub priority: i32,
366    pub created_at: DateTime<Utc>,
367    pub started_at: Option<DateTime<Utc>>,
368    pub completed_at: Option<DateTime<Utc>>,
369    pub error_message: Option<String>,
370    pub retry_count: i32,
371    pub max_retries: i32,
372    pub scheduled_at: Option<DateTime<Utc>>,
373}
374
375/// 数据库下载任务表
376#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
377pub struct DbDownloadTask {
378    pub id: Uuid,
379    pub model_id: Uuid,
380    pub url: String,
381    pub file_path: String,
382    pub total_size: i64,
383    pub downloaded_size: i64,
384    pub status: String, // pending, downloading, completed, failed, paused
385    pub progress_percent: f32,
386    pub download_speed_bps: i64,
387    pub estimated_time_remaining: Option<i32>, // seconds
388    pub created_at: DateTime<Utc>,
389    pub started_at: Option<DateTime<Utc>>,
390    pub completed_at: Option<DateTime<Utc>>,
391    pub error_message: Option<String>,
392}