Render UserCountCharts for all groups.

This commit is contained in:
Bauke 2023-06-07 19:09:44 +02:00
parent 43fbb060ff
commit c432b410ee
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 27 additions and 12 deletions

View File

@ -23,8 +23,12 @@ pub struct UserCountChart {
impl UserCountChart {
/// Render the chart and write it to file.
pub async fn render(&self, parent: &PathBuf, group_name: &str) -> Result<()> {
let parent = parent.join("charts");
pub async fn render(
&self,
parent: &PathBuf,
group_name: &str,
) -> Result<PathBuf> {
let parent = parent.join("charts/user-count");
create_dir_all(&parent).await?;
let (mut datapoints, mut min_count, mut max_count) = (vec![], i64::MAX, 0);
@ -45,7 +49,8 @@ impl UserCountChart {
let min_count = min_count - 10;
let max_count = max_count + 10;
let path = parent.join("user-count.svg");
let path = parent.join(format!("{group_name}.svg"));
let output_path = path.clone();
let chart_root = SVGBackend::new(&path, (1280, 720)).into_drawing_area();
chart_root.fill(&BACKGROUND_2)?;
@ -112,6 +117,6 @@ impl UserCountChart {
},
))?;
Ok(())
Ok(output_path)
}
}

View File

@ -1,8 +1,11 @@
//! All logic for running the CLI.
use {
async_std::fs::create_dir_all, clap::Parser, color_eyre::Result,
sea_orm_migration::MigratorTrait, tracing::info,
async_std::fs::{copy, create_dir_all},
clap::Parser,
color_eyre::Result,
sea_orm_migration::MigratorTrait,
tracing::info,
};
use crate::{
@ -106,6 +109,16 @@ pub async fn run() -> Result<()> {
};
create_dir_all(&output).await?;
for group in &groups {
UserCountChart {
groups: GroupDataModel::get_n_most_recent(&db, 30, &group.name)
.await?,
}
.render(&output, &group.name)
.await?;
}
HomeTemplate::new(
groups,
user_count_group.as_ref().map(|group| group.subscribers),
@ -117,11 +130,8 @@ pub async fn run() -> Result<()> {
write_assets(&output).await?;
if let Some(group) = user_count_group {
let groups =
GroupDataModel::get_n_most_recent(&db, 30, &group.name).await?;
UserCountChart { groups }
.render(&output, &group.name)
.await?;
let path = output.join(&format!("charts/user-count/{}", &group.name));
copy(path, output.join("charts/main-user-count.svg")).await?;
}
}
},

View File

@ -19,7 +19,7 @@
registered users on Tildes.
</p>
<img class="chart" src="/charts/user-count.svg" alt="User Count Chart">
<img class="chart" src="/charts/main-user-count.svg" alt="User Count Chart">
<table>
<thead>