aboutsummaryrefslogtreecommitdiff
path: root/src/db/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs34
1 files changed, 34 insertions, 0 deletions
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<Meme> {
.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<Stats> {
+ 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");