Builder sees live stats per user (follow → client pushes) and server stats on Overview
Browse files at this commit3ba46960934c758b4a5ee6ef4f2705a2c4d2ae0fParent: 0f8f4da- feat
- builder sees live stats per user (follow → client pushes) and server stats on Overview
- usage sampler is its own crate
- Save/Confirm only when changed
- clearer toggle
- aligned builder actions
35 changed files +624 -181
- Cargo.toml +1 -1
- crates/client/Cargo.toml +1 -4
- crates/client/src/gui.rs +2 -0
- crates/client/src/gui/builder.rs +5 -3
- crates/client/src/gui/builder/overview.rs +49 -4
- crates/client/src/gui/builder/server.rs +1 -1
- crates/client/src/gui/builder/ui.rs +1 -0
- crates/client/src/gui/builder/users.rs +99 -21
- crates/client/src/gui/chat.rs +57 -25
- crates/client/src/gui/dock.rs +3 -3
- crates/client/src/gui/parts.rs +6 -3
- crates/client/src/gui/stream_stats.rs +2 -1
- crates/client/src/gui/voice_stats.rs +28 -18
- crates/client/src/lib.rs +1 -1
- crates/client/src/media/decode.rs +1 -5
- crates/client/src/share/session.rs +1 -30
- crates/client/src/voice/engine.rs +1 -8
- crates/client/src/voice/mod.rs +2 -7
- crates/common/src/call.rs +17 -0
- crates/common/src/event.rs +6 -0
- crates/common/src/lib.rs +1 -0
- crates/common/src/stats.rs +112 -0
- crates/server/Cargo.toml +1 -0
- crates/server/src/call/mod.rs +4 -0
- crates/server/src/call/stats.rs +49 -0
- crates/server/src/hub.rs +11 -0
- crates/server/src/lib.rs +1 -0
- crates/server/src/network.rs +27 -0
- crates/server/src/stats.rs +109 -0
- crates/usage/Cargo.toml +19 -0
- crates/client/src/usage/drm.rs → crates/usage/src/drm.rs +1 -1
- crates/client/src/usage/mod.rs → crates/usage/src/lib.rs +2 -42
- crates/client/src/usage/nvidia.rs → crates/usage/src/nvidia.rs +3 -3
- crates/client/src/usage/threads.rs → crates/usage/src/threads.rs +0 -0
- crates/client/src/usage/windows.rs → crates/usage/src/windows.rs +0 -0
Cargo.tomlmodified+1 -1
| @@ -1,6 +1,6 @@ | |||
| 1 | [workspace] | 1 | [workspace] |
| 2 | resolver = "2" | 2 | resolver = "2" |
| 3 | -members = ["crates/client", "crates/common", "crates/pixel", "crates/spectrum", "crates/server"] | 3 | +members = ["crates/client", "crates/common", "crates/pixel", "crates/spectrum", "crates/server", "crates/usage"] |
| 4 | 4 | ||
| 5 | [profile.dev] | 5 | [profile.dev] |
| 6 | debug = "line-tables-only" | 6 | debug = "line-tables-only" |
crates/client/Cargo.tomlmodified+1 -4
| @@ -4,6 +4,7 @@ version = "0.1.0" | |||
| 4 | edition = "2024" | 4 | edition = "2024" |
| 5 | 5 | ||
| 6 | [dependencies] | 6 | [dependencies] |
| 7 | +usage = { path = "../usage" } | ||
| 7 | common.workspace = true | 8 | common.workspace = true |
| 8 | pixel.workspace = true | 9 | pixel.workspace = true |
| 9 | spectrum.workspace = true | 10 | spectrum.workspace = true |
| @@ -37,7 +38,6 @@ sysinfo = { version = "0.40.4", default-features = false, features = ["system"] | |||
| 37 | 38 | ||
| 38 | [target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies] | 39 | [target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies] |
| 39 | nokhwa = { version = "0.10.11", default-features = false, features = ["input-native"] } | 40 | nokhwa = { version = "0.10.11", default-features = false, features = ["input-native"] } |
| 40 | -nvml-wrapper = "0.13.0" | ||
| 41 | 41 | ||
| 42 | [target.'cfg(target_os = "linux")'.dependencies] | 42 | [target.'cfg(target_os = "linux")'.dependencies] |
| 43 | pipewire = { version = "0.10.1", features = ["v0_3_49"] } | 43 | pipewire = { version = "0.10.1", features = ["v0_3_49"] } |
| @@ -47,9 +47,6 @@ ashpd = { version = "0.14.3", default-features = false, features = ["tokio", "sc | |||
| 47 | [target.'cfg(target_os = "windows")'.dependencies] | 47 | [target.'cfg(target_os = "windows")'.dependencies] |
| 48 | wasapi = "0.27.0" | 48 | wasapi = "0.27.0" |
| 49 | windows-capture = "2.0.1" | 49 | windows-capture = "2.0.1" |
| 50 | -wmi = "0.18.4" | ||
| 51 | -winreg = "0.56.0" | ||
| 52 | -serde = { version = "1.0.229", features = ["derive"] } | ||
| 53 | 50 | ||
| 54 | [target.'cfg(target_os = "macos")'.dependencies] | 51 | [target.'cfg(target_os = "macos")'.dependencies] |
| 55 | coreaudio-rs = "0.14.2" | 52 | coreaudio-rs = "0.14.2" |
crates/client/src/gui.rsmodified+2 -0
| @@ -886,6 +886,8 @@ impl Yumush { | |||
| 886 | Event::ShareStopped { user_id, kind, .. } if self.is_watching(*user_id, *kind) => { | 886 | Event::ShareStopped { user_id, kind, .. } if self.is_watching(*user_id, *kind) => { |
| 887 | self.close_viewer(cx) | 887 | self.close_viewer(cx) |
| 888 | } | 888 | } |
| 889 | + Event::StatsWanted(true) => self.start_stats_push(cx), | ||
| 890 | + Event::StatsWanted(false) => self.chat.stats_task = None, | ||
| 889 | Event::Typing { .. } => { | 891 | Event::Typing { .. } => { |
| 890 | cx.spawn(async move |this, cx| { | 892 | cx.spawn(async move |this, cx| { |
| 891 | cx.background_executor().timer(chat::TYPING_TTL).await; | 893 | cx.background_executor().timer(chat::TYPING_TTL).await; |
crates/client/src/gui/builder.rsmodified+5 -3
| @@ -117,7 +117,8 @@ impl Yumush { | |||
| 117 | BuilderTab::Log => self.log_body(entity, palette), | 117 | BuilderTab::Log => self.log_body(entity, palette), |
| 118 | }; | 118 | }; |
| 119 | let mut sections = vec![ | 119 | let mut sections = vec![ |
| 120 | - page_heading(title, self.tab_actions(tab, entity, palette), palette).into_any_element(), | 120 | + page_heading(title, self.tab_actions(tab, entity, palette, cx), palette) |
| 121 | + .into_any_element(), | ||
| 121 | ]; | 122 | ]; |
| 122 | sections.extend(body); | 123 | sections.extend(body); |
| 123 | 124 | ||
| @@ -162,6 +163,7 @@ impl Yumush { | |||
| 162 | tab: BuilderTab, | 163 | tab: BuilderTab, |
| 163 | entity: &Entity<Self>, | 164 | entity: &Entity<Self>, |
| 164 | palette: Palette, | 165 | palette: Palette, |
| 166 | + cx: &App, | ||
| 165 | ) -> Vec<AnyElement> { | 167 | ) -> Vec<AnyElement> { |
| 166 | match tab { | 168 | match tab { |
| 167 | BuilderTab::Voice => vec![ | 169 | BuilderTab::Voice => vec![ |
| @@ -175,7 +177,7 @@ impl Yumush { | |||
| 175 | ) | 177 | ) |
| 176 | .into_any_element(), | 178 | .into_any_element(), |
| 177 | ], | 179 | ], |
| 178 | - BuilderTab::Server => vec![ | 180 | + BuilderTab::Server if self.server_form(cx) != self.server => vec![ |
| 179 | accent_button( | 181 | accent_button( |
| 180 | "save_server", | 182 | "save_server", |
| 181 | "Save", | 183 | "Save", |
| @@ -281,7 +283,7 @@ impl Yumush { | |||
| 281 | .is_some_and(|me| permission::is_builder(me.role)) | 283 | .is_some_and(|me| permission::is_builder(me.role)) |
| 282 | } | 284 | } |
| 283 | 285 | ||
| 284 | - pub(super) fn load_builder(&self, window: &mut Window, cx: &mut Context<Self>) { | 286 | + pub(super) fn load_builder(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 285 | self.load_open_reports(window, cx); | 287 | self.load_open_reports(window, cx); |
| 286 | match self.chat.builder_tab { | 288 | match self.chat.builder_tab { |
| 287 | BuilderTab::Overview => self.load_overview(window, cx), | 289 | BuilderTab::Overview => self.load_overview(window, cx), |
crates/client/src/gui/builder/overview.rsmodified+49 -4
| @@ -1,21 +1,26 @@ | |||
| 1 | use crate::net::Fail; | 1 | use crate::net::Fail; |
| 2 | use common::{ | 2 | use common::{ |
| 3 | - call::{GetDailyStats, GetOverview}, | 3 | + call::{GetDailyStats, GetOverview, GetServerStats}, |
| 4 | model::DayStat, | 4 | model::DayStat, |
| 5 | time::DAY_MILLIS, | 5 | time::DAY_MILLIS, |
| 6 | }; | 6 | }; |
| 7 | use gpui_kit::{Context, Div, IntoElement, ParentElement, Styled, Window, div, px}; | 7 | use gpui_kit::{Context, Div, IntoElement, ParentElement, Styled, Window, div, px}; |
| 8 | use jiff::Timestamp; | 8 | use jiff::Timestamp; |
| 9 | +use std::time::Duration; | ||
| 9 | 10 | ||
| 10 | use super::ui::{card, card_header, stat_tile}; | 11 | use super::ui::{card, card_header, stat_tile}; |
| 11 | use crate::gui::{ | 12 | use crate::gui::{ |
| 12 | - DAY, Palette, Yumush, storage, | 13 | + DAY, Palette, Yumush, |
| 14 | + builder::BuilderTab, | ||
| 15 | + chat::View, | ||
| 16 | + storage, | ||
| 13 | style::{radius, size, space, text}, | 17 | style::{radius, size, space, text}, |
| 14 | - text, | 18 | + text, voice_stats, |
| 15 | }; | 19 | }; |
| 16 | 20 | ||
| 17 | const CHART_HEIGHT: f32 = 80.; | 21 | const CHART_HEIGHT: f32 = 80.; |
| 18 | const PANEL_HEIGHT: f32 = 140.; | 22 | const PANEL_HEIGHT: f32 = 140.; |
| 23 | +const SERVER_STATS_TICK: Duration = Duration::from_secs(1); | ||
| 19 | 24 | ||
| 20 | fn day_label(day: u64) -> String { | 25 | fn day_label(day: u64) -> String { |
| 21 | Timestamp::from_millisecond((day * DAY_MILLIS) as i64) | 26 | Timestamp::from_millisecond((day * DAY_MILLIS) as i64) |
| @@ -119,7 +124,36 @@ fn bar_chart( | |||
| 119 | } | 124 | } |
| 120 | 125 | ||
| 121 | impl Yumush { | 126 | impl Yumush { |
| 122 | - pub(super) fn load_overview(&self, window: &mut Window, cx: &mut Context<Self>) { | 127 | + fn watch_server_stats(&mut self, cx: &mut Context<Self>) { |
| 128 | + if self.chat.server_stats_task.is_some() { | ||
| 129 | + return; | ||
| 130 | + } | ||
| 131 | + let network = self.network.clone(); | ||
| 132 | + self.chat.server_stats_task = Some(cx.spawn(async move |this, cx| { | ||
| 133 | + loop { | ||
| 134 | + let open = this | ||
| 135 | + .update(cx, |yumush, _| { | ||
| 136 | + yumush.chat.view == View::Builder | ||
| 137 | + && yumush.chat.builder_tab == BuilderTab::Overview | ||
| 138 | + }) | ||
| 139 | + .unwrap_or(false); | ||
| 140 | + if !open { | ||
| 141 | + let _ = this.update(cx, |yumush, _| yumush.chat.server_stats_task = None); | ||
| 142 | + break; | ||
| 143 | + } | ||
| 144 | + if let Ok(stats) = network.call(GetServerStats).await { | ||
| 145 | + let _ = this.update(cx, |yumush, cx| { | ||
| 146 | + yumush.chat.server_stats = Some(stats); | ||
| 147 | + cx.notify(); | ||
| 148 | + }); | ||
| 149 | + } | ||
| 150 | + cx.background_executor().timer(SERVER_STATS_TICK).await; | ||
| 151 | + } | ||
| 152 | + })); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + pub(super) fn load_overview(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| 156 | + self.watch_server_stats(cx); | ||
| 123 | self.fetch_builder( | 157 | self.fetch_builder( |
| 124 | window, | 158 | window, |
| 125 | cx, | 159 | cx, |
| @@ -177,6 +211,17 @@ impl Yumush { | |||
| 177 | palette, | 211 | palette, |
| 178 | )), | 212 | )), |
| 179 | ); | 213 | ); |
| 214 | + if let Some(stats) = &self.chat.server_stats { | ||
| 215 | + rows.push( | ||
| 216 | + card(palette) | ||
| 217 | + .child(card_header("Server", Vec::new(), palette)) | ||
| 218 | + .child(div().p(px(space::LG)).child(voice_stats::stats_panel( | ||
| 219 | + stats, | ||
| 220 | + &|id| self.chat.name_of(id), | ||
| 221 | + palette, | ||
| 222 | + ))), | ||
| 223 | + ); | ||
| 224 | + } | ||
| 180 | 225 | ||
| 181 | rows | 226 | rows |
| 182 | } | 227 | } |
crates/client/src/gui/builder/server.rsmodified+1 -1
| @@ -100,7 +100,7 @@ impl Yumush { | |||
| 100 | ); | 100 | ); |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | - fn server_form(&self, cx: &App) -> Option<ServerSettings> { | 103 | + pub(super) fn server_form(&self, cx: &App) -> Option<ServerSettings> { |
| 104 | let chat = &self.chat; | 104 | let chat = &self.chat; |
| 105 | Some(ServerSettings { | 105 | Some(ServerSettings { |
| 106 | name: input_text(&chat.server_name, cx), | 106 | name: input_text(&chat.server_name, cx), |
crates/client/src/gui/builder/ui.rsmodified+1 -0
| @@ -80,6 +80,7 @@ pub(super) fn action_button( | |||
| 80 | clickable(id, palette, action) | 80 | clickable(id, palette, action) |
| 81 | .tip(tip) | 81 | .tip(tip) |
| 82 | .h(px(size::CONTROL_SM)) | 82 | .h(px(size::CONTROL_SM)) |
| 83 | + .min_w(px(size::THUMB)) | ||
| 83 | .px(px(space::MD)) | 84 | .px(px(space::MD)) |
| 84 | .flex() | 85 | .flex() |
| 85 | .items_center() | 86 | .items_center() |
crates/client/src/gui/builder/users.rsmodified+99 -21
| @@ -3,11 +3,13 @@ use std::future::Future; | |||
| 3 | 3 | ||
| 4 | use common::{ | 4 | use common::{ |
| 5 | call::{ | 5 | call::{ |
| 6 | - BanUser, ClearProfile, CreateUser, DeleteUser, ListSessions, PurgeUser, RenameUser, | 6 | + BanUser, ClearProfile, CreateUser, DeleteUser, FollowStats, ListSessions, PurgeUser, |
| 7 | - ResetPassword, RevokeSession, RevokeSessions, SetRole, SuspendUser, TimeoutUser, | 7 | + RenameUser, ResetPassword, RevokeSession, RevokeSessions, SetRole, SuspendUser, |
| 8 | + TimeoutUser, | ||
| 8 | }, | 9 | }, |
| 9 | model::{Account, BanScope, Role, SessionInfo, User}, | 10 | model::{Account, BanScope, Role, SessionInfo, User}, |
| 10 | permission, | 11 | permission, |
| 12 | + stats::Stats, | ||
| 11 | time::{DAY_MILLIS, HOUR_MILLIS, MINUTE_MILLIS, now_ms}, | 13 | time::{DAY_MILLIS, HOUR_MILLIS, MINUTE_MILLIS, now_ms}, |
| 12 | }; | 14 | }; |
| 13 | use gpui_kit::{ | 15 | use gpui_kit::{ |
| @@ -193,6 +195,28 @@ impl Yumush { | |||
| 193 | }); | 195 | }); |
| 194 | } | 196 | } |
| 195 | 197 | ||
| 198 | + pub(super) fn toggle_builder_stats(&mut self, user_id: Id, cx: &mut Context<Self>) { | ||
| 199 | + let target = match self.chat.followed { | ||
| 200 | + Some((id, _)) if id == user_id => None, | ||
| 201 | + _ => Some(user_id), | ||
| 202 | + }; | ||
| 203 | + self.chat.followed = target.map(|id| (id, Stats::default())); | ||
| 204 | + let network = self.network.clone(); | ||
| 205 | + cx.spawn(async move |this, cx| { | ||
| 206 | + let latest = network.call(FollowStats { user_id: target }).await; | ||
| 207 | + let _ = this.update(cx, |yumush, cx| { | ||
| 208 | + if let (Ok(Some(stats)), Some(followed)) = (latest, yumush.chat.followed.as_mut()) | ||
| 209 | + && Some(followed.0) == target | ||
| 210 | + { | ||
| 211 | + followed.1 = stats; | ||
| 212 | + } | ||
| 213 | + cx.notify(); | ||
| 214 | + }); | ||
| 215 | + }) | ||
| 216 | + .detach(); | ||
| 217 | + cx.notify(); | ||
| 218 | + } | ||
| 219 | + | ||
| 196 | pub(super) fn toggle_builder_password(&mut self, user_id: Id, cx: &mut Context<Self>) { | 220 | pub(super) fn toggle_builder_password(&mut self, user_id: Id, cx: &mut Context<Self>) { |
| 197 | if self.chat.pending_builder_password == Some(user_id) { | 221 | if self.chat.pending_builder_password == Some(user_id) { |
| 198 | self.chat.pending_builder_password = None; | 222 | self.chat.pending_builder_password = None; |
| @@ -238,6 +262,9 @@ impl Yumush { | |||
| 238 | window: &mut Window, | 262 | window: &mut Window, |
| 239 | cx: &mut Context<Self>, | 263 | cx: &mut Context<Self>, |
| 240 | ) { | 264 | ) { |
| 265 | + if let Some((followed, _)) = self.chat.followed { | ||
| 266 | + self.toggle_builder_stats(followed, cx); | ||
| 267 | + } | ||
| 241 | if self | 268 | if self |
| 242 | .chat | 269 | .chat |
| 243 | .builder_sessions | 270 | .builder_sessions |
| @@ -417,7 +444,7 @@ impl Yumush { | |||
| 417 | .to_lowercase() | 444 | .to_lowercase() |
| 418 | .contains(query.as_str()) | 445 | .contains(query.as_str()) |
| 419 | }) | 446 | }) |
| 420 | - .map(|account| self.builder_row(account, me, entity, palette)) | 447 | + .map(|account| self.builder_row(account, me, entity, palette, cx)) |
| 421 | .collect(), | 448 | .collect(), |
| 422 | None => Vec::new(), | 449 | None => Vec::new(), |
| 423 | }; | 450 | }; |
| @@ -608,7 +635,16 @@ impl Yumush { | |||
| 608 | user_id: Id, | 635 | user_id: Id, |
| 609 | entity: &Entity<Self>, | 636 | entity: &Entity<Self>, |
| 610 | palette: Palette, | 637 | palette: Palette, |
| 638 | + cx: &App, | ||
| 611 | ) -> Div { | 639 | ) -> Div { |
| 640 | + let value = self.chat.builder_rename.read(cx).text().to_string(); | ||
| 641 | + let current = self | ||
| 642 | + .chat | ||
| 643 | + .builder_users | ||
| 644 | + .iter() | ||
| 645 | + .find(|account| account.user.id == user_id) | ||
| 646 | + .map_or("", |account| account.user.username.as_str()); | ||
| 647 | + let ready = !value.is_empty() && value != current; | ||
| 612 | div() | 648 | div() |
| 613 | .h(px(size::ROW)) | 649 | .h(px(size::ROW)) |
| 614 | .px(px(space::MD)) | 650 | .px(px(space::MD)) |
| @@ -621,14 +657,16 @@ impl Yumush { | |||
| 621 | .min_w_0() | 657 | .min_w_0() |
| 622 | .child(field(palette, &self.chat.builder_rename)), | 658 | .child(field(palette, &self.chat.builder_rename)), |
| 623 | ) | 659 | ) |
| 624 | - .child(plain_button( | 660 | + .when(ready, |row| { |
| 625 | - format!("confirm_rename_{user_id}"), | 661 | + row.child(plain_button( |
| 626 | - "Confirm", | 662 | + format!("confirm_rename_{user_id}"), |
| 627 | - palette, | 663 | + "Confirm", |
| 628 | - entity_action(entity, |yumush, window, cx| { | 664 | + palette, |
| 629 | - yumush.rename_builder_user(window, cx) | 665 | + entity_action(entity, |yumush, window, cx| { |
| 630 | - }), | 666 | + yumush.rename_builder_user(window, cx) |
| 631 | - )) | 667 | + }), |
| 668 | + )) | ||
| 669 | + }) | ||
| 632 | } | 670 | } |
| 633 | 671 | ||
| 634 | pub(super) fn builder_password_row( | 672 | pub(super) fn builder_password_row( |
| @@ -636,7 +674,10 @@ impl Yumush { | |||
| 636 | user_id: Id, | 674 | user_id: Id, |
| 637 | entity: &Entity<Self>, | 675 | entity: &Entity<Self>, |
| 638 | palette: Palette, | 676 | palette: Palette, |
| 677 | + cx: &App, | ||
| 639 | ) -> Div { | 678 | ) -> Div { |
| 679 | + let value = self.chat.builder_password.read(cx).text().to_string(); | ||
| 680 | + let ready = !value.is_empty(); | ||
| 640 | div() | 681 | div() |
| 641 | .h(px(size::ROW)) | 682 | .h(px(size::ROW)) |
| 642 | .px(px(space::MD)) | 683 | .px(px(space::MD)) |
| @@ -649,14 +690,16 @@ impl Yumush { | |||
| 649 | .min_w_0() | 690 | .min_w_0() |
| 650 | .child(field(palette, &self.chat.builder_password)), | 691 | .child(field(palette, &self.chat.builder_password)), |
| 651 | ) | 692 | ) |
| 652 | - .child(plain_button( | 693 | + .when(ready, |row| { |
| 653 | - format!("confirm_password_{user_id}"), | 694 | + row.child(plain_button( |
| 654 | - "Confirm", | 695 | + format!("confirm_password_{user_id}"), |
| 655 | - palette, | 696 | + "Confirm", |
| 656 | - entity_action(entity, |yumush, window, cx| { | 697 | + palette, |
| 657 | - yumush.reset_builder_password(window, cx) | 698 | + entity_action(entity, |yumush, window, cx| { |
| 658 | - }), | 699 | + yumush.reset_builder_password(window, cx) |
| 659 | - )) | 700 | + }), |
| 701 | + )) | ||
| 702 | + }) | ||
| 660 | } | 703 | } |
| 661 | 704 | ||
| 662 | pub(super) fn builder_row( | 705 | pub(super) fn builder_row( |
| @@ -665,6 +708,7 @@ impl Yumush { | |||
| 665 | me: &User, | 708 | me: &User, |
| 666 | entity: &Entity<Self>, | 709 | entity: &Entity<Self>, |
| 667 | palette: Palette, | 710 | palette: Palette, |
| 711 | + cx: &App, | ||
| 668 | ) -> Div { | 712 | ) -> Div { |
| 669 | let user = &account.user; | 713 | let user = &account.user; |
| 670 | let is_self = user.id == me.id; | 714 | let is_self = user.id == me.id; |
| @@ -754,6 +798,29 @@ impl Yumush { | |||
| 754 | }), | 798 | }), |
| 755 | ); | 799 | ); |
| 756 | 800 | ||
| 801 | + let stats_id = user.id; | ||
| 802 | + let stats_open = self | ||
| 803 | + .chat | ||
| 804 | + .followed | ||
| 805 | + .as_ref() | ||
| 806 | + .filter(|(id, _)| *id == user.id) | ||
| 807 | + .map(|(_, stats)| stats); | ||
| 808 | + let stats_button = action_button( | ||
| 809 | + format!("stats_user_{}", user.id), | ||
| 810 | + IconName::Activity, | ||
| 811 | + "Stats", | ||
| 812 | + "Live connection and machine stats", | ||
| 813 | + if stats_open.is_some() { | ||
| 814 | + palette.text | ||
| 815 | + } else { | ||
| 816 | + tone | ||
| 817 | + }, | ||
| 818 | + palette, | ||
| 819 | + entity_action(entity, move |yumush, _, cx| { | ||
| 820 | + yumush.toggle_builder_stats(stats_id, cx) | ||
| 821 | + }), | ||
| 822 | + ); | ||
| 823 | + | ||
| 757 | let password_button = action_button( | 824 | let password_button = action_button( |
| 758 | format!("password_user_{}", user.id), | 825 | format!("password_user_{}", user.id), |
| 759 | IconName::KeyRound, | 826 | IconName::KeyRound, |
| @@ -933,6 +1000,7 @@ impl Yumush { | |||
| 933 | manage.then(|| rename_button.into_any_element()), | 1000 | manage.then(|| rename_button.into_any_element()), |
| 934 | manage.then(|| password_button.into_any_element()), | 1001 | manage.then(|| password_button.into_any_element()), |
| 935 | can_role.then(|| role_button.into_any_element()), | 1002 | can_role.then(|| role_button.into_any_element()), |
| 1003 | + Some(stats_button.into_any_element()), | ||
| 936 | ]; | 1004 | ]; |
| 937 | let moderation_actions = [ | 1005 | let moderation_actions = [ |
| 938 | manage.then(|| timeout_button.into_any_element()), | 1006 | manage.then(|| timeout_button.into_any_element()), |
| @@ -968,9 +1036,11 @@ impl Yumush { | |||
| 968 | account_actions.into_iter().flatten().collect(), | 1036 | account_actions.into_iter().flatten().collect(), |
| 969 | )) | 1037 | )) |
| 970 | .children( | 1038 | .children( |
| 971 | - password_open.then(|| self.builder_password_row(user.id, entity, palette)), | 1039 | + password_open.then(|| self.builder_password_row(user.id, entity, palette, cx)), |
| 1040 | + ) | ||
| 1041 | + .children( | ||
| 1042 | + rename_open.then(|| self.builder_rename_row(user.id, entity, palette, cx)), | ||
| 972 | ) | 1043 | ) |
| 973 | - .children(rename_open.then(|| self.builder_rename_row(user.id, entity, palette))) | ||
| 974 | .children(action_group( | 1044 | .children(action_group( |
| 975 | "Moderation", | 1045 | "Moderation", |
| 976 | palette, | 1046 | palette, |
| @@ -984,6 +1054,14 @@ impl Yumush { | |||
| 984 | danger_actions.into_iter().flatten().collect(), | 1054 | danger_actions.into_iter().flatten().collect(), |
| 985 | )) | 1055 | )) |
| 986 | .children(action_group("Sessions", palette, sessions)) | 1056 | .children(action_group("Sessions", palette, sessions)) |
| 1057 | + .children(stats_open.map(|stats| { | ||
| 1058 | + super::super::voice_stats::stats_panel( | ||
| 1059 | + stats, | ||
| 1060 | + &|id| self.chat.name_of(id), | ||
| 1061 | + palette, | ||
| 1062 | + ) | ||
| 1063 | + .pt(px(space::MD)) | ||
| 1064 | + })) | ||
| 987 | }); | 1065 | }); |
| 988 | 1066 | ||
| 989 | div().flex().flex_col().child(row).children(panel) | 1067 | div().flex().flex_col().child(row).children(panel) |
crates/client/src/gui/chat.rsmodified+57 -25
| @@ -32,12 +32,11 @@ use crate::gui::{ | |||
| 32 | use crate::net::Levels; | 32 | use crate::net::Levels; |
| 33 | use crate::settings::VOLUME; | 33 | use crate::settings::VOLUME; |
| 34 | use crate::share::session::ShareSample; | 34 | use crate::share::session::ShareSample; |
| 35 | -use crate::voice::VoiceSample; | ||
| 36 | use common::{ | 35 | use common::{ |
| 37 | call::{ | 36 | call::{ |
| 38 | CreateCommunity, DeclineCall, DeleteMessage, GetUser, LeaveCommunity, ListBans, | 37 | CreateCommunity, DeclineCall, DeleteMessage, GetUser, LeaveCommunity, ListBans, |
| 39 | ListCommunities, ListMembers, ListMessages, ListPins, ListUnreads, ListVoiceParticipants, | 38 | ListCommunities, ListMembers, ListMessages, ListPins, ListUnreads, ListVoiceParticipants, |
| 40 | - SearchCommunity, WatchCommunity, | 39 | + PushStats, SearchCommunity, WatchCommunity, |
| 41 | }, | 40 | }, |
| 42 | constant::MESSAGE_PAGE_LIMIT, | 41 | constant::MESSAGE_PAGE_LIMIT, |
| 43 | event::Event, | 42 | event::Event, |
| @@ -261,13 +260,14 @@ pub struct Chat { | |||
| 261 | pub(super) server_muted: HashSet<Id>, | 260 | pub(super) server_muted: HashSet<Id>, |
| 262 | pub(super) builder_voice_rooms: Vec<VoiceRoom>, | 261 | pub(super) builder_voice_rooms: Vec<VoiceRoom>, |
| 263 | pub(super) voice_task: Option<Task<()>>, | 262 | pub(super) voice_task: Option<Task<()>>, |
| 263 | + pub(super) stats_task: Option<Task<()>>, | ||
| 264 | + pub(super) server_stats: Option<common::stats::Stats>, | ||
| 265 | + pub(super) server_stats_task: Option<Task<()>>, | ||
| 264 | pub(super) user_volume: Option<UserVolume>, | 266 | pub(super) user_volume: Option<UserVolume>, |
| 265 | - pub(super) voice_samples: VecDeque<VoiceSample>, | 267 | + pub(super) stats: common::stats::Stats, |
| 266 | - pub(super) traffic: VecDeque<(f32, f32)>, | 268 | + pub(super) followed: Option<(Id, common::stats::Stats)>, |
| 267 | - pub(super) share_sample: ShareSample, | ||
| 268 | pub(super) share_loss: HashMap<(Id, Kind), VecDeque<f32>>, | 269 | pub(super) share_loss: HashMap<(Id, Kind), VecDeque<f32>>, |
| 269 | - pub(super) stats: Option<StatsAt>, | 270 | + pub(super) stats_at: Option<StatsAt>, |
| 270 | - pub(super) usage: Option<crate::usage::Usage>, | ||
| 271 | pub(super) usage_monitor: crate::usage::Monitor, | 271 | pub(super) usage_monitor: crate::usage::Monitor, |
| 272 | pub(super) ringing: HashSet<Id>, | 272 | pub(super) ringing: HashSet<Id>, |
| 273 | pub(super) ring_task: Option<Task<()>>, | 273 | pub(super) ring_task: Option<Task<()>>, |
| @@ -508,12 +508,13 @@ impl Chat { | |||
| 508 | server_muted: HashSet::new(), | 508 | server_muted: HashSet::new(), |
| 509 | builder_voice_rooms: Vec::new(), | 509 | builder_voice_rooms: Vec::new(), |
| 510 | voice_task: None, | 510 | voice_task: None, |
| 511 | - voice_samples: VecDeque::new(), | 511 | + stats_task: None, |
| 512 | - traffic: VecDeque::new(), | 512 | + server_stats: None, |
| 513 | - share_sample: ShareSample::default(), | 513 | + server_stats_task: None, |
| 514 | + stats: common::stats::Stats::default(), | ||
| 515 | + followed: None, | ||
| 514 | share_loss: HashMap::new(), | 516 | share_loss: HashMap::new(), |
| 515 | - stats: None, | 517 | + stats_at: None, |
| 516 | - usage: None, | ||
| 517 | usage_monitor: crate::usage::Monitor::default(), | 518 | usage_monitor: crate::usage::Monitor::default(), |
| 518 | ringing: HashSet::new(), | 519 | ringing: HashSet::new(), |
| 519 | ring_task: None, | 520 | ring_task: None, |
| @@ -560,6 +561,11 @@ impl Chat { | |||
| 560 | } | 561 | } |
| 561 | } | 562 | } |
| 562 | 563 | ||
| 564 | + pub fn name_of(&self, user_id: Id) -> String { | ||
| 565 | + self.author(user_id) | ||
| 566 | + .map_or_else(|| user_id.to_string(), |user| user.username.clone()) | ||
| 567 | + } | ||
| 568 | + | ||
| 563 | pub fn slow_seconds(&self) -> Option<u64> { | 569 | pub fn slow_seconds(&self) -> Option<u64> { |
| 564 | let remaining = self.slowed_until?.saturating_sub(common::time::now_ms()); | 570 | let remaining = self.slowed_until?.saturating_sub(common::time::now_ms()); |
| 565 | (remaining > 0).then(|| remaining.div_ceil(1000)) | 571 | (remaining > 0).then(|| remaining.div_ceil(1000)) |
| @@ -706,6 +712,12 @@ impl Chat { | |||
| 706 | shares.retain(|share| share.user_id != user_id || share.kind != kind); | 712 | shares.retain(|share| share.user_id != user_id || share.kind != kind); |
| 707 | } | 713 | } |
| 708 | } | 714 | } |
| 715 | + Event::StatsWanted(_) => {} | ||
| 716 | + Event::StatsOf { user_id, stats } => { | ||
| 717 | + if let Some(followed) = self.followed.as_mut().filter(|(id, _)| *id == user_id) { | ||
| 718 | + followed.1 = stats; | ||
| 719 | + } | ||
| 720 | + } | ||
| 709 | Event::ReportCreated | Event::SettingsUpdated(_) => {} | 721 | Event::ReportCreated | Event::SettingsUpdated(_) => {} |
| 710 | } | 722 | } |
| 711 | } | 723 | } |
| @@ -1185,9 +1197,9 @@ impl Chat { | |||
| 1185 | self.voice_state = VoiceState::Off; | 1197 | self.voice_state = VoiceState::Off; |
| 1186 | self.waves.clear(); | 1198 | self.waves.clear(); |
| 1187 | self.voice_task = None; | 1199 | self.voice_task = None; |
| 1188 | - self.voice_samples.clear(); | 1200 | + self.stats.voice.clear(); |
| 1189 | - self.traffic.clear(); | 1201 | + self.stats.traffic.clear(); |
| 1190 | - self.share_sample = ShareSample::default(); | 1202 | + self.stats.share = ShareSample::default(); |
| 1191 | self.share_loss.clear(); | 1203 | self.share_loss.clear(); |
| 1192 | } | 1204 | } |
| 1193 | 1205 | ||
| @@ -1235,7 +1247,7 @@ impl Chat { | |||
| 1235 | history.pop_front(); | 1247 | history.pop_front(); |
| 1236 | } | 1248 | } |
| 1237 | } | 1249 | } |
| 1238 | - self.share_sample = sample; | 1250 | + self.stats.share = sample; |
| 1239 | } | 1251 | } |
| 1240 | } | 1252 | } |
| 1241 | 1253 | ||
| @@ -1323,13 +1335,13 @@ impl RenderOnce for MembersTrigger { | |||
| 1323 | 1335 | ||
| 1324 | impl Yumush { | 1336 | impl Yumush { |
| 1325 | pub(super) fn toggle_stats(&mut self, at: StatsAt) { | 1337 | pub(super) fn toggle_stats(&mut self, at: StatsAt) { |
| 1326 | - self.chat.stats = (self.chat.stats != Some(at)).then_some(at); | 1338 | + self.chat.stats_at = (self.chat.stats_at != Some(at)).then_some(at); |
| 1327 | self.refresh_usage(); | 1339 | self.refresh_usage(); |
| 1328 | } | 1340 | } |
| 1329 | 1341 | ||
| 1330 | - fn refresh_usage(&mut self) { | 1342 | + pub(super) fn refresh_usage(&mut self) { |
| 1331 | if let Some(usage) = self.chat.usage_monitor.latest() { | 1343 | if let Some(usage) = self.chat.usage_monitor.latest() { |
| 1332 | - self.chat.usage = Some(usage); | 1344 | + self.chat.stats.usage = Some(usage); |
| 1333 | } | 1345 | } |
| 1334 | } | 1346 | } |
| 1335 | 1347 | ||
| @@ -1403,7 +1415,7 @@ impl Yumush { | |||
| 1403 | fn stats_sheet(&self, palette: Palette) -> Option<impl IntoElement> { | 1415 | fn stats_sheet(&self, palette: Palette) -> Option<impl IntoElement> { |
| 1404 | let at = self | 1416 | let at = self |
| 1405 | .chat | 1417 | .chat |
| 1406 | - .stats | 1418 | + .stats_at |
| 1407 | .filter(|_| self.chat.voice_community().is_some())?; | 1419 | .filter(|_| self.chat.voice_community().is_some())?; |
| 1408 | Some( | 1420 | Some( |
| 1409 | div() | 1421 | div() |
| @@ -1423,7 +1435,11 @@ impl Yumush { | |||
| 1423 | .border_color(palette.border) | 1435 | .border_color(palette.border) |
| 1424 | .bg(palette.tokens.raise()) | 1436 | .bg(palette.tokens.raise()) |
| 1425 | .shadow_lg() | 1437 | .shadow_lg() |
| 1426 | - .child(voice_stats::stats_panel(&self.chat, palette)), | 1438 | + .child(voice_stats::stats_panel( |
| 1439 | + &self.chat.stats, | ||
| 1440 | + &|id| self.chat.name_of(id), | ||
| 1441 | + palette, | ||
| 1442 | + )), | ||
| 1427 | ) | 1443 | ) |
| 1428 | } | 1444 | } |
| 1429 | 1445 | ||
| @@ -2319,6 +2335,22 @@ impl Yumush { | |||
| 2319 | cx.notify(); | 2335 | cx.notify(); |
| 2320 | } | 2336 | } |
| 2321 | 2337 | ||
| 2338 | + pub(super) fn start_stats_push(&mut self, cx: &mut Context<Self>) { | ||
| 2339 | + let network = self.network.clone(); | ||
| 2340 | + self.chat.stats_task = Some(cx.spawn(async move |this, cx| { | ||
| 2341 | + loop { | ||
| 2342 | + let Ok(stats) = this.update(cx, |yumush, _| { | ||
| 2343 | + yumush.refresh_usage(); | ||
| 2344 | + yumush.chat.stats.clone() | ||
| 2345 | + }) else { | ||
| 2346 | + break; | ||
| 2347 | + }; | ||
| 2348 | + let _ = network.call(PushStats { stats }).await; | ||
| 2349 | + cx.background_executor().timer(VOICE_SAMPLE_INTERVAL).await; | ||
| 2350 | + } | ||
| 2351 | + })); | ||
| 2352 | + } | ||
| 2353 | + | ||
| 2322 | fn start_voice_poll(&mut self, cx: &mut Context<Self>) { | 2354 | fn start_voice_poll(&mut self, cx: &mut Context<Self>) { |
| 2323 | let network = self.network.clone(); | 2355 | let network = self.network.clone(); |
| 2324 | 2356 | ||
| @@ -2350,14 +2382,14 @@ impl Yumush { | |||
| 2350 | .iter() | 2382 | .iter() |
| 2351 | .map(|(_, _, stats)| stats.kbps as f32) | 2383 | .map(|(_, _, stats)| stats.kbps as f32) |
| 2352 | .sum::<f32>(); | 2384 | .sum::<f32>(); |
| 2353 | - let traffic = &mut yumush.chat.traffic; | 2385 | + let traffic = &mut yumush.chat.stats.traffic; |
| 2354 | traffic.push_back((up, down)); | 2386 | traffic.push_back((up, down)); |
| 2355 | while traffic.len() > SAMPLE_HISTORY { | 2387 | while traffic.len() > SAMPLE_HISTORY { |
| 2356 | traffic.pop_front(); | 2388 | traffic.pop_front(); |
| 2357 | } | 2389 | } |
| 2358 | } | 2390 | } |
| 2359 | if let Some(sample) = sample { | 2391 | if let Some(sample) = sample { |
| 2360 | - let samples = &mut yumush.chat.voice_samples; | 2392 | + let samples = &mut yumush.chat.stats.voice; |
| 2361 | tracing::debug!( | 2393 | tracing::debug!( |
| 2362 | target: "metrics", | 2394 | target: "metrics", |
| 2363 | up = sample.traffic.up_kbps, | 2395 | up = sample.traffic.up_kbps, |
| @@ -2376,7 +2408,7 @@ impl Yumush { | |||
| 2376 | yumush.chat.record_share(share); | 2408 | yumush.chat.record_share(share); |
| 2377 | cx.notify(); | 2409 | cx.notify(); |
| 2378 | } | 2410 | } |
| 2379 | - if due && yumush.chat.stats.is_some() { | 2411 | + if due && yumush.chat.stats_at.is_some() { |
| 2380 | yumush.refresh_usage(); | 2412 | yumush.refresh_usage(); |
| 2381 | } | 2413 | } |
| 2382 | yumush.update_waves(levels, cx); | 2414 | yumush.update_waves(levels, cx); |
| @@ -2703,7 +2735,7 @@ impl Yumush { | |||
| 2703 | .child(stats_button( | 2735 | .child(stats_button( |
| 2704 | entity, | 2736 | entity, |
| 2705 | palette, | 2737 | palette, |
| 2706 | - self.chat.stats == Some(StatsAt::Header), | 2738 | + self.chat.stats_at == Some(StatsAt::Header), |
| 2707 | )) | 2739 | )) |
| 2708 | })), | 2740 | })), |
| 2709 | ) | 2741 | ) |
crates/client/src/gui/dock.rsmodified+3 -3
| @@ -28,7 +28,7 @@ impl Yumush { | |||
| 28 | let user = self.user.as_ref()?; | 28 | let user = self.user.as_ref()?; |
| 29 | let me = user.id; | 29 | let me = user.id; |
| 30 | let t = Tokens::of(cx); | 30 | let t = Tokens::of(cx); |
| 31 | - let frayed = voice_stats::quality(&self.chat.voice_samples) == Some(Quality::Poor); | 31 | + let frayed = voice_stats::quality(&self.chat.stats.voice) == Some(Quality::Poor); |
| 32 | let voice = if frayed { | 32 | let voice = if frayed { |
| 33 | Voice::Frayed | 33 | Voice::Frayed |
| 34 | } else if !muted && self.chat.waves.speaking.contains(&me) { | 34 | } else if !muted && self.chat.waves.speaking.contains(&me) { |
| @@ -40,12 +40,12 @@ impl Yumush { | |||
| 40 | name: user.username.clone().into(), | 40 | name: user.username.clone().into(), |
| 41 | bands: self.chat.waves.voice(me).cloned(), | 41 | bands: self.chat.waves.voice(me).cloned(), |
| 42 | voice, | 42 | voice, |
| 43 | - ping: self.chat.voice_samples.back().map(|sample| sample.ping_ms), | 43 | + ping: self.chat.stats.voice.back().map(|sample| sample.ping_ms), |
| 44 | muted, | 44 | muted, |
| 45 | deafened, | 45 | deafened, |
| 46 | camera: self.network.share_source(Kind::Camera).is_some(), | 46 | camera: self.network.share_source(Kind::Camera).is_some(), |
| 47 | screen: self.network.share_source(Kind::Screen).is_some(), | 47 | screen: self.network.share_source(Kind::Screen).is_some(), |
| 48 | - stats: self.chat.stats == Some(super::chat::StatsAt::Dock), | 48 | + stats: self.chat.stats_at == Some(super::chat::StatsAt::Dock), |
| 49 | }; | 49 | }; |
| 50 | let go_id = community.id; | 50 | let go_id = community.id; |
| 51 | let actions = CallActions { | 51 | let actions = CallActions { |
crates/client/src/gui/parts.rsmodified+6 -3
| @@ -147,10 +147,11 @@ pub fn toggle( | |||
| 147 | t: Tokens, | 147 | t: Tokens, |
| 148 | flip: impl Fn(&mut Window, &mut App) + 'static, | 148 | flip: impl Fn(&mut Window, &mut App) + 'static, |
| 149 | ) -> Stateful<Div> { | 149 | ) -> Stateful<Div> { |
| 150 | - let (bar, knob, left) = if on { | 150 | + let hue = t.hue(super::style::my_hue()); |
| 151 | - (t.hue(super::style::my_hue()), t.paper(), 20.) | 151 | + let (bar, knob, ring, left) = if on { |
| 152 | + (hue, hue, hue, 20.) | ||
| 152 | } else { | 153 | } else { |
| 153 | - (t.faint(), t.faint(), 0.) | 154 | + (t.off(), t.ground(), t.faint(), 0.) |
| 154 | }; | 155 | }; |
| 155 | div() | 156 | div() |
| 156 | .id(id) | 157 | .id(id) |
| @@ -175,6 +176,8 @@ pub fn toggle( | |||
| 175 | .left(px(left)) | 176 | .left(px(left)) |
| 176 | .size(px(10.)) | 177 | .size(px(10.)) |
| 177 | .rounded_full() | 178 | .rounded_full() |
| 179 | + .border(px(stroke::LINE)) | ||
| 180 | + .border_color(ring) | ||
| 178 | .bg(knob), | 181 | .bg(knob), |
| 179 | ) | 182 | ) |
| 180 | } | 183 | } |
crates/client/src/gui/stream_stats.rsmodified+2 -1
| @@ -12,7 +12,8 @@ use crate::share::session::WatchStats; | |||
| 12 | pub(super) fn watch_stats(yumush: &Yumush, user_id: Id, kind: Kind) -> Option<&WatchStats> { | 12 | pub(super) fn watch_stats(yumush: &Yumush, user_id: Id, kind: Kind) -> Option<&WatchStats> { |
| 13 | yumush | 13 | yumush |
| 14 | .chat | 14 | .chat |
| 15 | - .share_sample | 15 | + .stats |
| 16 | + .share | ||
| 16 | .watching | 17 | .watching |
| 17 | .iter() | 18 | .iter() |
| 18 | .find(|(id, sent, _)| *id == user_id && *sent == kind) | 19 | .find(|(id, sent, _)| *id == user_id && *sent == kind) |
crates/client/src/gui/voice_stats.rsmodified+28 -18
| @@ -4,11 +4,13 @@ use common::{ | |||
| 4 | model::Id, | 4 | model::Id, |
| 5 | share::{Kind, Layer}, | 5 | share::{Kind, Layer}, |
| 6 | }; | 6 | }; |
| 7 | -use gpui_kit::{AnyElement, Div, FontWeight, Hsla, IntoElement, ParentElement, Styled, div, px}; | 7 | +use gpui_kit::{ |
| 8 | + AnyElement, Div, FontWeight, Hsla, IntoElement, ParentElement, Styled, div, | ||
| 9 | + prelude::FluentBuilder as _, px, | ||
| 10 | +}; | ||
| 8 | 11 | ||
| 9 | use super::{ | 12 | use super::{ |
| 10 | Palette, | 13 | Palette, |
| 11 | - chat::Chat, | ||
| 12 | parts::{Fill, Ink, Text, band, grow, spark, stack, track, words}, | 14 | parts::{Fill, Ink, Text, band, grow, spark, stack, track, words}, |
| 13 | style::{Tokens, size, space}, | 15 | style::{Tokens, size, space}, |
| 14 | }; | 16 | }; |
| @@ -16,6 +18,7 @@ use crate::media::decode::Engine; | |||
| 16 | use crate::share::session::{LayerStats, WatchStats}; | 18 | use crate::share::session::{LayerStats, WatchStats}; |
| 17 | use crate::usage::Usage; | 19 | use crate::usage::Usage; |
| 18 | use crate::voice::VoiceSample; | 20 | use crate::voice::VoiceSample; |
| 21 | +use common::stats::Stats; | ||
| 19 | 22 | ||
| 20 | pub const SAMPLE_HISTORY: usize = 60; | 23 | pub const SAMPLE_HISTORY: usize = 60; |
| 21 | const LABEL_W: f32 = size::ROW * 2.; | 24 | const LABEL_W: f32 = size::ROW * 2.; |
| @@ -68,24 +71,28 @@ pub fn quality(samples: &VecDeque<VoiceSample>) -> Option<Quality> { | |||
| 68 | samples.back().map(Quality::of) | 71 | samples.back().map(Quality::of) |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -pub fn stats_panel(chat: &Chat, palette: Palette) -> Div { | 74 | +pub fn stats_panel(stats: &Stats, name: &dyn Fn(Id) -> String, palette: Palette) -> Div { |
| 72 | let t = palette.tokens; | 75 | let t = palette.tokens; |
| 73 | - let share = &chat.share_sample; | 76 | + let share = &stats.share; |
| 74 | band(space::XXL) | 77 | band(space::XXL) |
| 75 | .items_start() | 78 | .items_start() |
| 76 | .flex_wrap() | 79 | .flex_wrap() |
| 77 | .child( | 80 | .child( |
| 78 | stack(space::XL) | 81 | stack(space::XL) |
| 79 | .w(px(COLUMN_W)) | 82 | .w(px(COLUMN_W)) |
| 80 | - .child(connection(chat, t)) | 83 | + .when(!stats.voice.is_empty(), |column| { |
| 81 | - .child(traffic(chat, t)) | 84 | + column.child(connection(stats, t)) |
| 82 | - .children(chat.usage.as_ref().map(|usage| machine(usage, t))), | 85 | + }) |
| 86 | + .when(!stats.traffic.is_empty(), |column| { | ||
| 87 | + column.child(traffic(stats, t)) | ||
| 88 | + }) | ||
| 89 | + .children(stats.usage.as_ref().map(|usage| machine(usage, t))), | ||
| 83 | ) | 90 | ) |
| 84 | .child( | 91 | .child( |
| 85 | stack(space::XL) | 92 | stack(space::XL) |
| 86 | .w(px(COLUMN_W)) | 93 | .w(px(COLUMN_W)) |
| 87 | .children(sending(&share.sending, t)) | 94 | .children(sending(&share.sending, t)) |
| 88 | - .children(watching(&share.watching, chat, t)), | 95 | + .children(watching(&share.watching, name, t)), |
| 89 | ) | 96 | ) |
| 90 | } | 97 | } |
| 91 | 98 | ||
| @@ -138,8 +145,8 @@ pub(super) fn number(value: String, name: &'static str, bad: bool, t: Tokens) -> | |||
| 138 | .child(words(name, Text::Caption, Ink::Faint, t)) | 145 | .child(words(name, Text::Caption, Ink::Faint, t)) |
| 139 | } | 146 | } |
| 140 | 147 | ||
| 141 | -fn connection(chat: &Chat, t: Tokens) -> Div { | 148 | +fn connection(stats: &Stats, t: Tokens) -> Div { |
| 142 | - let latest = chat.voice_samples.back(); | 149 | + let latest = stats.voice.back(); |
| 143 | let quality = latest.map(Quality::of); | 150 | let quality = latest.map(Quality::of); |
| 144 | let bad = quality == Some(Quality::Poor); | 151 | let bad = quality == Some(Quality::Poor); |
| 145 | let field = |value: Option<String>| value.unwrap_or_else(|| "…".into()); | 152 | let field = |value: Option<String>| value.unwrap_or_else(|| "…".into()); |
| @@ -185,7 +192,8 @@ fn connection(chat: &Chat, t: Tokens) -> Div { | |||
| 185 | ) | 192 | ) |
| 186 | .child( | 193 | .child( |
| 187 | div().h(px(size::CONTROL_SM)).child(spark( | 194 | div().h(px(size::CONTROL_SM)).child(spark( |
| 188 | - chat.voice_samples | 195 | + stats |
| 196 | + .voice | ||
| 189 | .iter() | 197 | .iter() |
| 190 | .map(|sample| (sample.ping_ms / PING_SCALE).clamp(0., 1.)) | 198 | .map(|sample| (sample.ping_ms / PING_SCALE).clamp(0., 1.)) |
| 191 | .collect(), | 199 | .collect(), |
| @@ -195,9 +203,9 @@ fn connection(chat: &Chat, t: Tokens) -> Div { | |||
| 195 | ) | 203 | ) |
| 196 | } | 204 | } |
| 197 | 205 | ||
| 198 | -fn traffic(chat: &Chat, t: Tokens) -> Div { | 206 | +fn traffic(stats: &Stats, t: Tokens) -> Div { |
| 199 | - let up: Vec<f32> = chat.traffic.iter().map(|(up, _)| *up).collect(); | 207 | + let up: Vec<f32> = stats.traffic.iter().map(|(up, _)| *up).collect(); |
| 200 | - let down: Vec<f32> = chat.traffic.iter().map(|(_, down)| *down).collect(); | 208 | + let down: Vec<f32> = stats.traffic.iter().map(|(_, down)| *down).collect(); |
| 201 | let max = up.iter().chain(&down).copied().fold(1., f32::max); | 209 | let max = up.iter().chain(&down).copied().fold(1., f32::max); |
| 202 | let line = |values: &[f32], ink: Hsla| { | 210 | let line = |values: &[f32], ink: Hsla| { |
| 203 | div() | 211 | div() |
| @@ -347,7 +355,11 @@ fn sending(sending: &[(Kind, LayerStats)], t: Tokens) -> Vec<Div> { | |||
| 347 | .collect() | 355 | .collect() |
| 348 | } | 356 | } |
| 349 | 357 | ||
| 350 | -fn watching(watching: &[(Id, Kind, WatchStats)], chat: &Chat, t: Tokens) -> Option<Div> { | 358 | +fn watching( |
| 359 | + watching: &[(Id, Kind, WatchStats)], | ||
| 360 | + name: &dyn Fn(Id) -> String, | ||
| 361 | + t: Tokens, | ||
| 362 | +) -> Option<Div> { | ||
| 351 | if watching.is_empty() { | 363 | if watching.is_empty() { |
| 352 | return None; | 364 | return None; |
| 353 | } | 365 | } |
| @@ -374,9 +386,7 @@ fn watching(watching: &[(Id, Kind, WatchStats)], chat: &Chat, t: Tokens) -> Opti | |||
| 374 | t, | 386 | t, |
| 375 | )) | 387 | )) |
| 376 | .children(watching.iter().map(|(user_id, kind, stats)| { | 388 | .children(watching.iter().map(|(user_id, kind, stats)| { |
| 377 | - let name = chat | 389 | + let name = name(*user_id); |
| 378 | - .author(*user_id) | ||
| 379 | - .map_or_else(|| user_id.to_string(), |user| user.username.clone()); | ||
| 380 | let detail = format!( | 390 | let detail = format!( |
| 381 | "{} · {}", | 391 | "{} · {}", |
| 382 | kind_word(*kind), | 392 | kind_word(*kind), |
crates/client/src/lib.rsmodified+1 -1
| @@ -13,7 +13,7 @@ pub mod net; | |||
| 13 | pub mod settings; | 13 | pub mod settings; |
| 14 | pub mod share; | 14 | pub mod share; |
| 15 | pub mod tray; | 15 | pub mod tray; |
| 16 | -pub mod usage; | 16 | +pub use usage; |
| 17 | pub mod voice; | 17 | pub mod voice; |
| 18 | 18 | ||
| 19 | pub const NAME: &str = "Yumush"; | 19 | pub const NAME: &str = "Yumush"; |
crates/client/src/media/decode.rsmodified+1 -5
| @@ -178,11 +178,7 @@ impl Picture { | |||
| 178 | } | 178 | } |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] | 181 | +pub use common::stats::Engine; |
| 182 | -pub enum Engine { | ||
| 183 | - Hardware, | ||
| 184 | - Software, | ||
| 185 | -} | ||
| 186 | 182 | ||
| 187 | pub enum VideoDecoder { | 183 | pub enum VideoDecoder { |
| 188 | H264(Box<H264Decoder>), | 184 | H264(Box<H264Decoder>), |
crates/client/src/share/session.rsmodified+1 -30
| @@ -95,36 +95,7 @@ pub enum Tick { | |||
| 95 | Loss(u16), | 95 | Loss(u16), |
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | -pub struct LayerStats { | 98 | +pub use common::stats::{LayerStats, ShareSample, WatchStats}; |
| 99 | - pub layer: Layer, | ||
| 100 | - pub engine: Engine, | ||
| 101 | - pub kbps: u32, | ||
| 102 | - pub fps: f32, | ||
| 103 | - pub gap_ms: (i64, i64), | ||
| 104 | -} | ||
| 105 | - | ||
| 106 | -#[derive(Default)] | ||
| 107 | -pub struct WatchStats { | ||
| 108 | - pub layer: Option<Layer>, | ||
| 109 | - pub engine: Option<Engine>, | ||
| 110 | - pub kbps: u32, | ||
| 111 | - pub fps: f32, | ||
| 112 | - pub loss: f32, | ||
| 113 | - pub keyframes: u32, | ||
| 114 | - pub buffer_ms: i64, | ||
| 115 | - pub latency_ms: Option<i64>, | ||
| 116 | - pub nacks: u32, | ||
| 117 | - pub shown_fps: f32, | ||
| 118 | - pub dropped: u32, | ||
| 119 | - pub interval_ms: (i64, i64), | ||
| 120 | - pub swing_ms: i64, | ||
| 121 | -} | ||
| 122 | - | ||
| 123 | -#[derive(Default)] | ||
| 124 | -pub struct ShareSample { | ||
| 125 | - pub sending: Vec<(Kind, LayerStats)>, | ||
| 126 | - pub watching: Vec<(Id, Kind, WatchStats)>, | ||
| 127 | -} | ||
| 128 | 99 | ||
| 129 | struct Tally { | 100 | struct Tally { |
| 130 | engine: Engine, | 101 | engine: Engine, |
crates/client/src/voice/engine.rsmodified+1 -8
| @@ -84,14 +84,7 @@ struct Speaker { | |||
| 84 | spectrum: Spectrum, | 84 | spectrum: Spectrum, |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | -#[derive(Debug, Clone, Copy, Default)] | 87 | +pub use common::stats::Traffic; |
| 88 | -pub struct Traffic { | ||
| 89 | - pub jitter_ms: f32, | ||
| 90 | - pub buffer_ms: f32, | ||
| 91 | - pub loss: f32, | ||
| 92 | - pub up_kbps: f32, | ||
| 93 | - pub down_kbps: f32, | ||
| 94 | -} | ||
| 95 | 88 | ||
| 96 | impl Speaker { | 89 | impl Speaker { |
| 97 | fn new(sequence: u32, now: Instant) -> Self { | 90 | fn new(sequence: u32, now: Instant) -> Self { |
crates/client/src/voice/mod.rsmodified+2 -7
| @@ -20,7 +20,7 @@ pub mod engine; | |||
| 20 | 20 | ||
| 21 | use crate::settings::VoiceSettings; | 21 | use crate::settings::VoiceSettings; |
| 22 | use audio::{Audio, Choice, Worker}; | 22 | use audio::{Audio, Choice, Worker}; |
| 23 | -use engine::{Engine, SILENT_DB, Traffic}; | 23 | +use engine::{Engine, SILENT_DB}; |
| 24 | 24 | ||
| 25 | const RING_SAMPLES: usize = VOICE_SAMPLE_RATE as usize / 2; | 25 | const RING_SAMPLES: usize = VOICE_SAMPLE_RATE as usize / 2; |
| 26 | const PLAYBACK_RING: usize = RING_SAMPLES * PLAYBACK_CHANNELS; | 26 | const PLAYBACK_RING: usize = RING_SAMPLES * PLAYBACK_CHANNELS; |
| @@ -34,12 +34,7 @@ pub const PLAYBACK_CHANNELS: usize = 2; | |||
| 34 | pub type Capture = Arc<Mutex<Producer<f32>>>; | 34 | pub type Capture = Arc<Mutex<Producer<f32>>>; |
| 35 | pub type Playback = Arc<Mutex<Consumer<f32>>>; | 35 | pub type Playback = Arc<Mutex<Consumer<f32>>>; |
| 36 | 36 | ||
| 37 | -#[derive(Debug, Clone, Copy, Default)] | 37 | +pub use common::stats::VoiceSample; |
| 38 | -pub struct VoiceSample { | ||
| 39 | - pub ping_ms: f32, | ||
| 40 | - pub traffic: Traffic, | ||
| 41 | - pub bitrate_kbps: u32, | ||
| 42 | -} | ||
| 43 | 38 | ||
| 44 | pub struct Voice { | 39 | pub struct Voice { |
| 45 | engine: Arc<Mutex<Engine>>, | 40 | engine: Arc<Mutex<Engine>>, |
crates/common/src/call.rsmodified+17 -0
| @@ -11,6 +11,7 @@ use crate::{ | |||
| 11 | Role, ServerBan, ServerSettings, SessionInfo, Unread, Usage, User, VoiceRoom, | 11 | Role, ServerBan, ServerSettings, SessionInfo, Unread, Usage, User, VoiceRoom, |
| 12 | }, | 12 | }, |
| 13 | share::{Codec, Kind, Quality, Share, Source}, | 13 | share::{Codec, Kind, Quality, Share, Source}, |
| 14 | + stats::Stats, | ||
| 14 | validate::{ | 15 | validate::{ |
| 15 | validate_avatar_size, validate_community_name, validate_forward, validate_new_message, | 16 | validate_avatar_size, validate_community_name, validate_forward, validate_new_message, |
| 16 | validate_password, validate_poll, validate_reaction, validate_reason, | 17 | validate_password, validate_poll, validate_reaction, validate_reason, |
| @@ -619,6 +620,19 @@ pub struct CreateBackup; | |||
| 619 | #[derive(Debug, Clone, Encode, Decode)] | 620 | #[derive(Debug, Clone, Encode, Decode)] |
| 620 | pub struct ListBackups; | 621 | pub struct ListBackups; |
| 621 | 622 | ||
| 623 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 624 | +pub struct FollowStats { | ||
| 625 | + pub user_id: Option<Id>, | ||
| 626 | +} | ||
| 627 | + | ||
| 628 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 629 | +pub struct GetServerStats; | ||
| 630 | + | ||
| 631 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 632 | +pub struct PushStats { | ||
| 633 | + pub stats: Stats, | ||
| 634 | +} | ||
| 635 | + | ||
| 622 | #[derive(Debug, Clone, Encode, Decode)] | 636 | #[derive(Debug, Clone, Encode, Decode)] |
| 623 | pub struct DeleteBackup { | 637 | pub struct DeleteBackup { |
| 624 | pub name: String, | 638 | pub name: String, |
| @@ -744,4 +758,7 @@ calls! { | |||
| 744 | CreateBackup: Builder -> BackupInfo; | 758 | CreateBackup: Builder -> BackupInfo; |
| 745 | ListBackups: Builder -> Vec<BackupInfo>; | 759 | ListBackups: Builder -> Vec<BackupInfo>; |
| 746 | DeleteBackup: Builder -> (); | 760 | DeleteBackup: Builder -> (); |
| 761 | + FollowStats: Builder -> Option<Stats>; | ||
| 762 | + PushStats: User -> (); | ||
| 763 | + GetServerStats: Builder -> Stats; | ||
| 747 | } | 764 | } |
crates/common/src/event.rsmodified+6 -0
| @@ -5,6 +5,7 @@ use crate::model::{ | |||
| 5 | User, | 5 | User, |
| 6 | }; | 6 | }; |
| 7 | use crate::share::{Kind, Layer, Share}; | 7 | use crate::share::{Kind, Layer, Share}; |
| 8 | +use crate::stats::Stats; | ||
| 8 | 9 | ||
| 9 | #[derive(Debug, Clone, Encode, Decode)] | 10 | #[derive(Debug, Clone, Encode, Decode)] |
| 10 | pub enum Event { | 11 | pub enum Event { |
| @@ -80,6 +81,11 @@ pub enum Event { | |||
| 80 | user_id: Id, | 81 | user_id: Id, |
| 81 | kind: Kind, | 82 | kind: Kind, |
| 82 | }, | 83 | }, |
| 84 | + StatsWanted(bool), | ||
| 85 | + StatsOf { | ||
| 86 | + user_id: Id, | ||
| 87 | + stats: Stats, | ||
| 88 | + }, | ||
| 83 | } | 89 | } |
| 84 | 90 | ||
| 85 | #[derive(Debug, Clone, Encode, Decode)] | 91 | #[derive(Debug, Clone, Encode, Decode)] |
crates/common/src/lib.rsmodified+1 -0
| @@ -10,6 +10,7 @@ pub mod open; | |||
| 10 | pub mod permission; | 10 | pub mod permission; |
| 11 | pub mod session; | 11 | pub mod session; |
| 12 | pub mod share; | 12 | pub mod share; |
| 13 | +pub mod stats; | ||
| 13 | pub mod time; | 14 | pub mod time; |
| 14 | pub mod transport; | 15 | pub mod transport; |
| 15 | pub mod validate; | 16 | pub mod validate; |
crates/common/src/stats.rsadded+112 -0
| @@ -0,0 +1,112 @@ | |||
| 1 | +use std::collections::VecDeque; | ||
| 2 | + | ||
| 3 | +use bitcode::{Decode, Encode}; | ||
| 4 | + | ||
| 5 | +use crate::{ | ||
| 6 | + model::Id, | ||
| 7 | + share::{Kind, Layer}, | ||
| 8 | +}; | ||
| 9 | + | ||
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] | ||
| 11 | +pub enum Engine { | ||
| 12 | + Hardware, | ||
| 13 | + Software, | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +#[derive(Debug, Clone, Copy, Default, Encode, Decode)] | ||
| 17 | +pub struct Traffic { | ||
| 18 | + pub jitter_ms: f32, | ||
| 19 | + pub buffer_ms: f32, | ||
| 20 | + pub loss: f32, | ||
| 21 | + pub up_kbps: f32, | ||
| 22 | + pub down_kbps: f32, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +#[derive(Debug, Clone, Copy, Default, Encode, Decode)] | ||
| 26 | +pub struct VoiceSample { | ||
| 27 | + pub ping_ms: f32, | ||
| 28 | + pub traffic: Traffic, | ||
| 29 | + pub bitrate_kbps: u32, | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +#[derive(Debug, Clone, Default, Encode, Decode)] | ||
| 33 | +pub struct Usage { | ||
| 34 | + pub cpu_model: String, | ||
| 35 | + pub machine: f32, | ||
| 36 | + pub total: f32, | ||
| 37 | + pub threads: Option<Threads>, | ||
| 38 | + pub memory: Memory, | ||
| 39 | + pub gpu: Option<Gpu>, | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +#[derive(Debug, Clone, Copy, Default, Encode, Decode)] | ||
| 43 | +pub struct Threads { | ||
| 44 | + pub gui: f32, | ||
| 45 | + pub camera: f32, | ||
| 46 | + pub encode: f32, | ||
| 47 | + pub decode: f32, | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +#[derive(Debug, Clone, Copy, Default, Encode, Decode)] | ||
| 51 | +pub struct Memory { | ||
| 52 | + pub own_mb: u32, | ||
| 53 | + pub used_mb: u32, | ||
| 54 | + pub total_mb: u32, | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +#[derive(Debug, Clone, Default, Encode, Decode)] | ||
| 58 | +pub struct Gpu { | ||
| 59 | + pub model: String, | ||
| 60 | + pub gfx: f32, | ||
| 61 | + pub video: f32, | ||
| 62 | + pub vram_mb: u32, | ||
| 63 | + pub machine: GpuMachine, | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +#[derive(Debug, Clone, Copy, Default, Encode, Decode)] | ||
| 67 | +pub struct GpuMachine { | ||
| 68 | + pub gfx: f32, | ||
| 69 | + pub video: f32, | ||
| 70 | + pub vram_used_mb: u32, | ||
| 71 | + pub vram_total_mb: u32, | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 75 | +pub struct LayerStats { | ||
| 76 | + pub layer: Layer, | ||
| 77 | + pub engine: Engine, | ||
| 78 | + pub kbps: u32, | ||
| 79 | + pub fps: f32, | ||
| 80 | + pub gap_ms: (i64, i64), | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +#[derive(Debug, Clone, Default, Encode, Decode)] | ||
| 84 | +pub struct WatchStats { | ||
| 85 | + pub layer: Option<Layer>, | ||
| 86 | + pub engine: Option<Engine>, | ||
| 87 | + pub kbps: u32, | ||
| 88 | + pub fps: f32, | ||
| 89 | + pub loss: f32, | ||
| 90 | + pub keyframes: u32, | ||
| 91 | + pub buffer_ms: i64, | ||
| 92 | + pub latency_ms: Option<i64>, | ||
| 93 | + pub nacks: u32, | ||
| 94 | + pub shown_fps: f32, | ||
| 95 | + pub dropped: u32, | ||
| 96 | + pub interval_ms: (i64, i64), | ||
| 97 | + pub swing_ms: i64, | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +#[derive(Debug, Clone, Default, Encode, Decode)] | ||
| 101 | +pub struct ShareSample { | ||
| 102 | + pub sending: Vec<(Kind, LayerStats)>, | ||
| 103 | + pub watching: Vec<(Id, Kind, WatchStats)>, | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +#[derive(Debug, Clone, Default, Encode, Decode)] | ||
| 107 | +pub struct Stats { | ||
| 108 | + pub voice: VecDeque<VoiceSample>, | ||
| 109 | + pub traffic: VecDeque<(f32, f32)>, | ||
| 110 | + pub usage: Option<Usage>, | ||
| 111 | + pub share: ShareSample, | ||
| 112 | +} | ||
crates/server/Cargo.tomlmodified+1 -0
| @@ -4,6 +4,7 @@ version = "0.1.0" | |||
| 4 | edition = "2024" | 4 | edition = "2024" |
| 5 | 5 | ||
| 6 | [dependencies] | 6 | [dependencies] |
| 7 | +usage = { path = "../usage" } | ||
| 7 | common.workspace = true | 8 | common.workspace = true |
| 8 | tokio.workspace = true | 9 | tokio.workspace = true |
| 9 | quinn = { workspace = true, features = ["rustls-aws-lc-rs"] } | 10 | quinn = { workspace = true, features = ["rustls-aws-lc-rs"] } |
crates/server/src/call/mod.rsmodified+4 -0
| @@ -2,6 +2,7 @@ mod admin; | |||
| 2 | mod community; | 2 | mod community; |
| 3 | mod message; | 3 | mod message; |
| 4 | mod social; | 4 | mod social; |
| 5 | +mod stats; | ||
| 5 | mod user; | 6 | mod user; |
| 6 | mod voice; | 7 | mod voice; |
| 7 | 8 | ||
| @@ -213,5 +214,8 @@ pub(crate) async fn dispatch(request: Request, cx: &mut Cx<'_>) -> Vec<u8> { | |||
| 213 | Request::CreateBackup(call) => serve(call, cx).await, | 214 | Request::CreateBackup(call) => serve(call, cx).await, |
| 214 | Request::ListBackups(call) => serve(call, cx).await, | 215 | Request::ListBackups(call) => serve(call, cx).await, |
| 215 | Request::DeleteBackup(call) => serve(call, cx).await, | 216 | Request::DeleteBackup(call) => serve(call, cx).await, |
| 217 | + Request::FollowStats(call) => serve(call, cx).await, | ||
| 218 | + Request::PushStats(call) => serve(call, cx).await, | ||
| 219 | + Request::GetServerStats(call) => serve(call, cx).await, | ||
| 216 | } | 220 | } |
| 217 | } | 221 | } |
crates/server/src/call/stats.rsadded+49 -0
| @@ -0,0 +1,49 @@ | |||
| 1 | +use common::{ | ||
| 2 | + call::{FollowStats, GetServerStats, PushStats}, | ||
| 3 | + event::Event, | ||
| 4 | + stats::Stats, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use crate::{ | ||
| 8 | + call::{Cx, Handle, Me}, | ||
| 9 | + error::ServerError, | ||
| 10 | + publish::To, | ||
| 11 | +}; | ||
| 12 | + | ||
| 13 | +impl Handle for FollowStats { | ||
| 14 | + async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Option<Stats>, ServerError> { | ||
| 15 | + let change = cx.sh.services.stats.follow(cx.client.id, self.user_id); | ||
| 16 | + if let Some(user_id) = change.stopped { | ||
| 17 | + cx.emit(To::User(user_id), Event::StatsWanted(false)); | ||
| 18 | + } | ||
| 19 | + if let Some(user_id) = change.started { | ||
| 20 | + cx.emit(To::User(user_id), Event::StatsWanted(true)); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + Ok(change.latest) | ||
| 24 | + } | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +impl Handle for PushStats { | ||
| 28 | + async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { | ||
| 29 | + let user_id = me.user()?.id; | ||
| 30 | + for conn in cx.sh.services.stats.push(user_id, self.stats.clone()) { | ||
| 31 | + cx.emit( | ||
| 32 | + To::Conn(conn), | ||
| 33 | + Event::StatsOf { | ||
| 34 | + user_id, | ||
| 35 | + stats: self.stats.clone(), | ||
| 36 | + }, | ||
| 37 | + ); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + Ok(()) | ||
| 41 | + } | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +impl Handle for GetServerStats { | ||
| 45 | + async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Stats, ServerError> { | ||
| 46 | + let services = &cx.sh.services; | ||
| 47 | + Ok(services.machine.sample(services.hub.bytes())) | ||
| 48 | + } | ||
| 49 | +} | ||
crates/server/src/hub.rsmodified+11 -0
| @@ -146,6 +146,17 @@ impl Hub { | |||
| 146 | .collect() | 146 | .collect() |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | + pub fn bytes(&self) -> (u64, u64) { | ||
| 150 | + let clients = self.0.lock().unwrap(); | ||
| 151 | + let mut total = (0, 0); | ||
| 152 | + for conn in clients.values().filter_map(|client| client.conn.as_ref()) { | ||
| 153 | + let stats = conn.stats(); | ||
| 154 | + total.0 += stats.udp_tx.bytes; | ||
| 155 | + total.1 += stats.udp_rx.bytes; | ||
| 156 | + } | ||
| 157 | + total | ||
| 158 | + } | ||
| 159 | + | ||
| 149 | pub fn all(&self) -> Vec<ConnId> { | 160 | pub fn all(&self) -> Vec<ConnId> { |
| 150 | self.0.lock().unwrap().keys().copied().collect() | 161 | self.0.lock().unwrap().keys().copied().collect() |
| 151 | } | 162 | } |
crates/server/src/lib.rsmodified+1 -0
| @@ -13,6 +13,7 @@ pub mod rate_limit; | |||
| 13 | pub mod request; | 13 | pub mod request; |
| 14 | pub mod retention; | 14 | pub mod retention; |
| 15 | pub mod rooms; | 15 | pub mod rooms; |
| 16 | +pub mod stats; | ||
| 16 | 17 | ||
| 17 | #[cfg(test)] | 18 | #[cfg(test)] |
| 18 | mod test; | 19 | mod test; |
crates/server/src/network.rsmodified+27 -0
| @@ -1,3 +1,5 @@ | |||
| 1 | +use crate::publish::{Broadcast, To}; | ||
| 2 | +use common::event::Event; | ||
| 1 | use std::{path::PathBuf, sync::Arc, sync::atomic::Ordering, time::Duration}; | 3 | use std::{path::PathBuf, sync::Arc, sync::atomic::Ordering, time::Duration}; |
| 2 | 4 | ||
| 3 | use bytes::Bytes; | 5 | use bytes::Bytes; |
| @@ -26,6 +28,7 @@ use crate::{ | |||
| 26 | rate_limit::RateLimit, | 28 | rate_limit::RateLimit, |
| 27 | request, retention, | 29 | request, retention, |
| 28 | rooms::Rooms, | 30 | rooms::Rooms, |
| 31 | + stats::{Follows, Machine}, | ||
| 29 | }; | 32 | }; |
| 30 | 33 | ||
| 31 | const HELLO_DEADLINE: Duration = Duration::from_secs(5); | 34 | const HELLO_DEADLINE: Duration = Duration::from_secs(5); |
| @@ -35,6 +38,8 @@ pub struct Services { | |||
| 35 | pub rooms: Rooms, | 38 | pub rooms: Rooms, |
| 36 | pub hub: Hub, | 39 | pub hub: Hub, |
| 37 | pub rate_limit: RateLimit, | 40 | pub rate_limit: RateLimit, |
| 41 | + pub stats: Follows, | ||
| 42 | + pub machine: Machine, | ||
| 38 | } | 43 | } |
| 39 | 44 | ||
| 40 | #[derive(Clone)] | 45 | #[derive(Clone)] |
| @@ -145,6 +150,19 @@ async fn serve_connection(incoming: Incoming, shared: Shared) -> Result<(), Serv | |||
| 145 | }; | 150 | }; |
| 146 | tokio::spawn(write_events(events, send_stream)); | 151 | tokio::spawn(write_events(events, send_stream)); |
| 147 | publish_presence(&shared, presence).await; | 152 | publish_presence(&shared, presence).await; |
| 153 | + if client | ||
| 154 | + .user_id() | ||
| 155 | + .is_some_and(|user_id| shared.services.stats.is_followed(user_id)) | ||
| 156 | + { | ||
| 157 | + let conn = client.id; | ||
| 158 | + publish::job(&shared, async |_| { | ||
| 159 | + Ok(vec![Broadcast::new( | ||
| 160 | + To::Conn(conn), | ||
| 161 | + Event::StatsWanted(true), | ||
| 162 | + )]) | ||
| 163 | + }) | ||
| 164 | + .await?; | ||
| 165 | + } | ||
| 148 | 166 | ||
| 149 | tokio::spawn(datagram::relay_datagrams( | 167 | tokio::spawn(datagram::relay_datagrams( |
| 150 | connection.clone(), | 168 | connection.clone(), |
| @@ -163,6 +181,15 @@ async fn serve_connection(incoming: Incoming, shared: Shared) -> Result<(), Serv | |||
| 163 | 181 | ||
| 164 | client.closed.store(true, Ordering::Release); | 182 | client.closed.store(true, Ordering::Release); |
| 165 | let presence = hub.remove(client.id); | 183 | let presence = hub.remove(client.id); |
| 184 | + if let Some(user_id) = shared.services.stats.leave(client.id) { | ||
| 185 | + publish::job(&shared, async |_| { | ||
| 186 | + Ok(vec![Broadcast::new( | ||
| 187 | + To::User(user_id), | ||
| 188 | + Event::StatsWanted(false), | ||
| 189 | + )]) | ||
| 190 | + }) | ||
| 191 | + .await?; | ||
| 192 | + } | ||
| 166 | 193 | ||
| 167 | if let Some(me) = &client.me { | 194 | if let Some(me) = &client.me { |
| 168 | publish::job(&shared, async |db| { | 195 | publish::job(&shared, async |db| { |
crates/server/src/stats.rsadded+109 -0
| @@ -0,0 +1,109 @@ | |||
| 1 | +use std::{ | ||
| 2 | + collections::{HashMap, VecDeque}, | ||
| 3 | + sync::{Arc, Mutex}, | ||
| 4 | + time::Instant, | ||
| 5 | +}; | ||
| 6 | + | ||
| 7 | +use common::{model::Id, stats::Stats}; | ||
| 8 | + | ||
| 9 | +use usage::Monitor; | ||
| 10 | + | ||
| 11 | +use crate::hub::ConnId; | ||
| 12 | + | ||
| 13 | +#[derive(Default)] | ||
| 14 | +struct Inner { | ||
| 15 | + following: HashMap<ConnId, Id>, | ||
| 16 | + latest: HashMap<Id, Stats>, | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +pub struct Change { | ||
| 20 | + pub stopped: Option<Id>, | ||
| 21 | + pub started: Option<Id>, | ||
| 22 | + pub latest: Option<Stats>, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +#[derive(Clone, Default)] | ||
| 26 | +pub struct Follows(Arc<Mutex<Inner>>); | ||
| 27 | + | ||
| 28 | +impl Follows { | ||
| 29 | + pub fn follow(&self, conn: ConnId, target: Option<Id>) -> Change { | ||
| 30 | + let mut inner = self.0.lock().unwrap(); | ||
| 31 | + let old = inner.following.remove(&conn); | ||
| 32 | + if let Some(target) = target { | ||
| 33 | + inner.following.insert(conn, target); | ||
| 34 | + } | ||
| 35 | + let followed = |user: Id| inner.following.values().any(|id| *id == user); | ||
| 36 | + Change { | ||
| 37 | + stopped: old.filter(|old| !followed(*old)), | ||
| 38 | + started: target.filter(|target| { | ||
| 39 | + old != Some(*target) | ||
| 40 | + && inner.following.values().filter(|id| *id == target).count() == 1 | ||
| 41 | + }), | ||
| 42 | + latest: target.and_then(|target| inner.latest.get(&target).cloned()), | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + pub fn leave(&self, conn: ConnId) -> Option<Id> { | ||
| 47 | + self.follow(conn, None).stopped | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + pub fn push(&self, user: Id, stats: Stats) -> Vec<ConnId> { | ||
| 51 | + let mut inner = self.0.lock().unwrap(); | ||
| 52 | + inner.latest.insert(user, stats); | ||
| 53 | + inner | ||
| 54 | + .following | ||
| 55 | + .iter() | ||
| 56 | + .filter(|(_, id)| **id == user) | ||
| 57 | + .map(|(conn, _)| *conn) | ||
| 58 | + .collect() | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + pub fn is_followed(&self, user: Id) -> bool { | ||
| 62 | + self.0 | ||
| 63 | + .lock() | ||
| 64 | + .unwrap() | ||
| 65 | + .following | ||
| 66 | + .values() | ||
| 67 | + .any(|id| *id == user) | ||
| 68 | + } | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +const HISTORY: usize = 60; | ||
| 72 | + | ||
| 73 | +#[derive(Default)] | ||
| 74 | +struct Rates { | ||
| 75 | + last: Option<(Instant, u64, u64)>, | ||
| 76 | + history: VecDeque<(f32, f32)>, | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +#[derive(Clone, Default)] | ||
| 80 | +pub struct Machine { | ||
| 81 | + monitor: Arc<Monitor>, | ||
| 82 | + rates: Arc<Mutex<Rates>>, | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +impl Machine { | ||
| 86 | + pub fn sample(&self, (sent, received): (u64, u64)) -> Stats { | ||
| 87 | + let now = Instant::now(); | ||
| 88 | + let mut rates = self.rates.lock().unwrap(); | ||
| 89 | + if let Some((at, last_sent, last_received)) = rates.last { | ||
| 90 | + let seconds = now.duration_since(at).as_secs_f32().max(f32::EPSILON); | ||
| 91 | + let kbps = | ||
| 92 | + |bytes: u64, last: u64| bytes.saturating_sub(last) as f32 * 8. / 1000. / seconds; | ||
| 93 | + let rate = (kbps(sent, last_sent), kbps(received, last_received)); | ||
| 94 | + rates.history.push_back(rate); | ||
| 95 | + while rates.history.len() > HISTORY { | ||
| 96 | + rates.history.pop_front(); | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + rates.last = Some((now, sent, received)); | ||
| 100 | + Stats { | ||
| 101 | + traffic: rates.history.clone(), | ||
| 102 | + usage: self.monitor.latest().map(|usage| usage::Usage { | ||
| 103 | + threads: None, | ||
| 104 | + ..usage | ||
| 105 | + }), | ||
| 106 | + ..Stats::default() | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | +} | ||
crates/usage/Cargo.tomladded+19 -0
| @@ -0,0 +1,19 @@ | |||
| 1 | +[package] | ||
| 2 | +name = "usage" | ||
| 3 | +version = "0.1.0" | ||
| 4 | +edition = "2024" | ||
| 5 | + | ||
| 6 | +[dependencies] | ||
| 7 | +common.workspace = true | ||
| 8 | +sysinfo = { version = "0.40.4", default-features = false, features = ["system"] } | ||
| 9 | + | ||
| 10 | +[target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies] | ||
| 11 | +nvml-wrapper = "0.13.0" | ||
| 12 | + | ||
| 13 | +[target.'cfg(target_os = "windows")'.dependencies] | ||
| 14 | +wmi = "0.18.4" | ||
| 15 | +winreg = "0.56.0" | ||
| 16 | +serde = { version = "1.0.229", features = ["derive"] } | ||
| 17 | + | ||
| 18 | +[lints] | ||
| 19 | +workspace = true | ||
crates/client/src/usage/drm.rs → crates/usage/src/drm.rsrenamed+1 -1
| @@ -104,7 +104,7 @@ impl Sampler { | |||
| 104 | }, | 104 | }, |
| 105 | ); | 105 | ); |
| 106 | Some(Gpu { | 106 | Some(Gpu { |
| 107 | - model, | 107 | + model: model.to_string(), |
| 108 | gfx: percent(current.own.gfx, previous.own.gfx, wall), | 108 | gfx: percent(current.own.gfx, previous.own.gfx, wall), |
| 109 | video: percent(current.own.video, previous.own.video, wall), | 109 | video: percent(current.own.video, previous.own.video, wall), |
| 110 | vram_mb: (current.own.vram_kib >> 10) as u32, | 110 | vram_mb: (current.own.vram_kib >> 10) as u32, |
crates/client/src/usage/mod.rs → crates/usage/src/lib.rsrenamed+2 -42
| @@ -21,47 +21,7 @@ mod windows; | |||
| 21 | const INTERVAL: Duration = Duration::from_secs(1); | 21 | const INTERVAL: Duration = Duration::from_secs(1); |
| 22 | const IDLE_AFTER: Duration = Duration::from_secs(3); | 22 | const IDLE_AFTER: Duration = Duration::from_secs(3); |
| 23 | 23 | ||
| 24 | -#[derive(Clone, Default)] | 24 | +pub use common::stats::{Gpu, GpuMachine, Memory, Threads, Usage}; |
| 25 | -pub struct Usage { | ||
| 26 | - pub cpu_model: Arc<str>, | ||
| 27 | - pub machine: f32, | ||
| 28 | - pub total: f32, | ||
| 29 | - pub threads: Option<Threads>, | ||
| 30 | - pub memory: Memory, | ||
| 31 | - pub gpu: Option<Gpu>, | ||
| 32 | -} | ||
| 33 | - | ||
| 34 | -#[derive(Clone, Copy, Default)] | ||
| 35 | -pub struct Threads { | ||
| 36 | - pub gui: f32, | ||
| 37 | - pub camera: f32, | ||
| 38 | - pub encode: f32, | ||
| 39 | - pub decode: f32, | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -#[derive(Clone, Copy, Default)] | ||
| 43 | -pub struct Memory { | ||
| 44 | - pub own_mb: u32, | ||
| 45 | - pub used_mb: u32, | ||
| 46 | - pub total_mb: u32, | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -#[derive(Clone, Default)] | ||
| 50 | -pub struct Gpu { | ||
| 51 | - pub model: Arc<str>, | ||
| 52 | - pub gfx: f32, | ||
| 53 | - pub video: f32, | ||
| 54 | - pub vram_mb: u32, | ||
| 55 | - pub machine: GpuMachine, | ||
| 56 | -} | ||
| 57 | - | ||
| 58 | -#[derive(Clone, Copy, Default)] | ||
| 59 | -pub struct GpuMachine { | ||
| 60 | - pub gfx: f32, | ||
| 61 | - pub video: f32, | ||
| 62 | - pub vram_used_mb: u32, | ||
| 63 | - pub vram_total_mb: u32, | ||
| 64 | -} | ||
| 65 | 25 | ||
| 66 | struct Shared { | 26 | struct Shared { |
| 67 | latest: Option<Usage>, | 27 | latest: Option<Usage>, |
| @@ -174,7 +134,7 @@ impl Sampler { | |||
| 174 | #[cfg(not(target_os = "linux"))] | 134 | #[cfg(not(target_os = "linux"))] |
| 175 | let threads = None; | 135 | let threads = None; |
| 176 | Some(Usage { | 136 | Some(Usage { |
| 177 | - cpu_model: self.cpu_model.clone(), | 137 | + cpu_model: self.cpu_model.to_string(), |
| 178 | machine: self.system.global_cpu_usage(), | 138 | machine: self.system.global_cpu_usage(), |
| 179 | total: process.cpu_usage() / self.cores, | 139 | total: process.cpu_usage() / self.cores, |
| 180 | threads, | 140 | threads, |
crates/client/src/usage/nvidia.rs → crates/usage/src/nvidia.rsrenamed+3 -3
| @@ -10,7 +10,7 @@ const MIB: u64 = 1024 * 1024; | |||
| 10 | enum State { | 10 | enum State { |
| 11 | #[default] | 11 | #[default] |
| 12 | Unset, | 12 | Unset, |
| 13 | - Ready(Nvml), | 13 | + Ready(Box<Nvml>), |
| 14 | Failed, | 14 | Failed, |
| 15 | } | 15 | } |
| 16 | 16 | ||
| @@ -25,7 +25,7 @@ impl Sampler { | |||
| 25 | pub fn sample(&mut self) -> Option<Gpu> { | 25 | pub fn sample(&mut self) -> Option<Gpu> { |
| 26 | if let State::Unset = self.state { | 26 | if let State::Unset = self.state { |
| 27 | self.state = match Nvml::init() { | 27 | self.state = match Nvml::init() { |
| 28 | - Ok(nvml) => State::Ready(nvml), | 28 | + Ok(nvml) => State::Ready(Box::new(nvml)), |
| 29 | Err(_) => State::Failed, | 29 | Err(_) => State::Failed, |
| 30 | }; | 30 | }; |
| 31 | } | 31 | } |
| @@ -91,7 +91,7 @@ impl Sampler { | |||
| 91 | }) | 91 | }) |
| 92 | .clone(); | 92 | .clone(); |
| 93 | return Some(Gpu { | 93 | return Some(Gpu { |
| 94 | - model, | 94 | + model: model.to_string(), |
| 95 | gfx: gfx as f32, | 95 | gfx: gfx as f32, |
| 96 | video: video as f32, | 96 | video: video as f32, |
| 97 | vram_mb, | 97 | vram_mb, |
crates/client/src/usage/threads.rs → crates/usage/src/threads.rsrenamed+0 -0
crates/client/src/usage/windows.rs → crates/usage/src/windows.rsrenamed+0 -0