use std::collections::BTreeMap;
use serde::Serialize;
use super::outcomes::{Class, Fact};
use super::scope::Window;
use crate::logs::FailureCategory;
#[derive(Default, Serialize)]
pub(crate) struct WhereRunsFailed {
pub(crate) failed: usize,
pub(crate) observed: usize,
pub(crate) missing: usize,
pub(crate) rows: Vec<Stopped>,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Ending {
Recorded(FailureCategory),
Died,
Failed,
}
impl Ending {
pub(crate) fn category(self) -> &'static str {
match self {
Self::Recorded(category) => category.name(),
Self::Died => "died",
Self::Failed => "failed",
}
}
pub(crate) fn recorded(self) -> bool {
matches!(self, Self::Recorded(_))
}
pub(crate) fn named(category: &str) -> Option<Self> {
FailureCategory::ALL
.into_iter()
.map(Self::Recorded)
.chain([Self::Died, Self::Failed])
.find(|ending| ending.category() == category)
}
}
impl Serialize for Ending {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
#[derive(Serialize)]
struct Shown {
category: &'static str,
recorded: bool,
}
Shown {
category: self.category(),
recorded: self.recorded(),
}
.serialize(serializer)
}
}
#[derive(Serialize)]
pub(crate) struct Stopped {
pub(crate) stopped_at: Option<String>,
#[serde(flatten)]
pub(crate) ending: Ending,
pub(crate) failed: usize,
pub(crate) commands: Vec<FailedCommand>,
}
impl Stopped {
pub(crate) fn selectors(&self) -> impl Iterator<Item = &String> {
self.commands.iter().map(|command| &command.selector)
}
}
#[derive(Serialize)]
pub(crate) struct FailedCommand {
pub(crate) name: String,
pub(crate) kind: &'static str,
pub(crate) account: Option<String>,
pub(crate) started_at: String,
pub(crate) selector: String,
pub(crate) repository: String,
pub(crate) url: Option<String>,
}
pub(super) fn read(facts: &[Fact<'_>], window: &Window) -> WhereRunsFailed {
let mut rows = BTreeMap::<_, Vec<FailedCommand>>::new();
let mut breakdown = WhereRunsFailed::default();
for fact in facts
.iter()
.filter(|fact| fact.counted.class == Class::Failed && window.shows(fact.counted.bucket))
{
let record = &fact.command.record;
let (stopped_at, ending) = match record.failure_category {
Some(category) => (record.stopped_at.clone(), Ending::Recorded(category)),
None => (
record.stopped_stage().map(str::to_owned),
if fact.command.state == "died" {
Ending::Died
} else {
Ending::Failed
},
),
};
breakdown.failed += 1;
if ending.recorded() {
breakdown.observed += 1;
} else {
breakdown.missing += 1;
}
rows.entry((stopped_at, ending))
.or_default()
.push(FailedCommand {
name: record.name.clone(),
kind: fact.counted.kind,
account: fact.counted.account.clone(),
started_at: record.started_at.clone(),
selector: fact.counted.selector.clone(),
repository: record.repository.clone(),
url: record
.pull_request
.clone()
.or_else(|| record.issue.as_ref().map(|issue| issue.url.clone())),
});
}
breakdown.rows = rows
.into_iter()
.map(|((stopped_at, ending), commands)| Stopped {
stopped_at,
ending,
failed: commands.len(),
commands,
})
.collect();
breakdown
.rows
.sort_by_key(|row| std::cmp::Reverse(row.failed));
breakdown
}