use std::{
mem::take,
time::{Duration, Instant as WallInstant},
};
use coarsetime::Instant;
use i18n_progress::{Progress, View};
use crate::event::Name;
const TAIL_CAP: usize = 4096;
pub(crate) struct RetryState {
pub(crate) nth: usize,
pub(crate) total: usize,
pub(crate) wait: Duration,
pub(crate) at: WallInstant,
pub(crate) base: String,
pub(crate) err: String,
pub(crate) streak: usize,
}
impl RetryState {
pub(crate) fn rem(&self, now: WallInstant) -> Duration {
self
.wait
.saturating_sub(now.saturating_duration_since(self.at))
}
}
pub(crate) struct Run {
pub(crate) id: u32,
pub(crate) name: Name,
pub(crate) weight: u64,
pub(crate) begin: Instant,
pub(crate) think: String,
pub(crate) reply: String,
pub(crate) retry: Option<RetryState>,
pub(crate) retry_cnt: usize,
}
impl Run {
pub(crate) fn push_tail(&mut self, s: &str, reply: bool) {
self.retry = None;
let buf = if reply {
&mut self.reply
} else {
&mut self.think
};
buf.push_str(s);
if let Some(over) = buf.len().checked_sub(TAIL_CAP) {
buf.drain(..buf.ceil_char_boundary(over));
}
}
}
pub(crate) struct Ended {
pub(crate) name: Name,
pub(crate) sec: f32,
pub(crate) retry_cnt: usize,
pub(crate) err: Option<String>,
}
pub(crate) struct Board {
pub(crate) progress: Progress,
pub(crate) run_li: Vec<Run>,
pub(crate) hit: usize,
pub(crate) ok: usize,
pub(crate) fail: usize,
pub(crate) retry: usize,
begin: Instant,
}
impl Board {
pub(crate) fn new() -> Self {
Self {
progress: Progress::new(0, 0),
run_li: Vec::new(),
hit: 0,
ok: 0,
fail: 0,
retry: 0,
begin: Instant::now(),
}
}
pub(crate) fn plan(&mut self, weight: u64, job: usize) {
self.progress = Progress::new(weight, job);
}
pub(crate) fn hit(&mut self, weight: u64) {
self.hit += 1;
self.progress.skip(weight);
}
pub(crate) fn start(&mut self, id: u32, name: Name, weight: u64) {
self.progress.start();
self.run_li.push(Run {
id,
name,
weight,
begin: Instant::now(),
think: String::new(),
reply: String::new(),
retry: None,
retry_cnt: 0,
});
}
pub(crate) fn run(&mut self, id: u32) -> Option<&mut Run> {
self.run_li.iter_mut().find(|run| run.id == id)
}
pub(crate) fn on_retry(
&mut self,
id: u32,
nth: usize,
total: usize,
wait: Duration,
base: String,
err: String,
) -> Option<&Run> {
let pos = self.run_li.iter().position(|run| run.id == id)?;
self.retry += 1;
let run = &mut self.run_li[pos];
run.retry_cnt += 1;
let streak = run.retry.as_ref().map_or(1, |r| r.streak + 1);
run.retry = Some(RetryState {
nth,
total,
wait,
at: WallInstant::now(),
base,
err,
streak,
});
Some(&self.run_li[pos])
}
pub(crate) fn end(&mut self, id: u32, err: Option<String>) -> Option<(Run, Ended)> {
let pos = self.run_li.iter().position(|run| run.id == id)?;
let mut run = self.run_li.remove(pos);
if err.is_some() {
self.fail += 1;
self.progress.skip(run.weight);
} else {
self.ok += 1;
self.progress.finish(run.weight);
}
let ended = Ended {
name: Name {
lang: take(&mut run.name.lang),
file: take(&mut run.name.file),
},
sec: sec(run.begin),
retry_cnt: run.retry_cnt,
err,
};
Some((run, ended))
}
pub(crate) fn view(&self) -> View {
self.progress.view()
}
pub(crate) fn sec(&self) -> f32 {
sec(self.begin)
}
}
pub(crate) fn sec(begin: Instant) -> f32 {
begin.elapsed().as_f64() as f32
}