From a2d2d5d9308303209ed9fda8f1e452670a0af24e Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 12 Apr 2021 15:52:37 +0200 Subject: [PATCH] Add statistics table to database to store command statistics Signed-off-by: trivernis --- Cargo.lock | 2 +- bot-database/Cargo.toml | 2 +- .../down.sql | 2 ++ .../up.sql | 9 +++++++ bot-database/src/database.rs | 26 +++++++++++++++++++ bot-database/src/models.rs | 11 ++++++++ bot-database/src/schema.rs | 12 +++++++++ src/client.rs | 15 ++++++++++- 8 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 bot-database/migrations/2021-04-12-133308_create_statistics/down.sql create mode 100644 bot-database/migrations/2021-04-12-133308_create_statistics/up.sql diff --git a/Cargo.lock b/Cargo.lock index 20a8fa0..9bd10da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,7 +191,7 @@ dependencies = [ [[package]] name = "bot-database" -version = "0.2.0" +version = "0.3.0" dependencies = [ "chrono", "diesel", diff --git a/bot-database/Cargo.toml b/bot-database/Cargo.toml index ee6bd11..80694bc 100644 --- a/bot-database/Cargo.toml +++ b/bot-database/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bot-database" -version = "0.2.0" +version = "0.3.0" authors = ["trivernis "] edition = "2018" diff --git a/bot-database/migrations/2021-04-12-133308_create_statistics/down.sql b/bot-database/migrations/2021-04-12-133308_create_statistics/down.sql new file mode 100644 index 0000000..b660849 --- /dev/null +++ b/bot-database/migrations/2021-04-12-133308_create_statistics/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE statistics; \ No newline at end of file diff --git a/bot-database/migrations/2021-04-12-133308_create_statistics/up.sql b/bot-database/migrations/2021-04-12-133308_create_statistics/up.sql new file mode 100644 index 0000000..75d42be --- /dev/null +++ b/bot-database/migrations/2021-04-12-133308_create_statistics/up.sql @@ -0,0 +1,9 @@ +-- Your SQL goes here +CREATE TABLE statistics ( + id BIGSERIAL PRIMARY KEY, + version VARCHAR(32) NOT NULL, + command VARCHAR(255) NOT NULL, + executed_at TIMESTAMP NOT NULL, + success BOOLEAN NOT NULL DEFAULT TRUE, + error_msg TEXT +) \ No newline at end of file diff --git a/bot-database/src/database.rs b/bot-database/src/database.rs index cf60aa4..83caccc 100644 --- a/bot-database/src/database.rs +++ b/bot-database/src/database.rs @@ -7,6 +7,7 @@ use diesel::{delete, insert_into}; use std::any; use std::fmt::Debug; use std::str::FromStr; +use std::time::SystemTime; use tokio_diesel::*; #[derive(Clone)] @@ -199,4 +200,29 @@ impl Database { Ok(()) } + + /// Adds a command statistic to the database + pub async fn add_statistic( + &self, + version: &str, + command: &str, + executed_at: SystemTime, + success: bool, + error_msg: Option, + ) -> DatabaseResult<()> { + use statistics::dsl; + log::trace!("Adding statistic to database"); + insert_into(dsl::statistics) + .values(StatisticsInsert { + version: version.to_string(), + command: command.to_string(), + executed_at, + success, + error_msg, + }) + .execute_async(&self.pool) + .await?; + + Ok(()) + } } diff --git a/bot-database/src/models.rs b/bot-database/src/models.rs index 0ff1064..6e47497 100644 --- a/bot-database/src/models.rs +++ b/bot-database/src/models.rs @@ -1,4 +1,5 @@ use crate::schema::*; +use std::time::SystemTime; #[derive(Queryable, Debug)] pub struct GuildSetting { @@ -45,3 +46,13 @@ pub struct GifInsert { pub name: Option, pub url: String, } + +#[derive(Insertable, Debug)] +#[table_name = "statistics"] +pub struct StatisticsInsert { + pub version: String, + pub command: String, + pub executed_at: SystemTime, + pub success: bool, + pub error_msg: Option, +} diff --git a/bot-database/src/schema.rs b/bot-database/src/schema.rs index db3a1db..eb45c7c 100644 --- a/bot-database/src/schema.rs +++ b/bot-database/src/schema.rs @@ -23,8 +23,20 @@ table! { } } +table! { + statistics (id) { + id -> Int8, + version -> Varchar, + command -> Varchar, + executed_at -> Timestamp, + success -> Bool, + error_msg -> Nullable, + } +} + allow_tables_to_appear_in_same_query!( gifs, guild_playlists, guild_settings, + statistics, ); diff --git a/src/client.rs b/src/client.rs index 93f27eb..5b4fef9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -12,11 +12,12 @@ use songbird::SerenityInit; use crate::commands::*; use crate::handler::Handler; -use crate::utils::context_data::{DatabaseContainer, Store, StoreData}; +use crate::utils::context_data::{get_database_from_context, DatabaseContainer, Store, StoreData}; use crate::utils::error::{BotError, BotResult}; use bot_serenityutils::menu::EventDrivenMessageContainer; use serenity::framework::standard::buckets::LimitedFor; use std::sync::Arc; +use std::time::SystemTime; use tokio::sync::Mutex; pub async fn get_client() -> BotResult { @@ -84,7 +85,9 @@ pub async fn get_framework() -> StandardFramework { #[hook] async fn after_hook(ctx: &Context, msg: &Message, cmd_name: &str, error: CommandResult) { // Print out an error if it happened + let mut error_msg = None; if let Err(why) = error { + error_msg = Some(why.to_string()); let _ = msg .channel_id .send_message(ctx, |m| { @@ -93,6 +96,16 @@ async fn after_hook(ctx: &Context, msg: &Message, cmd_name: &str, error: Command .await; log::warn!("Error in {}: {:?}", cmd_name, why); } + let database = get_database_from_context(ctx).await; + let _ = database + .add_statistic( + env!("CARGO_PKG_VERSION"), + cmd_name, + SystemTime::now(), + error_msg.is_none(), + error_msg, + ) + .await; } #[hook]