use super::keep;
use super::snapshot::{Count, Place, Snapshot, Turn};
use super::told::{self, Claim, Told};
use crate::assets::{CONTEXT, folded, render};
use crate::store::time::Stamp;
pub const MOST: usize = 8;
const DAY: i64 = 86_400;
const HOST_SCALARS: usize = 120;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Notice {
pub line: String,
pub claim: Claim,
}
fn template(key: &str) -> String {
folded(CONTEXT, key).expect("context.yaml carries the recording notices")
}
pub fn size(bytes: u64) -> String {
const UNITS: [&str; 3] = ["KiB", "MiB", "GiB"];
if bytes < 1024 {
return format!("{bytes} {}", if bytes == 1 { "byte" } else { "bytes" });
}
let (mut value, mut unit) = (bytes as f64 / 1024.0, 0);
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
format!("{value:.1} {}", UNITS[unit])
}
pub fn limit(bytes: u64) -> String {
match bytes {
b if b > 0 && b % (1 << 30) == 0 => format!("{} GiB", b >> 30),
b if b > 0 && b % (1 << 20) == 0 => format!("{} MiB", b >> 20),
b => size(b),
}
}
fn safe(host: &str) -> String {
host.chars()
.filter(|c| !c.is_control())
.take(HOST_SCALARS)
.collect()
}
fn kept(count: Count) -> String {
let size = size(count.bytes);
match count.turns {
1 => render(&template("notice_turn"), &[("size", &size)]),
n => render(
&template("notice_turns"),
&[("count", &n.to_string()), ("size", &size)],
),
}
}
fn line(key: &str, host: &str, count: Count, reasons: &str) -> String {
let variables = [
("host", safe(host)),
("kept", kept(count)),
("reasons", reasons.to_string()),
];
let pairs: Vec<(&str, &str)> = variables.iter().map(|(k, v)| (*k, v.as_str())).collect();
render(&template(key), &pairs)
}
fn reason(name: &str) -> &'static str {
match name {
"storage" => "storage",
"credential" => "credential",
_ => "forbidden",
}
}
fn untold<'a>(turns: &'a [Turn], told: &std::collections::BTreeSet<String>) -> Vec<&'a Turn> {
turns
.iter()
.filter(|turn| !told.contains(&turn.name))
.collect()
}
fn names(turns: &[&Turn]) -> Vec<String> {
turns.iter().map(|turn| turn.name.clone()).collect()
}
fn count(turns: &[&Turn]) -> Count {
Count {
turns: turns.len() as u64,
bytes: turns.iter().map(|turn| turn.bytes).sum(),
}
}
fn held(place: &Place, turns: &[&Turn]) -> Notice {
let mut reasons: Vec<&str> = turns.iter().filter_map(|turn| turn.reason).collect();
reasons.sort_unstable();
reasons.dedup();
let key = match (place.key.is_empty(), reasons.as_slice()) {
(true, _) => "notice_set_aside",
(false, ["refused"]) => "notice_refused",
(false, _) => "notice_held",
};
Notice {
line: line(key, &place.host, count(turns), &reasons.join(", ")),
claim: Claim::Held(names(turns)),
}
}
fn a_day(since: &str, now: Stamp) -> bool {
Stamp::parse(since).is_ok_and(|since| now.instant() - since.instant() >= DAY * 1_000_000)
}
pub fn fresh(snapshot: &Snapshot, told: &Told, now: Stamp) -> Vec<Notice> {
let mut blocks = Vec::new();
let mut failing = Vec::new();
let mut held_ = Vec::new();
let mut unserved = Vec::new();
for place in &snapshot.places {
let waiting = Place::count(&place.pending);
let record = place.record.as_ref();
if let Some(block) = record.and_then(|record| record.block.as_ref()) {
let known = told.blocks.iter().any(|old| {
old.key == place.key && old.reason == block.reason && old.since == block.since
});
if !known {
let key = format!("notice_{}", reason(&block.reason));
blocks.push(Notice {
line: line(&key, &place.host, waiting, ""),
claim: Claim::Block(told::Block {
key: place.key.clone(),
host: safe(&place.host),
reason: block.reason.clone(),
since: block.since.clone(),
}),
});
}
} else if let Some(since) = record.and_then(|record| record.failing_since.as_ref())
&& a_day(since, now)
&& !told
.failing
.iter()
.any(|old| old.key == place.key && old.since == *since)
{
failing.push(Notice {
line: line("notice_failing", &place.host, waiting, ""),
claim: Claim::Failing(told::Failing {
key: place.key.clone(),
since: since.clone(),
}),
});
}
let new = untold(&place.held, &told.held);
if !new.is_empty() {
held_.push(held(place, &new));
}
let new = untold(&place.pending, &told.unserved);
if place.served == Some(false) && !new.is_empty() {
unserved.push(Notice {
line: line("notice_unserved", &place.host, count(&new), ""),
claim: Claim::Unserved(names(&new)),
});
}
}
let ended = told.blocks.iter().filter(|old| {
!snapshot.places.iter().any(|place| {
place.key == old.key && place.record.as_ref().is_some_and(|r| r.block.is_some())
})
});
let ended: Vec<Notice> = ended
.map(|old| Notice {
line: line("notice_unblocked", &old.host, Count::default(), ""),
claim: Claim::Unblock(old.key.clone()),
})
.collect();
let new = untold(&snapshot.loose.held, &told.held);
if !new.is_empty() {
held_.push(held(&snapshot.loose, &new));
}
let mut notices = blocks;
notices.extend(ended);
notices.extend(failing);
notices.extend(held_);
notices.extend(unserved);
notices.extend(dropped(snapshot, told));
notices
}
fn dropped(snapshot: &Snapshot, told: &Told) -> Option<Notice> {
let drops = snapshot.drops.as_ref()?;
let base = told.dropped.as_ref().filter(|old| {
old.first == drops.first && old.turns <= drops.turns && old.bytes <= drops.bytes
});
let (turns, bytes) = base.map_or((drops.turns, drops.bytes), |old| {
(drops.turns - old.turns, drops.bytes - old.bytes)
});
if turns == 0 && bytes == 0 {
return None;
}
let limit = limit(snapshot.limit.unwrap_or(keep::DEFAULT));
let text = match turns {
0 => render(
&template("notice_dropped_bytes"),
&[("limit", &limit), ("size", &size(bytes))],
),
_ => render(
&template("notice_dropped"),
&[("limit", &limit), ("kept", &kept(Count { turns, bytes }))],
),
};
Some(Notice {
line: text,
claim: Claim::Dropped(told::Dropped {
first: drops.first.clone(),
turns: drops.turns,
bytes: drops.bytes,
}),
})
}
pub fn block(notices: &[Notice]) -> String {
let mut lines = vec![template("notice_header")];
lines.extend(notices.iter().map(|notice| notice.line.clone()));
lines.join("\n")
}
pub fn fit(notices: Vec<Notice>, room: usize) -> Vec<Notice> {
let mut kept = Vec::new();
for notice in notices.into_iter().take(MOST) {
kept.push(notice);
if crate::text::len(&block(&kept)) + 2 > room {
kept.pop();
break;
}
}
kept
}