use chrono::{DateTime, Duration, NaiveDate, Utc};
use serde::Serialize;
use serde_json::{json, Value};
pub const CAP_DEADLINES: usize = 5;
pub const CAP_INCIDENTS: usize = 5;
pub const CAP_WORK: usize = 7;
pub const CAP_RETRACTED: usize = 5;
pub const CAP_CHANGED: usize = 5;
pub const MAX_LIMIT: usize = 50;
pub const DEFAULT_SINCE: &str = "7d";
pub fn parse_since(raw: &str, now: DateTime<Utc>) -> Option<DateTime<Utc>> {
let s = raw.trim();
let ago = |n: &str, unit: fn(i64) -> Duration| {
n.parse::<i64>().ok().filter(|n| *n >= 0).map(|n| now - unit(n))
};
let at = if let Some(n) = s.strip_suffix('d') {
ago(n, Duration::days)
} else if let Some(n) = s.strip_suffix('h') {
ago(n, Duration::hours)
} else if let Ok(day) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
day.and_hms_opt(0, 0, 0).map(|t| t.and_utc())
} else {
DateTime::parse_from_rfc3339(s).ok().map(|t| t.with_timezone(&Utc))
};
at.filter(|t| *t <= now)
}
#[derive(Debug, Default, Clone)]
pub struct Scope {
pub tags: Vec<String>,
pub service: Option<String>,
}
impl Scope {
pub fn admits(&self, tags: &[String], service: &str) -> bool {
let has = |want: &str| tags.iter().any(|t| t.eq_ignore_ascii_case(want));
self.tags.iter().all(|t| has(t))
&& self.service.as_deref().is_none_or(|s| service.eq_ignore_ascii_case(s) || has(s))
}
}
pub fn section<T: Serialize>(mut rows: Vec<T>, cap: usize) -> Value {
let count = rows.len();
rows.truncate(cap);
json!({"count": count, "more": count - rows.len(), "items": rows})
}
fn rank(order: &[&str], value: &str) -> usize {
order.iter().position(|x| *x == value).unwrap_or(order.len())
}
pub fn severity_rank(severity: &str) -> usize {
rank(&["critical", "high", "medium", "low"], severity)
}
pub fn priority_rank(priority: &str) -> usize {
rank(&["critical", "high", "medium", "low"], priority)
}
pub fn age_days(since: &str, now: DateTime<Utc>) -> Option<i64> {
let t = DateTime::parse_from_rfc3339(since.trim()).ok()?;
Some((now - t.with_timezone(&Utc)).num_days().max(0))
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct IncidentRow {
pub key: String,
pub title: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub service: String,
pub severity: String,
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub age_days: Option<i64>,
}
pub fn sort_incidents(rows: &mut [IncidentRow]) {
rows.sort_by(|a, b| {
severity_rank(&a.severity)
.cmp(&severity_rank(&b.severity))
.then(
rank(&["open", "mitigated"], &a.status)
.cmp(&rank(&["open", "mitigated"], &b.status)),
)
.then(b.age_days.cmp(&a.age_days))
.then(a.key.cmp(&b.key))
});
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkRow {
pub key: String,
pub title: String,
pub status: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub priority: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub assignee: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub blocked_reason: String,
}
pub fn is_active_work(status: &str, priority: &str) -> bool {
matches!(status, "in_progress" | "blocked")
|| (status == "open" && matches!(priority, "high" | "critical"))
}
pub fn sort_work(rows: &mut [WorkRow]) {
rows.sort_by(|a, b| {
rank(&["in_progress", "blocked", "open"], &a.status)
.cmp(&rank(&["in_progress", "blocked", "open"], &b.status))
.then(priority_rank(&a.priority).cmp(&priority_rank(&b.priority)))
.then(a.key.cmp(&b.key))
});
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ChangeRow {
pub key: String,
pub title: String,
pub at: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub author: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub by: Option<crate::auth::By>,
}
pub fn sort_changes(rows: &mut [ChangeRow]) {
rows.sort_by(|a, b| b.at.cmp(&a.at).then(a.key.cmp(&b.key)));
}
#[cfg(test)]
#[path = "brief_tests.rs"]
mod tests;