1
0
Fork 0
mirror of https://github.com/mCaptcha/mCaptcha.git synced 2025-05-03 21:52:49 +03:00

feat: store pow performance statistics against statistics

This commit is contained in:
Aravinth Manivannan 2023-06-27 19:47:04 +05:30
parent 2a1bda653d
commit 5ae3b1f26e
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
8 changed files with 293 additions and 0 deletions

View file

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS mcaptcha_pow_analytics (
config_id INTEGER references mcaptcha_config(config_id) ON DELETE CASCADE,
time INTEGER NOT NULL,
difficulty_factor INTEGER NOT NULL,
worker_type VARCHAR(100) NOT NULL UNIQUE,
ID SERIAL PRIMARY KEY NOT NULL
);

View file

@ -493,6 +493,21 @@
},
"query": "SELECT EXISTS (SELECT 1 from mcaptcha_users WHERE name = $1)"
},
"af47990880a92c63d1cf5192203899c72621479dc6bb47859fb4498264b78033": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Text",
"Int4",
"Int4",
"Varchar"
]
}
},
"query": "INSERT INTO mcaptcha_pow_analytics \n (config_id, time, difficulty_factor, worker_type)\n VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2, $3, $4)"
},
"b97d810814fbeb2df19f47bcfa381bc6fb7ac6832d040b377cf4fca2ca896cfb": {
"describe": {
"columns": [],
@ -611,6 +626,38 @@
},
"query": "DELETE FROM mcaptcha_users WHERE name = ($1)"
},
"d4ca28f0d895ef832d5cc1bc56c17e94c33efdfe63e10f29bbe54b6841a43320": {
"describe": {
"columns": [
{
"name": "time",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "difficulty_factor",
"ordinal": 1,
"type_info": "Int4"
},
{
"name": "worker_type",
"ordinal": 2,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT time, difficulty_factor, worker_type FROM mcaptcha_pow_analytics\n WHERE \n config_id = (\n SELECT \n config_id FROM mcaptcha_config \n WHERE \n key = $1\n )\n ORDER BY ID"
},
"d7dd6cd6a7626e79c62377b2d59115067c5851ec044911ff8833779a08bbb8f7": {
"describe": {
"columns": [],

View file

@ -901,6 +901,72 @@ impl MCDatabase for Database {
Ok(Date::dates_to_unix(records))
}
/// record PoW timing
async fn analysis_save(
&self,
captcha_id: &str,
d: &PerformanceAnalytics,
) -> DBResult<()> {
let _ = sqlx::query!(
"INSERT INTO mcaptcha_pow_analytics
(config_id, time, difficulty_factor, worker_type)
VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2, $3, $4)",
captcha_id,
d.time as i32,
d.difficulty_factor as i32,
&d.worker_type,
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?;
Ok(())
}
/// fetch PoW analytics
async fn analytics_fetch(
&self,
captcha_id: &str,
) -> DBResult<Vec<PerformanceAnalytics>> {
struct P {
time: i32,
difficulty_factor: i32,
worker_type: String,
}
impl From<P> for PerformanceAnalytics {
fn from(v: P) -> Self {
Self {
time: v.time as u32,
difficulty_factor: v.difficulty_factor as u32,
worker_type: v.worker_type,
}
}
}
let mut c = sqlx::query_as!(
P,
"SELECT time, difficulty_factor, worker_type FROM mcaptcha_pow_analytics
WHERE
config_id = (
SELECT
config_id FROM mcaptcha_config
WHERE
key = $1
)
ORDER BY ID",
&captcha_id,
)
.fetch_all(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?;
let mut res = Vec::with_capacity(c.len());
for i in c.drain(0..) {
res.push(i.into())
}
Ok(res)
}
}
#[derive(Clone)]