canon-archive 0.2.2

A CLI tool for organizing large media libraries into a canonical archive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use anyhow::Result;
use std::collections::HashSet;
use std::path::PathBuf;

use crate::domain::path::canonicalize_scopes;
use crate::domain::root::{parse_root_spec, Root};
use crate::domain::scope::ScopeMatch;
use crate::domain::source::Source;
use crate::expr::filter::{self, Filter};
use crate::repo::{self, Db};

/// Statistics for a single root or overall
struct CoverageStats {
    root_id: Option<i64>,
    root_path: Option<String>,
    root_role: Option<String>,
    total_sources: i64,
    excluded_sources: i64,
    hashed_sources: i64,
    archived_sources: i64,
}

impl CoverageStats {
    fn new() -> Self {
        CoverageStats {
            root_id: None,
            root_path: None,
            root_role: None,
            total_sources: 0,
            excluded_sources: 0,
            hashed_sources: 0,
            archived_sources: 0,
        }
    }

    fn included_sources(&self) -> i64 {
        self.total_sources - self.excluded_sources
    }

    fn hashed_pct(&self) -> f64 {
        let included = self.included_sources();
        if included == 0 {
            0.0
        } else {
            (self.hashed_sources as f64 / included as f64) * 100.0
        }
    }

    fn archived_pct(&self) -> f64 {
        if self.hashed_sources == 0 {
            0.0
        } else {
            (self.archived_sources as f64 / self.hashed_sources as f64) * 100.0
        }
    }

    fn unarchived(&self) -> i64 {
        self.hashed_sources - self.archived_sources
    }
}

pub fn run(
    db: &mut Db,
    scope_paths: &[PathBuf],
    filter_strs: &[String],
    archive_spec: Option<&str>,
    include_archived: bool,
    _include_excluded: bool,
    compact: bool,
) -> Result<()> {
    let conn = db.conn();

    // Parse filters
    let filters: Vec<Filter> = filter_strs
        .iter()
        .map(|f| Filter::parse(f))
        .collect::<Result<Vec<_>>>()?;

    // Resolve scope paths
    let scope_prefixes = canonicalize_scopes(scope_paths)?;
    let scopes = ScopeMatch::classify_all(&scope_prefixes);

    // Fetch all roots for spec resolution
    let roots = repo::root::fetch_all(conn)?;

    // Parse and validate archive spec (must be archive role)
    let archive_root_id = if let Some(spec) = archive_spec {
        Some(parse_root_spec(&roots, spec, Some("archive"))?)
    } else {
        None
    };

    // Get mutable reference for operations
    let conn = db.conn_mut();

    // Compute and display stats
    if !scope_prefixes.is_empty() {
        // Scoped mode
        let stats =
            compute_scoped_stats(conn, &scopes, &filters, archive_root_id, include_archived)?;
        let scope_display = if scope_prefixes.len() == 1 {
            Some(scope_prefixes[0].as_str())
        } else {
            None
        };
        if compact {
            display_compact_scoped(&stats, scope_display);
        } else {
            display_scoped_stats(&stats, scope_display, archive_spec);
        }
    } else {
        // Per-root breakdown mode
        let (per_root_stats, overall) =
            compute_per_root_stats(conn, &filters, archive_root_id, include_archived)?;
        if compact {
            display_compact_per_root(&per_root_stats, &overall);
        } else {
            display_per_root_stats(&per_root_stats, &overall, archive_spec);
        }
    }

    Ok(())
}

/// Get all sources matching scope/role criteria, then apply filters.
fn get_matching_sources(
    conn: &mut rusqlite::Connection,
    scopes: &[ScopeMatch],
    filters: &[Filter],
    include_archived: bool,
) -> Result<Vec<Source>> {
    // Get all roots and extract IDs
    let roots = repo::root::fetch_all(conn)?;
    let root_ids: Vec<i64> = roots.iter().map(|r| r.id).collect();

    // Fetch all present sources
    let all_sources = repo::source::batch_fetch_by_roots(conn, &root_ids)?;

    // Filter using domain predicates
    let filtered: Vec<Source> = all_sources
        .into_iter()
        .filter(|s| s.is_active())
        .filter(|s| include_archived || s.is_from_role("source"))
        .filter(|s| s.matches_scope(scopes))
        .collect();

    // Apply --where filters if present
    if filters.is_empty() {
        return Ok(filtered);
    }

    let source_ids: Vec<i64> = filtered.iter().map(|s| s.id).collect();
    let filtered_ids: HashSet<i64> = filter::apply_filters(conn, &source_ids, filters)?
        .into_iter()
        .collect();

    Ok(filtered
        .into_iter()
        .filter(|s| filtered_ids.contains(&s.id))
        .collect())
}

/// Compute coverage stats for sources under a specific path scope
fn compute_scoped_stats(
    conn: &mut rusqlite::Connection,
    scopes: &[ScopeMatch],
    filters: &[Filter],
    archive_root_id: Option<i64>,
    include_archived: bool,
) -> Result<CoverageStats> {
    let sources = get_matching_sources(conn, scopes, filters, include_archived)?;
    compute_stats_from_sources(conn, &sources, archive_root_id)
}

/// Compute coverage stats per root, plus overall totals
fn compute_per_root_stats(
    conn: &mut rusqlite::Connection,
    filters: &[Filter],
    archive_root_id: Option<i64>,
    include_archived: bool,
) -> Result<(Vec<CoverageStats>, CoverageStats)> {
    // Get list of roots, filtered by role and suspension status
    let all_roots = repo::root::fetch_all(conn)?;
    let roots: Vec<&Root> = all_roots
        .iter()
        .filter(|r| r.is_active())
        .filter(|r| include_archived || r.is_source())
        .collect();

    // Get all matching sources (unscoped)
    let all_sources = get_matching_sources(conn, &[], filters, include_archived)?;

    // Group by root_id
    let mut per_root_stats = Vec::new();
    let mut overall = CoverageStats::new();

    for root in &roots {
        // Filter sources for this root
        let root_sources: Vec<&Source> = all_sources
            .iter()
            .filter(|s| s.root_id == root.id)
            .collect();

        let mut stats = compute_stats_from_source_refs(conn, &root_sources, archive_root_id)?;
        stats.root_id = Some(root.id);
        stats.root_path = Some(root.path.clone());
        stats.root_role = Some(root.role.clone());

        // Add to overall totals
        overall.total_sources += stats.total_sources;
        overall.excluded_sources += stats.excluded_sources;
        overall.hashed_sources += stats.hashed_sources;
        overall.archived_sources += stats.archived_sources;

        per_root_stats.push(stats);
    }

    Ok((per_root_stats, overall))
}

/// Compute stats from a list of sources using domain predicates.
/// Uses is_excluded() which checks BOTH source-level and object-level exclusion.
fn compute_stats_from_sources(
    conn: &mut rusqlite::Connection,
    sources: &[Source],
    archive_root_id: Option<i64>,
) -> Result<CoverageStats> {
    let refs: Vec<&Source> = sources.iter().collect();
    compute_stats_from_source_refs(conn, &refs, archive_root_id)
}

/// Compute stats from source references.
fn compute_stats_from_source_refs(
    conn: &mut rusqlite::Connection,
    sources: &[&Source],
    archive_root_id: Option<i64>,
) -> Result<CoverageStats> {
    let mut stats = CoverageStats::new();

    // Total sources
    stats.total_sources = sources.len() as i64;

    // Excluded sources - uses is_excluded() which checks BOTH source and object level
    stats.excluded_sources = sources.iter().filter(|s| s.is_excluded()).count() as i64;

    // Hashed sources (have object_id AND not excluded)
    let hashed_sources: Vec<&&Source> = sources
        .iter()
        .filter(|s| s.object_id.is_some() && !s.is_excluded())
        .collect();
    stats.hashed_sources = hashed_sources.len() as i64;

    // Archived sources - use batch archive detection
    if stats.hashed_sources > 0 {
        // Collect object IDs from hashed sources
        let object_ids: Vec<i64> = hashed_sources.iter().filter_map(|s| s.object_id).collect();

        // Batch check which objects are archived
        let archived_set = repo::object::batch_check_archived(conn, &object_ids, archive_root_id)?;

        // Count sources whose object_id is in the archived set
        // (not unique objects — multiple sources can have the same object)
        stats.archived_sources = hashed_sources
            .iter()
            .filter(|s| s.object_id.is_some_and(|oid| archived_set.contains(&oid)))
            .count() as i64;
    }

    Ok(stats)
}

fn display_compact_scoped(stats: &CoverageStats, scope: Option<&str>) {
    let label = scope.unwrap_or("(all)");
    print_compact_line(label, stats, true);
}

fn display_compact_per_root(per_root: &[CoverageStats], overall: &CoverageStats) {
    let mut first = true;
    for stats in per_root {
        if stats.total_sources == 0 {
            continue;
        }
        let id = stats
            .root_id
            .map(|i| i.to_string())
            .unwrap_or_else(|| "?".to_string());
        let path = stats.root_path.as_deref().unwrap_or("unknown");
        let label = format_compact_label(&id, path);
        print_compact_line(&label, stats, first);
        first = false;
    }

    // Overall summary if multiple roots
    if per_root.len() > 1 && overall.total_sources > 0 {
        print_compact_line("(total)", overall, false);
    }
}

fn format_compact_label(id: &str, path: &str) -> String {
    const MAX_PATH_LEN: usize = 35;
    let id_prefix = format!("id:{id:<2}");

    if path.len() <= MAX_PATH_LEN {
        format!("{id_prefix} {path}")
    } else {
        // Show ...last_n_chars
        let truncated = &path[path.len() - MAX_PATH_LEN + 3..];
        format!("{id_prefix} ...{truncated}")
    }
}

fn print_compact_line(label: &str, stats: &CoverageStats, show_legend: bool) {
    let sources = stats.included_sources();
    let hashed_pct = stats.hashed_pct();
    let archived_pct = stats.archived_pct();

    let legend = if show_legend {
        "  (sources/hashed/archived)"
    } else {
        ""
    };

    println!(
        "{:<42} {:>10}/{:>5.1}%/{:>5.1}%{}",
        label,
        format_number(sources),
        hashed_pct,
        archived_pct,
        legend
    );
}

fn display_scoped_stats(stats: &CoverageStats, scope: Option<&str>, archive: Option<&str>) {
    if let Some(arch) = archive {
        println!("Archive Coverage (relative to {arch})");
    } else {
        println!("Archive Coverage");
    }

    if let Some(s) = scope {
        println!("Scope: {s}\n");
    } else {
        println!();
    }

    if stats.total_sources == 0 {
        println!("No sources match the given filters.");
        return;
    }

    // Always show included sources as total (excluded are filtered out conceptually)
    println!(
        "  Total sources:   {:>8}",
        format_number(stats.included_sources())
    );
    println!(
        "  Hashed:          {:>8} ({:.1}%)",
        format_number(stats.hashed_sources),
        stats.hashed_pct()
    );

    if archive.is_some() {
        println!(
            "  In this archive: {:>8} ({:.1}% of hashed)",
            format_number(stats.archived_sources),
            stats.archived_pct()
        );
        println!(
            "  Not in archive:  {:>8}",
            format_number(stats.unarchived())
        );
    } else {
        println!(
            "  Archived:        {:>8} ({:.1}% of hashed)",
            format_number(stats.archived_sources),
            stats.archived_pct()
        );
        println!(
            "  Unarchived:      {:>8}",
            format_number(stats.unarchived())
        );
    }
}

fn display_per_root_stats(
    per_root: &[CoverageStats],
    overall: &CoverageStats,
    archive: Option<&str>,
) {
    if let Some(arch) = archive {
        println!("Archive Coverage Report (relative to {arch})\n");
    } else {
        println!("Archive Coverage Report\n");
    }

    if per_root.is_empty() || overall.total_sources == 0 {
        println!("No sources match the given filters.");
        return;
    }

    for stats in per_root {
        if stats.total_sources == 0 {
            continue;
        }

        let root_id = stats
            .root_id
            .map(|id| id.to_string())
            .unwrap_or_else(|| "?".to_string());
        let root_path = stats.root_path.as_deref().unwrap_or("unknown");
        let root_role = stats.root_role.as_deref().unwrap_or("unknown");
        println!("Root {root_id}: {root_path} ({root_role})");

        println!(
            "  Total sources:   {:>8}",
            format_number(stats.included_sources())
        );
        println!(
            "  Hashed:          {:>8} ({:.1}%)",
            format_number(stats.hashed_sources),
            stats.hashed_pct()
        );

        if archive.is_some() {
            println!(
                "  In this archive: {:>8} ({:.1}% of hashed)",
                format_number(stats.archived_sources),
                stats.archived_pct()
            );
            println!(
                "  Not in archive:  {:>8}",
                format_number(stats.unarchived())
            );
        } else {
            println!(
                "  Archived:        {:>8} ({:.1}% of hashed)",
                format_number(stats.archived_sources),
                stats.archived_pct()
            );
            println!(
                "  Unarchived:      {:>8}",
                format_number(stats.unarchived())
            );
        }
        println!();
    }

    // Overall summary
    println!("{}", "─".repeat(40));
    println!("Overall:");

    println!(
        "  Total sources:   {:>8}",
        format_number(overall.included_sources())
    );
    println!(
        "  Hashed:          {:>8} ({:.1}%)",
        format_number(overall.hashed_sources),
        overall.hashed_pct()
    );

    if archive.is_some() {
        println!(
            "  In this archive: {:>8} ({:.1}% of hashed)",
            format_number(overall.archived_sources),
            overall.archived_pct()
        );
        println!(
            "  Not in archive:  {:>8}",
            format_number(overall.unarchived())
        );
    } else {
        println!(
            "  Archived:        {:>8} ({:.1}% of hashed)",
            format_number(overall.archived_sources),
            overall.archived_pct()
        );
        println!(
            "  Unarchived:      {:>8}",
            format_number(overall.unarchived())
        );
    }
}

fn format_number(n: i64) -> String {
    let s = n.to_string();
    let mut result = String::new();
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.push(',');
        }
        result.push(c);
    }
    result.chars().rev().collect()
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::repo::open_in_memory_for_test;
    use rusqlite::Connection as RusqliteConnection;

    fn setup_test_db() -> RusqliteConnection {
        open_in_memory_for_test()
    }

    fn insert_root(conn: &RusqliteConnection, path: &str, role: &str, suspended: bool) -> i64 {
        conn.execute(
            "INSERT INTO roots (path, role, suspended) VALUES (?, ?, ?)",
            rusqlite::params![path, role, suspended as i64],
        )
        .unwrap();
        conn.last_insert_rowid()
    }

    fn insert_object(conn: &RusqliteConnection, hash: &str, excluded: bool) -> i64 {
        conn.execute(
            "INSERT INTO objects (hash_type, hash_value, excluded) VALUES ('sha256', ?, ?)",
            rusqlite::params![hash, excluded as i64],
        )
        .unwrap();
        conn.last_insert_rowid()
    }

    fn insert_source(
        conn: &RusqliteConnection,
        root_id: i64,
        rel_path: &str,
        object_id: Option<i64>,
    ) -> i64 {
        conn.execute(
            "INSERT INTO sources (root_id, rel_path, object_id, size, mtime, partial_hash, scanned_at, last_seen_at, device, inode)
             VALUES (?, ?, ?, 1000, 1704067200, '', 0, 0, 0, 0)",
            rusqlite::params![root_id, rel_path, object_id],
        )
        .unwrap();
        conn.last_insert_rowid()
    }

    /// Test that get_matching_sources respects scope filtering.
    ///
    /// This validates the full scope-filtering pipeline:
    /// - Sources are filtered by path using Source::matches_scope()
    /// - Empty scopes returns all sources
    #[test]
    fn test_get_matching_sources_respects_scope() {
        use crate::domain::scope::ScopeMatch;

        let mut conn = setup_test_db();

        // Create two roots at different paths
        let photos_root = insert_root(&conn, "/photos", "source", false);
        let videos_root = insert_root(&conn, "/videos", "source", false);

        // Add sources to each root
        let photo1_id = insert_source(&conn, photos_root, "photo1.jpg", None);
        let photo2_id = insert_source(&conn, photos_root, "photo2.jpg", None);
        let video1_id = insert_source(&conn, videos_root, "video1.mp4", None);

        // Test 1: Scoped to /photos - should return only photo sources
        let scopes = vec![ScopeMatch::UnderDirectory("/photos".to_string())];
        let sources = get_matching_sources(&mut conn, &scopes, &[], false).unwrap();
        let source_ids: Vec<i64> = sources.iter().map(|s| s.id).collect();

        assert_eq!(source_ids.len(), 2, "Should return 2 photo sources");
        assert!(source_ids.contains(&photo1_id), "Should contain photo1");
        assert!(source_ids.contains(&photo2_id), "Should contain photo2");
        assert!(
            !source_ids.contains(&video1_id),
            "Should NOT contain video1"
        );

        // Test 2: Unscoped (empty scopes = all) - should return all sources
        let sources = get_matching_sources(&mut conn, &[], &[], false).unwrap();
        let source_ids: Vec<i64> = sources.iter().map(|s| s.id).collect();

        assert_eq!(source_ids.len(), 3, "Should return all 3 sources");
        assert!(source_ids.contains(&photo1_id), "Should contain photo1");
        assert!(source_ids.contains(&photo2_id), "Should contain photo2");
        assert!(source_ids.contains(&video1_id), "Should contain video1");
    }

    /// Test that archived_sources counts sources, not unique objects.
    ///
    /// This guards against the Object Infrastructure bug pattern where
    /// archived_set.len() was used instead of counting sources.
    #[test]
    fn test_coverage_archived_counts_sources_not_objects() {
        let mut conn = setup_test_db();

        // Create source root and archive root
        let source_root = insert_root(&conn, "/photos", "source", false);
        let archive_root = insert_root(&conn, "/archive", "archive", false);

        // Create ONE object that will be archived
        let archived_obj = insert_object(&conn, "abc123archived", false);

        // Create 3 source files pointing to the SAME object
        insert_source(&conn, source_root, "photo1.jpg", Some(archived_obj));
        insert_source(&conn, source_root, "photo2.jpg", Some(archived_obj));
        insert_source(&conn, source_root, "photo3.jpg", Some(archived_obj));

        // Create another object that is NOT archived
        let unarchived_obj = insert_object(&conn, "def456unarchived", false);
        insert_source(&conn, source_root, "photo4.jpg", Some(unarchived_obj));

        // Put the archived object in the archive root
        insert_source(&conn, archive_root, "backup.jpg", Some(archived_obj));

        // Fetch sources from source root only (simulating what coverage does)
        let sources = repo::source::batch_fetch_by_roots(&conn, &[source_root]).unwrap();
        assert_eq!(sources.len(), 4, "Should have 4 sources in source root");

        // Compute stats using the actual function
        let stats = compute_stats_from_sources(&mut conn, &sources, None).unwrap();

        // The critical assertion: archived_sources should be 3 (sources), not 1 (object)
        assert_eq!(
            stats.archived_sources, 3,
            "Should count 3 SOURCES with archived objects, not 1 unique object"
        );

        // Verify other stats are correct
        assert_eq!(stats.total_sources, 4);
        assert_eq!(stats.hashed_sources, 4);
    }
}