From 6ead0d12df92cd88293c29f5f1b12ff211b4fb05 Mon Sep 17 00:00:00 2001 From: cwilvx Date: Mon, 14 Oct 2024 17:45:18 +0300 Subject: [PATCH] fix: new artists played report on charts --- app/api/scrobble/__init__.py | 2 +- app/utils/stats.py | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/api/scrobble/__init__.py b/app/api/scrobble/__init__.py index fea6cbf8..76e4364d 100644 --- a/app/api/scrobble/__init__.py +++ b/app/api/scrobble/__init__.py @@ -200,7 +200,7 @@ def get_top_artists(query: ChartItemsQuery): current_period_artists = get_artists_in_period(start_time, end_time) previous_period_artists = get_artists_in_period(previous_start_time, start_time) - new_artists = calculate_new_artists(current_period_artists, previous_period_artists) + new_artists = calculate_new_artists(current_period_artists, start_time) scrobble_trend = calculate_scrobble_trend( len(current_period_artists), len(previous_period_artists) ) diff --git a/app/utils/stats.py b/app/utils/stats.py index 13c54713..0b57ddb6 100644 --- a/app/utils/stats.py +++ b/app/utils/stats.py @@ -63,7 +63,9 @@ def get_tracks_in_period(start_time: int, end_time: int): for scrobble in scrobbles: if scrobble.trackhash not in tracks: try: - track = copy.deepcopy(TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])[0]) + track = copy.deepcopy( + TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])[0] + ) except IndexError: continue @@ -153,11 +155,28 @@ def calculate_scrobble_trend(current_scrobbles: int, previous_scrobbles: int) -> ) -def calculate_new_artists( - current_artists: List[dict[str, Any]], previous_artists: List[dict[str, Any]] -): +def calculate_new_artists(current_artists: List[dict[str, Any]], timestamp: int): + """ + Calculate the number of new artists based on the current and all previous scrobbles. + """ current_artists_set = set(artist["artisthash"] for artist in current_artists) - previous_artists_set = set(artist["artisthash"] for artist in previous_artists) + all_records = ScrobbleTable.get_all(0, None) + trackhashes = set( + record.trackhash for record in all_records if record.timestamp < timestamp + ) + + previous_artists_set = set() + + for record in trackhashes: + entry = TrackStore.trackhashmap.get(record) + if not entry: + continue + + entry = entry.tracks[0] + + for artist in entry.artists: + artisthash = artist["artisthash"] + previous_artists_set.add(artisthash) return len(current_artists_set - previous_artists_set)