mecomp-daemon 0.7.2

RPC server for the Mecomp, the Metadata Enhanced Collection Orientated Music Player
Documentation
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
use std::{
    collections::{HashMap, HashSet},
    num::NonZeroUsize,
    path::PathBuf,
    time::Duration,
};

use log::{debug, error, info, warn};
use mecomp_analysis::{
    clustering::{ClusteringHelper, KOptimal},
    decoder::{Decoder, MecompDecoder},
    embeddings::ModelConfig,
};
use mecomp_core::config::{AnalysisKind, AnalysisSettings, ReclusterSettings};
use mecomp_prost::{LibraryBrief, LibraryFull, LibraryHealth};
use one_or_many::OneOrMany;
use surrealdb::{Connection, Surreal};
use tracing::{Instrument, instrument};
use walkdir::WalkDir;

use mecomp_storage::{
    db::{
        health::{
            count_albums, count_artists, count_collections, count_dynamic_playlists,
            count_orphaned_albums, count_orphaned_artists, count_orphaned_collections,
            count_orphaned_playlists, count_playlists, count_songs, count_unanalyzed_songs,
        },
        schemas::{
            album::Album,
            analysis::Analysis,
            artist::Artist,
            collection::Collection,
            dynamic::DynamicPlaylist,
            playlist::Playlist,
            song::{Song, SongMetadata},
        },
    },
    errors::Error,
    util::MetadataConflictResolution,
};

use crate::termination::InterruptReceiver;

/// Index the library.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
/// or if there is an error reading from the file system.
/// or if there is an error writing to the database.
#[instrument]
#[inline]
pub async fn rescan<C: Connection>(
    db: &Surreal<C>,
    paths: &[PathBuf],
    artist_name_separator: &OneOrMany<String>,
    protected_artist_names: &OneOrMany<String>,
    genre_separator: Option<&str>,
    conflict_resolution_mode: MetadataConflictResolution,
) -> Result<(), Error> {
    // for each song, check if the file still exists
    let mut visited_paths = check_library(
        db,
        artist_name_separator,
        protected_artist_names,
        genre_separator,
        conflict_resolution_mode,
    )
    .await?;

    // now, index all the songs in the library that haven't been indexed yet
    index_new_songs(
        db,
        paths,
        &mut visited_paths,
        artist_name_separator,
        protected_artist_names,
        genre_separator,
    )
    .await?;

    // find and delete any remaining orphaned albums and artists
    delete_orphans(db).await?;

    info!("Library rescan complete");
    info!("Library health: {:?}", health(db).await?);

    Ok(())
}

/// Check library for missing or updated songs.
///
/// # Returns
///
/// A set of paths already present in the library, which should be skipped during indexing.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
async fn check_library<C: Connection>(
    db: &Surreal<C>,
    artist_name_separator: &OneOrMany<String>,
    protected_artist_names: &OneOrMany<String>,
    genre_separator: Option<&str>,
    conflict_resolution_mode: MetadataConflictResolution,
) -> Result<HashSet<PathBuf>, Error> {
    // use a hashset because hashing is faster than linear search, especially for large libraries
    // though a trie could be even faster
    let mut paths_to_skip = HashSet::new();

    let songs = Song::read_all(db).await?;
    for song in songs {
        let path = &song.path;
        if !path.exists() {
            // remove the song from the library
            warn!("Song {} no longer exists, deleting", path.display());
            Song::delete(db, song.id).await?;
            continue;
        }

        debug!("loading metadata for {}", path.display());
        // check if the metadata of the file is the same as the metadata in the database
        match SongMetadata::load_from_path(
            path.clone(),
            artist_name_separator,
            protected_artist_names,
            genre_separator,
        ) {
            // if we have metadata and the metadata is different from the song's metadata, and ...
            Ok(metadata) if metadata != SongMetadata::from(&song) => {
                let log_postfix = if conflict_resolution_mode == MetadataConflictResolution::Skip {
                    "but conflict resolution mode is \"skip\", so we do nothing"
                } else {
                    "resolving conflict"
                };
                info!(
                    "{} has conflicting metadata with index, {log_postfix}",
                    path.display(),
                );

                match conflict_resolution_mode {
                    // ... we are in "overwrite" mode, update the song's metadata
                    MetadataConflictResolution::Overwrite => {
                        // if the file has been modified, update the song's metadata
                        Song::update(db, song.id.clone(), metadata.merge_with_song(&song)).await?;
                    }
                    // ... we are in "skip" mode, do nothing
                    MetadataConflictResolution::Skip => {}
                }
            }
            // if we have an error, delete the song from the library
            Err(e) => {
                warn!("Error reading metadata for {}: {e}", path.display());
                info!(
                    "assuming the file isn't a song or doesn't exist anymore, removing from library"
                );
                Song::delete(db, song.id).await?;
            }
            // if the metadata is the same, do nothing
            _ => {}
        }

        // now, add the path to the list of paths to skip so that we don't index the song again
        paths_to_skip.insert(path.clone());
    }

    Ok(paths_to_skip)
}

/// Index all new songs in the given paths.
///
/// This expects to be passed as input, the output of `check_library`
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
async fn index_new_songs<C: Connection>(
    db: &Surreal<C>,
    paths: &[PathBuf],
    visited_paths: &mut HashSet<PathBuf>,
    artist_name_separator: &OneOrMany<String>,
    protected_artist_names: &OneOrMany<String>,
    genre_separator: Option<&str>,
) -> Result<(), Error> {
    debug!("Indexing paths: {paths:?}");

    const BATCH_SIZE: usize = 100;
    let mut metadata_batch = Vec::with_capacity(BATCH_SIZE);
    let mut processed_count = 0;

    for path in paths
        .iter()
        .filter_map(|p| {
            p.canonicalize()
                .inspect_err(|e| warn!("Error canonicalizing path: {e}"))
                .ok()
        })
        .flat_map(|x| WalkDir::new(x).into_iter())
        .filter_map(|x| x.inspect_err(|e| warn!("Error reading path: {e}")).ok())
        .filter_map(|x| x.file_type().is_file().then_some(x))
        .filter(|path| visited_paths.insert(path.path().to_owned()))
    {
        // Load metadata from file
        match SongMetadata::load_from_path(
            path.path().to_path_buf(),
            artist_name_separator,
            protected_artist_names,
            genre_separator,
        ) {
            Ok(metadata) => {
                metadata_batch.push(metadata);

                // Process batch when full
                if metadata_batch.len() >= BATCH_SIZE {
                    match Song::bulk_load_into_db(db, &metadata_batch).await {
                        Ok(songs) => {
                            processed_count += songs.len();
                            info!("Indexed batch: {processed_count} songs total");
                        }
                        Err(e) => error!("Error indexing batch: {e}"),
                    }
                    metadata_batch.clear();
                }
            }
            Err(e) => warn!("Error reading metadata for {}: {e}", path.path().display()),
        }
    }

    // Process remaining songs in partial batch
    if !metadata_batch.is_empty() {
        match Song::bulk_load_into_db(db, &metadata_batch).await {
            Ok(songs) => {
                processed_count += songs.len();
                info!("Indexed final batch: {processed_count} songs total");
            }
            Err(e) => error!("Error indexing final batch: {e}"),
        }
    }

    info!("Finished indexing {processed_count} new songs");

    Ok(())
}

/// Clean up orphaned items in the library.
async fn delete_orphans<C: Connection>(db: &Surreal<C>) -> Result<(), Error> {
    // find and delete any remaining orphaned albums and artists
    macro_rules! delete_orphans {
        ($model:ident, $db:expr) => {
            let orphans = $model::delete_orphaned($db)
                .instrument(tracing::info_span!(concat!(
                    "Deleting orphaned ",
                    stringify!($model)
                )))
                .await?;
            if !orphans.is_empty() {
                info!("Deleted orphaned {}: {orphans:?}", stringify!($model));
            }
        };
    }

    delete_orphans!(Album, db);
    delete_orphans!(Artist, db);
    delete_orphans!(Collection, db);
    delete_orphans!(Playlist, db);

    Ok(())
}

/// Analyze the library.
///
/// In order, this function will:
/// - if `overwrite` is true, delete all existing analyses.
/// - get all the songs that aren't currently analyzed.
/// - start analyzing those songs in batches.
/// - update the database with the analyses.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
///
/// # Panics
///
/// This function will panic if the thread(s) that analyzes the songs panics.
#[instrument]
#[inline]
pub async fn analyze<C: Connection>(
    db: &Surreal<C>,
    mut interrupt: InterruptReceiver,
    overwrite: bool,
    settings: &AnalysisSettings,
    config: ModelConfig,
) -> Result<(), Error> {
    if overwrite {
        // delete all the analyses
        Analysis::delete_all(db)
            .instrument(tracing::info_span!("Deleting existing analyses"))
            .await?;
    }

    // get all the songs that don't have an analysis
    let songs_to_analyze: Vec<Song> = Analysis::read_songs_without_analysis(db).await?;
    // crate a hashmap mapping paths to song ids
    let paths = songs_to_analyze
        .into_iter()
        .map(|song| (song.path, song.id))
        .collect::<HashMap<_, _>>();

    let keys = paths.keys().cloned().collect::<Vec<_>>();

    // Use a bounded channel to apply backpressure on the producer.
    // This prevents unbounded memory growth when analysis is faster than database writes.
    // The buffer size is set to 2x the number of threads to allow some buffering
    // while still limiting memory usage.
    const ONE: NonZeroUsize = NonZeroUsize::new(1).unwrap();
    let channel_buffer_size = 2 * settings
        .num_threads
        .unwrap_or_else(|| std::thread::available_parallelism().unwrap_or(ONE))
        .get();
    let (tx, rx) = std::sync::mpsc::sync_channel(channel_buffer_size);

    let Ok(decoder) = MecompDecoder::new() else {
        error!("Error creating decoder");
        return Ok(());
    };

    // analyze the songs in batches, this is a blocking operation
    let num_threads = settings.num_threads;
    let handle = tokio::task::spawn_blocking(move || {
        if let Some(num) = num_threads {
            decoder.process_songs_with_cores(&keys, tx, num, config.clone())
        } else {
            decoder.process_songs(&keys, tx, config.clone())
        }
    });
    let abort = handle.abort_handle();

    async {
        for (song_path, maybe_analysis, maybe_embedding) in rx {
            if interrupt.is_stopped() {
                info!("Analysis interrupted");
                break;
            }

            let displayable_path = song_path.display();
            let Some(song_id) = paths.get(&song_path) else {
                error!("No song id found for path: {displayable_path}");
                continue;
            };

            // handle errors in embedding generation
            let embedding = match maybe_embedding {
                Ok(embedding) => *embedding.inner(),
                Err(e) => {
                    error!("Error generating embedding for {displayable_path}: {e}");
                    continue;
                }
            };

            // handle errors in analysis generation
            let features = match maybe_analysis {
                Ok(analysis) => *analysis.inner(),
                Err(e) => {
                    error!("Error generating analysis for {displayable_path}: {e}");
                    continue;
                }
            };

            if let Err(e) = Analysis::create(
                db,
                song_id.clone(),
                Analysis {
                    id: Analysis::generate_id(),
                    features,
                    embedding,
                },
            )
            .await
            {
                error!("Error saving analysis for {displayable_path}: {e}");
            } else {
                info!("Analyzed {displayable_path}");
            }
        }

        <Result<(), Error>>::Ok(())
    }
    .instrument(tracing::info_span!("Adding analyses to database"))
    .await?;

    tokio::select! {
        // wait for the interrupt signal
        _ = interrupt.wait() => {
            info!("Analysis interrupted");
            abort.abort();
        }
        // wait for the analysis to finish
        result = handle => match result {
            Ok(Ok(())) => {
                info!("Analysis complete");
                info!("Library health: {:?}", health(db).await?);
            }
            Ok(Err(e)) => {
                error!("Error analyzing songs: {e}");
            }
            Err(e) => {
                error!("Error joining task: {e}");
            }
        }
    }

    Ok(())
}

/// Recluster the library.
///
/// This function will remove and recompute all the "collections" (clusters) in the library.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
#[inline]
pub async fn recluster<C: Connection>(
    db: &Surreal<C>,
    settings: ReclusterSettings,
    analysis_settings: &AnalysisSettings,
    mut interrupt: InterruptReceiver,
) -> Result<(), Error> {
    // collect all the analyses
    let samples = Analysis::read_all(db).await?;

    if samples.is_empty() {
        info!("No analyses found, nothing to recluster");
        return Ok(());
    }

    let analysis_array = if matches!(analysis_settings.kind, AnalysisKind::Features) {
        samples
            .iter()
            .map(|analysis| analysis.features)
            .collect::<Vec<_>>()
            .into()
    } else {
        samples
            .iter()
            .map(|analysis| analysis.embedding)
            .collect::<Vec<_>>()
            .into()
    };

    // use clustering algorithm to cluster the analyses
    let clustering = move || {
        let Ok(model) = ClusteringHelper::new(
            analysis_array,
            settings.max_clusters,
            KOptimal::GapStatistic {
                b: settings.gap_statistic_reference_datasets,
            },
            settings.algorithm.into(),
            settings.projection_method.into(),
        )
        .inspect_err(|e| error!("There was an error creating the clustering helper: {e}")) else {
            return None;
        };

        let Ok(Ok(model)) = model
            .initialize()
            .inspect_err(|e| error!("There was an error initializing the clustering helper: {e}"))
            .map(ClusteringHelper::cluster)
        else {
            return None;
        };

        Some(model)
    };

    // use clustering algorithm to cluster the analyses
    let handle = tokio::task::spawn_blocking(clustering)
        .instrument(tracing::info_span!("Clustering library"));
    let abort = handle.inner().abort_handle();

    // wait for the clustering to finish
    let model = tokio::select! {
        _ = interrupt.wait() => {
            info!("Reclustering interrupted");
            abort.abort();
            return Ok(());
        }
        result = handle => match result {
            Ok(Some(model)) => model,
            Ok(None) => {
                return Ok(());
            }
            Err(e) => {
                error!("Error joining task: {e}");
                return Ok(());
            }
        }
    };

    // delete all the collections
    async {
        // NOTE: For some reason, if a collection has too many songs, it will fail to delete with "DbError(Db(Tx("Max transaction entries limit exceeded")))"
        // (this was happening with 892 songs in a collection)
        for collection in Collection::read_all(db).await? {
            Collection::delete(db, collection.id.clone()).await?;
        }

        <Result<(), Error>>::Ok(())
    }
    .instrument(tracing::info_span!("Deleting old collections"))
    .await?;

    // get the clusters from the clustering
    async {
        let analysis_ids = samples.into_iter().map(|a| a.id).collect();
        let clusters = model.extract_analysis_clusters(analysis_ids);

        // create the collections
        for (i, cluster) in clusters.into_iter().filter(|c| !c.is_empty()).enumerate() {
            let collection = Collection {
                id: Collection::generate_id(),
                name: format!("Collection {i}"),
                runtime: Duration::default(),
                song_count: Default::default(),
            };
            let collection = Collection::create(db, collection)
                .await?
                .ok_or(Error::NotCreated)?;

            async {
                let songs = Analysis::read_songs(db, cluster).await?;
                let song_ids = songs.into_iter().map(|s| s.id).collect::<Vec<_>>();

                Collection::add_songs(db, collection.id.clone(), song_ids).await?;

                <Result<(), Error>>::Ok(())
            }
            .instrument(tracing::info_span!("Adding songs to collection"))
            .await?;
        }
        Ok::<(), Error>(())
    }
    .instrument(tracing::info_span!("Creating new collections"))
    .await?;

    info!("Library recluster complete");
    info!("Library health: {:?}", health(db).await?);

    Ok(())
}

/// Get a brief overview of the library.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
#[inline]
pub async fn brief<C: Connection>(db: &Surreal<C>) -> Result<LibraryBrief, Error> {
    let artists = Artist::read_all_brief(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    let albums = Album::read_all_brief(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    let songs = Song::read_all_brief(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    let playlists = Playlist::read_all_brief(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    let collections = Collection::read_all_brief(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    let dynamic_playlists = DynamicPlaylist::read_all(db)
        .await?
        .into_iter()
        .map(Into::into)
        .collect();
    Ok(LibraryBrief {
        artists,
        albums,
        songs,
        playlists,
        collections,
        dynamic_playlists,
    })
}

/// Get the full library.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
#[inline]
pub async fn full<C: Connection>(db: &Surreal<C>) -> Result<LibraryFull, Error> {
    Ok(LibraryFull {
        artists: Artist::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
        albums: Album::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
        songs: Song::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
        playlists: Playlist::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
        collections: Collection::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
        dynamic_playlists: DynamicPlaylist::read_all(db)
            .await?
            .into_iter()
            .map(Into::into)
            .collect(),
    })
}

/// Get the health of the library.
///
/// This function will return the health of the library, including the number of orphaned items.
///
/// # Errors
///
/// This function will return an error if there is an error reading from the database.
#[instrument]
#[inline]
pub async fn health<C: Connection>(db: &Surreal<C>) -> Result<LibraryHealth, Error> {
    Ok(LibraryHealth {
        artists: count_artists(db).await?,
        albums: count_albums(db).await?,
        songs: count_songs(db).await?,
        unanalyzed_songs: Some(count_unanalyzed_songs(db).await?),
        playlists: count_playlists(db).await?,
        collections: count_collections(db).await?,
        dynamic_playlists: count_dynamic_playlists(db).await?,
        orphaned_artists: count_orphaned_artists(db).await?,
        orphaned_albums: count_orphaned_albums(db).await?,
        orphaned_playlists: count_orphaned_playlists(db).await?,
        orphaned_collections: count_orphaned_collections(db).await?,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::init;

    use mecomp_core::config::{ClusterAlgorithm, ProjectionMethod};
    use mecomp_storage::db::schemas::song::{SongChangeSet, SongMetadata};
    use mecomp_storage::test_utils::{
        ARTIST_NAME_SEPARATOR, SongCase, arb_feature_array, arb_song_case, arb_vec,
        create_song_metadata, create_song_with_overrides, init_test_database,
    };
    use one_or_many::OneOrMany;
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    #[tokio::test]
    #[allow(clippy::too_many_lines)]
    async fn test_rescan() {
        init();
        let tempdir = tempfile::tempdir().unwrap();
        let db = init_test_database().await.unwrap();

        // populate the tempdir with songs that aren't in the database
        let song_cases = arb_vec(&arb_song_case(), 10..=15)();
        let metadatas = song_cases
            .into_iter()
            .map(|song_case| create_song_metadata(&tempdir, song_case))
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        // also make some songs that are in the database
        //  - a song that whose file was deleted
        let song_with_nonexistent_path = create_song_with_overrides(
            &db,
            arb_song_case()(),
            SongChangeSet {
                path: Some(tempdir.path().join("nonexistent.mp3")),
                ..Default::default()
            },
        )
        .await
        .unwrap();
        let mut metadata_of_song_with_outdated_metadata =
            create_song_metadata(&tempdir, arb_song_case()()).unwrap();
        metadata_of_song_with_outdated_metadata.genre = OneOrMany::None;
        let song_with_outdated_metadata =
            Song::try_load_into_db(&db, metadata_of_song_with_outdated_metadata)
                .await
                .unwrap();
        // also add a "song" that can't be read
        let invalid_song_path = tempdir.path().join("invalid1.mp3");
        std::fs::write(&invalid_song_path, "this is not a song").unwrap();
        // add another invalid song, this time also put it in the database
        let invalid_song_path = tempdir.path().join("invalid2.mp3");
        std::fs::write(&invalid_song_path, "this is not a song").unwrap();
        let song_with_invalid_metadata = create_song_with_overrides(
            &db,
            arb_song_case()(),
            SongChangeSet {
                path: Some(tempdir.path().join("invalid2.mp3")),
                ..Default::default()
            },
        )
        .await
        .unwrap();

        // rescan the library
        rescan(
            &db,
            &[tempdir.path().to_owned()],
            &ARTIST_NAME_SEPARATOR.to_string().into(),
            &OneOrMany::None,
            Some(ARTIST_NAME_SEPARATOR),
            MetadataConflictResolution::Overwrite,
        )
        .await
        .unwrap();

        // check that everything was done correctly
        // - `song_with_nonexistent_path` was deleted
        assert_eq!(
            Song::read(&db, song_with_nonexistent_path.id)
                .await
                .unwrap(),
            None
        );
        // - `song_with_invalid_metadata` was deleted
        assert_eq!(
            Song::read(&db, song_with_invalid_metadata.id)
                .await
                .unwrap(),
            None
        );
        // - `song_with_outdated_metadata` was updated
        assert!(
            Song::read(&db, song_with_outdated_metadata.id)
                .await
                .unwrap()
                .unwrap()
                .genre
                .is_some()
        );
        // - all the other songs were added
        //   and their artists, albums, and album_artists were added and linked correctly
        for metadata in metadatas {
            // the song was created
            let song = Song::read_by_path(&db, metadata.path.clone())
                .await
                .unwrap();
            assert!(song.is_some());
            let song = song.unwrap();

            // the song's metadata is correct
            assert_eq!(SongMetadata::from(&song), metadata);

            // the song's artists were created
            let artists = Artist::read_by_names(&db, Vec::from(metadata.artist.clone()))
                .await
                .unwrap();
            assert_eq!(artists.len(), metadata.artist.len());
            // the song is linked to the artists
            for artist in &artists {
                assert!(metadata.artist.contains(&artist.name));
                assert!(
                    Artist::read_songs(&db, artist.id.clone())
                        .await
                        .unwrap()
                        .contains(&song)
                );
            }
            // the artists are linked to the song
            if let Ok(song_artists) = Song::read_artist(&db, song.id.clone()).await {
                for artist in artists {
                    assert!(song_artists.contains(&artist));
                }
            } else {
                panic!("Error reading song artists");
            }

            // the song's album was created
            let album = Album::read_by_name_and_album_artist(
                &db,
                &metadata.album,
                metadata.album_artist.clone(),
            )
            .await
            .unwrap();
            assert!(album.is_some());
            let album = album.unwrap();
            // the song is linked to the album
            assert_eq!(
                Song::read_album(&db, song.id.clone()).await.unwrap(),
                Some(album.clone())
            );
            // the album is linked to the song
            assert!(
                Album::read_songs(&db, album.id.clone())
                    .await
                    .unwrap()
                    .contains(&song)
            );

            // the album's album artists were created
            let album_artists =
                Artist::read_by_names(&db, Vec::from(metadata.album_artist.clone()))
                    .await
                    .unwrap();
            assert_eq!(album_artists.len(), metadata.album_artist.len());
            // the album is linked to the album artists
            for album_artist in album_artists {
                assert!(metadata.album_artist.contains(&album_artist.name));
                assert!(
                    Artist::read_albums(&db, album_artist.id.clone())
                        .await
                        .unwrap()
                        .contains(&album)
                );
            }
        }
    }

    #[tokio::test]
    async fn rescan_deletes_preexisting_orphans() {
        init();
        let tempdir = tempfile::tempdir().unwrap();
        let db = init_test_database().await.unwrap();

        // create a song with an artist and an album
        let metadata = create_song_metadata(&tempdir, arb_song_case()()).unwrap();
        let song = Song::try_load_into_db(&db, metadata.clone()).await.unwrap();

        // delete the song, leaving orphaned artist and album
        std::fs::remove_file(&song.path).unwrap();
        Song::delete(&db, (song.id.clone(), false)).await.unwrap();

        // rescan the library
        rescan(
            &db,
            &[tempdir.path().to_owned()],
            &ARTIST_NAME_SEPARATOR.to_string().into(),
            &OneOrMany::None,
            Some(ARTIST_NAME_SEPARATOR),
            MetadataConflictResolution::Overwrite,
        )
        .await
        .unwrap();

        // check that the album and artist deleted
        assert_eq!(Song::read_all(&db).await.unwrap().len(), 0);
        assert_eq!(Album::read_all(&db).await.unwrap().len(), 0);
        let artists = Artist::read_all(&db).await.unwrap();
        for artist in artists {
            assert_eq!(artist.album_count, 0);
            assert_eq!(artist.song_count, 0);
        }
        assert_eq!(Artist::read_all(&db).await.unwrap().len(), 0);
    }

    #[tokio::test]
    async fn rescan_deletes_orphaned_albums_and_artists() {
        init();
        let tempdir = tempfile::tempdir().unwrap();
        let db = init_test_database().await.unwrap();

        // create a song with an artist and an album
        let metadata = create_song_metadata(&tempdir, arb_song_case()()).unwrap();
        let song = Song::try_load_into_db(&db, metadata.clone()).await.unwrap();
        let artist = Artist::read_by_names(&db, Vec::from(metadata.artist.clone()))
            .await
            .unwrap()
            .pop()
            .unwrap();
        let album = Album::read_by_name_and_album_artist(
            &db,
            &metadata.album,
            metadata.album_artist.clone(),
        )
        .await
        .unwrap()
        .unwrap();

        // delete the song, leaving orphaned artist and album
        std::fs::remove_file(&song.path).unwrap();

        // rescan the library
        rescan(
            &db,
            &[tempdir.path().to_owned()],
            &ARTIST_NAME_SEPARATOR.to_string().into(),
            &OneOrMany::None,
            Some(ARTIST_NAME_SEPARATOR),
            MetadataConflictResolution::Overwrite,
        )
        .await
        .unwrap();

        // check that the artist and album were deleted
        assert_eq!(Artist::read(&db, artist.id.clone()).await.unwrap(), None);
        assert_eq!(Album::read(&db, album.id.clone()).await.unwrap(), None);
    }

    #[tokio::test]
    async fn test_analyze() {
        init();
        let dir = tempfile::tempdir().unwrap();
        let db = init_test_database().await.unwrap();
        let interrupt = InterruptReceiver::dummy();
        let config = mecomp_analysis::embeddings::ModelConfig::default();
        let settings = AnalysisSettings::default();

        // load some songs into the database
        let song_cases = arb_vec(&arb_song_case(), 10..=15)();
        let song_cases = song_cases.into_iter().enumerate().map(|(i, sc)| SongCase {
            song: u8::try_from(i).unwrap(),
            ..sc
        });
        let metadatas = song_cases
            .into_iter()
            .map(|song_case| create_song_metadata(&dir, song_case))
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        for metadata in &metadatas {
            Song::try_load_into_db(&db, metadata.clone()).await.unwrap();
        }

        // check that there are no analyses before.
        assert_eq!(
            Analysis::read_songs_without_analysis(&db)
                .await
                .unwrap()
                .len(),
            metadatas.len()
        );

        // analyze the library
        analyze(&db, interrupt, true, &settings, config)
            .await
            .unwrap();

        // check that all the songs have analyses
        assert_eq!(
            Analysis::read_songs_without_analysis(&db)
                .await
                .unwrap()
                .len(),
            0
        );
        for metadata in &metadatas {
            let song = Song::read_by_path(&db, metadata.path.clone())
                .await
                .unwrap()
                .unwrap();
            let analysis = Analysis::read_for_song(&db, song.id.clone()).await.unwrap();
            assert!(analysis.is_some());
        }

        // check that if we ask for the nearest neighbors of one of these songs, we get all the other songs
        for analysis in Analysis::read_all(&db).await.unwrap() {
            let neighbors = Analysis::nearest_neighbors(&db, analysis.id.clone(), 100)
                .await
                .unwrap();
            assert!(!neighbors.contains(&analysis));
            assert_eq!(neighbors.len(), metadatas.len() - 1);
            assert_eq!(
                neighbors.len(),
                neighbors
                    .iter()
                    .map(|n| n.id.clone())
                    .collect::<HashSet<_>>()
                    .len()
            );
        }
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_recluster(
        #[values(ProjectionMethod::TSne, ProjectionMethod::None, ProjectionMethod::Pca)]
        projection_method: ProjectionMethod,
    ) {
        init();
        let dir = tempfile::tempdir().unwrap();
        let db = init_test_database().await.unwrap();
        let settings = ReclusterSettings {
            gap_statistic_reference_datasets: 5,
            max_clusters: 18,
            algorithm: ClusterAlgorithm::GMM,
            projection_method,
        };
        let analysis_settings = AnalysisSettings::default();

        // load some songs into the database
        let song_cases = arb_vec(&arb_song_case(), 32..=32)();
        let song_cases = song_cases.into_iter().enumerate().map(|(i, sc)| SongCase {
            song: u8::try_from(i).unwrap(),
            ..sc
        });
        let metadatas = song_cases
            .into_iter()
            .map(|song_case| create_song_metadata(&dir, song_case))
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        let mut songs = Vec::with_capacity(metadatas.len());
        for metadata in &metadatas {
            songs.push(Song::try_load_into_db(&db, metadata.clone()).await.unwrap());
        }

        // load some dummy analyses into the database
        for song in &songs {
            Analysis::create(
                &db,
                song.id.clone(),
                Analysis {
                    id: Analysis::generate_id(),
                    features: arb_feature_array()(),
                    embedding: arb_feature_array()(),
                },
            )
            .await
            .unwrap();
        }

        // recluster the library
        recluster(
            &db,
            settings,
            &analysis_settings,
            InterruptReceiver::dummy(),
        )
        .await
        .unwrap();

        // check that there are collections
        let collections = Collection::read_all(&db).await.unwrap();
        assert!(!collections.is_empty());
        for collection in collections {
            let songs = Collection::read_songs(&db, collection.id.clone())
                .await
                .unwrap();
            assert!(!songs.is_empty());
        }
    }

    #[tokio::test]
    async fn test_brief() {
        init();
        let db = init_test_database().await.unwrap();
        let brief = brief(&db).await.unwrap();
        assert_eq!(brief.artists, Vec::default());
        assert_eq!(brief.albums, Vec::default());
        assert_eq!(brief.songs, Vec::default());
        assert_eq!(brief.playlists, Vec::default());
        assert_eq!(brief.collections, Vec::default());
    }

    #[tokio::test]
    async fn test_full() {
        init();
        let db = init_test_database().await.unwrap();
        let full = full(&db).await.unwrap();
        assert_eq!(full.artists.len(), 0);
        assert_eq!(full.albums.len(), 0);
        assert_eq!(full.songs.len(), 0);
        assert_eq!(full.playlists.len(), 0);
        assert_eq!(full.collections.len(), 0);
    }

    #[tokio::test]
    async fn test_health() {
        init();
        let db = init_test_database().await.unwrap();
        let health = health(&db).await.unwrap();
        assert_eq!(health.artists, 0);
        assert_eq!(health.albums, 0);
        assert_eq!(health.songs, 0);
        assert_eq!(health.unanalyzed_songs, Some(0));
        assert_eq!(health.playlists, 0);
        assert_eq!(health.collections, 0);
        assert_eq!(health.orphaned_artists, 0);
        assert_eq!(health.orphaned_albums, 0);
        assert_eq!(health.orphaned_playlists, 0);
        assert_eq!(health.orphaned_collections, 0);
    }
}