Add statistics table to database to store command statistics

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/10/head
trivernis 3 years ago
parent 29e670da66
commit a2d2d5d930
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -191,7 +191,7 @@ dependencies = [
[[package]]
name = "bot-database"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"chrono",
"diesel",

@ -1,6 +1,6 @@
[package]
name = "bot-database"
version = "0.2.0"
version = "0.3.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE statistics;

@ -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
)

@ -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<String>,
) -> 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(())
}
}

@ -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<String>,
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<String>,
}

@ -23,8 +23,20 @@ table! {
}
}
table! {
statistics (id) {
id -> Int8,
version -> Varchar,
command -> Varchar,
executed_at -> Timestamp,
success -> Bool,
error_msg -> Nullable<Text>,
}
}
allow_tables_to_appear_in_same_query!(
gifs,
guild_playlists,
guild_settings,
statistics,
);

@ -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<Client> {
@ -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]

Loading…
Cancel
Save