2022-10-06 12:54:25 +00:00
|
|
|
//! All logic for running the CLI.
|
|
|
|
|
|
|
|
use {
|
2022-10-07 13:27:43 +00:00
|
|
|
async_std::fs::create_dir_all, clap::Parser, color_eyre::Result,
|
|
|
|
sea_orm_migration::MigratorTrait, tracing::info,
|
2022-10-06 12:54:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2022-10-07 13:27:43 +00:00
|
|
|
charts::UserCountChart,
|
|
|
|
cli::{
|
|
|
|
Cli, MainSubcommands, MigrateSubcommands, SnapshotSubcommands,
|
|
|
|
WebSubcommands,
|
|
|
|
},
|
|
|
|
group_data::GroupDataModel,
|
2022-10-06 12:54:25 +00:00
|
|
|
migrations::Migrator,
|
2022-10-07 13:27:43 +00:00
|
|
|
scss::generate_css,
|
|
|
|
snapshots::SnapshotModel,
|
|
|
|
templates::HomeTemplate,
|
2022-10-06 12:54:25 +00:00
|
|
|
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 } => {
|
2022-10-07 13:27:43 +00:00
|
|
|
SnapshotModel::create(&db, force).await?;
|
2022-10-06 12:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SnapshotSubcommands::List {} => {
|
2022-10-07 13:27:43 +00:00
|
|
|
for snapshot in SnapshotModel::get_all(&db).await? {
|
2022-10-06 12:54:25 +00:00
|
|
|
info!("Snapshot {snapshot:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:20:49 +00:00
|
|
|
SnapshotSubcommands::Show { date, id } => {
|
2022-10-06 12:54:25 +00:00
|
|
|
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-06 12:54:25 +00:00
|
|
|
};
|
|
|
|
|
2022-10-09 10:22:29 +00:00
|
|
|
info!("Snapshot {snapshot:?}");
|
2022-10-07 13:27:43 +00:00
|
|
|
for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?
|
|
|
|
{
|
2022-10-06 12:54:25 +00:00
|
|
|
info!(
|
|
|
|
id = group.id,
|
|
|
|
name = group.name,
|
|
|
|
subscribers = group.subscribers,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-10-07 13:27:43 +00:00
|
|
|
|
|
|
|
MainSubcommands::Web {
|
|
|
|
command: web_command,
|
|
|
|
} => match web_command {
|
|
|
|
WebSubcommands::Build { output } => {
|
2022-10-08 20:11:52 +00:00
|
|
|
let (groups, user_count_group) =
|
2022-10-07 13:27:43 +00:00
|
|
|
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?,
|
|
|
|
)
|
2022-10-07 13:27:43 +00:00
|
|
|
} else {
|
2022-10-08 20:11:52 +00:00
|
|
|
(vec![], None)
|
2022-10-07 13:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
create_dir_all(&output).await?;
|
|
|
|
HomeTemplate::new(
|
2022-10-08 20:11:52 +00:00
|
|
|
groups,
|
2022-10-07 13:27:43 +00:00
|
|
|
user_count_group.as_ref().map(|group| group.subscribers),
|
|
|
|
)
|
|
|
|
.render_to_file(&output)
|
|
|
|
.await?;
|
|
|
|
generate_css(&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?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-10-06 12:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|