Add number of times used to stats command

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

@ -2,6 +2,7 @@ use crate::error::DatabaseResult;
use crate::models::*;
use crate::schema::*;
use crate::PoolConnection;
use diesel::dsl::count;
use diesel::prelude::*;
use diesel::{delete, insert_into};
use std::any;
@ -213,7 +214,7 @@ impl Database {
use statistics::dsl;
log::trace!("Adding statistic to database");
insert_into(dsl::statistics)
.values(StatisticsInsert {
.values(StatisticInsert {
version: version.to_string(),
command: command.to_string(),
executed_at,
@ -225,4 +226,16 @@ impl Database {
Ok(())
}
/// Returns the total number of commands executed
pub async fn get_total_commands_statistic(&self) -> DatabaseResult<u64> {
use statistics::dsl;
log::trace!("Querying total number of commands");
let total_count: i64 = dsl::statistics
.select(count(dsl::id))
.first_async::<i64>(&self.pool)
.await?;
Ok(total_count as u64)
}
}

@ -21,4 +21,13 @@ pub enum DatabaseError {
#[error("AsyncError: {0}")]
AsyncError(#[from] tokio_diesel::AsyncError),
#[error("{0}")]
Msg(String),
}
impl From<&str> for DatabaseError {
fn from(s: &str) -> Self {
Self::Msg(s.to_string())
}
}

@ -49,7 +49,7 @@ pub struct GifInsert {
#[derive(Insertable, Debug)]
#[table_name = "statistics"]
pub struct StatisticsInsert {
pub struct StatisticInsert {
pub version: String,
pub command: String,
pub executed_at: SystemTime,

@ -9,6 +9,7 @@ use serenity::prelude::*;
use sysinfo::{ProcessExt, SystemExt};
use crate::commands::common::handle_autodelete;
use crate::utils::context_data::get_database_from_context;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
@ -18,6 +19,7 @@ const VERSION: &'static str = env!("CARGO_PKG_VERSION");
#[bucket("general")]
async fn stats(ctx: &Context, msg: &Message) -> CommandResult {
log::debug!("Reading system stats");
let database = get_database_from_context(ctx).await;
let mut system = sysinfo::System::new_all();
system.refresh_all();
let kernel_version = system.get_kernel_version().unwrap_or("n/a".to_string());
@ -32,14 +34,16 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult {
let current_time_seconds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let uptime = current_time_seconds - Duration::from_secs(own_process.start_time());
let uptime = ChronoDuration::from_std(uptime).unwrap();
let total_commands_executed = database.get_total_commands_statistic().await?;
let discord_info = format!(
r#"
Version: {}
Owner: <@{}>
Guilds: {}
Times Used: {}
"#,
VERSION, bot_info.owner.id, guild_count
VERSION, bot_info.owner.id, guild_count, total_commands_executed
);
log::trace!("Discord info {}", discord_info);

Loading…
Cancel
Save