feat(app): M5 step 2, service control and the dashboard data
- control: one Controller starts, stops and restarts the services (stopped, starting, running, stopping, failed); a failed start leaves nothing running and keeps its error with secrets masked. The app binary uses it. - dashboard: built from the core's events alone: the bot's tag, the voice channel, live captions (screen only, cleared on leaving or stopping), the last 20 voice answers split into stages with medians (matched by the new AnswerAsked event), and missed requests without text. - system: memory in use and per NVIDIA GPU its use and VRAM (nvidia-smi). - DiscordCore::voice_members, WorkerClient::hello, the conversation's missed requests shared with the dashboard. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
277
app/src/dashboard.rs
Normal file
277
app/src/dashboard.rs
Normal file
@@ -0,0 +1,277 @@
|
||||
//! 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::{Duration, 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).
|
||||
#[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<u64>,
|
||||
/// The answer asked to its text.
|
||||
pub brain_ms: Option<u64>,
|
||||
/// The text to the first sound of the answer.
|
||||
pub voice_ms: Option<u64>,
|
||||
/// Speech end to the first sound.
|
||||
pub total_ms: Option<u64>,
|
||||
}
|
||||
|
||||
struct Pending {
|
||||
asked: Instant,
|
||||
/// The asker's latest final: when their speech ended and when the final came.
|
||||
heard: Option<(Instant, Instant)>,
|
||||
reply: Option<Instant>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Dashboard {
|
||||
bot_tag: Option<String>,
|
||||
voice_channel: Option<u64>,
|
||||
captions: VecDeque<Caption>,
|
||||
last_final: HashMap<u64, (Instant, Instant)>,
|
||||
pending: HashMap<String, Pending>,
|
||||
answers: VecDeque<AnswerTiming>,
|
||||
}
|
||||
|
||||
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, .. } => self.voice_channel = Some(*channel_id),
|
||||
CoreEvent::Left { .. } => {
|
||||
self.voice_channel = None;
|
||||
self.captions.clear();
|
||||
self.pending.clear();
|
||||
}
|
||||
CoreEvent::Partial(h, text) => self.caption(h.speaker_id, &h.speaker_name, h.utt, text, false),
|
||||
CoreEvent::Final(f) => {
|
||||
let h = &f.heard;
|
||||
self.caption(h.speaker_id, &h.speaker_name, h.utt, &f.text, true);
|
||||
if !f.text.trim().is_empty() {
|
||||
let ended = now.checked_sub(Duration::from_millis(f.wait_ms)).unwrap_or(now);
|
||||
self.last_final.insert(h.speaker_id, (ended, now));
|
||||
}
|
||||
}
|
||||
CoreEvent::AnswerAsked { id, user } => {
|
||||
let heard = self.last_final.get(user).copied();
|
||||
self.pending.insert(id.clone(), Pending { asked: now, heard, 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: p.heard.map(|(e, f)| ms(e, f)),
|
||||
brain_ms: p.reply.map(|r| ms(p.asked, r)),
|
||||
voice_ms: p.reply.map(|r| ms(r, now)),
|
||||
total_ms: p.heard.map(|(e, _)| ms(e, 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<Caption> {
|
||||
self.captions.iter().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn answers(&self) -> Vec<AnswerTiming> {
|
||||
self.answers.iter().copied().collect()
|
||||
}
|
||||
|
||||
/// The median of each stage over the answers kept.
|
||||
pub fn medians(&self) -> AnswerTiming {
|
||||
fn median(mut v: Vec<u64>) -> Option<u64> {
|
||||
v.sort_unstable();
|
||||
(!v.is_empty()).then(|| v[v.len() / 2])
|
||||
}
|
||||
let pick = |f: fn(&AnswerTiming) -> Option<u64>| 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;
|
||||
|
||||
const A: u64 = 11;
|
||||
fn heard(utt: u32) -> Heard {
|
||||
Heard { speaker_id: A, speaker_name: "민수".into(), utt }
|
||||
}
|
||||
fn fin(utt: u32, text: &str, wait_ms: u64) -> CoreEvent {
|
||||
CoreEvent::Final(HeardFinal { heard: heard(utt), text: text.into(), end_reason: "silence".into(),
|
||||
audio_ms: 900, wait_ms, stt_ms: 200 })
|
||||
}
|
||||
fn at(t: Instant, ms: u64) -> Instant {
|
||||
t + Duration::from_millis(ms)
|
||||
}
|
||||
|
||||
#[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(&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<_>>(),
|
||||
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<_>>(),
|
||||
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() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&CoreEvent::Joined { guild_id: 1, channel_id: 5 }, 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(&CoreEvent::Left { guild_id: 1, channel_id: 5, reason: "command".into() }, t);
|
||||
assert!(d.captions().is_empty());
|
||||
assert_eq!(d.view(&[], t)["voiceChannel"], Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_answer_is_timed_from_its_askers_speech_end_to_its_first_sound() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&fin(1, "자비스 몇 시야", 700), at(t, 1000)); // speech ended at 300
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: "r1".into(), user: A }, at(t, 1010));
|
||||
d.on_event(&CoreEvent::ReplyText { id: "r1".into(), text: "열 시입니다.".into(), failed: false }, 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(&CoreEvent::AnswerAsked { id: "r3".into(), user: A }, 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 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(&fin(i as u32, "자비스", 600), at(t, base + 1000));
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: format!("r{i}"), user: A }, at(t, base + 1000));
|
||||
d.on_event(&CoreEvent::ReplyText { id: format!("r{i}"), text: "네".into(), failed: false },
|
||||
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(&CoreEvent::AnswerAsked { id: format!("x{i}"), user: 99 }, 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().final_ms, None, "no final seen for the asker of the answers kept");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missed_requests_show_newest_first_without_text() {
|
||||
let t = Instant::now();
|
||||
let missed: Vec<Missed> = (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}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user