use serde::{Deserialize, Serialize};
use crate::store::TaskStore;
const MAX_TEXT: usize = 500;
const KEEP: usize = 5;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Report {
pub worktree: String,
pub done: String,
pub left: String,
pub state: String,
pub cli_committed: bool,
#[serde(default)]
pub cli_error: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Record {
pub node: String,
pub ts: String,
pub branch: String,
pub commit: String,
pub committed_by: String,
pub pushed_by: String,
#[serde(default)]
pub error: Option<String>,
pub done: String,
pub left: String,
pub state: String,
}
pub fn cut(s: &str) -> String {
s.chars().take(MAX_TEXT).collect()
}
fn key(task_id: i64) -> String {
format!("progress:{task_id}")
}
pub async fn list(store: &TaskStore, task_id: i64) -> anyhow::Result<Vec<Record>> {
match store.kv_get(&key(task_id)).await? {
Some(json) => Ok(serde_json::from_str(&json)?),
None => Ok(Vec::new()),
}
}
pub async fn append(store: &TaskStore, task_id: i64, record: &Record) -> anyhow::Result<()> {
let mut records = list(store, task_id).await?;
records.push(record.clone());
let skip = records.len().saturating_sub(KEEP);
let records: Vec<Record> = records.into_iter().skip(skip).collect();
store
.kv_set(&key(task_id), &serde_json::to_string(&records)?)
.await?;
Ok(())
}