mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-04-01 08:55:27 +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 log::*;
|
||||||
use opt::{AuthMode, Opt};
|
use opt::{AuthMode, Opt};
|
||||||
use rocket::{State, http::Status, serde::json::Json};
|
use rocket::{State, http::Status, serde::json::Json};
|
||||||
use stats::{Snapshot, Total};
|
use stats::{Snap, Total};
|
||||||
use std::{future::Future, sync::Arc};
|
use std::{future::Future, sync::Arc};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use tokio::{net::TcpListener, task};
|
use tokio::{net::TcpListener, task};
|
||||||
|
|
||||||
#[rocket::get("/")]
|
#[rocket::get("/")]
|
||||||
async fn index(totals: &State<Arc<Total>>) -> Json<Snapshot> {
|
async fn index(totals: &State<Arc<Total>>) -> Json<Snap> {
|
||||||
Json(totals.inner().snapshot())
|
Json(totals.inner().snap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::get("/allow/<rule>")]
|
#[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};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
pub use snap::Snap;
|
||||||
pub struct Snapshot {
|
|
||||||
pub request: u64,
|
|
||||||
pub blocked: u64,
|
|
||||||
pub entries: u64,
|
|
||||||
pub percent: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Total {
|
pub struct Total {
|
||||||
|
|
@ -23,19 +18,12 @@ impl Total {
|
||||||
..Self::default()
|
..Self::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn snapshot(&self) -> Snapshot {
|
pub fn snap(&self) -> Snap {
|
||||||
let request = self.request.load(Ordering::Relaxed);
|
Snap::shot(
|
||||||
let blocked = self.blocked.load(Ordering::Relaxed);
|
self.entries.load(Ordering::Relaxed),
|
||||||
Snapshot {
|
self.request.load(Ordering::Relaxed),
|
||||||
request,
|
self.blocked.load(Ordering::Relaxed),
|
||||||
blocked,
|
)
|
||||||
entries: self.entries.load(Ordering::Relaxed),
|
|
||||||
percent: if request > 0 {
|
|
||||||
blocked as f32 * 100.0 / request as f32
|
|
||||||
} else {
|
|
||||||
0.0
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_entries(&self, value: u64) {
|
pub fn set_entries(&self, value: u64) {
|
||||||
self.entries.store(value, Ordering::Relaxed);
|
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