feat: migrate fetching stats to use db_*

This commit is contained in:
realaravinth 2022-05-27 17:09:27 +05:30
parent bc90a51d30
commit 098d0cfc24
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
10 changed files with 50 additions and 299 deletions

View file

@ -19,7 +19,6 @@ use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use crate::errors::*;
use crate::stats::fetch::{Stats, StatsUnixTimestamp};
use crate::AppData;
pub mod routes {
@ -50,7 +49,6 @@ pub async fn get(
id: Identity,
) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap();
let stats = Stats::new(&username, &payload.key, &data.db).await?;
let stats = StatsUnixTimestamp::from_stats(&stats);
let stats = data.stats.fetch(&data, &username, &payload.key).await?;
Ok(HttpResponse::Ok().json(&stats))
}

View file

@ -49,7 +49,7 @@ pub async fn get_config(
match data.captcha.get_pow(payload.key.clone()).await {
Ok(Some(config)) => {
data.stats.record_fetch(&data, &payload.key).await;
data.stats.record_fetch(&data, &payload.key).await?;
Ok(HttpResponse::Ok().json(config))
}
Ok(None) => {
@ -61,7 +61,7 @@ pub async fn get_config(
.expect("mcaptcha should be initialized and ready to go");
// background it. would require data::Data to be static
// to satidfy lifetime
data.stats.record_fetch(&data, &payload.key).await;
data.stats.record_fetch(&data, &payload.key).await?;
Ok(HttpResponse::Ok().json(config))
}
Err(e) => Err(e.into()),

View file

@ -42,7 +42,7 @@ pub async fn verify_pow(
) -> ServiceResult<impl Responder> {
let key = payload.key.clone();
let res = data.captcha.verify_pow(payload.into_inner()).await?;
data.stats.record_solve(&data, &key).await;
data.stats.record_solve(&data, &key).await?;
let payload = ValidationToken { token: res };
Ok(HttpResponse::Ok().json(payload))
}

View file

@ -43,7 +43,7 @@ pub async fn validate_captcha_token(
.validate_verification_tokens(payload.into_inner())
.await?;
let payload = CaptchaValidateResp { valid: res };
data.stats.record_confirm(&data, &key).await;
data.stats.record_confirm(&data, &key).await?;
//println!("{:?}", &payload);
Ok(HttpResponse::Ok().json(payload))
}

View file

@ -65,6 +65,12 @@ impl Date {
pub fn date(&self) -> String {
self.time.format("%F %r %z")
}
pub fn new(unix: i64) -> Self {
Self {
time: OffsetDateTime::from_unix_timestamp(unix),
}
}
}
#[cfg(test)]

View file

@ -17,13 +17,12 @@
use actix_identity::Identity;
use actix_web::{web, HttpResponse, Responder};
use futures::{future::TryFutureExt, try_join};
use sailfish::TemplateOnce;
use libmcaptcha::defense::Level;
use crate::errors::*;
use crate::stats::fetch::Stats;
use crate::stats::CaptchaStats;
use crate::AppData;
const PAGE: &str = "SiteKeys";
@ -42,12 +41,12 @@ struct IndexPage {
name: String,
key: String,
levels: Vec<Level>,
stats: Stats,
stats: CaptchaStats,
}
impl IndexPage {
fn new(
stats: Stats,
stats: CaptchaStats,
config: McaptchaConfig,
levels: Vec<Level>,
key: String,
@ -87,8 +86,7 @@ pub async fn view_sitekey(
.await?;
let levels = data.dblib.get_captcha_levels(Some(&username), &key).await?;
let stats = Stats::new(&username, &key, &data.db).await?;
let stats = data.stats.fetch(&data, &username, &key).await?;
let body = IndexPage::new(stats, config, levels, key)
.render_once()

View file

@ -14,14 +14,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod fetch;
//pub mod record;
pub use fetch::StatsUnixTimestamp;
use async_trait::async_trait;
use db_core::errors::DBResult;
use serde::{Deserialize, Serialize};
use crate::data::Data;
@ -35,6 +30,9 @@ pub trait Stats: std::marker::Send + std::marker::Sync + CloneStats {
/// record PoWConfig confirms
async fn record_confirm(&self, d: &Data, key: &str) -> DBResult<()>;
/// fetch stats
async fn fetch(&self, d: &Data, user: &str, key: &str) -> DBResult<CaptchaStats>;
}
/// Trait to clone MCDatabase
@ -59,6 +57,13 @@ impl Clone for Box<dyn CloneStats> {
}
}
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct CaptchaStats {
pub config_fetches: Vec<i64>,
pub solves: Vec<i64>,
pub confirms: Vec<i64>,
}
#[derive(Clone, Default, PartialEq, Debug)]
pub struct Real;
@ -78,6 +83,24 @@ impl Stats for Real {
async fn record_confirm(&self, d: &Data, key: &str) -> DBResult<()> {
d.dblib.record_confirm(key).await
}
/// fetch stats
async fn fetch(&self, d: &Data, user: &str, key: &str) -> DBResult<CaptchaStats> {
let config_fetches_fut = d.dblib.fetch_config_fetched(user, key);
let solves_fut = d.dblib.fetch_solve(user, key);
let confirms_fut = d.dblib.fetch_confirm(user, key);
let (config_fetches, solves, confirms) =
futures::try_join!(config_fetches_fut, solves_fut, confirms_fut)?;
let res = CaptchaStats {
config_fetches,
solves,
confirms,
};
Ok(res)
}
}
#[derive(Clone, Default, PartialEq, Debug)]
@ -99,4 +122,9 @@ impl Stats for Dummy {
async fn record_confirm(&self, _: &Data, _: &str) -> DBResult<()> {
Ok(())
}
/// fetch stats
async fn fetch(&self, _: &Data, _: &str, _: &str) -> DBResult<CaptchaStats> {
Ok(CaptchaStats::default())
}
}

View file

@ -1,218 +0,0 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use crate::date::Date;
use crate::errors::*;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatsUnixTimestamp {
pub config_fetches: Vec<i64>,
pub solves: Vec<i64>,
pub confirms: Vec<i64>,
}
#[derive(Debug, Clone)]
pub struct Stats {
pub config_fetches: Vec<Date>,
pub solves: Vec<Date>,
pub confirms: Vec<Date>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatsPayload {
pub key: String,
}
impl Stats {
pub async fn new(user: &str, key: &str, db: &PgPool) -> ServiceResult<Self> {
let config_fetches_fut = runners::fetch_config_fetched(user, key, db);
let solves_fut = runners::fetch_solve(user, key, db);
let confirms_fut = runners::fetch_confirm(user, key, db);
let (config_fetches, solves, confirms) =
futures::try_join!(config_fetches_fut, solves_fut, confirms_fut)?;
let res = Self {
config_fetches,
solves,
confirms,
};
Ok(res)
}
}
impl StatsUnixTimestamp {
pub fn from_stats(stats: &Stats) -> Self {
let config_fetches = Self::unix_timestamp(&stats.config_fetches);
let solves = Self::unix_timestamp(&stats.solves);
let confirms = Self::unix_timestamp(&stats.confirms);
Self {
config_fetches,
solves,
confirms,
}
}
/// featch PoWConfig confirms
#[inline]
fn unix_timestamp(dates: &[Date]) -> Vec<i64> {
let mut res: Vec<i64> = Vec::with_capacity(dates.len());
dates
.iter()
.for_each(|record| res.push(record.time.unix_timestamp()));
res
}
}
pub mod runners {
use super::*;
/// featch PoWConfig fetches
#[inline]
pub async fn fetch_config_fetched(
user: &str,
key: &str,
db: &PgPool,
) -> ServiceResult<Vec<Date>> {
let records = sqlx::query_as!(
Date,
"SELECT time FROM mcaptcha_pow_fetched_stats
WHERE
config_id = (
SELECT
config_id FROM mcaptcha_config
WHERE
key = $1
AND
user_id = (
SELECT
ID FROM mcaptcha_users WHERE name = $2))
ORDER BY time DESC",
&key,
&user,
)
.fetch_all(db)
.await?;
Ok(records)
}
/// featch PoWConfig solves
#[inline]
pub async fn fetch_solve(
user: &str,
key: &str,
db: &PgPool,
) -> ServiceResult<Vec<Date>> {
let records = sqlx::query_as!(
Date,
"SELECT time FROM mcaptcha_pow_solved_stats
WHERE config_id = (
SELECT config_id FROM mcaptcha_config
WHERE
key = $1
AND
user_id = (
SELECT
ID FROM mcaptcha_users WHERE name = $2))
ORDER BY time DESC",
&key,
&user
)
.fetch_all(db)
.await?;
Ok(records)
}
/// featch PoWConfig confirms
#[inline]
pub async fn fetch_confirm(
user: &str,
key: &str,
db: &PgPool,
) -> ServiceResult<Vec<Date>> {
let records = sqlx::query_as!(
Date,
"SELECT time FROM mcaptcha_pow_confirmed_stats
WHERE
config_id = (
SELECT config_id FROM mcaptcha_config
WHERE
key = $1
AND
user_id = (
SELECT
ID FROM mcaptcha_users WHERE name = $2))
ORDER BY time DESC",
&key,
&user
)
.fetch_all(db)
.await?;
Ok(records)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::*;
#[actix_rt::test]
async fn stats_works() {
const NAME: &str = "statsuser";
const PASSWORD: &str = "testingpas";
const EMAIL: &str = "statsuser@a.com";
let data = get_data().await;
let data = &data;
delete_user(data, NAME).await;
register_and_signin(data, NAME, EMAIL, PASSWORD).await;
let (_, _, token_key) = add_levels_util(data, NAME, PASSWORD).await;
let key = token_key.key.clone();
let stats = Stats::new(NAME, &key, &data.db).await.unwrap();
assert_eq!(stats.config_fetches.len(), 0);
assert_eq!(stats.solves.len(), 0);
assert_eq!(stats.confirms.len(), 0);
let _ = futures::join!(
data.stats.record_fetch(&data, &key,),
data.stats.record_solve(&data, &key,),
data.stats.record_confirm(&data, &key,),
);
let stats = Stats::new(NAME, &key, &data.db).await.unwrap();
assert_eq!(stats.config_fetches.len(), 1);
assert_eq!(stats.solves.len(), 1);
assert_eq!(stats.confirms.len(), 1);
let ustats = StatsUnixTimestamp::from_stats(&stats);
assert_eq!(ustats.config_fetches.len(), 1);
assert_eq!(ustats.solves.len(), 1);
assert_eq!(ustats.confirms.len(), 1);
}
}

View file

@ -1,61 +0,0 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use sqlx::types::time::OffsetDateTime;
use sqlx::PgPool;
/// record PoWConfig fetches
#[inline]
pub async fn record_fetch(key: &str, db: &PgPool) {
let now = OffsetDateTime::now_utc();
let _ = sqlx::query!(
"INSERT INTO mcaptcha_pow_fetched_stats
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
&key,
&now,
)
.execute(db)
.await;
}
/// record PoWConfig solves
#[inline]
pub async fn record_solve(key: &str, db: &PgPool) {
let now = OffsetDateTime::now_utc();
let _ = sqlx::query!(
"INSERT INTO mcaptcha_pow_solved_stats
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
&key,
&now,
)
.execute(db)
.await;
}
/// record PoWConfig confirms
#[inline]
pub async fn record_confirm(key: &str, db: &PgPool) {
let now = OffsetDateTime::now_utc();
let _ = sqlx::query!(
"INSERT INTO mcaptcha_pow_confirmed_stats
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
&key,
&now
)
.execute(db)
.await;
}

View file

@ -16,7 +16,7 @@
</h3>
</td>
<td>
<p class="notification__item-text"><.= val.date() .></p>
<p class="notification__item-text"><.= crate::date::Date::new(*val).date() .></p>
</td>
</tr>
<. } .>