use std::{
mem::take,
time::{Duration, Instant as WallInstant},
};
use coarsetime::Instant;
use i18n_progress::Progress;
use crate::event::{Event, Name};
pub(crate) 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) 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) think: 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,
pub(crate) plan_ready: bool,
}
pub(crate) enum Act {
None,
Start(u32),
Text { id: u32, text: String, reply: bool },
Retry(u32),
End(Ended),
Log(String),
Close,
}
impl Board {
pub(crate) fn new() -> Self {
Self {
progress: Progress::new(0),
run_li: Vec::new(),
hit: 0,
ok: 0,
fail: 0,
retry: 0,
begin: Instant::now(),
plan_ready: false,
}
}
pub(crate) fn apply(&mut self, event: Event) -> Act {
match event {
Event::Plan {
job,
concurrency,
cache,
} => {
self.plan(job, concurrency, cache);
Act::None
}
Event::Hit => {
self.hit();
Act::None
}
Event::Grow => {
self.grow();
Act::None
}
Event::Skip => {
self.skip();
Act::None
}
Event::Start { id, name } => {
self.start(id, name);
Act::Start(id)
}
Event::Text { id, text, reply } => Act::Text { id, text, reply },
Event::Retry {
id,
nth,
total,
wait,
base,
err,
} => {
self.on_retry(id, nth, total, wait, base, err);
Act::Retry(id)
}
Event::End { id, err, active } => match self.end(id, err, active) {
Some(ended) => Act::End(ended),
None => Act::None,
},
Event::Log(text) => Act::Log(text),
Event::Close => Act::Close,
}
}
pub(crate) fn plan(&mut self, job: usize, concurrency: u16, cache: usize) {
self.progress = Progress::new(job).with_concurrency(concurrency);
self.hit = cache;
self.plan_ready = true;
}
pub(crate) fn hit(&mut self) {
self.hit += 1;
self.progress.hit();
}
pub(crate) fn grow(&mut self) {
self.progress.grow();
}
pub(crate) fn skip(&mut self) {
self.progress.skip();
}
pub(crate) fn start(&mut self, id: u32, name: Name) {
self.run_li.push(Run {
id,
name,
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,
) {
let Some(run) = self.run_li.iter_mut().find(|run| run.id == id) else {
return;
};
self.retry += 1;
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,
});
}
pub(crate) fn end(&mut self, id: u32, err: Option<String>, active: Duration) -> Option<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();
} else {
self.ok += 1;
self.progress.finish(active);
}
let ended = Ended {
name: Name {
from: take(&mut run.name.from),
out: take(&mut run.name.out),
},
sec: sec(run.begin),
retry_cnt: run.retry_cnt,
err,
think: take(&mut run.think),
};
Some(ended)
}
pub(crate) fn sec(&self) -> f32 {
sec(self.begin)
}
}
pub(crate) fn sec(begin: Instant) -> f32 {
begin.elapsed().as_f64() as f32
}