Updates for the "full flow"

This commit is contained in:
Connor Johnstone
2026-03-17 21:38:27 -04:00
parent fed3a070fc
commit ea1e3c6ac5
5 changed files with 129 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
use std::fmt;
use std::time::Duration;
use sea_orm::DatabaseConnection;
use sea_orm::{ActiveValue::Set, DatabaseConnection};
use shanty_db::entities::download_queue::DownloadStatus;
use shanty_db::entities::wanted_item::WantedStatus;
@@ -106,20 +106,45 @@ pub async fn run_queue(
)
.await?;
// Update wanted item status if linked
// Update wanted item status and create track record with MBID
if let Some(wanted_id) = item.wanted_item_id {
if let Err(e) = queries::wanted::update_status(
conn,
wanted_id,
WantedStatus::Downloaded,
)
.await
{
tracing::warn!(
wanted_id = wanted_id,
error = %e,
"failed to update wanted item status"
);
if let Ok(wanted) = queries::wanted::get_by_id(conn, wanted_id).await {
// Create a track record with the MBID so the tagger
// can skip searching and go straight to the right recording
let now = chrono::Utc::now().naive_utc();
let file_path = result.file_path.to_string_lossy().to_string();
let file_size = std::fs::metadata(&result.file_path)
.map(|m| m.len() as i64)
.unwrap_or(0);
let track_active = shanty_db::entities::track::ActiveModel {
file_path: Set(file_path),
title: Set(Some(result.title.clone())),
artist: Set(result.artist.clone()),
file_size: Set(file_size),
musicbrainz_id: Set(wanted.musicbrainz_id.clone()),
artist_id: Set(wanted.artist_id),
added_at: Set(now),
updated_at: Set(now),
..Default::default()
};
if let Err(e) = queries::tracks::upsert(conn, track_active).await {
tracing::warn!(error = %e, "failed to create track record after download");
}
if let Err(e) = queries::wanted::update_status(
conn,
wanted_id,
WantedStatus::Downloaded,
)
.await
{
tracing::warn!(
wanted_id = wanted_id,
error = %e,
"failed to update wanted item status"
);
}
}
}
@@ -170,6 +195,79 @@ pub async fn run_queue(
Ok(stats)
}
/// Sync stats from a queue sync operation.
#[derive(Debug, Default)]
pub struct SyncStats {
pub found: u64,
pub enqueued: u64,
pub skipped: u64,
}
impl fmt::Display for SyncStats {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"found: {}, enqueued: {}, skipped (already queued): {}",
self.found, self.enqueued, self.skipped,
)
}
}
/// Sync wanted items to the download queue.
/// Finds all Track-type Wanted items and enqueues them for download,
/// skipping any that already have a queue entry.
pub async fn sync_wanted_to_queue(
conn: &DatabaseConnection,
dry_run: bool,
) -> DlResult<SyncStats> {
let wanted = queries::wanted::list(conn, Some(WantedStatus::Wanted)).await?;
let mut stats = SyncStats::default();
for item in &wanted {
stats.found += 1;
// Build search query from the name + artist
let artist_name = if let Some(id) = item.artist_id {
queries::artists::get_by_id(conn, id)
.await
.map(|a| a.name)
.ok()
} else {
None
};
let query = match artist_name {
Some(ref artist) if !item.name.is_empty() => format!("{} {}", artist, item.name),
_ if !item.name.is_empty() => item.name.clone(),
Some(ref artist) => artist.clone(),
None => {
tracing::warn!(id = item.id, "cannot build query — no name or artist");
continue;
}
};
// Check if already queued
if let Some(_existing) = queries::downloads::find_by_wanted_item_id(conn, item.id).await? {
tracing::debug!(id = item.id, name = %item.name, "already in queue, skipping");
stats.skipped += 1;
continue;
}
if dry_run {
println!("Would enqueue: {query}");
stats.enqueued += 1;
continue;
}
queries::downloads::enqueue(conn, &query, Some(item.id), "ytdlp").await?;
tracing::info!(id = item.id, query = %query, "enqueued for download");
stats.enqueued += 1;
}
tracing::info!(%stats, "sync complete");
Ok(stats)
}
/// Download a single item directly (not from queue).
pub async fn download_single(
backend: &impl DownloadBackend,