forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

stream_stats.rs127 lines, 4.1 KiBHistoryRawDownload
1use crate::lang::w;
2use common::{model::Id, share::Kind};
3use gpui_kit::{Div, FontWeight, ParentElement, Styled, div, px};
4
5use super::{
6 Palette, Yumush,
7 parts::{Ink, Text, band, chart, popover, words},
8 style::{size, space},
9 voice_stats::{
10 GOOD_LOSS, SAMPLE_HISTORY, engine_label, kind_word, number, resolution_text, stamp,
11 },
12};
13use crate::share::session::WatchStats;
14
15pub(super) fn watch_stats(yumush: &Yumush, user_id: Id, kind: Kind) -> Option<&WatchStats> {
16 yumush
17 .chat
18 .stats
19 .share
20 .watching
21 .iter()
22 .find(|(id, sent, _)| *id == user_id && *sent == kind)
23 .map(|(_, _, stats)| stats)
24}
25
26pub(super) fn stream_card(yumush: &Yumush, user_id: Id, kind: Kind, palette: Palette) -> Div {
27 let t = palette.tokens;
28 let name = yumush
29 .chat
30 .author(user_id)
31 .map_or_else(|| user_id.to_string(), |user| user.username.clone());
32 let stats = watch_stats(yumush, user_id, kind);
33 let losses: Vec<(u64, f32)> = yumush
34 .chat
35 .share_loss
36 .get(&(user_id, kind))
37 .into_iter()
38 .flatten()
39 .copied()
40 .collect();
41 let top = losses
42 .iter()
43 .fold(GOOD_LOSS / 100., |top, (_, loss)| top.max(*loss));
44 let loss = stats.map(|stats| stats.loss * 100.);
45 let bad = loss.is_some_and(|loss| loss > GOOD_LOSS);
46 let field = |value: Option<String>| value.unwrap_or_else(|| "…".into());
47 let detail = stats.map_or("…".to_string(), |stats| {
48 let layer = stats.layer.map_or("…".to_string(), |layer| {
49 format!(
50 "{} · {}",
51 resolution_text(layer, stats.fps),
52 layer.codec.name()
53 )
54 });
55 match stats.engine {
56 Some(engine) => format!("{layer} · {}", engine_label(engine)),
57 None => layer,
58 }
59 });
60
61 popover(t)
62 .w(px(size::POPOVER))
63 .gap(px(space::MD))
64 .child(
65 band(space::SM)
66 .child(
67 words(name, Text::Label, Ink::Paper, t)
68 .text_color(t.person(user_id))
69 .font_weight(FontWeight::MEDIUM)
70 .flex_none(),
71 )
72 .child(words(kind_word(kind), Text::Caption, Ink::Faint, t)),
73 )
74 .child(words(detail, Text::Caption, Ink::Dim, t))
75 .child(
76 band(space::LG)
77 .child(number(
78 field(stats.map(|stats| stats.kbps.to_string())),
79 "kb/s",
80 false,
81 t,
82 ))
83 .child(number(
84 field(loss.map(|loss| format!("{loss:.1}"))),
85 w().stream_stats.loss,
86 bad,
87 t,
88 ))
89 .child(number(
90 field(
91 stats
92 .and_then(|stats| stats.latency_ms)
93 .map(|ms| ms.to_string()),
94 ),
95 w().stream_stats.latency,
96 false,
97 t,
98 )),
99 )
100 .child(
101 div().h(px(size::CONTROL_SM)).child(chart(
102 "loss_chart",
103 losses
104 .iter()
105 .map(|(at, loss)| (loss / top, stamp(*at, format!("{:.1} %", loss * 100.))))
106 .collect(),
107 SAMPLE_HISTORY,
108 if bad { t.bad() } else { t.paper() },
109 t,
110 )),
111 )
112 .child(
113 band(space::MD).children(
114 stats
115 .map(|stats| {
116 [
117 w().stream_stats.buffer(stats.buffer_ms),
118 w().stream_stats.keyframes(stats.keyframes),
119 format!("NACKs {}", stats.nacks),
120 ]
121 .map(|text| words(text, Text::Caption, Ink::Faint, t))
122 })
123 .into_iter()
124 .flatten(),
125 ),
126 )
127}