Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DONE] Use cache to store video data #969

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pod/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
# Local contexts
"pod.main.context_processors.context_settings",
"pod.main.context_processors.context_footer",
"pod.video.context_processors.context_navbar",
"pod.video.context_processors.context_video_data",
"pod.video.context_processors.context_video_settings",
"pod.authentication.context_processors.context_authentication_settings",
"pod.recorder.context_processors.context_recorder_settings",
Expand Down
74 changes: 44 additions & 30 deletions pod/video/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models import OuterRef

from datetime import timedelta
from django.core.cache import cache
from django.contrib.sites.shortcuts import get_current_site
from pod.video_encode_transcript.models import EncodingVideo
from pod.video_encode_transcript.models import PlaylistVideo
Expand All @@ -19,7 +20,7 @@
HIDE_USER_FILTER = getattr(django_settings, "HIDE_USER_FILTER", False)
OEMBED = getattr(django_settings, "OEMBED", False)
USE_STATS_VIEW = getattr(django_settings, "USE_STATS_VIEW", False)

CACHE_VIDEO_DEFAULT_TIMEOUT = getattr(django_settings, "CACHE_VIDEO_DEFAULT_TIMEOUT", 600)
__AVAILABLE_VIDEO_FILTER__ = {
"encoding_in_progress": False,
"is_draft": False,
Expand Down Expand Up @@ -78,37 +79,50 @@ def context_video_settings(request):
return new_settings


def context_navbar(request):
types = (
Type.objects.filter(
sites=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
def context_video_data(request):
ptitloup marked this conversation as resolved.
Show resolved Hide resolved
"""Get video data in cache, if not, create and add it in cache."""
types = cache.get('TYPES')
if types is None:
types = (
Type.objects.filter(
sites=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)

disciplines = (
Discipline.objects.filter(
site=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
cache.set("TYPES", types, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

disciplines = cache.get('DISCIPLINES')
if disciplines is None:
disciplines = (
Discipline.objects.filter(
site=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)

v_filter = get_available_videos_filter(request)

aggregate_videos = v_filter.aggregate(duration=Sum("duration"), number=Count("id"))

VIDEOS_COUNT = aggregate_videos["number"]
VIDEOS_DURATION = (
str(timedelta(seconds=aggregate_videos["duration"]))
if aggregate_videos["duration"]
else 0
)
cache.set("DISCIPLINES", disciplines, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

VIDEOS_COUNT = cache.get('VIDEOS_COUNT')
VIDEOS_DURATION = cache.get('VIDEOS_DURATION')
if VIDEOS_COUNT is None:
v_filter = get_available_videos_filter(request)
aggregate_videos = v_filter.aggregate(
duration=Sum("duration"),
number=Count("id")
)
VIDEOS_COUNT = aggregate_videos["number"]
cache.set("VIDEOS_COUNT", VIDEOS_COUNT, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)
VIDEOS_DURATION = (
str(timedelta(seconds=aggregate_videos["duration"]))
if aggregate_videos["duration"]
else 0
)
cache.set("VIDEOS_DURATION", VIDEOS_DURATION, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

return {
"TYPES": types,
Expand Down
28 changes: 28 additions & 0 deletions pod/video/management/commands/cache_video_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand
from django.core.cache import cache
from pod.video.context_processors import context_video_data
from django.core import serializers
import json


class Command(BaseCommand):
ptitloup marked this conversation as resolved.
Show resolved Hide resolved
"""Command to store video data in cache."""
help = "Store video data in django cache : " \
+ "types, discipline, video count and videos duration"

def handle(self, *args, **options):
ptitloup marked this conversation as resolved.
Show resolved Hide resolved
"""Function called to store video data in cache."""
cache.delete_many(['DISCIPLINES', 'VIDEOS_COUNT', 'VIDEOS_DURATION', 'TYPES'])
video_data = context_video_data(request=None)
msg = 'Successfully store video data in cache'
for data in video_data:
try:
msg += "\n %s : %s" % (
data,
json.dumps(serializers.serialize("json", video_data[data]))
)
except (TypeError, AttributeError):
msg += "\n %s : %s" % (data, video_data[data])
self.stdout.write(
self.style.SUCCESS(msg)
)