orch/store/cluster/
progress.rs1use serde::{Deserialize, Serialize};
5
6use crate::store::TaskStore;
7
8const MAX_TEXT: usize = 500;
10const KEEP: usize = 5;
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Report {
16 pub worktree: String,
17 pub done: String,
18 pub left: String,
19 pub state: String,
20 pub cli_committed: bool,
21 #[serde(default)]
22 pub cli_error: Option<String>,
23}
24
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct Record {
27 pub node: String,
28 pub ts: String,
29 pub branch: String,
30 pub commit: String,
31 pub committed_by: String,
33 pub pushed_by: String,
35 #[serde(default)]
36 pub error: Option<String>,
37 pub done: String,
38 pub left: String,
39 pub state: String,
40}
41
42pub fn cut(s: &str) -> String {
44 s.chars().take(MAX_TEXT).collect()
45}
46
47fn key(task_id: i64) -> String {
48 format!("progress:{task_id}")
49}
50
51pub async fn list(store: &TaskStore, task_id: i64) -> anyhow::Result<Vec<Record>> {
52 match store.kv_get(&key(task_id)).await? {
53 Some(json) => Ok(serde_json::from_str(&json)?),
54 None => Ok(Vec::new()),
55 }
56}
57
58pub async fn append(store: &TaskStore, task_id: i64, record: &Record) -> anyhow::Result<()> {
60 let mut records = list(store, task_id).await?;
61 records.push(record.clone());
62 let skip = records.len().saturating_sub(KEEP);
63 let records: Vec<Record> = records.into_iter().skip(skip).collect();
64 store
65 .kv_set(&key(task_id), &serde_json::to_string(&records)?)
66 .await?;
67 Ok(())
68}