use crate::net::Fail; use common::{ call::{GetDailyStats, GetOverview, GetServerStats}, model::DayStat, time::DAY_MILLIS, }; use gpui_kit::{Context, Div, IntoElement, ParentElement, Styled, Window, div, px}; use jiff::Timestamp; use std::time::Duration; use super::ui::{caption_text, card, card_header, stat_tile}; use crate::gui::{ Yumush, builder::BuilderTab, chat::View, storage, style::{Tokens, radius, size, space}, voice_stats, }; use crate::lang::w; const CHART_HEIGHT: f32 = 80.; const PANEL_HEIGHT: f32 = 140.; const SERVER_STATS_TICK: Duration = Duration::from_secs(1); fn day_label(day: u64) -> String { Timestamp::from_millisecond((day * DAY_MILLIS) as i64) .map(|timestamp| { let format = crate::gui::month_named( w().common.day, timestamp.to_zoned_datetime(jiff::tz::TimeZone::UTC).month(), ); timestamp.strftime(&format).to_string() }) .unwrap_or_default() } fn tile_row(tiles: &[(&str, String)], t: Tokens) -> Div { div().flex().gap(px(space::MD)).children( tiles .iter() .map(|(label, value)| stat_tile(label, value, t).flex_1().min_w(px(size::SIDEBAR))), ) } fn bar_chart( title: &'static str, stats: &[DayStat], value: fn(&DayStat) -> u64, summary: u64, t: Tokens, ) -> Div { let values: Vec = stats.iter().map(value).collect(); let maximum = values.iter().copied().max().unwrap_or_default(); let first = stats .first() .map(|stat| day_label(stat.day)) .unwrap_or_default(); let last = stats .last() .map(|stat| day_label(stat.day)) .unwrap_or_default(); card() .flex_1() .min_w_0() .child(card_header( title, vec![caption_text(summary.to_string(), t.dim()).into_any_element()], t, )) .child( div() .h(px(PANEL_HEIGHT)) .py(px(space::MD)) .flex() .flex_col() .gap(px(space::XS)) .child(caption_text(maximum.to_string(), t.dim())) .child( div() .h(px(CHART_HEIGHT)) .flex() .items_end() .gap(px(space::XXS)) .children(values.iter().map(|&value| { let (height, color) = if value == 0 { (space::XXS, t.hov()) } else { ( (CHART_HEIGHT * value as f32 / maximum as f32).max(space::XXS), t.paper(), ) }; div() .flex_1() .h(px(height)) .rounded_t(px(radius::SM)) .bg(color) })), ) .child( div() .pt(px(space::XS)) .border_t_1() .border_color(t.thread()) .flex() .justify_between() .child(caption_text(first, t.dim())) .child(caption_text(last, t.dim())), ), ) } impl Yumush { fn watch_server_stats(&mut self, cx: &mut Context) { if self.chat.admin.server_stats_task.is_some() { return; } let network = self.network.clone(); self.chat.admin.server_stats_task = Some(cx.spawn(async move |this, cx| { loop { let open = this .update(cx, |yumush, _| { yumush.chat.session.view == View::Builder && yumush.chat.admin.builder_tab == BuilderTab::Overview }) .unwrap_or(false); if !open { let _ = this.update(cx, |yumush, _| yumush.chat.admin.server_stats_task = None); break; } if let Ok(stats) = network.call(GetServerStats).await { let _ = this.update(cx, |yumush, cx| { yumush.chat.admin.server_stats = Some(stats); cx.notify(); }); } cx.background_executor().timer(SERVER_STATS_TICK).await; } })); } pub(super) fn load_overview(&mut self, window: &mut Window, cx: &mut Context) { self.watch_server_stats(cx); self.fetch_builder( window, cx, |network| async move { Ok::<_, Fail>(( network.call(GetOverview).await?, network.call(GetDailyStats { days: 30 }).await?, )) }, |yumush, (overview, stats), cx| { yumush.chat.admin.builder_overview = Some(overview); yumush.chat.admin.builder_stats = stats; cx.notify(); }, ); } pub(super) fn overview_rows(&self, t: Tokens) -> Vec
{ let Some(overview) = &self.chat.admin.builder_overview else { return Vec::new(); }; let stats = &self.chat.admin.builder_stats; let active = stats.last().map_or(0, |stat| stat.active_users); let messages = stats.iter().map(|stat| stat.messages).sum::(); let tiles = [ (w().builder_overview.users, overview.users.to_string()), (w().builder_overview.builders, overview.builders.to_string()), ( w().builder_overview.communities, overview.communities.to_string(), ), (w().common.presence_online, overview.online.to_string()), (w().builder_overview.in_voice, overview.voice.to_string()), ( w().builder_overview.storage, storage(overview.storage_bytes), ), ]; let mut rows: Vec
= tiles.chunks(3).map(|chunk| tile_row(chunk, t)).collect(); rows.push( div() .flex() .gap(px(space::MD)) .child(bar_chart( w().builder_overview.active_users, stats, |stat| stat.active_users, active, t, )) .child(bar_chart( w().common.messages, stats, |stat| stat.messages, messages, t, )), ); if let Some(stats) = &self.chat.admin.server_stats { rows.push( card() .child(card_header(w().builder_overview.server, Vec::new(), t)) .child(div().pt(px(space::SM)).child(voice_stats::stats_panel( stats, &|id| self.chat.name_of(id), voice_stats::Detail::Full, t, ))), ); } rows } }