From bbdf68cad0966ca89dab3ffb0aef5c0d3bfe8789 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sat, 2 Mar 2019 23:01:37 -0500 Subject: add stats command --- src/commands/meme.rs | 17 +++++++++++++++-- src/commands/mod.rs | 11 +++++------ src/db/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/commands/meme.rs b/src/commands/meme.rs index a1b0671..f83b793 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -244,8 +244,21 @@ pub fn delmeme(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> { msg.react("💀") } -pub fn renamememe(_: &mut Context, msg: &Message, _: Args) -> Result<()> { - send(msg.channel_id, "hwaet", msg.tts) +pub fn stats(_: &mut Context, msg: &Message, _: Args) -> Result<()> { + use db; + + let conn = connection()?; + let stats = db::stats(&conn)?; + + let s = format!( + "{} memes total\n{} memes with audio ({:0.1}%)\n{} memes with images ({:0.1}%)", + stats.memes_overall, + stats.audio_memes, + (stats.audio_memes as f64) / (stats.memes_overall as f64) * 100., + stats.image_memes, + (stats.image_memes as f64) / (stats.memes_overall as f64) * 100., + ); + send(msg.channel_id, s, msg.tts) } fn rand_meme(ctx: &Context, message: &Message, audio_only: bool) -> Result<()> { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 067d9be..8009093 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -103,12 +103,6 @@ fn register_db(f: StandardFramework) -> StandardFramework { .desc("delete a meme by name (exact match only)") .cmd(delmeme) ) - .command("renamememe", |c| c - .guild_only(true) - .desc("not currently working") - .help_available(false) - .cmd(renamememe) - ) .command("wat", |c| c .known_as("what") .known_as("last") @@ -117,6 +111,11 @@ fn register_db(f: StandardFramework) -> StandardFramework { .desc("check info for last meme") .cmd(wat) ) + .command("stats", |c| c + .guild_only(true) + .desc("get meme stats") + .cmd(stats) + ) } #[cfg(not(feature = "diesel"))] diff --git a/src/db/mod.rs b/src/db/mod.rs index 00b2138..f4042d0 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -155,4 +155,38 @@ pub fn rand_audio_meme(conn: &PgConnection) -> Result { .map_err(Error::from) } +#[derive(Debug, Copy, Clone, Default)] +pub struct Stats { + pub memes_overall: usize, + pub audio_memes: usize, + pub image_memes: usize, +} + +pub fn stats(conn: &PgConnection) -> Result { + use diesel::dsl::{count_star, count}; + + let total_count: i64 = memes::table + .select(count_star()) + .first(conn) + .map_err(Error::from)?; + + let image_count: i64 = memes::table + .select(count(memes::image_id)) + .filter(memes::image_id.is_not_null()) + .first(conn) + .map_err(Error::from)?; + + let audio_count: i64 = memes::table + .select(count(memes::audio_id)) + .filter(memes::audio_id.is_not_null()) + .first(conn) + .map_err(Error::from)?; + + Ok(Stats { + memes_overall: total_count as usize, + image_memes: image_count as usize, + audio_memes: audio_count as usize, + }) +} + no_arg_sql_function!(random, sql_types::Double, "SQL random() function"); -- cgit v1.3.1