//! What the dashboard shows, built from the core's events (`app/src/admin.spec.md`, "Dashboard data"). //! //! Pure: `on_event` takes the time, so every rule is tested with made-up events. Captions hold what //! people said for the screen only; nothing here is logged or written. use std::collections::{HashMap, VecDeque}; use std::time::Instant; use serde::Serialize; use serde_json::{json, Value}; use crate::conversation::Missed; use crate::discord::CoreEvent; pub const CAPTIONS_KEPT: usize = 30; pub const ANSWERS_KEPT: usize = 20; pub const MISSED_SHOWN: usize = 20; #[derive(Debug, Clone, PartialEq, Serialize)] pub struct Caption { pub speaker: String, pub name: String, pub text: String, #[serde(rename = "final")] pub is_final: bool, #[serde(skip)] utt: u32, } /// One voice answer's stages in milliseconds (None: that stage could not be seen, e.g. no answer text /// before the sound). #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Default)] #[serde(rename_all = "camelCase")] pub struct AnswerTiming { /// Speech end to its final transcript. pub final_ms: Option, /// The answer asked to its text. pub brain_ms: Option, /// The text to the first sound of the answer. pub voice_ms: Option, /// Speech end to the first sound. pub total_ms: Option, } struct Pending { asked: Instant, /// When the request's speech ended and when its final came. heard: (Instant, Instant), reply: Option, } #[derive(Default)] pub struct Dashboard { bot_tag: Option, voice_channel: Option, /// The voice session Jarvis is in (from `Joined`). session: Option, captions: VecDeque, pending: HashMap, answers: VecDeque, } fn ms(from: Instant, to: Instant) -> u64 { to.saturating_duration_since(from).as_millis() as u64 } impl Dashboard { pub fn on_event(&mut self, ev: &CoreEvent, now: Instant) { match ev { CoreEvent::Ready { tag, .. } => self.bot_tag = Some(tag.clone()), CoreEvent::Joined { channel_id, session, .. } => { self.voice_channel = Some(*channel_id); self.session = Some(*session); } CoreEvent::Left { .. } => { self.voice_channel = None; self.session = None; self.captions.clear(); self.pending.clear(); } // A transcript belongs to the session its utterance started in: one of an earlier session // (arriving after a leave, or after a move to another channel) is not shown. CoreEvent::Partial(h, text) if self.session.is_some() && h.session == self.session => self.caption(h.speaker_id, &h.speaker_name, h.utt, text, false), CoreEvent::Final(f) if self.session.is_some() && f.heard.session == self.session => { let h = &f.heard; self.caption(h.speaker_id, &h.speaker_name, h.utt, &f.text, true); } // The request's own times: a second question waiting behind the first does not move them. CoreEvent::AnswerAsked { id, ended_at, final_at, .. } => { self.pending.insert(id.clone(), Pending { asked: now, heard: (*ended_at, *final_at), reply: None }); } CoreEvent::ReplyText { id, .. } => { if let Some(p) = self.pending.get_mut(id) { p.reply = Some(now); } } CoreEvent::SpeakStart(id) => { if let Some(p) = self.pending.remove(id) { let t = AnswerTiming { final_ms: Some(ms(p.heard.0, p.heard.1)), brain_ms: p.reply.map(|r| ms(p.asked, r)), voice_ms: p.reply.map(|r| ms(r, now)), total_ms: Some(ms(p.heard.0, now)), }; if self.answers.len() == ANSWERS_KEPT { self.answers.pop_front(); } self.answers.push_back(t); } } // An answer that ends without starting (failed, stopped) has nothing to time. CoreEvent::SpeakEnd(e) => { self.pending.remove(&e.id); } _ => {} } } /// A partial replaces the same utterance's earlier caption; the final replaces the partial /// (an empty final removes it). fn caption(&mut self, speaker: u64, name: &str, utt: u32, text: &str, is_final: bool) { let id = speaker.to_string(); let pos = self.captions.iter().position(|c| c.speaker == id && c.utt == utt && !c.is_final); if let Some(i) = pos { self.captions.remove(i); } if text.trim().is_empty() { return; } if self.captions.len() == CAPTIONS_KEPT { self.captions.pop_front(); } self.captions.push_back(Caption { speaker: id, name: name.into(), text: text.into(), is_final, utt }); } /// Forget what was said (the services stopped). pub fn clear(&mut self) { *self = Self::default(); } pub fn captions(&self) -> Vec { self.captions.iter().cloned().collect() } pub fn answers(&self) -> Vec { self.answers.iter().copied().collect() } /// The median of each stage over the answers kept. pub fn medians(&self) -> AnswerTiming { fn median(mut v: Vec) -> Option { v.sort_unstable(); (!v.is_empty()).then(|| v[v.len() / 2]) } let pick = |f: fn(&AnswerTiming) -> Option| median(self.answers.iter().filter_map(f).collect()); AnswerTiming { final_ms: pick(|a| a.final_ms), brain_ms: pick(|a| a.brain_ms), voice_ms: pick(|a| a.voice_ms), total_ms: pick(|a| a.total_ms) } } pub fn view(&self, missed: &[Missed], now: Instant) -> Value { json!({ "discord": self.bot_tag, "voiceChannel": self.voice_channel.map(|c| c.to_string()), "captions": self.captions(), "latency": {"answers": self.answers(), "median": self.medians()}, "missed": missed_view(missed, now), }) } } /// Newest first, at most `MISSED_SHOWN`: who, why, how long ago and how long the request was. pub fn missed_view(missed: &[Missed], now: Instant) -> Value { Value::Array(missed.iter().rev().take(MISSED_SHOWN).map(|m| json!({ "speaker": m.speaker.to_string(), "reason": m.reason, "secondsAgo": now.saturating_duration_since(m.at).as_secs(), "chars": m.chars, })).collect()) } #[cfg(test)] mod tests { use super::*; use crate::discord::{Heard, HeardFinal}; use crate::playback::PlaybackEnd; use crate::policy::MissReason; use std::time::Duration; const A: u64 = 11; fn heard_in(utt: u32, session: u64) -> Heard { Heard { speaker_id: A, speaker_name: "민수".into(), utt, session: Some(session) } } fn heard(utt: u32) -> Heard { heard_in(utt, 1) } fn fin_in(utt: u32, text: &str, wait_ms: u64, session: u64) -> CoreEvent { CoreEvent::Final(HeardFinal { heard: heard_in(utt, session), text: text.into(), end_reason: "silence".into(), audio_ms: 900, wait_ms, stt_ms: 200 }) } fn fin(utt: u32, text: &str, wait_ms: u64) -> CoreEvent { fin_in(utt, text, wait_ms, 1) } fn at(t: Instant, ms: u64) -> Instant { t + Duration::from_millis(ms) } fn joined() -> CoreEvent { CoreEvent::Joined { guild_id: 1, channel_id: 5, session: 1 } } fn left() -> CoreEvent { CoreEvent::Left { guild_id: 1, channel_id: 5, reason: "command".into() } } fn asked(id: &str, t: Instant, ended: u64, final_at: u64) -> CoreEvent { CoreEvent::AnswerAsked { id: id.into(), user: A, ended_at: at(t, ended), final_at: at(t, final_at) } } fn reply(id: &str) -> CoreEvent { CoreEvent::ReplyText { id: id.into(), text: "네".into(), failed: false } } #[test] fn a_partial_is_replaced_by_the_next_and_then_by_the_final() { let (mut d, t) = (Dashboard::default(), Instant::now()); d.on_event(&joined(), t); d.on_event(&CoreEvent::Partial(heard(1), "자비스 오늘".into()), t); d.on_event(&CoreEvent::Partial(heard(1), "자비스 오늘 날씨".into()), t); assert_eq!(d.captions().iter().map(|c| (c.text.as_str(), c.is_final)).collect::>(), vec![("자비스 오늘 날씨", false)]); d.on_event(&fin(1, "자비스 오늘 날씨 어때?", 700), t); d.on_event(&CoreEvent::Partial(heard(2), "그리고".into()), t); assert_eq!(d.captions().iter().map(|c| (c.text.as_str(), c.is_final)).collect::>(), vec![("자비스 오늘 날씨 어때?", true), ("그리고", false)]); // An empty final removes its partial. d.on_event(&fin(2, "", 300), t); assert_eq!(d.captions().len(), 1); assert_eq!(d.captions()[0].name, "민수"); } #[test] fn captions_are_capped_and_cleared_when_jarvis_leaves_and_late_ones_stay_out() { let (mut d, t) = (Dashboard::default(), Instant::now()); d.on_event(&joined(), t); for i in 0..(CAPTIONS_KEPT as u32 + 5) { d.on_event(&fin(i, &format!("말 {i}"), 100), t); } let caps = d.captions(); assert_eq!(caps.len(), CAPTIONS_KEPT); assert_eq!(caps.last().unwrap().text, format!("말 {}", CAPTIONS_KEPT + 4)); d.on_event(&left(), t); assert!(d.captions().is_empty()); // A transcript of the session that ended arrives after the leave: it does not come back. d.on_event(&CoreEvent::Partial(heard(90), "늦게 온 말".into()), t); d.on_event(&fin(90, "늦게 온 말", 100), t); assert!(d.captions().is_empty()); assert_eq!(d.view(&[], t)["voiceChannel"], Value::Null); } #[test] fn a_transcript_of_the_channel_jarvis_moved_away_from_stays_out_of_the_new_ones_captions() { let (mut d, t) = (Dashboard::default(), Instant::now()); d.on_event(&joined(), t); d.on_event(&CoreEvent::SpeechStart(heard_in(7, 1)), t); // started in channel A, session 1 d.on_event(&left(), t); d.on_event(&CoreEvent::Joined { guild_id: 1, channel_id: 6, session: 2 }, t); d.on_event(&CoreEvent::Partial(heard_in(7, 1), "A에서 한 말".into()), t); d.on_event(&fin_in(7, "A에서 한 말", 300, 1), t); assert!(d.captions().is_empty(), "session 1's utterance is not session 2's"); // An utterance whose start was not seen in a session (None) is not shown either. d.on_event(&CoreEvent::Partial(Heard { speaker_id: A, speaker_name: "민수".into(), utt: 8, session: None }, "어디서 온 말".into()), t); assert!(d.captions().is_empty()); // The same person speaking in B is. d.on_event(&fin_in(9, "B에서 한 말", 300, 2), t); assert_eq!(d.captions().iter().map(|c| c.text.as_str()).collect::>(), vec!["B에서 한 말"]); } #[test] fn an_answer_is_timed_from_its_own_requests_speech_end_to_its_first_sound() { let (mut d, t) = (Dashboard::default(), Instant::now()); d.on_event(&joined(), t); d.on_event(&asked("r1", t, 300, 1000), at(t, 1010)); d.on_event(&reply("r1"), at(t, 1700)); d.on_event(&CoreEvent::SpeakStart("r1".into()), at(t, 1900)); assert_eq!(d.answers(), vec![AnswerTiming { final_ms: Some(700), brain_ms: Some(690), voice_ms: Some(200), total_ms: Some(1600) }]); // An answer stopped before it started is not timed; the earcon (another id) is not either. d.on_event(&asked("r3", t, 2000, 2500), at(t, 3000)); d.on_event(&CoreEvent::SpeakStart("r2".into()), at(t, 3000)); d.on_event(&CoreEvent::SpeakEnd(PlaybackEnd { id: "r3".into(), played: 0, cancelled: true, failed: false }), at(t, 3100)); d.on_event(&CoreEvent::SpeakStart("r3".into()), at(t, 3200)); assert_eq!(d.answers().len(), 1); } #[test] fn a_second_question_waiting_does_not_change_the_first_answers_times() { let (mut d, t) = (Dashboard::default(), Instant::now()); d.on_event(&joined(), t); // First question ends at 0 (final at 700); the same person asks again, ending at 1000, while // the first is still answered. The first answer starts sounding at 3700. d.on_event(&fin(1, "첫 질문", 700), at(t, 700)); d.on_event(&asked("r1", t, 0, 700), at(t, 710)); d.on_event(&fin(2, "둘째 질문", 700), at(t, 1700)); d.on_event(&reply("r1"), at(t, 3500)); d.on_event(&CoreEvent::SpeakStart("r1".into()), at(t, 3700)); assert_eq!(d.answers()[0].total_ms, Some(3700)); assert_eq!(d.answers()[0].final_ms, Some(700)); } #[test] fn the_medians_cover_the_last_answers_kept() { let (mut d, t) = (Dashboard::default(), Instant::now()); for (i, brain) in [500u64, 900, 700].into_iter().enumerate() { let base = i as u64 * 10_000; d.on_event(&asked(&format!("r{i}"), t, base + 400, base + 1000), at(t, base + 1000)); d.on_event(&reply(&format!("r{i}")), at(t, base + 1000 + brain)); d.on_event(&CoreEvent::SpeakStart(format!("r{i}")), at(t, base + 1000 + brain + 100)); } let m = d.medians(); assert_eq!((m.final_ms, m.brain_ms, m.voice_ms, m.total_ms), (Some(600), Some(700), Some(100), Some(1400))); for i in 0..ANSWERS_KEPT as u64 { d.on_event(&asked(&format!("x{i}"), t, 100_000, 100_000), at(t, 100_000 + i)); d.on_event(&CoreEvent::SpeakStart(format!("x{i}")), at(t, 100_000 + i)); } assert_eq!(d.answers().len(), ANSWERS_KEPT); assert_eq!(d.medians().brain_ms, None, "no answer text seen for the answers kept"); } #[test] fn missed_requests_show_newest_first_without_text() { let t = Instant::now(); let missed: Vec = (0..25).map(|i| Missed { speaker: i, reason: MissReason::Busy, at: t, chars: 7 }).collect(); let v = missed_view(&missed, at(t, 3000)); let a = v.as_array().unwrap(); assert_eq!(a.len(), MISSED_SHOWN); assert_eq!(a[0], json!({"speaker": "24", "reason": "busy", "secondsAgo": 3, "chars": 7})); } }