[scrobbles] Improve query efficency
This commit is contained in:
@ -29,7 +29,7 @@ from django.views.generic.list import ListView
|
||||
from moods.models import Mood
|
||||
from music.aggregators import (
|
||||
artist_scrobble_count,
|
||||
live_charts,
|
||||
batch_live_charts,
|
||||
live_tv_charts,
|
||||
live_youtube_channel_charts,
|
||||
scrobble_counts,
|
||||
@ -794,22 +794,14 @@ class ChartRecordView(TemplateView):
|
||||
"year": "This year",
|
||||
"all": "All time",
|
||||
}
|
||||
context_data["current_artist_charts"] = {
|
||||
"today": list(live_charts(**artist, chart_period="today")),
|
||||
"last7": list(live_charts(**artist, chart_period="last7")),
|
||||
"last30": list(live_charts(**artist, chart_period="last30")),
|
||||
"year": list(live_charts(**artist, chart_period="year")),
|
||||
"all": list(live_charts(**artist)),
|
||||
}
|
||||
context_data["current_artist_charts"] = batch_live_charts(
|
||||
user=user, media_type="Artist", limit=limit
|
||||
)
|
||||
|
||||
track = {"user": user, "media_type": "Track", "limit": limit}
|
||||
context_data["current_track_charts"] = {
|
||||
"today": list(live_charts(**track, chart_period="today")),
|
||||
"last7": list(live_charts(**track, chart_period="last7")),
|
||||
"last30": list(live_charts(**track, chart_period="last30")),
|
||||
"year": list(live_charts(**track, chart_period="year")),
|
||||
"all": list(live_charts(**track)),
|
||||
}
|
||||
context_data["current_track_charts"] = batch_live_charts(
|
||||
user=user, media_type="Track", limit=limit
|
||||
)
|
||||
|
||||
now = timezone.now()
|
||||
tzinfo = now.tzinfo
|
||||
@ -821,44 +813,53 @@ class ChartRecordView(TemplateView):
|
||||
start_day_of_week = start_of_today - timedelta(days=now.isoweekday() % 7)
|
||||
start_day_of_month = now.replace(day=1)
|
||||
|
||||
# TV Series Scrobbles
|
||||
series_scrobbles = Scrobble.objects.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.TV_EPISODE,
|
||||
played_to_completion=True,
|
||||
# TV Series Scrobbles - fetch all and filter in Python
|
||||
series_all = list(
|
||||
Scrobble.objects.with_related()
|
||||
.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.TV_EPISODE,
|
||||
played_to_completion=True,
|
||||
timestamp__gte=start_day_of_month,
|
||||
)
|
||||
.order_by("-timestamp")
|
||||
)
|
||||
context_data["series_this_week"] = series_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_week
|
||||
).order_by("-timestamp")
|
||||
context_data["series_this_month"] = series_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_month
|
||||
).order_by("-timestamp")
|
||||
context_data["series_this_week"] = [
|
||||
s for s in series_all if s.timestamp >= start_day_of_week
|
||||
]
|
||||
context_data["series_this_month"] = series_all
|
||||
|
||||
# Movie Scrobbles
|
||||
movie_scrobbles = Scrobble.objects.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.MOVIE,
|
||||
played_to_completion=True,
|
||||
# Movie Scrobbles - fetch all and filter in Python
|
||||
movies_all = list(
|
||||
Scrobble.objects.with_related()
|
||||
.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.MOVIE,
|
||||
played_to_completion=True,
|
||||
timestamp__gte=start_day_of_month,
|
||||
)
|
||||
.order_by("-timestamp")
|
||||
)
|
||||
context_data["videos_this_week"] = movie_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_week
|
||||
).order_by("-timestamp")
|
||||
context_data["videos_this_month"] = movie_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_month
|
||||
).order_by("-timestamp")
|
||||
context_data["videos_this_week"] = [
|
||||
v for v in movies_all if v.timestamp >= start_day_of_week
|
||||
]
|
||||
context_data["videos_this_month"] = movies_all
|
||||
|
||||
# YouTube Scrobbles
|
||||
youtube_scrobbles = Scrobble.objects.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.YOUTUBE,
|
||||
played_to_completion=True,
|
||||
# YouTube Scrobbles - fetch all and filter in Python
|
||||
youtube_all = list(
|
||||
Scrobble.objects.with_related()
|
||||
.filter(
|
||||
user=user,
|
||||
video__video_type=Video.VideoType.YOUTUBE,
|
||||
played_to_completion=True,
|
||||
timestamp__gte=start_day_of_month,
|
||||
)
|
||||
.order_by("-timestamp")
|
||||
)
|
||||
context_data["youtube_this_week"] = youtube_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_week
|
||||
).order_by("-timestamp")
|
||||
context_data["youtube_this_month"] = youtube_scrobbles.filter(
|
||||
timestamp__gte=start_day_of_month
|
||||
).order_by("-timestamp")
|
||||
context_data["youtube_this_week"] = [
|
||||
y for y in youtube_all if y.timestamp >= start_day_of_week
|
||||
]
|
||||
context_data["youtube_this_month"] = youtube_all
|
||||
|
||||
context_data["tv_show_charts"] = {
|
||||
"today": list(live_tv_charts(user=user, chart_period="today")),
|
||||
|
||||
Reference in New Issue
Block a user