use std::collections::{BTreeMap, BTreeSet};
use chrono::{DateTime, NaiveDate, Utc};
use serde::Serialize;
use super::super::command_record::StageName;
use super::super::reading::Command;
use super::super::run_metrics::Position;
use super::super::session_signals::Signals;
use super::bug_origins::ConfirmedCounts;
use super::outcomes::{Class, Fact};
use super::scope::{Filters, Window};
use super::trends::Signal;
use crate::github::{PrState, PullRequestState};
use crate::scoring::ScoreHistory;
pub(super) struct Sources<'a> {
pub(super) origins: &'a ConfirmedCounts,
pub(super) kept: &'a BTreeMap<String, PullRequestState>,
pub(super) scores: &'a ScoreHistory,
pub(super) filters: &'a Filters,
}
#[derive(Serialize)]
pub(crate) struct Groups {
#[serde(rename = "yield")]
pub(crate) yield_: Yield,
pub(crate) quality: Quality,
pub(crate) efficiency: Group<Setup, Efficiency>,
pub(crate) friction: Group<AccountKind, Friction>,
}
#[derive(Serialize)]
pub(crate) struct Yield {
pub(crate) pull_requests: Group<AccountKind, Survival>,
}
#[derive(Serialize)]
pub(crate) struct Quality {
pub(crate) bug_origins: Group<AccountKind, Cohort>,
pub(crate) scores: Group<CriterionKey, CriterionScores>,
pub(crate) interventions: Group<AccountKind, Interventions>,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub(crate) struct AccountKind {
pub(crate) account: Option<String>,
pub(crate) kind: &'static str,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub(crate) struct Setup {
pub(crate) account: Option<String>,
pub(crate) kind: &'static str,
pub(crate) harness: String,
pub(crate) model: Option<String>,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub(crate) struct CriterionKey {
pub(crate) criterion: &'static str,
pub(crate) calibrated: bool,
}
#[derive(Clone, Copy)]
pub(crate) enum Unit {
Number,
Fraction,
Usd,
}
impl Unit {
fn value(self, value: Option<f64>) -> String {
match (self, value) {
(_, None) => "missing".into(),
(Self::Fraction, Some(value)) => format!("{:.0}%", value * 100.0),
(Self::Usd, Some(value)) => format!("${value:.2}"),
(Self::Number, Some(value)) if value.fract() == 0.0 => format!("{value:.0}"),
(Self::Number, Some(value)) => format!("{value:.1}"),
}
}
fn change(self, change: Option<f64>) -> String {
match (self, change) {
(_, None) => "missing".into(),
(Self::Fraction, Some(change)) => format!("{:+.0} pts", change * 100.0),
(Self::Usd, Some(change)) => format!("{change:+.2}"),
(Self::Number, Some(change)) if change.fract() == 0.0 => format!("{change:+.0}"),
(Self::Number, Some(change)) => format!("{change:+.1}"),
}
}
}
pub(crate) struct Reading {
name: &'static str,
unit: Unit,
value: Option<f64>,
}
fn reading(name: &'static str, unit: Unit, value: Option<f64>) -> Reading {
Reading { name, unit, value }
}
pub(crate) trait Headline {
fn headline(&self) -> Vec<Reading>;
fn n(&self) -> usize;
}
pub(super) trait Measure<I>: Headline + Default {
fn add(&mut self, item: &I);
}
#[derive(Serialize)]
pub(crate) struct Group<K, T> {
pub(crate) axis: Vec<NaiveDate>,
pub(crate) lines: Vec<Line<K, T>>,
}
#[derive(Serialize)]
pub(crate) struct Line<K, T> {
#[serde(flatten)]
pub(crate) key: K,
pub(crate) window: T,
pub(crate) buckets: Vec<Point<T>>,
pub(crate) comparison: Option<Compared<T>>,
}
#[derive(Serialize)]
pub(crate) struct Point<T> {
pub(crate) bucket: NaiveDate,
#[serde(flatten)]
pub(crate) value: T,
}
#[derive(Serialize)]
pub(crate) struct Compared<T> {
pub(crate) current: T,
pub(crate) prior: T,
pub(crate) delta: BTreeMap<&'static str, Option<f64>>,
}
impl<K: Ord + Clone, T> Group<K, T> {
fn of<I>(items: impl IntoIterator<Item = (NaiveDate, K, I)>, window: &Window) -> Self
where
T: Measure<I>,
{
let compare = window.current().zip(window.prior);
let mut axis = BTreeSet::new();
let mut lines = BTreeMap::<K, (T, BTreeMap<NaiveDate, T>, Option<(T, T)>)>::new();
for (bucket, key, item) in items {
let shown = window.shows(bucket);
let side =
compare.map(|(current, prior)| (current.contains(bucket), prior.contains(bucket)));
if !shown && !side.is_some_and(|(_, prior)| prior) {
continue;
}
let (whole, buckets, compared) = lines.entry(key).or_insert_with(|| {
(
T::default(),
BTreeMap::new(),
compare.map(|_| Default::default()),
)
});
if shown {
axis.insert(bucket);
whole.add(&item);
buckets.entry(bucket).or_default().add(&item);
}
if let (Some((current, prior)), Some((in_current, in_prior))) = (compared, side) {
if in_current {
current.add(&item);
} else if in_prior {
prior.add(&item);
}
}
}
Self {
axis: axis.into_iter().collect(),
lines: lines
.into_iter()
.map(|(key, (window, buckets, compared))| Line {
key,
window,
buckets: buckets
.into_iter()
.map(|(bucket, value)| Point { bucket, value })
.collect(),
comparison: compared.map(|(current, prior)| Compared::of(current, prior)),
})
.collect(),
}
}
}
impl<T: Headline> Compared<T> {
fn of(current: T, prior: T) -> Self {
let before: BTreeMap<_, _> = prior
.headline()
.into_iter()
.map(|reading| (reading.name, reading.value))
.collect();
let delta = current
.headline()
.into_iter()
.map(|now| {
let then = before.get(now.name).copied().flatten();
(now.name, now.value.zip(then).map(|(now, then)| now - then))
})
.collect();
Self {
current,
prior,
delta,
}
}
pub(crate) fn text(&self) -> String {
self.current
.headline()
.into_iter()
.map(|now| {
let change = self.delta.get(now.name).copied().flatten();
format!("{} {}", label(now.name), now.unit.change(change))
})
.chain([format!("n {}, prior {}", self.current.n(), self.prior.n())])
.collect::<Vec<_>>()
.join("; ")
}
}
pub(crate) struct Spark {
pub(crate) path: String,
pub(crate) label: String,
}
impl<K, T: Headline> Line<K, T> {
pub(crate) fn spark(&self, axis: &[NaiveDate]) -> Spark {
let drawn = self.window.headline().into_iter().next();
let (name, unit) = drawn.map_or(("", Unit::Number), |first| (first.name, first.unit));
let values: Vec<_> = axis
.iter()
.map(|bucket| {
let value = self
.buckets
.iter()
.find(|point| point.bucket == *bucket)
.and_then(|point| point.value.headline().into_iter().next()?.value);
(*bucket, value)
})
.collect();
spark(name, unit, &values)
}
}
fn label(name: &str) -> String {
name.replace('_', " ")
}
fn spark(name: &str, unit: Unit, values: &[(NaiveDate, Option<f64>)]) -> Spark {
let top = values
.iter()
.filter_map(|(_, value)| *value)
.fold(0.0, f64::max);
let step = if values.len() > 1 {
100.0 / (values.len() - 1) as f64
} else {
0.0
};
let mut path = Vec::new();
let mut drawing = false;
for (index, (_, value)) in values.iter().enumerate() {
let Some(value) = value else {
drawing = false;
continue;
};
let alone = !drawing && values.get(index + 1).is_none_or(|(_, next)| next.is_none());
let x = if values.len() > 1 {
index as f64 * step
} else {
50.0
};
let y = if top > 0.0 {
18.0 - 16.0 * value / top
} else {
18.0
};
path.push(format!("{}{x:.2} {y:.2}", if drawing { "L" } else { "M" }));
if alone {
path.push(format!("L{x:.2} {y:.2}"));
}
drawing = true;
}
let readings = values
.iter()
.map(|(bucket, value)| format!("{bucket} {}", unit.value(*value)))
.collect::<Vec<_>>()
.join(", ");
Spark {
path: path.join(" "),
label: format!("{}: {readings}", label(name)),
}
}
fn ratio(part: f64, whole: f64) -> Option<f64> {
(whole > 0.0).then(|| part / whole)
}
#[derive(Clone, Copy)]
pub(super) enum Fate {
Merged,
Closed,
Open,
NotKept,
}
#[derive(Default, Serialize)]
pub(crate) struct Survival {
#[serde(skip)]
fates: BTreeMap<String, Fate>,
pub(crate) merged: usize,
pub(crate) closed: usize,
pub(crate) open: usize,
pub(crate) not_kept: usize,
pub(crate) survival_rate: Option<f64>,
}
impl Measure<(String, Fate)> for Survival {
fn add(&mut self, (url, fate): &(String, Fate)) {
self.fates.entry(url.clone()).or_insert(*fate);
let count = |wanted: fn(Fate) -> bool| self.fates.values().filter(|f| wanted(**f)).count();
self.merged = count(|fate| matches!(fate, Fate::Merged));
self.closed = count(|fate| matches!(fate, Fate::Closed));
self.open = count(|fate| matches!(fate, Fate::Open));
self.not_kept = count(|fate| matches!(fate, Fate::NotKept));
self.survival_rate = ratio(self.merged as f64, (self.merged + self.closed) as f64);
}
}
impl Survival {
pub(crate) fn rate(&self) -> String {
Unit::Fraction.value(self.survival_rate)
}
}
impl Headline for Survival {
fn headline(&self) -> Vec<Reading> {
vec![
reading("survival_rate", Unit::Fraction, self.survival_rate),
reading("merged", Unit::Number, Some(self.merged as f64)),
reading("closed_unmerged", Unit::Number, Some(self.closed as f64)),
]
}
fn n(&self) -> usize {
self.merged + self.closed
}
}
#[derive(Default, Serialize)]
pub(crate) struct Cohort {
#[serde(skip)]
origins: BTreeMap<String, usize>,
pub(crate) merged: usize,
pub(crate) confirmed_bug_origins: usize,
pub(crate) bug_origins_per_merged: Option<f64>,
}
impl Measure<(String, usize)> for Cohort {
fn add(&mut self, (url, origins): &(String, usize)) {
*self.origins.entry(url.clone()).or_default() += origins;
self.merged = self.origins.len();
self.confirmed_bug_origins = self.origins.values().sum();
self.bug_origins_per_merged = ratio(self.confirmed_bug_origins as f64, self.merged as f64);
}
}
impl Cohort {
pub(crate) fn per_merged(&self) -> String {
Unit::Number.value(self.bug_origins_per_merged)
}
}
impl Headline for Cohort {
fn headline(&self) -> Vec<Reading> {
vec![
reading(
"confirmed_bug_origins",
Unit::Number,
Some(self.confirmed_bug_origins as f64),
),
reading(
"bug_origins_per_merged",
Unit::Number,
self.bug_origins_per_merged,
),
]
}
fn n(&self) -> usize {
self.merged
}
}
#[derive(Default, Serialize)]
pub(crate) struct CriterionScores {
pub(crate) graded: usize,
pub(crate) passed: usize,
pub(crate) pass_rate: Option<f64>,
}
pub(super) struct Scored {
passed: bool,
calibrated: bool,
}
impl Measure<Scored> for CriterionScores {
fn add(&mut self, scored: &Scored) {
self.graded += 1;
self.passed += usize::from(scored.passed);
self.pass_rate =
ratio(self.passed as f64, self.graded as f64).filter(|_| scored.calibrated);
}
}
impl CriterionScores {
pub(crate) fn rate(&self) -> String {
Unit::Fraction.value(self.pass_rate)
}
}
impl Headline for CriterionScores {
fn headline(&self) -> Vec<Reading> {
vec![reading("pass_rate", Unit::Fraction, self.pass_rate)]
}
fn n(&self) -> usize {
self.graded
}
}
#[derive(Default, Serialize)]
pub(crate) struct Interventions {
pub(crate) commands: usize,
pub(crate) needs_you: usize,
pub(crate) stops: usize,
}
pub(super) struct Asked {
needs_you: bool,
stop: bool,
}
impl Measure<Asked> for Interventions {
fn add(&mut self, asked: &Asked) {
self.commands += 1;
self.needs_you += usize::from(asked.needs_you);
self.stops += usize::from(asked.stop);
}
}
impl Headline for Interventions {
fn headline(&self) -> Vec<Reading> {
vec![
reading("needs_you", Unit::Number, Some(self.needs_you as f64)),
reading("stops", Unit::Number, Some(self.stops as f64)),
]
}
fn n(&self) -> usize {
self.commands
}
}
#[derive(Default, Serialize)]
pub(crate) struct Efficiency {
pub(crate) ended: usize,
pub(crate) succeeded: usize,
pub(crate) succeeded_minutes: Signal,
pub(crate) succeeded_repairs: Signal,
pub(crate) succeeded_ci_rounds: Signal,
pub(crate) first_pass_clean: usize,
pub(crate) first_pass_clean_rate: Option<f64>,
pub(crate) attempt_minutes: Signal,
pub(crate) attempt_cost_usd: Signal,
pub(crate) minutes_per_success: Option<f64>,
pub(crate) cost_usd_per_success: Option<f64>,
pub(crate) wasted_minutes: Signal,
pub(crate) wasted_cost_usd: Signal,
pub(crate) wasted_time_share: Option<f64>,
pub(crate) wasted_spend_share: Option<f64>,
}
pub(super) struct Attempt {
class: Class,
minutes: Option<f64>,
cost_usd: Option<f64>,
repairs: Option<usize>,
ci_rounds: Option<usize>,
}
impl Measure<Attempt> for Efficiency {
fn add(&mut self, attempt: &Attempt) {
self.ended += 1;
self.attempt_minutes.add(attempt.minutes);
self.attempt_cost_usd.add(attempt.cost_usd);
match attempt.class {
Class::Succeeded => {
self.succeeded += 1;
self.succeeded_minutes.add(attempt.minutes);
self.succeeded_repairs
.add(attempt.repairs.map(|n| n as f64));
self.succeeded_ci_rounds
.add(attempt.ci_rounds.map(|n| n as f64));
self.first_pass_clean += usize::from(attempt.repairs == Some(0));
}
Class::Failed | Class::Stopped => {
self.wasted_minutes.add(attempt.minutes);
self.wasted_cost_usd.add(attempt.cost_usd);
}
Class::Paused | Class::Live => {}
}
self.first_pass_clean_rate = ratio(
self.first_pass_clean as f64,
self.succeeded_repairs.observed as f64,
);
let per_success =
|total: &Signal| total.sum.and_then(|sum| ratio(sum, self.succeeded as f64));
self.minutes_per_success = per_success(&self.attempt_minutes);
self.cost_usd_per_success = per_success(&self.attempt_cost_usd);
let share = |wasted: &Signal, total: &Signal| {
total
.sum
.and_then(|sum| ratio(wasted.sum.unwrap_or(0.0), sum))
};
self.wasted_time_share = share(&self.wasted_minutes, &self.attempt_minutes);
self.wasted_spend_share = share(&self.wasted_cost_usd, &self.attempt_cost_usd);
}
}
fn covered(unit: Unit, value: Option<f64>, signal: &Signal) -> String {
format!(
"{} ({} observed, {} missing)",
unit.value(value),
signal.observed,
signal.missing
)
}
impl Efficiency {
pub(crate) fn first_pass(&self) -> String {
format!(
"{}/{} ({})",
self.first_pass_clean,
self.succeeded_repairs.observed,
Unit::Fraction.value(self.first_pass_clean_rate)
)
}
pub(crate) fn minutes_per_success_text(&self) -> String {
covered(
Unit::Number,
self.minutes_per_success,
&self.attempt_minutes,
)
}
pub(crate) fn cost_per_success_text(&self) -> String {
covered(Unit::Usd, self.cost_usd_per_success, &self.attempt_cost_usd)
}
pub(crate) fn wasted_time_text(&self) -> String {
covered(Unit::Fraction, self.wasted_time_share, &self.wasted_minutes)
}
pub(crate) fn wasted_spend_text(&self) -> String {
covered(
Unit::Fraction,
self.wasted_spend_share,
&self.wasted_cost_usd,
)
}
}
impl Headline for Efficiency {
fn headline(&self) -> Vec<Reading> {
vec![
reading(
"minutes_per_success",
Unit::Number,
self.minutes_per_success,
),
reading("cost_usd_per_success", Unit::Usd, self.cost_usd_per_success),
reading(
"first_pass_clean_rate",
Unit::Fraction,
self.first_pass_clean_rate,
),
reading("wasted_time_share", Unit::Fraction, self.wasted_time_share),
reading(
"wasted_spend_share",
Unit::Fraction,
self.wasted_spend_share,
),
reading(
"succeeded_minutes",
Unit::Number,
self.succeeded_minutes.mean,
),
reading(
"succeeded_repairs",
Unit::Number,
self.succeeded_repairs.mean,
),
reading(
"succeeded_ci_rounds",
Unit::Number,
self.succeeded_ci_rounds.mean,
),
]
}
fn n(&self) -> usize {
self.ended
}
}
#[derive(Default, Serialize)]
pub(crate) struct Friction {
pub(crate) runs: usize,
pub(crate) stages: BTreeMap<String, Above>,
#[serde(flatten)]
pub(crate) all_stages: Above,
pub(crate) sessions: usize,
pub(crate) missing_sessions: usize,
pub(crate) repeated_tool_calls: Signal,
pub(crate) tool_errors: Signal,
pub(crate) permission_denials: Signal,
pub(crate) killed_work: Signal,
pub(crate) destructive_sessions: usize,
pub(crate) destructive_checked: usize,
pub(crate) destructive_incidence_rate: Option<f64>,
pub(crate) security_holds: usize,
pub(crate) security_observed: usize,
pub(crate) security_missing: usize,
}
#[derive(Default, Serialize)]
pub(crate) struct Above {
pub(crate) above: usize,
pub(crate) ranged: usize,
pub(crate) above_share: Option<f64>,
}
impl Above {
fn add(&mut self, position: Position) {
if position == Position::TooFewRuns {
return;
}
self.ranged += 1;
self.above += usize::from(position == Position::Above);
self.above_share = ratio(self.above as f64, self.ranged as f64);
}
pub(crate) fn text(&self) -> String {
format!(
"{}/{} ({})",
self.above,
self.ranged,
Unit::Fraction.value(self.above_share)
)
}
}
pub(super) struct Trajectory<'a> {
stages: Vec<(&'a str, Position)>,
sessions: Vec<Option<&'a Signals>>,
security: Option<usize>,
}
impl Measure<Trajectory<'_>> for Friction {
fn add(&mut self, run: &Trajectory<'_>) {
self.runs += 1;
for (name, position) in &run.stages {
self.stages
.entry((*name).to_owned())
.or_default()
.add(*position);
self.all_stages.add(*position);
}
for session in &run.sessions {
let Some(signals) = session else {
self.missing_sessions += 1;
continue;
};
self.sessions += 1;
self.repeated_tool_calls
.add(Some(signals.repeated_tool_calls as f64));
self.tool_errors.add(Some(signals.tool_errors as f64));
self.permission_denials
.add(Some(signals.permission_denials as f64));
self.killed_work.add(Some(signals.killed_work as f64));
if let Some(flags) = &signals.destructive_commands {
self.destructive_checked += 1;
self.destructive_sessions += usize::from(!flags.is_empty());
}
}
self.destructive_incidence_rate = ratio(
self.destructive_sessions as f64,
self.destructive_checked as f64,
);
match run.security {
Some(count) => {
self.security_observed += 1;
self.security_holds += usize::from(count > 0);
}
None => self.security_missing += 1,
}
}
}
impl Friction {
pub(crate) fn stages_text(&self) -> String {
self.stages
.iter()
.map(|(name, above)| format!("{name} {}", above.text()))
.collect::<Vec<_>>()
.join(", ")
}
pub(crate) fn destructive(&self) -> String {
format!(
"{}/{} ({})",
self.destructive_sessions,
self.destructive_checked,
Unit::Fraction.value(self.destructive_incidence_rate)
)
}
pub(crate) fn security(&self) -> String {
format!(
"{} ({} observed, {} missing)",
self.security_holds, self.security_observed, self.security_missing
)
}
}
impl Headline for Friction {
fn headline(&self) -> Vec<Reading> {
vec![
reading(
"above_usual_range_share",
Unit::Fraction,
self.all_stages.above_share,
),
reading(
"repeated_tool_calls",
Unit::Number,
self.repeated_tool_calls.mean,
),
reading("tool_errors", Unit::Number, self.tool_errors.mean),
reading(
"permission_denials",
Unit::Number,
self.permission_denials.mean,
),
reading("killed_work", Unit::Number, self.killed_work.mean),
reading(
"destructive_incidence_rate",
Unit::Fraction,
self.destructive_incidence_rate,
),
reading(
"security_holds",
Unit::Number,
Some(self.security_holds as f64),
),
]
}
fn n(&self) -> usize {
self.runs
}
}
pub(super) fn read(facts: &[Fact<'_>], window: &Window, sources: &Sources<'_>) -> Groups {
let kind = |fact: &Fact<'_>| AccountKind {
account: fact.counted.account.clone(),
kind: fact.counted.kind,
};
let ended = || {
facts
.iter()
.filter(|fact| fact.counted.class != Class::Live)
};
let pull_requests = || {
ended().filter_map(|fact| {
let url = fact.command.record.pull_request.as_deref()?;
let key = super::super::reading::pull_request_key(url);
let kept = sources.kept.get(&key);
Some((fact, key, kept))
})
};
let survival = pull_requests().map(|(fact, key, kept)| {
let fate = match kept.map(|kept| kept.state) {
Some(PrState::Merged) => Fate::Merged,
Some(PrState::Closed) => Fate::Closed,
Some(PrState::Open) => Fate::Open,
None => Fate::NotKept,
};
(fact.counted.bucket, kind(fact), (key, fate))
});
let cohorts = pull_requests().filter_map(|(fact, key, kept)| {
let origins = sources.origins.for_run(fact.command);
let record = &fact.command.record;
let merged_at = match kept {
Some(kept) if kept.state == PrState::Merged => kept.merged_at.as_deref()?,
Some(_) => return None,
None if origins > 0 || record.outcome.as_deref() == Some("merged") => {
record.ended_at.as_deref()?
}
None => return None,
};
let merged = DateTime::parse_from_rfc3339(merged_at).ok()?;
let bucket = window.bucket_on(merged.with_timezone(&Utc).date_naive());
Some((bucket, kind(fact), (key, origins)))
});
let interventions = facts.iter().map(|fact| {
let needs_you = match fact.counted.class {
Class::Failed | Class::Paused => true,
Class::Stopped => !fact.requeued,
Class::Succeeded | Class::Live => false,
} || security(fact.command).is_some_and(|count| count > 0)
|| fact.command.record.security_findings_waiting;
(
fact.counted.bucket,
kind(fact),
Asked {
needs_you,
stop: fact.stop_asked,
},
)
});
let efficiency = ended().map(|fact| {
let metrics = fact.command.record.run_metrics.as_ref();
(
fact.counted.bucket,
Setup {
account: fact.counted.account.clone(),
kind: fact.counted.kind,
harness: fact.command.record.harness.clone(),
model: fact.command.record.model.clone(),
},
Attempt {
class: fact.counted.class,
minutes: attempt_minutes(fact.command),
cost_usd: cost_usd(&sessions(fact.command)),
repairs: metrics.map(|metrics| metrics.repairs),
ci_rounds: metrics.map(|metrics| metrics.ci_rounds),
},
)
});
let friction = ended().filter_map(|fact| {
let metrics = fact.command.record.run_metrics.as_ref()?;
Some((
fact.counted.bucket,
kind(fact),
Trajectory {
stages: metrics
.stages
.iter()
.map(|stage| (stage.name.as_str(), stage.position))
.collect(),
sessions: sessions(fact.command),
security: security(fact.command),
},
))
});
Groups {
yield_: Yield {
pull_requests: Group::of(survival, window),
},
quality: Quality {
bug_origins: Group::of(cohorts, window),
scores: Group::of(scores(sources, window), window),
interventions: Group::of(interventions, window),
},
efficiency: Group::of(efficiency, window),
friction: Group::of(friction, window),
}
}
fn scores(sources: &Sources<'_>, window: &Window) -> Vec<(NaiveDate, CriterionKey, Scored)> {
let mut items = Vec::new();
for week in &sources.scores.weeks {
let last_day = (week.end - chrono::Duration::seconds(1)).max(week.start);
let bucket = window.bucket_on(last_day.date_naive());
for pr in &week.pull_requests {
if !sources
.filters
.admit_scored(&pr.repository, &pr.harness, &pr.model)
{
continue;
}
for (criterion, passed) in &pr.scores {
let calibrated = sources
.scores
.calibrated
.get(criterion)
.copied()
.unwrap_or(false);
items.push((
bucket,
CriterionKey {
criterion: criterion.name(),
calibrated,
},
Scored {
passed: *passed,
calibrated,
},
));
}
}
}
items
}
fn security(command: &Command) -> Option<usize> {
command
.record
.run_metrics
.as_ref()?
.unaddressed_findings
.as_ref()?
.security
}
fn attempt_minutes(command: &Command) -> Option<f64> {
if let Some(metrics) = &command.record.run_metrics {
return metrics.total_minutes;
}
if command.state != "died" {
return None;
}
let began = DateTime::parse_from_rfc3339(&command.record.started_at).ok()?;
let last: DateTime<Utc> = std::fs::metadata(&command.command_log)
.ok()?
.modified()
.ok()?
.into();
let milliseconds = (last - began.with_timezone(&Utc)).num_milliseconds();
(milliseconds >= 0).then(|| milliseconds as f64 / 60_000.0)
}
fn sessions(command: &Command) -> Vec<Option<&Signals>> {
let Some(metrics) = &command.record.run_metrics else {
return Vec::new();
};
let mut seen = BTreeSet::new();
metrics
.stages
.iter()
.enumerate()
.filter(|(_, stage)| StageName::is_session(&stage.name))
.filter(|(index, stage)| {
command
.record
.stages
.get(*index)
.filter(|recorded| recorded.name == stage.name)
.and_then(|recorded| recorded.session_log.as_deref())
.is_none_or(|log| seen.insert(super::super::session::original(log)))
})
.map(|(_, stage)| {
stage
.session_signals
.as_ref()
.map(|session| &session.signals)
})
.collect()
}
fn cost_usd(sessions: &[Option<&Signals>]) -> Option<f64> {
if sessions.is_empty() {
return None;
}
sessions
.iter()
.map(|session| session.and_then(|signals| signals.cost_usd))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_missing_session_cost_leaves_the_commands_spend_missing() {
let priced = Signals {
cost_usd: Some(1.5),
..Signals::default()
};
assert_eq!(cost_usd(&[Some(&priced), Some(&priced)]), Some(3.0));
assert_eq!(cost_usd(&[Some(&priced), None]), None);
assert_eq!(cost_usd(&[]), None);
}
#[test]
fn sparklines_break_at_missing_buckets_and_say_their_values() {
let day = |d: u32| NaiveDate::from_ymd_opt(2026, 11, d).unwrap();
let drawn = spark(
"wasted_time_share",
Unit::Fraction,
&[(day(1), Some(0.5)), (day(2), None), (day(3), Some(0.25))],
);
assert_eq!(
drawn.path,
"M0.00 2.00 L0.00 2.00 M100.00 10.00 L100.00 10.00"
);
let one = spark("needs_you", Unit::Number, &[(day(1), Some(2.0))]);
assert_eq!(one.path, "M50.00 2.00 L50.00 2.00");
assert_eq!(
drawn.label,
"wasted time share: 2026-11-01 50%, 2026-11-02 missing, 2026-11-03 25%"
);
}
#[test]
fn deltas_need_both_windows() {
let mut current = Interventions::default();
current.add(&Asked {
needs_you: true,
stop: true,
});
let compared = Compared::of(current, Interventions::default());
assert_eq!(compared.delta["needs_you"], Some(1.0));
assert_eq!(compared.text(), "needs you +1; stops +1; n 1, prior 0");
let mut current = CriterionScores::default();
current.add(&Scored {
passed: true,
calibrated: true,
});
let compared = Compared::of(current, CriterionScores::default());
assert_eq!(compared.text(), "pass rate missing; n 1, prior 0");
}
}