use std::collections::BTreeMap;
use std::sync::Arc;
use adk_core::NewRun;
use serde_json::Value;
use uuid::Uuid;
use super::signal::SignalStore;
use super::site::SiteProcessKind;
use super::tracker::KpiTracker;
use crate::inventory::InventoryWarning;
use crate::kernel::KernelBuilder;
use crate::partition::PartitionId;
use crate::wake::Wake;
#[derive(Clone)]
pub struct CharterKit {
site: Uuid,
app: String,
user: String,
back_office: PartitionId,
signals: Arc<dyn SignalStore>,
jobs: BTreeMap<SiteProcessKind, Job>,
skipped: BTreeMap<SiteProcessKind, String>,
tracker: Option<KpiTracker>,
tracker_cron: String,
}
#[derive(Clone)]
struct Job {
target: String,
input: Value,
wake: Option<Wake>,
}
impl std::fmt::Debug for CharterKit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CharterKit")
.field("site", &self.site)
.field("app", &self.app)
.field("back_office", &self.back_office)
.field("jobs", &self.jobs.keys().collect::<Vec<_>>())
.field("tracker", &self.tracker.is_some())
.finish_non_exhaustive()
}
}
impl CharterKit {
pub const JOB_UNBOUND: &'static str = "charter.job_unbound";
pub const DEFAULT_TRACKER_CRON: &'static str = "0 0 * * * *";
pub fn new(
site: Uuid,
app: impl Into<String>,
back_office: impl Into<PartitionId>,
signals: Arc<dyn SignalStore>,
) -> Self {
Self {
site,
app: app.into(),
user: "site".into(),
back_office: back_office.into(),
signals,
jobs: BTreeMap::new(),
skipped: BTreeMap::new(),
tracker: None,
tracker_cron: Self::DEFAULT_TRACKER_CRON.into(),
}
}
#[must_use]
pub fn with_user(mut self, user: impl Into<String>) -> Self {
self.user = user.into();
self
}
#[must_use]
pub fn job(mut self, kind: SiteProcessKind, target: impl Into<String>, input: Value) -> Self {
self.skipped.remove(&kind);
self.jobs.insert(kind, Job { target: target.into(), input, wake: None });
self
}
#[must_use]
pub fn job_on(
mut self,
kind: SiteProcessKind,
target: impl Into<String>,
input: Value,
wake: Wake,
) -> Self {
self.skipped.remove(&kind);
self.jobs.insert(kind, Job { target: target.into(), input, wake: Some(wake) });
self
}
#[must_use]
pub fn skip(mut self, kind: SiteProcessKind, reason: impl Into<String>) -> Self {
self.jobs.remove(&kind);
self.skipped.insert(kind, reason.into());
self
}
#[must_use]
pub fn tracker(mut self, tracker: KpiTracker) -> Self {
self.tracker = Some(tracker);
self
}
#[must_use]
pub fn tracker_cron(mut self, expr: impl Into<String>) -> Self {
self.tracker_cron = expr.into();
self
}
pub fn bound(&self) -> Vec<SiteProcessKind> {
self.jobs.keys().copied().collect()
}
pub fn install(self, mut builder: KernelBuilder) -> KernelBuilder {
let bound = self.bound();
for (kind, job) in self.jobs {
let wake = job.wake.unwrap_or_else(|| kind.default_wake(&self.back_office));
let run =
NewRun::new(kind.queue().as_str(), &self.app, &self.user, job.target, job.input);
builder = builder.trigger(kind.trigger(wake, run, self.back_office.clone()));
}
for kind in SiteProcessKind::ALL.into_iter().filter(|kind| !bound.contains(kind)) {
let reason = self.skipped.get(&kind).map_or("no job is bound to it", String::as_str);
builder = builder.inventory_warning(
InventoryWarning::new(
Self::JOB_UNBOUND,
format!("site {} runs no `{kind}` process: {reason}", self.site),
)
.with_subject(kind.as_str()),
);
}
if let Some(tracker) = self.tracker {
builder = builder.kpi_tracker(tracker, &self.app, &self.tracker_cron);
}
builder.signals(self.signals, self.back_office)
}
}