tildes-statistics/source/cli/run.rs

174 lines
4.5 KiB
Rust
Raw Normal View History

//! All logic for running the CLI.
use {
2023-06-07 17:09:44 +00:00
async_std::fs::{copy, create_dir_all},
clap::Parser,
color_eyre::Result,
sea_orm_migration::MigratorTrait,
tracing::info,
};
use crate::{
2022-10-09 11:37:37 +00:00
assets::write_assets,
charts::UserCountChart,
cli::{
Cli, MainSubcommands, MigrateSubcommands, SnapshotSubcommands,
WebSubcommands,
},
group_data::GroupDataModel,
migrations::Migrator,
scss::generate_css,
snapshots::SnapshotModel,
2023-06-08 11:12:44 +00:00
templates::{GroupTemplate, HomeTemplate},
utilities::{create_db, today},
};
/// Run the CLI.
pub async fn run() -> Result<()> {
let cli = Cli::parse();
let db = create_db(cli.sql_logging).await?;
if !cli.no_migrate {
Migrator::up(&db, None).await?;
}
match cli.command {
MainSubcommands::Migrate {
command: migrate_command,
} => match migrate_command {
MigrateSubcommands::Down { amount } => {
Migrator::down(&db, Some(amount)).await?;
}
MigrateSubcommands::Status => {
Migrator::status(&db).await?;
}
MigrateSubcommands::Up { amount } => {
Migrator::up(&db, Some(amount)).await?;
}
},
MainSubcommands::Snapshot {
command: snapshot_command,
} => match snapshot_command {
SnapshotSubcommands::Create { force } => {
SnapshotModel::create(&db, force).await?;
}
SnapshotSubcommands::List {} => {
for snapshot in SnapshotModel::get_all(&db).await? {
info!("Snapshot {snapshot:?}")
}
}
2022-10-09 10:20:49 +00:00
SnapshotSubcommands::Show { date, id } => {
let date = date.unwrap_or_else(today);
2022-10-09 10:20:49 +00:00
let snapshot = match id {
Some(id) => SnapshotModel::get_by_id(&db, id).await,
None => SnapshotModel::get_by_date(&db, date).await,
}?;
let snapshot = match (snapshot, id) {
(None, Some(id)) => {
info!("No snapshot exists for id {id}");
return Ok(());
}
(None, None) => {
info!("No snapshot exists for date {date}");
return Ok(());
}
(Some(snapshot), _) => snapshot,
};
2022-10-09 10:22:29 +00:00
info!("Snapshot {snapshot:?}");
for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?
{
info!(
id = group.id,
name = group.name,
subscribers = group.subscribers,
);
}
}
},
MainSubcommands::Web {
command: web_command,
} => match web_command {
WebSubcommands::Build { output } => {
2022-10-08 20:11:52 +00:00
let (groups, user_count_group) =
if let Some(snapshot) = SnapshotModel::get_most_recent(&db).await? {
2022-10-08 20:11:52 +00:00
(
GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?,
GroupDataModel::get_highest_subscribers(&db, &snapshot).await?,
)
} else {
2022-10-08 20:11:52 +00:00
(vec![], None)
};
create_dir_all(&output).await?;
2023-06-07 17:09:44 +00:00
for group in &groups {
2023-06-14 10:44:41 +00:00
let chart = UserCountChart {
groups: GroupDataModel::get_n_most_recent(&db, 31, &group.name)
2023-06-07 17:09:44 +00:00
.await?,
2023-06-14 10:44:41 +00:00
};
chart
.render(&output, &group.name, true, true, "charts")
.await?;
chart
.render(&output, &group.name, true, false, "charts-untruncated")
.await?;
2023-06-14 10:44:41 +00:00
{
let total_chart = UserCountChart {
groups: GroupDataModel::get_all(&db, &group.name).await?,
};
total_chart
.render(&output, &group.name, false, true, "charts-total")
.await?;
total_chart
.render(
&output,
&group.name,
false,
false,
"charts-total-untruncated",
)
.await?;
}
2023-06-08 11:12:44 +00:00
2023-06-09 10:53:37 +00:00
GroupTemplate::new(group.description.clone(), &group.name)
2023-06-08 11:12:44 +00:00
.await
.render_to_file(&output)
.await?;
2023-06-07 17:09:44 +00:00
}
HomeTemplate::new(
2022-10-08 20:11:52 +00:00
groups,
user_count_group.as_ref().map(|group| group.subscribers),
)
.await
.render_to_file(&output)
.await?;
generate_css(&output).await?;
2022-10-09 11:37:37 +00:00
write_assets(&output).await?;
if let Some(group) = user_count_group {
for dir in ["charts", "charts-untruncated"] {
let from_path =
output.join(&format!("{}/user-count/{}.svg", dir, &group.name));
let to_path = output.join(format!("{}/main-user-count.svg", dir));
copy(from_path, to_path).await?;
}
}
}
},
}
Ok(())
}