forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.07.2 MiB4b67e80Tahinli1 hour ago400 commits

overview.rs245 lines, 7.9 KiBHistoryRawDownload
1use crate::net::Fail;
2use common::{
3 call::{GetDailyStats, GetOverview, GetServerStats},
4 model::DayStat,
5 time::DAY_MILLIS,
6};
7use gpui_kit::{Context, Div, IntoElement, ParentElement, Styled, Window, div, px};
8use jiff::Timestamp;
9use std::time::Duration;
10
11use super::ui::{card, card_header, stat_tile};
12use crate::gui::{
13 Palette, Yumush,
14 builder::BuilderTab,
15 chat::View,
16 storage,
17 style::{radius, size, space, text},
18 text, voice_stats,
19};
20use crate::lang::w;
21
22const CHART_HEIGHT: f32 = 80.;
23const PANEL_HEIGHT: f32 = 140.;
24const SERVER_STATS_TICK: Duration = Duration::from_secs(1);
25
26fn day_label(day: u64) -> String {
27 Timestamp::from_millisecond((day * DAY_MILLIS) as i64)
28 .map(|timestamp| {
29 let format = crate::gui::month_named(
30 w().common.day,
31 timestamp.to_zoned_datetime(jiff::tz::TimeZone::UTC).month(),
32 );
33 timestamp.strftime(&format).to_string()
34 })
35 .unwrap_or_default()
36}
37
38fn tile_row(tiles: &[(&str, String)], palette: Palette) -> Div {
39 div()
40 .flex()
41 .gap(px(space::MD))
42 .children(tiles.iter().map(|(label, value)| {
43 stat_tile(label, value, palette)
44 .flex_1()
45 .min_w(px(size::SIDEBAR))
46 }))
47}
48
49fn bar_chart(
50 title: &'static str,
51 stats: &[DayStat],
52 value: fn(&DayStat) -> u64,
53 summary: u64,
54 palette: Palette,
55) -> Div {
56 let values: Vec<u64> = stats.iter().map(value).collect();
57 let maximum = values.iter().copied().max().unwrap_or_default();
58 let first = stats
59 .first()
60 .map(|stat| day_label(stat.day))
61 .unwrap_or_default();
62 let last = stats
63 .last()
64 .map(|stat| day_label(stat.day))
65 .unwrap_or_default();
66
67 card(palette)
68 .flex_1()
69 .min_w_0()
70 .child(card_header(
71 title,
72 vec![
73 div()
74 .text_size(text(text::CAPTION))
75 .text_color(palette.muted)
76 .child(summary.to_string())
77 .into_any_element(),
78 ],
79 palette,
80 ))
81 .child(
82 div()
83 .h(px(PANEL_HEIGHT))
84 .py(px(space::MD))
85 .flex()
86 .flex_col()
87 .gap(px(space::XS))
88 .child(
89 div()
90 .text_size(text(text::CAPTION))
91 .text_color(palette.muted)
92 .child(maximum.to_string()),
93 )
94 .child(
95 div()
96 .h(px(CHART_HEIGHT))
97 .flex()
98 .items_end()
99 .gap(px(space::XXS))
100 .children(values.iter().map(|&value| {
101 let (height, color) = if value == 0 {
102 (space::XXS, palette.hover)
103 } else {
104 (
105 (CHART_HEIGHT * value as f32 / maximum as f32).max(space::XXS),
106 palette.accent,
107 )
108 };
109
110 div()
111 .flex_1()
112 .h(px(height))
113 .rounded_t(px(radius::SM))
114 .bg(color)
115 })),
116 )
117 .child(
118 div()
119 .pt(px(space::XS))
120 .border_t_1()
121 .border_color(palette.border)
122 .flex()
123 .justify_between()
124 .text_size(text(text::CAPTION))
125 .text_color(palette.muted)
126 .child(first)
127 .child(last),
128 ),
129 )
130}
131
132impl Yumush {
133 fn watch_server_stats(&mut self, cx: &mut Context<Self>) {
134 if self.chat.server_stats_task.is_some() {
135 return;
136 }
137 let network = self.network.clone();
138 self.chat.server_stats_task = Some(cx.spawn(async move |this, cx| {
139 loop {
140 let open = this
141 .update(cx, |yumush, _| {
142 yumush.chat.view == View::Builder
143 && yumush.chat.builder_tab == BuilderTab::Overview
144 })
145 .unwrap_or(false);
146 if !open {
147 let _ = this.update(cx, |yumush, _| yumush.chat.server_stats_task = None);
148 break;
149 }
150 if let Ok(stats) = network.call(GetServerStats).await {
151 let _ = this.update(cx, |yumush, cx| {
152 yumush.chat.server_stats = Some(stats);
153 cx.notify();
154 });
155 }
156 cx.background_executor().timer(SERVER_STATS_TICK).await;
157 }
158 }));
159 }
160
161 pub(super) fn load_overview(&mut self, window: &mut Window, cx: &mut Context<Self>) {
162 self.watch_server_stats(cx);
163 self.fetch_builder(
164 window,
165 cx,
166 |network| async move {
167 Ok::<_, Fail>((
168 network.call(GetOverview).await?,
169 network.call(GetDailyStats { days: 30 }).await?,
170 ))
171 },
172 |yumush, (overview, stats), cx| {
173 yumush.chat.builder_overview = Some(overview);
174 yumush.chat.builder_stats = stats;
175 cx.notify();
176 },
177 );
178 }
179
180 pub(super) fn overview_rows(&self, palette: Palette) -> Vec<Div> {
181 let Some(overview) = &self.chat.builder_overview else {
182 return Vec::new();
183 };
184 let stats = &self.chat.builder_stats;
185 let active = stats.last().map_or(0, |stat| stat.active_users);
186 let messages = stats.iter().map(|stat| stat.messages).sum::<u64>();
187
188 let tiles = [
189 (w().builder_overview.users, overview.users.to_string()),
190 (w().builder_overview.builders, overview.builders.to_string()),
191 (
192 w().builder_overview.communities,
193 overview.communities.to_string(),
194 ),
195 (w().common.presence_online, overview.online.to_string()),
196 (w().builder_overview.in_voice, overview.voice.to_string()),
197 (
198 w().builder_overview.storage,
199 storage(overview.storage_bytes),
200 ),
201 ];
202
203 let mut rows: Vec<Div> = tiles
204 .chunks(3)
205 .map(|chunk| tile_row(chunk, palette))
206 .collect();
207 rows.push(
208 div()
209 .flex()
210 .gap(px(space::MD))
211 .child(bar_chart(
212 w().builder_overview.active_users,
213 stats,
214 |stat| stat.active_users,
215 active,
216 palette,
217 ))
218 .child(bar_chart(
219 w().common.messages,
220 stats,
221 |stat| stat.messages,
222 messages,
223 palette,
224 )),
225 );
226 if let Some(stats) = &self.chat.server_stats {
227 rows.push(
228 card(palette)
229 .child(card_header(
230 w().builder_overview.server,
231 Vec::new(),
232 palette,
233 ))
234 .child(div().pt(px(space::SM)).child(voice_stats::stats_panel(
235 stats,
236 &|id| self.chat.name_of(id),
237 voice_stats::Detail::Full,
238 palette,
239 ))),
240 );
241 }
242
243 rows
244 }
245}