Files
poimen-memory/crates/mem-cli/src/endpoints.rs
T

63 lines
1.6 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use uuid::Uuid;
/// Ingest request.
#[derive(Deserialize, Clone)]
pub struct IngestRequest {
pub project: String,
pub source: String,
pub ingest_id: String,
}
/// Job status.
#[derive(Debug, Clone, Serialize)]
pub struct JobStatus {
pub job_id: String,
pub ingest_id: String,
pub project: String,
pub status: String,
pub chunks_seen: u32,
pub chunks_used: u32,
}
/// In-memory ingest queue.
pub struct IngestQueue {
jobs: BTreeMap<String, JobStatus>,
}
impl IngestQueue {
/// Create new queue.
pub fn new() -> Self {
Self {
jobs: BTreeMap::new(),
}
}
/// Submit job (idempotent by ingest_id).
pub fn submit(&mut self, project: &str, ingest_id: &str) -> (String, bool) {
if let Some(existing) = self.jobs.get(ingest_id) {
(existing.job_id.clone(), false)
} else {
let job_id = format!("ingest-{}", Uuid::new_v4());
self.jobs.insert(
ingest_id.to_string(),
JobStatus {
job_id: job_id.clone(),
ingest_id: ingest_id.to_string(),
project: project.to_string(),
status: "queued".to_string(),
chunks_seen: 0,
chunks_used: 0,
},
);
(job_id, true)
}
}
/// Get job status by job_id.
pub fn get_status(&self, job_id: &str) -> Option<JobStatus> {
self.jobs.values().find(|j| j.job_id == job_id).cloned()
}
}