From bc90a51d3083e977250300286891c0b6530ca42c Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 27 May 2022 17:08:55 +0530 Subject: [PATCH] feat: impl fetch captcha stats sqlx postgres --- db/db-sqlx-postgres/src/lib.rs | 91 +++++++++++++++++++++++++++++++++- sqlx-data.json | 63 ----------------------- 2 files changed, 90 insertions(+), 64 deletions(-) diff --git a/db/db-sqlx-postgres/src/lib.rs b/db/db-sqlx-postgres/src/lib.rs index aaa5a2c5..1762f138 100644 --- a/db/db-sqlx-postgres/src/lib.rs +++ b/db/db-sqlx-postgres/src/lib.rs @@ -744,7 +744,7 @@ impl MCDatabase for Database { ) .execute(&self.pool) .await - .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; Ok(()) } @@ -762,6 +762,95 @@ impl MCDatabase for Database { .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; Ok(()) } + + /// featch PoWConfig fetches + async fn fetch_config_fetched(&self, user: &str, key: &str) -> DBResult> { + 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(&self.pool) + .await + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + + Ok(Date::dates_to_unix(records)) + } + + /// featch PoWConfig solves + async fn fetch_solve(&self, user: &str, key: &str) -> DBResult> { + 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(&self.pool) + .await + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + + Ok(Date::dates_to_unix(records)) + } + + /// featch PoWConfig confirms + async fn fetch_confirm(&self, user: &str, key: &str) -> DBResult> { + 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(&self.pool) + .await + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + + Ok(Date::dates_to_unix(records)) + } +} + +#[derive(Clone)] +struct Date { + time: OffsetDateTime, +} + +impl Date { + fn dates_to_unix(mut d: Vec) -> Vec { + let mut dates = Vec::with_capacity(d.len()); + d.drain(0..) + .for_each(|x| dates.push(x.time.unix_timestamp())); + dates + } } fn now_unix_time_stamp() -> OffsetDateTime { diff --git a/sqlx-data.json b/sqlx-data.json index d17788cb..3a1b3cda 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -1,26 +1,5 @@ { "db": "PostgreSQL", - "4303f5c6ef98e0de9d8d3c2d781d3ffaa3dee5f7d27db831d327b26f03ba9d68": { - "describe": { - "columns": [ - { - "name": "time", - "ordinal": 0, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false - ], - "parameters": { - "Left": [ - "Text", - "Text" - ] - } - }, - "query": "SELECT time FROM mcaptcha_pow_confirmed_stats \n WHERE \n config_id = (\n SELECT config_id FROM mcaptcha_config \n WHERE \n key = $1\n AND\n user_id = (\n SELECT \n ID FROM mcaptcha_users WHERE name = $2))\n ORDER BY time DESC" - }, "4c3a9fe30a4c6bd49ab1cb8883c4495993aa05f2991483b4f04913b2e5043a63": { "describe": { "columns": [ @@ -73,27 +52,6 @@ }, "query": "SELECT key, name from mcaptcha_config WHERE\n user_id = (SELECT ID FROM mcaptcha_users WHERE name = $1) " }, - "84484cb6892db29121816bc5bff5702b9e857e20aa14e79d080d78ae7593153b": { - "describe": { - "columns": [ - { - "name": "time", - "ordinal": 0, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false - ], - "parameters": { - "Left": [ - "Text", - "Text" - ] - } - }, - "query": "SELECT time FROM mcaptcha_pow_solved_stats \n WHERE config_id = (\n SELECT config_id FROM mcaptcha_config \n WHERE \n key = $1\n AND\n user_id = (\n SELECT \n ID FROM mcaptcha_users WHERE name = $2)) \n ORDER BY time DESC" - }, "a1c49ee377d6ac57fb22c9eac0ef1927a97087abd58da092a91623d06fa7076e": { "describe": { "columns": [ @@ -147,26 +105,5 @@ } }, "query": "SELECT config_id, duration, name from mcaptcha_config WHERE\n key = $1 AND\n user_id = (SELECT ID FROM mcaptcha_users WHERE name = $2) " - }, - "c399efd5db1284dcb470c40f9b076851f77498c75a63a3b151d4a111bd3e2957": { - "describe": { - "columns": [ - { - "name": "time", - "ordinal": 0, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false - ], - "parameters": { - "Left": [ - "Text", - "Text" - ] - } - }, - "query": "SELECT time FROM mcaptcha_pow_fetched_stats\n WHERE \n config_id = (\n SELECT \n config_id FROM mcaptcha_config \n WHERE \n key = $1\n AND\n user_id = (\n SELECT \n ID FROM mcaptcha_users WHERE name = $2))\n ORDER BY time DESC" } } \ No newline at end of file