mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
reorganize snapshot stats, implement detailed summary
This commit is contained in:
parent
9b9c47cbb9
commit
4658e2c481
3 changed files with 47 additions and 24 deletions
|
|
@ -11,14 +11,14 @@ use list::List;
|
|||
use log::*;
|
||||
use opt::{AuthMode, Opt};
|
||||
use rocket::{State, http::Status, serde::json::Json};
|
||||
use stats::{Snapshot, Total};
|
||||
use stats::{Snap, Total};
|
||||
use std::{future::Future, sync::Arc};
|
||||
use structopt::StructOpt;
|
||||
use tokio::{net::TcpListener, task};
|
||||
|
||||
#[rocket::get("/")]
|
||||
async fn index(totals: &State<Arc<Total>>) -> Json<Snapshot> {
|
||||
Json(totals.inner().snapshot())
|
||||
async fn index(totals: &State<Arc<Total>>) -> Json<Snap> {
|
||||
Json(totals.inner().snap())
|
||||
}
|
||||
|
||||
#[rocket::get("/allow/<rule>")]
|
||||
|
|
|
|||
30
src/stats.rs
30
src/stats.rs
|
|
@ -1,13 +1,8 @@
|
|||
use rocket::serde::Serialize;
|
||||
mod snap;
|
||||
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Snapshot {
|
||||
pub request: u64,
|
||||
pub blocked: u64,
|
||||
pub entries: u64,
|
||||
pub percent: f32,
|
||||
}
|
||||
pub use snap::Snap;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Total {
|
||||
|
|
@ -23,19 +18,12 @@ impl Total {
|
|||
..Self::default()
|
||||
}
|
||||
}
|
||||
pub fn snapshot(&self) -> Snapshot {
|
||||
let request = self.request.load(Ordering::Relaxed);
|
||||
let blocked = self.blocked.load(Ordering::Relaxed);
|
||||
Snapshot {
|
||||
request,
|
||||
blocked,
|
||||
entries: self.entries.load(Ordering::Relaxed),
|
||||
percent: if request > 0 {
|
||||
blocked as f32 * 100.0 / request as f32
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
}
|
||||
pub fn snap(&self) -> Snap {
|
||||
Snap::shot(
|
||||
self.entries.load(Ordering::Relaxed),
|
||||
self.request.load(Ordering::Relaxed),
|
||||
self.blocked.load(Ordering::Relaxed),
|
||||
)
|
||||
}
|
||||
pub fn set_entries(&self, value: u64) {
|
||||
self.entries.store(value, Ordering::Relaxed);
|
||||
|
|
|
|||
35
src/stats/snap.rs
Normal file
35
src/stats/snap.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use rocket::serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Sum {
|
||||
total: u64,
|
||||
percent: f32,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Snap {
|
||||
rules: u64,
|
||||
request: Sum,
|
||||
blocked: Sum,
|
||||
}
|
||||
|
||||
impl Snap {
|
||||
pub fn shot(rules: u64, request: u64, blocked: u64) -> Self {
|
||||
let blocked_percent = if request > 0 {
|
||||
blocked as f32 * 100.0 / request as f32
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
Self {
|
||||
rules,
|
||||
request: Sum {
|
||||
total: request,
|
||||
percent: 100.0 - blocked_percent,
|
||||
},
|
||||
blocked: Sum {
|
||||
total: blocked,
|
||||
percent: blocked_percent,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue