Add progress.rs for monitoring
This commit is contained in:
75
src/api/progress.rs
Normal file
75
src/api/progress.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
//! 處理進度監控
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ProcessorProgress {
|
||||||
|
pub name: String,
|
||||||
|
pub status: ProcessorStatus,
|
||||||
|
pub progress: f32,
|
||||||
|
pub message: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum ProcessorStatus {
|
||||||
|
Pending,
|
||||||
|
Running,
|
||||||
|
Complete,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct VideoProgress {
|
||||||
|
pub uuid: String,
|
||||||
|
pub processors: Vec<ProcessorProgress>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VideoProgress {
|
||||||
|
pub fn new(uuid: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
uuid: uuid.to_string(),
|
||||||
|
processors: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_processor(&mut self, name: &str, status: ProcessorStatus, progress: f32) {
|
||||||
|
if let Some(p) = self.processors.iter_mut().find(|p| p.name == name) {
|
||||||
|
p.status = status;
|
||||||
|
p.progress = progress;
|
||||||
|
} else {
|
||||||
|
self.processors.push(ProcessorProgress {
|
||||||
|
name: name.to_string(),
|
||||||
|
status,
|
||||||
|
progress,
|
||||||
|
message: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_overall_progress(&self) -> f32 {
|
||||||
|
if self.processors.is_empty() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let total: f32 = self.processors.iter().map(|p| p.progress).sum();
|
||||||
|
total / self.processors.len() as f32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_complete(&self) -> bool {
|
||||||
|
self.processors.iter().all(|p| p.status == ProcessorStatus::Complete)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_known_processors() -> Vec<&'static str> {
|
||||||
|
vec![
|
||||||
|
"asr",
|
||||||
|
"asrx",
|
||||||
|
"yolo",
|
||||||
|
"ocr",
|
||||||
|
"face",
|
||||||
|
"pose",
|
||||||
|
"cut",
|
||||||
|
"trace",
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user