use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use awa_model::admin;
use awa_model::job::JobState;
use awa_model::AwaError;
use moka::future::Cache;
#[derive(Clone)]
pub struct DashboardCache {
pub stats: Cache<(), HashMap<JobState, i64>>,
pub queues: Cache<(), Vec<admin::QueueOverview>>,
pub runtime: Cache<(), admin::RuntimeOverview>,
pub queue_runtime: Cache<(), Vec<admin::QueueRuntimeSummary>>,
pub storage: Cache<(), awa_model::storage::StorageStatusReport>,
}
fn build_cache<V: Clone + Send + Sync + 'static>(ttl: Duration) -> Cache<(), V> {
Cache::builder().time_to_live(ttl).max_capacity(1).build()
}
impl DashboardCache {
pub fn new(ttl: Duration) -> Self {
Self {
stats: build_cache(ttl),
queues: build_cache(ttl),
runtime: build_cache(ttl),
queue_runtime: build_cache(ttl),
storage: build_cache(ttl),
}
}
}
#[derive(Debug, Clone)]
pub struct CacheError(pub Arc<AwaError>);
impl From<AwaError> for CacheError {
fn from(err: AwaError) -> Self {
Self(Arc::new(err))
}
}