Skip to content

Commit

Permalink
Make minute sorting faster
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Oct 15, 2024
1 parent 09642e1 commit ee950c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions endsong_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ pub async fn top_artists(
)
})
.collect(),
Sorting::Minutes => gather::artists(entries)
Sorting::Minutes => gather::artists_with_duration(entries)
.into_iter()
.map(|(artist, plays)| {
.map(|(artist, (plays, duration))| {
(
format!("/artist/{artist}"),
artist.clone(),
plays,
gather::listening_time(entries, &artist).num_minutes(),
duration.num_minutes(),
)
})
.sorted_unstable_by(|a, b| b.3.cmp(&a.3).then_with(|| a.1.cmp(&b.1)))
Expand Down
15 changes: 15 additions & 0 deletions src/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ pub fn artists(entries: &[SongEntry]) -> HashMap<Artist, usize> {
entries.iter().map(Artist::from).counts()
}

/// Returns a map with all [`Artists`][Artist], their playcount and time listened
#[must_use]
pub fn artists_with_duration(entries: &[SongEntry]) -> HashMap<Artist, (usize, TimeDelta)> {
let mut map = HashMap::with_capacity(entries.len() / 100);

for entry in entries {
let artist = Artist::from(entry);
let e: &mut (usize, TimeDelta) = map.entry(artist).or_default();
e.0 += 1;
e.1 += entry.time_played;
}

map
}

/// Counts up the plays of an [`Artist`], [`Album`] or [`Song`]
#[must_use]
pub fn plays<Asp: Music>(entries: &[SongEntry], aspect: &Asp) -> usize {
Expand Down

0 comments on commit ee950c6

Please sign in to comment.