fix: new artists played report on charts

This commit is contained in:
cwilvx
2024-10-14 17:45:18 +03:00
parent 24d52167ca
commit 6ead0d12df
2 changed files with 25 additions and 6 deletions
+1 -1
View File
@@ -200,7 +200,7 @@ def get_top_artists(query: ChartItemsQuery):
current_period_artists = get_artists_in_period(start_time, end_time) current_period_artists = get_artists_in_period(start_time, end_time)
previous_period_artists = get_artists_in_period(previous_start_time, start_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( scrobble_trend = calculate_scrobble_trend(
len(current_period_artists), len(previous_period_artists) len(current_period_artists), len(previous_period_artists)
) )
+24 -5
View File
@@ -63,7 +63,9 @@ def get_tracks_in_period(start_time: int, end_time: int):
for scrobble in scrobbles: for scrobble in scrobbles:
if scrobble.trackhash not in tracks: if scrobble.trackhash not in tracks:
try: 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: except IndexError:
continue continue
@@ -153,11 +155,28 @@ def calculate_scrobble_trend(current_scrobbles: int, previous_scrobbles: int) ->
) )
def calculate_new_artists( def calculate_new_artists(current_artists: List[dict[str, Any]], timestamp: int):
current_artists: List[dict[str, Any]], previous_artists: List[dict[str, Any]] """
): 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) 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) return len(current_artists_set - previous_artists_set)