mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 18:15:39 +00:00
captcha stats
This commit is contained in:
@@ -257,8 +257,10 @@ pub struct StatsPayload {
|
||||
async fn get_stats(
|
||||
payload: web::Json<StatsPayload>,
|
||||
data: AppData,
|
||||
id: Identity,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
let stats = Stats::new(&payload.key, &data.db).await?;
|
||||
let username = id.identity().unwrap();
|
||||
let stats = Stats::new(&username, &payload.key, &data.db).await?;
|
||||
let stats = StatsUnixTimestamp::from_stats(&stats);
|
||||
Ok(HttpResponse::Ok().json(&stats))
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ async fn update_levels(
|
||||
defense.build()?;
|
||||
|
||||
let mut futs = Vec::with_capacity(payload.levels.len() + 2);
|
||||
let del_fut = sqlx::query!(
|
||||
sqlx::query!(
|
||||
"DELETE FROM mcaptcha_levels
|
||||
WHERE config_id = (
|
||||
SELECT config_id FROM mcaptcha_config where key = ($1)
|
||||
@@ -150,7 +150,8 @@ async fn update_levels(
|
||||
&payload.key,
|
||||
&username
|
||||
)
|
||||
.execute(&data.db); //.await?;
|
||||
.execute(&data.db)
|
||||
.await?;
|
||||
|
||||
let update_fut = sqlx::query!(
|
||||
"UPDATE mcaptcha_config SET name = $1, duration = $2
|
||||
@@ -163,7 +164,6 @@ async fn update_levels(
|
||||
)
|
||||
.execute(&data.db); //.await?;
|
||||
|
||||
futs.push(del_fut);
|
||||
futs.push(update_fut);
|
||||
|
||||
for level in payload.levels.iter() {
|
||||
@@ -291,9 +291,7 @@ mod tests {
|
||||
assert_eq!(res_levels, add_level.levels);
|
||||
|
||||
// 3. update level
|
||||
|
||||
let levels = vec![L1, L2];
|
||||
|
||||
let update_level = UpdateLevels {
|
||||
key: key.key.clone(),
|
||||
levels: levels.clone(),
|
||||
|
||||
@@ -65,7 +65,10 @@ pub async fn get_config(
|
||||
match res.exists {
|
||||
Some(true) => {
|
||||
match data.captcha.get_pow(payload.key.clone()).await {
|
||||
Some(config) => Ok(HttpResponse::Ok().json(config)),
|
||||
Some(config) => {
|
||||
record_fetch(&payload.key, &data.db).await;
|
||||
Ok(HttpResponse::Ok().json(config))
|
||||
}
|
||||
None => {
|
||||
init_mcaptcha(&data, &payload.key).await?;
|
||||
let config = data
|
||||
|
||||
17
src/date.rs
17
src/date.rs
@@ -14,12 +14,23 @@
|
||||
* 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 std::fmt::Debug;
|
||||
|
||||
use sqlx::types::time::OffsetDateTime;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Date {
|
||||
pub time: OffsetDateTime,
|
||||
}
|
||||
|
||||
impl Debug for Date {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Date")
|
||||
.field("time", &self.print_date())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub const MINUTE: i64 = 60;
|
||||
pub const HOUR: i64 = MINUTE * 60;
|
||||
pub const DAY: i64 = HOUR * 24;
|
||||
@@ -45,9 +56,15 @@ impl Date {
|
||||
}
|
||||
}
|
||||
|
||||
/// print relative time from date
|
||||
pub fn print_date(&self) -> String {
|
||||
Self::format(&self.time)
|
||||
}
|
||||
|
||||
/// print date
|
||||
pub fn date(&self) -> String {
|
||||
self.time.format("%F %r %z")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -66,7 +66,6 @@ pub mod routes {
|
||||
pub sitekey: Sitekey,
|
||||
pub notifications: &'static str,
|
||||
pub settings: Settings,
|
||||
pub stats: &'static str,
|
||||
}
|
||||
|
||||
impl Panel {
|
||||
@@ -76,11 +75,10 @@ pub mod routes {
|
||||
sitekey: Sitekey::new(),
|
||||
notifications: "/notifications",
|
||||
settings: Settings::new(),
|
||||
stats: "/stats",
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn get_sitemap() -> [&'static str; 6] {
|
||||
pub const fn get_sitemap() -> [&'static str; 5] {
|
||||
const PANEL: Panel = Panel::new();
|
||||
const S: [&str; 2] = Sitekey::get_sitemap();
|
||||
|
||||
@@ -90,7 +88,6 @@ pub mod routes {
|
||||
S[0],
|
||||
S[1],
|
||||
Settings::get_sitemap()[0],
|
||||
PANEL.stats,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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/>.
|
||||
*/
|
||||
* Copyright (C) 2021 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 actix_identity::Identity;
|
||||
use actix_web::{HttpResponse, Responder};
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
*/
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use futures::{future::TryFutureExt, try_join};
|
||||
use sailfish::TemplateOnce;
|
||||
|
||||
use crate::errors::*;
|
||||
use crate::stats::fetch::Stats;
|
||||
use crate::AppData;
|
||||
|
||||
const PAGE: &str = "SiteKeys";
|
||||
@@ -79,7 +77,7 @@ pub async fn edit_sitekey(
|
||||
.fetch_one(&data.db)
|
||||
.await?;
|
||||
|
||||
let levels_fut = sqlx::query_as!(
|
||||
let levels = sqlx::query_as!(
|
||||
Level,
|
||||
"SELECT
|
||||
difficulty_factor, visitor_threshold
|
||||
@@ -89,9 +87,7 @@ pub async fn edit_sitekey(
|
||||
&config.config_id
|
||||
)
|
||||
.fetch_all(&data.db)
|
||||
.err_into();
|
||||
|
||||
let (_stats, levels) = try_join!(Stats::new(&key, &data.db), levels_fut)?;
|
||||
.await?;
|
||||
|
||||
let body = IndexPage::new(config, levels, key).render_once().unwrap();
|
||||
Ok(HttpResponse::Ok()
|
||||
|
||||
@@ -45,15 +45,22 @@ struct IndexPage {
|
||||
name: String,
|
||||
key: String,
|
||||
levels: Vec<Level>,
|
||||
stats: Stats,
|
||||
}
|
||||
|
||||
impl IndexPage {
|
||||
fn new(config: McaptchaConfig, levels: Vec<Level>, key: String) -> Self {
|
||||
fn new(
|
||||
stats: Stats,
|
||||
config: McaptchaConfig,
|
||||
levels: Vec<Level>,
|
||||
key: String,
|
||||
) -> Self {
|
||||
IndexPage {
|
||||
duration: config.duration as u32,
|
||||
name: config.name,
|
||||
levels,
|
||||
key,
|
||||
stats,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,9 +98,11 @@ pub async fn view_sitekey(
|
||||
.fetch_all(&data.db)
|
||||
.err_into();
|
||||
|
||||
let (_stats, levels) = try_join!(Stats::new(&key, &data.db), levels_fut)?;
|
||||
let (stats, levels) = try_join!(Stats::new(&username, &key, &data.db), levels_fut)?;
|
||||
|
||||
let body = IndexPage::new(config, levels, key).render_once().unwrap();
|
||||
let body = IndexPage::new(stats, config, levels, key)
|
||||
.render_once()
|
||||
.unwrap();
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(body))
|
||||
|
||||
@@ -14,13 +14,11 @@
|
||||
* 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 actix_web::{web, HttpResponse, Responder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::date::Date;
|
||||
use crate::errors::*;
|
||||
use crate::AppData;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct StatsUnixTimestamp {
|
||||
@@ -29,6 +27,7 @@ pub struct StatsUnixTimestamp {
|
||||
pub confirms: Vec<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Stats {
|
||||
pub config_fetches: Vec<Date>,
|
||||
pub solves: Vec<Date>,
|
||||
@@ -40,21 +39,11 @@ pub struct StatsPayload {
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
#[my_codegen::post(path = "crate::V1_API_ROUTES.auth.login", wrap = "crate::CheckLogin")]
|
||||
async fn get_stats(
|
||||
payload: web::Json<StatsPayload>,
|
||||
data: AppData,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
let stats = Stats::new(&payload.key, &data.db).await?;
|
||||
let stats = StatsUnixTimestamp::from_stats(&stats);
|
||||
Ok(HttpResponse::Ok().json(&stats))
|
||||
}
|
||||
|
||||
impl Stats {
|
||||
pub async fn new(key: &str, db: &PgPool) -> ServiceResult<Self> {
|
||||
let config_fetches_fut = runners::fetch_config_fetched(key, db);
|
||||
let solves_fut = runners::fetch_solve(key, db);
|
||||
let confirms_fut = runners::fetch_confirm(key, db);
|
||||
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)?;
|
||||
@@ -99,14 +88,26 @@ pub mod runners {
|
||||
/// 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)",
|
||||
"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?;
|
||||
@@ -116,12 +117,25 @@ pub mod runners {
|
||||
|
||||
/// featch PoWConfig solves
|
||||
#[inline]
|
||||
pub async fn fetch_solve(key: &str, db: &PgPool) -> ServiceResult<Vec<Date>> {
|
||||
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)",
|
||||
"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?;
|
||||
@@ -131,12 +145,26 @@ pub mod runners {
|
||||
|
||||
/// featch PoWConfig confirms
|
||||
#[inline]
|
||||
pub async fn fetch_confirm(key: &str, db: &PgPool) -> ServiceResult<Vec<Date>> {
|
||||
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)",
|
||||
&key
|
||||
"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?;
|
||||
@@ -165,7 +193,7 @@ mod tests {
|
||||
let (_, _, _, token_key) = add_levels_util(NAME, PASSWORD).await;
|
||||
let key = token_key.key.clone();
|
||||
|
||||
let stats = Stats::new(&key, &data.db).await.unwrap();
|
||||
let stats = Stats::new(&NAME, &key, &data.db).await.unwrap();
|
||||
|
||||
assert_eq!(stats.config_fetches.len(), 0);
|
||||
assert_eq!(stats.solves.len(), 0);
|
||||
@@ -177,7 +205,7 @@ mod tests {
|
||||
record_confirm(&key, &data.db)
|
||||
);
|
||||
|
||||
let stats = Stats::new(&key, &data.db).await.unwrap();
|
||||
let stats = Stats::new(&NAME, &key, &data.db).await.unwrap();
|
||||
|
||||
assert_eq!(stats.config_fetches.len(), 1);
|
||||
assert_eq!(stats.solves.len(), 1);
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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/>.
|
||||
*/
|
||||
* Copyright (C) 2021 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/>.
|
||||
*/
|
||||
|
||||
pub mod fetch;
|
||||
pub mod record;
|
||||
|
||||
@@ -15,15 +15,18 @@
|
||||
* 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) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1))",
|
||||
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
|
||||
&key,
|
||||
&now,
|
||||
)
|
||||
.execute(db)
|
||||
.await;
|
||||
@@ -32,10 +35,12 @@ pub async fn record_fetch(key: &str, db: &PgPool) {
|
||||
/// 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) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1))",
|
||||
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
|
||||
&key,
|
||||
&now,
|
||||
)
|
||||
.execute(db)
|
||||
.await;
|
||||
@@ -44,10 +49,12 @@ pub async fn record_solve(key: &str, db: &PgPool) {
|
||||
/// 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) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1))",
|
||||
(config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)",
|
||||
&key,
|
||||
&now
|
||||
)
|
||||
.execute(db)
|
||||
.await;
|
||||
|
||||
Reference in New Issue
Block a user