implement current rules total snap

This commit is contained in:
yggverse 2026-03-28 07:13:43 +02:00
parent a77bc468b6
commit b0475a3f24
5 changed files with 59 additions and 16 deletions

View file

@ -18,14 +18,30 @@ use std::{future::Future, sync::Arc, time::Instant};
use structopt::StructOpt; use structopt::StructOpt;
use tokio::{net::TcpListener, sync::RwLock, task}; use tokio::{net::TcpListener, sync::RwLock, task};
#[rocket::get("/")] #[rocket::get("/")] // @TODO implement Web UI here...
async fn index(totals: &State<Arc<Total>>, startup_time: &State<Instant>) -> Json<Snap> { async fn index(
Json(totals.inner().snap(startup_time.elapsed().as_secs())) // @TODO implement Web UI rules: &State<Arc<RwLock<Rules>>>,
totals: &State<Arc<Total>>,
startup_time: &State<Instant>,
) -> Json<Snap> {
let r = rules.read().await;
Json(totals.inner().snap(
stats::Rules::from_totals(r.total(true), r.total(false)),
startup_time.elapsed().as_secs(),
))
} }
#[rocket::get("/api/totals")] #[rocket::get("/api/totals")]
async fn api_totals(totals: &State<Arc<Total>>, startup_time: &State<Instant>) -> Json<Snap> { async fn api_totals(
Json(totals.inner().snap(startup_time.elapsed().as_secs())) rules: &State<Arc<RwLock<Rules>>>,
totals: &State<Arc<Total>>,
startup_time: &State<Instant>,
) -> Json<Snap> {
let r = rules.read().await;
Json(totals.inner().snap(
stats::Rules::from_totals(r.total(true), r.total(false)),
startup_time.elapsed().as_secs(),
))
} }
#[rocket::get("/api/list/enable/<alias>")] #[rocket::get("/api/list/enable/<alias>")]

View file

@ -49,13 +49,12 @@ impl Rules {
pub fn any(&self, value: &str) -> bool { pub fn any(&self, value: &str) -> bool {
self.0.values().any(|list| list.contains(value)) self.0.values().any(|list| list.contains(value))
} }
/*
/// Get total rules in session by `status` /// Get total rules in session by `status`
pub fn total(&self, status: bool) -> u64 { pub fn total(&self, is_enabled: bool) -> usize {
self.0 self.0
.values() .values()
.filter(|list| status == list.is_enabled()) .filter(|list| is_enabled == list.is_enabled())
.map(|list| list.total()) .map(|list| list.total())
.sum() .sum()
}*/ }
} }

View file

@ -45,6 +45,14 @@ impl List {
pub fn contains(&self, value: &str) -> bool { pub fn contains(&self, value: &str) -> bool {
self.is_enabled && self.rules.iter().any(|rule| rule.contains(value)) self.is_enabled && self.rules.iter().any(|rule| rule.contains(value))
} }
/// Get rules total in set
pub fn total(&self) -> usize {
self.rules.len()
}
/// Get list enabled status
pub fn is_enabled(&self) -> bool {
self.is_enabled
}
} }
async fn load(rules: &mut HashSet<Rule>, source: &Source) -> Result<()> { async fn load(rules: &mut HashSet<Rule>, source: &Source) -> Result<()> {
debug!("Load list `{source}`..."); debug!("Load list `{source}`...");

View file

@ -2,7 +2,7 @@ mod snap;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
pub use snap::Snap; pub use snap::{Rules, Snap};
#[derive(Default)] #[derive(Default)]
pub struct Total { pub struct Total {
@ -11,8 +11,9 @@ pub struct Total {
} }
impl Total { impl Total {
pub fn snap(&self, seconds_from_startup: u64) -> Snap { pub fn snap(&self, rules: Rules, seconds_from_startup: u64) -> Snap {
Snap::shot( Snap::shot(
rules,
self.request.load(Ordering::Relaxed), self.request.load(Ordering::Relaxed),
self.blocked.load(Ordering::Relaxed), self.blocked.load(Ordering::Relaxed),
seconds_from_startup, seconds_from_startup,

View file

@ -38,14 +38,32 @@ pub struct Request {
blocked: Sum, blocked: Sum,
} }
#[derive(Serialize)]
pub struct Rules {
pub disabled: usize,
pub enabled: usize,
pub total: usize,
}
impl Rules {
pub fn from_totals(enabled: usize, disabled: usize) -> Self {
Self {
enabled,
disabled,
total: enabled + disabled,
}
}
}
#[derive(Serialize)] #[derive(Serialize)]
pub struct Snap { pub struct Snap {
request: Request, request: Request,
rules: Rules,
up: Up, up: Up,
} }
impl Snap { impl Snap {
pub fn shot(request: u64, blocked: u64, seconds_from_startup: u64) -> Self { pub fn shot(rules: Rules, request: u64, blocked: u64, seconds_from_startup: u64) -> Self {
let blocked_percent = if request > 0 { let blocked_percent = if request > 0 {
blocked as f32 * 100.0 / request as f32 blocked as f32 * 100.0 / request as f32
} else { } else {
@ -67,6 +85,7 @@ impl Snap {
percent: blocked_percent, percent: blocked_percent,
}, },
}, },
rules,
up: Up::from_startup_seconds(seconds_from_startup), up: Up::from_startup_seconds(seconds_from_startup),
} }
} }