Skip to content

Commit

Permalink
More site-specific stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro committed Mar 1, 2024
1 parent f161ac3 commit 1258a9a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 14 deletions.
12 changes: 12 additions & 0 deletions queries/admin/flist_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT
COALESCE(
(
SELECT
count(*)
FROM
flist_file
WHERE
discovered_at > current_timestamp - interval '24 hours'
),
0
) "recent_posts!";
12 changes: 12 additions & 0 deletions queries/admin/reddit_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT
COALESCE(
(
SELECT
count(*)
FROM
reddit_post
WHERE
posted_at > current_timestamp - interval '24 hours'
),
0
) "recent_posts!";
36 changes: 36 additions & 0 deletions sqlx-data.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 33 additions & 10 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use actix_web::{get, post, services, web, HttpResponse, Scope};
use askama::Template;
use async_trait::async_trait;
use foxlib::jobs::{FaktoryProducer, JobQueue};
use futures::TryFutureExt;
use rand::Rng;
use serde::Deserialize;
use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -126,6 +127,8 @@ async fn admin_inject(
#[derive(Template)]
#[template(path = "admin/sites/reddit.html")]
struct AdminReddit {
active_subreddits: usize,
added_posts: i64,
subreddits: Vec<models::RedditSubreddit>,
}

Expand All @@ -139,19 +142,31 @@ async fn admin_sites_reddit(
return Err(actix_web::error::ErrorUnauthorized("Unauthorized").into());
}

let subreddits = models::RedditSubreddit::subreddits(&conn).await?;
let (subreddits, stats) = futures::try_join!(
models::RedditSubreddit::subreddits(&conn),
sqlx::query_file!("queries/admin/reddit_stats.sql")
.fetch_one(&**conn)
.map_err(Error::from)
)?;

let body = AdminReddit { subreddits }
.wrap_admin(&request, &user)
.await
.render()?;
let active_subreddits = subreddits.iter().filter(|s| !s.disabled).count();

let body = AdminReddit {
active_subreddits,
added_posts: stats.recent_posts,
subreddits,
}
.wrap_admin(&request, &user)
.await
.render()?;

Ok(HttpResponse::Ok().content_type("text/html").body(body))
}

#[derive(Template)]
#[template(path = "admin/sites/flist.html")]
struct AdminFlist {
added_posts: i64,
recent_flist_runs: Vec<models::FListImportRun>,
}

Expand All @@ -165,12 +180,20 @@ async fn admin_sites_flist(
return Err(actix_web::error::ErrorUnauthorized("Unauthorized").into());
}

let recent_flist_runs = models::FListImportRun::recent_runs(&conn).await?;
let (recent_flist_runs, stats) = futures::try_join!(
models::FListImportRun::recent_runs(&conn),
sqlx::query_file!("queries/admin/flist_stats.sql")
.fetch_one(&**conn)
.map_err(Error::from)
)?;

let body = AdminFlist { recent_flist_runs }
.wrap_admin(&request, &user)
.await
.render()?;
let body = AdminFlist {
added_posts: stats.recent_posts,
recent_flist_runs,
}
.wrap_admin(&request, &user)
.await
.render()?;

Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
Expand Down
4 changes: 2 additions & 2 deletions templates/admin/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<div class="level-item">
<div>
<p class="heading">Last 24 hr Sessions</p>
<p class="heading">24 hr Sessions</p>
<p class="title human-number">{{ recent_users }}</p>
</div>
</div>
Expand All @@ -30,7 +30,7 @@

<div class="level-item">
<div>
<p class="heading">Last 24 hr Media</p>
<p class="heading">24 hr Media</p>
<p class="title human-number">{{ recent_media }}</p>
</div>
</div>
Expand Down
23 changes: 22 additions & 1 deletion templates/admin/sites/flist.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<div class="block">
<h2 class="subtitle is-4">F-List Import Runs</h2>
<div class="level has-text-centered">
<div class="level-item">
<div>
<p class="heading">Last Import</p>
{% match recent_flist_runs.first() %}
{% when Some with (import_run) %}
<p class="title relative-time" data-timestamp="{{ import_run.started_at.timestamp() }}">
{{ import_run.started_at }}
</p>
{% else %}
<p class="title">Never</p>
{% endmatch %}
</div>
</div>

<div class="level-item">
<div>
<p class="heading">24 hr Posts</p>
<p class="title human-number">{{ added_posts }}</p>
</div>
</div>
</div>

<div class="table-container">
<table class="table is-fullwidth">
Expand Down
16 changes: 15 additions & 1 deletion templates/admin/sites/reddit.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@

<div class="block">
<h2 class="subtitle is-4">Monitored Subreddits</h2>
<div class="level has-text-centered">
<div class="level-item">
<div>
<p class="heading">Monitored Subreddits</p>
<p class="title human-number">{{ active_subreddits }}</p>
</div>
</div>

<div class="level-item">
<div>
<p class="heading">24 hr Posts</p>
<p class="title human-number">{{ added_posts }}</p>
</div>
</div>
</div>

<div class="table-container">
<table class="table is-fullwidth">
Expand Down

0 comments on commit 1258a9a

Please sign in to comment.