Compare commits

..

2 Commits

Author SHA1 Message Date
Bauke a3a4ae8ca4
Also log the snapshot in Show. 2022-10-09 12:22:29 +02:00
Bauke 9ec3b1c07d
Add showing snapshots by ID. 2022-10-09 12:20:49 +02:00
3 changed files with 36 additions and 10 deletions

View File

@ -90,8 +90,12 @@ pub enum SnapshotSubcommands {
/// Show a snapshot. /// Show a snapshot.
Show { Show {
/// The date of the snapshot to show, defaults to today. /// The date of the snapshot to show, defaults to today.
#[clap(short, long)] #[clap(short, long, group = "selection")]
date: Option<NaiveDate>, date: Option<NaiveDate>,
/// The ID of the snapshot to show.
#[clap(long, group = "selection")]
id: Option<i64>,
}, },
} }

View File

@ -58,18 +58,27 @@ pub async fn run() -> Result<()> {
} }
} }
SnapshotSubcommands::Show { date } => { SnapshotSubcommands::Show { date, id } => {
let date = date.unwrap_or_else(today); let date = date.unwrap_or_else(today);
let snapshot = if let Some(snapshot) =
SnapshotModel::get_by_date(&db, date).await? let snapshot = match id {
{ Some(id) => SnapshotModel::get_by_id(&db, id).await,
info!("Snapshot {snapshot:?}"); None => SnapshotModel::get_by_date(&db, date).await,
snapshot }?;
} else {
info!("No snapshot exists for {date}"); let snapshot = match (snapshot, id) {
return Ok(()); (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,
}; };
info!("Snapshot {snapshot:?}");
for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await? for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?
{ {
info!( info!(

View File

@ -27,6 +27,19 @@ impl SnapshotModel {
Ok(existing) Ok(existing)
} }
/// Get a snapshot by a given ID.
pub async fn get_by_id(
db: &DatabaseConnection,
id: i64,
) -> Result<Option<Self>> {
let snapshot = SnapshotEntity::find()
.filter(SnapshotColumn::Id.eq(id))
.one(db)
.await?;
Ok(snapshot)
}
/// Get all snapshots. /// Get all snapshots.
pub async fn get_all(db: &DatabaseConnection) -> Result<Vec<Self>> { pub async fn get_all(db: &DatabaseConnection) -> Result<Vec<Self>> {
let snapshots = SnapshotEntity::find().all(db).await?; let snapshots = SnapshotEntity::find().all(db).await?;