yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use std::path::PathBuf; |
| 2 | use std::{ |
| 3 | collections::{HashMap, VecDeque}, |
| 4 | sync::{Arc, Mutex}, |
| 5 | time::Instant, |
| 6 | }; |
| 7 | |
| 8 | use common::{model::Id, stats::Stats}; |
| 9 | |
| 10 | use usage::Monitor; |
| 11 | |
| 12 | use crate::hub::ConnId; |
| 13 | |
| 14 | #[derive(Default)] |
| 15 | struct Inner { |
| 16 | following: HashMap<ConnId, Id>, |
| 17 | latest: HashMap<Id, Stats>, |
| 18 | } |
| 19 | |
| 20 | pub struct Change { |
| 21 | pub stopped: Option<Id>, |
| 22 | pub started: Option<Id>, |
| 23 | pub latest: Option<Stats>, |
| 24 | } |
| 25 | |
| 26 | #[derive(Clone, Default)] |
| 27 | pub struct Follows(Arc<Mutex<Inner>>); |
| 28 | |
| 29 | impl Follows { |
| 30 | pub fn follow(&self, conn: ConnId, target: Option<Id>) -> Change { |
| 31 | let mut inner = self.0.lock().unwrap(); |
| 32 | let old = inner.following.remove(&conn); |
| 33 | if let Some(target) = target { |
| 34 | inner.following.insert(conn, target); |
| 35 | } |
| 36 | let followed = |user: Id| inner.following.values().any(|id| *id == user); |
| 37 | Change { |
| 38 | stopped: old.filter(|old| !followed(*old)), |
| 39 | started: target.filter(|target| { |
| 40 | old != Some(*target) |
| 41 | && inner.following.values().filter(|id| *id == target).count() == 1 |
| 42 | }), |
| 43 | latest: target.and_then(|target| inner.latest.get(&target).cloned()), |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn leave(&self, conn: ConnId) -> Option<Id> { |
| 48 | self.follow(conn, None).stopped |
| 49 | } |
| 50 | |
| 51 | pub fn push(&self, user: Id, stats: Stats) -> Vec<ConnId> { |
| 52 | let mut inner = self.0.lock().unwrap(); |
| 53 | inner.latest.insert(user, stats); |
| 54 | inner |
| 55 | .following |
| 56 | .iter() |
| 57 | .filter(|(_, id)| **id == user) |
| 58 | .map(|(conn, _)| *conn) |
| 59 | .collect() |
| 60 | } |
| 61 | |
| 62 | pub fn is_followed(&self, user: Id) -> bool { |
| 63 | self.0 |
| 64 | .lock() |
| 65 | .unwrap() |
| 66 | .following |
| 67 | .values() |
| 68 | .any(|id| *id == user) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const HISTORY: usize = 60; |
| 73 | |
| 74 | #[derive(Default)] |
| 75 | struct Rates { |
| 76 | last: Option<(Instant, u64, u64)>, |
| 77 | history: VecDeque<(u64, f32, f32)>, |
| 78 | } |
| 79 | |
| 80 | #[derive(Clone, Default)] |
| 81 | pub struct Machine { |
| 82 | monitor: Arc<Monitor>, |
| 83 | rates: Arc<Mutex<Rates>>, |
| 84 | } |
| 85 | |
| 86 | impl Machine { |
| 87 | pub fn watch(&self, directory: PathBuf) { |
| 88 | self.monitor.watch_disk(directory); |
| 89 | self.monitor.stay_awake(); |
| 90 | } |
| 91 | |
| 92 | pub fn sample(&self, (sent, received): (u64, u64)) -> Stats { |
| 93 | let now = Instant::now(); |
| 94 | let mut rates = self.rates.lock().unwrap(); |
| 95 | if let Some((at, last_sent, last_received)) = rates.last { |
| 96 | let seconds = now.duration_since(at).as_secs_f32().max(f32::EPSILON); |
| 97 | let kbps = |
| 98 | |bytes: u64, last: u64| bytes.saturating_sub(last) as f32 * 8. / 1000. / seconds; |
| 99 | let rate = ( |
| 100 | common::time::now_ms(), |
| 101 | kbps(sent, last_sent), |
| 102 | kbps(received, last_received), |
| 103 | ); |
| 104 | rates.history.push_back(rate); |
| 105 | while rates.history.len() > HISTORY { |
| 106 | rates.history.pop_front(); |
| 107 | } |
| 108 | } |
| 109 | rates.last = Some((now, sent, received)); |
| 110 | Stats { |
| 111 | traffic: rates.history.clone(), |
| 112 | usage: self.monitor.latest().map(|usage| usage::Usage { |
| 113 | threads: None, |
| 114 | ..usage |
| 115 | }), |
| 116 | ..Stats::default() |
| 117 | } |
| 118 | } |
| 119 | } |