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 tokio::{net::TcpListener, sync::RwLock, task};
#[rocket::get("/")]
async fn index(totals: &State<Arc<Total>>, startup_time: &State<Instant>) -> Json<Snap> {
Json(totals.inner().snap(startup_time.elapsed().as_secs())) // @TODO implement Web UI
#[rocket::get("/")] // @TODO implement Web UI here...
async fn index(
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")]
async fn api_totals(totals: &State<Arc<Total>>, startup_time: &State<Instant>) -> Json<Snap> {
Json(totals.inner().snap(startup_time.elapsed().as_secs()))
async fn api_totals(
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>")]

View file

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

View file

@ -45,6 +45,14 @@ impl List {
pub fn contains(&self, value: &str) -> bool {
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<()> {
debug!("Load list `{source}`...");

View file

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

View file

@ -38,14 +38,32 @@ pub struct Request {
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)]
pub struct Snap {
request: Request,
rules: Rules,
up: Up,
}
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 {
blocked as f32 * 100.0 / request as f32
} else {
@ -67,6 +85,7 @@ impl Snap {
percent: blocked_percent,
},
},
rules,
up: Up::from_startup_seconds(seconds_from_startup),
}
}