Skip to content

Commit

Permalink
Merge pull request #127 from mCaptcha/feat-auto-captcha
Browse files Browse the repository at this point in the history
feat: new dashboard page to show percentile scores on PoW performance analysis records
  • Loading branch information
realaravinth committed Jan 4, 2024
2 parents 26ad05d + 13c3066 commit 1b2096d
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 33 deletions.
1 change: 0 additions & 1 deletion db/db-sqlx-maria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub mod dev {
pub use super::errors::*;
pub use super::Database;
pub use db_core::dev::*;
pub use prelude::*;
pub use sqlx::Error;
}

Expand Down
1 change: 0 additions & 1 deletion db/db-sqlx-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub mod dev {
pub use super::errors::*;
pub use super::Database;
pub use db_core::dev::*;
pub use prelude::*;
pub use sqlx::Error;
}

Expand Down
55 changes: 31 additions & 24 deletions src/api/v1/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,82 +32,89 @@ pub mod routes {
}
}

/// Get difficulty factor with max time limit for percentile of stats
#[my_codegen::post(path = "crate::V1_API_ROUTES.stats.percentile_benches")]
async fn percentile_benches(
data: AppData,
payload: web::Json<PercentileReq>,
) -> ServiceResult<impl Responder> {
let count = data.db.stats_get_num_logs_under_time(payload.time).await?;
pub async fn percentile_bench_runner(
data: &AppData,
req: &PercentileReq,
) -> ServiceResult<PercentileResp> {
let count = data.db.stats_get_num_logs_under_time(req.time).await?;

if count == 0 {
return Ok(HttpResponse::Ok().json(PercentileResp {
return Ok(PercentileResp {
difficulty_factor: None,
}));
});
}

if count < 2 {
return Ok(HttpResponse::Ok().json(PercentileResp {
return Ok(PercentileResp {
difficulty_factor: None,
}));
});
}

let location = ((count - 1) as f64 * (payload.percentile / 100.00)) + 1.00;
let location = ((count - 1) as f64 * (req.percentile / 100.00)) + 1.00;
let fraction = location - location.floor();

if fraction > 0.00 {
if let (Some(base), Some(ceiling)) = (
data.db
.stats_get_entry_at_location_for_time_limit_asc(
payload.time,
req.time,
location.floor() as u32,
)
.await?,
data.db
.stats_get_entry_at_location_for_time_limit_asc(
payload.time,
req.time,
location.floor() as u32 + 1,
)
.await?,
) {
let res = base as u32 + ((ceiling - base) as f64 * fraction).floor() as u32;

return Ok(HttpResponse::Ok().json(PercentileResp {
return Ok(PercentileResp {
difficulty_factor: Some(res),
}));
});
}
} else {
if let Some(base) = data
.db
.stats_get_entry_at_location_for_time_limit_asc(
payload.time,
req.time,
location.floor() as u32,
)
.await?
{
let res = base as u32;

return Ok(HttpResponse::Ok().json(PercentileResp {
return Ok(PercentileResp {
difficulty_factor: Some(res),
}));
});
}
};
Ok(HttpResponse::Ok().json(PercentileResp {
Ok(PercentileResp {
difficulty_factor: None,
}))
})
}

/// Get difficulty factor with max time limit for percentile of stats
#[my_codegen::post(path = "crate::V1_API_ROUTES.stats.percentile_benches")]
async fn percentile_benches(
data: AppData,
payload: web::Json<PercentileReq>,
) -> ServiceResult<impl Responder> {
Ok(HttpResponse::Ok().json(percentile_bench_runner(&data, &payload).await?))
}

#[derive(Clone, Debug, Deserialize, Builder, Serialize)]
/// Health check return datatype
pub struct PercentileReq {
time: u32,
percentile: f64,
pub time: u32,
pub percentile: f64,
}

#[derive(Clone, Debug, Deserialize, Builder, Serialize)]
/// Health check return datatype
pub struct PercentileResp {
difficulty_factor: Option<u32>,
pub difficulty_factor: Option<u32>,
}

pub fn services(cfg: &mut web::ServiceConfig) {
Expand Down
8 changes: 7 additions & 1 deletion src/pages/panel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sailfish::TemplateOnce;
mod notifications;
mod settings;
pub mod sitekey;
mod utils;

use db_core::Captcha;

Expand Down Expand Up @@ -47,18 +48,21 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(panel);
settings::services(cfg);
sitekey::services(cfg);
utils::services(cfg);
cfg.service(notifications::notifications);
}

pub mod routes {
use super::settings::routes::Settings;
use super::sitekey::routes::Sitekey;
use super::utils::routes::Utils;

pub struct Panel {
pub home: &'static str,
pub sitekey: Sitekey,
pub notifications: &'static str,
pub settings: Settings,
pub utils: Utils,
}

impl Panel {
Expand All @@ -68,10 +72,11 @@ pub mod routes {
sitekey: Sitekey::new(),
notifications: "/notifications",
settings: Settings::new(),
utils: Utils::new(),
}
}

pub const fn get_sitemap() -> [&'static str; 5] {
pub const fn get_sitemap() -> [&'static str; 6] {
const PANEL: Panel = Panel::new();
const S: [&str; 2] = Sitekey::get_sitemap();

Expand All @@ -81,6 +86,7 @@ pub mod routes {
S[0],
S[1],
Settings::get_sitemap()[0],
Utils::get_sitemap()[0],
]
}
}
Expand Down

0 comments on commit 1b2096d

Please sign in to comment.