diff options
-rw-r--r-- | meetingtools/apps/content/tasks.py | 64 | ||||
-rw-r--r-- | meetingtools/apps/content/views.py | 68 |
2 files changed, 84 insertions, 48 deletions
diff --git a/meetingtools/apps/content/tasks.py b/meetingtools/apps/content/tasks.py index ba0940e..dcdae03 100644 --- a/meetingtools/apps/content/tasks.py +++ b/meetingtools/apps/content/tasks.py @@ -1,14 +1,31 @@ # -*- coding: utf-8 -*- __author__ = 'lundberg' +from django.core.cache import cache +from django.shortcuts import get_object_or_404 +from django.db.models import Sum, Count +from django.contrib.auth.models import User from meetingtools.ac import ac_api_client from meetingtools.ac.api import ACPException from meetingtools.apps.content.models import Content -import logging -from datetime import datetime, timedelta +from meetingtools.apps.cluster.models import ACCluster from celery.task import periodic_task from celery.schedules import crontab -from meetingtools.apps.cluster.models import ACCluster +from tagging.models import Tag, TaggedItem +from datetime import datetime, timedelta +import logging + + +@periodic_task(run_every=crontab(hour="*", minute="*/10", day_of_week="*")) +def import_all_content(): + for acc in ACCluster.objects.all(): + import_acc(acc, since=900) + + +@periodic_task(run_every=crontab(hour="*", minute="*/15", day_of_week="*")) +def cache_cluster_content(): + for acc in ACCluster.objects.all(): + get_cluster_content(acc) def import_acc(acc, since=0): @@ -28,10 +45,43 @@ def import_acc(acc, since=0): logging.info("%s: Imported %d content objects." % (acc, nr)) -@periodic_task(run_every=crontab(hour="*", minute="*/15", day_of_week="*")) -def import_all_content(): - for acc in ACCluster.objects.all(): - import_acc(acc, since=960) +def get_cluster_content(acc): + total_bytecount = 0 + domains = [] + tags = Tag.objects.usage_for_model(Content, filters={'sco__acc': acc}) + for tag in sorted(tags): + if tag.name.startswith('domain:'): + qs = TaggedItem.objects.get_by_model(Content, tag) + d = { + 'domain': tag.name.split('domain:')[1], + 'domain_bytes': qs.aggregate(Sum('bytecount'))['bytecount__sum'], + 'number_of_files': len(qs) + } + total_bytecount += d['domain_bytes'] + domains.append(d) + cache.set('%s-domains' % acc, domains, 900) + cache.set('%s-bytecount' % acc, total_bytecount, 900) + return domains, total_bytecount + + +def get_domain_content(domain_tag): + users = [] + qs = TaggedItem.objects.get_by_model(Content, domain_tag) + total_files = len(qs) + total_bytecount = qs.aggregate(Sum('bytecount'))['bytecount__sum'] + creators = qs.values('creator').annotate(num_files=Count('creator')) + for creator in creators: + domain_user = get_object_or_404(User, pk=creator['creator']) + d = { + 'username': domain_user.username, + 'number_of_files': creator['num_files'], + 'bytecount': Content.objects.filter(creator=domain_user).aggregate(Sum('bytecount'))['bytecount__sum'] + } + users.append(d) + cache.set('%s-users' % domain_tag, users, 900) + cache.set('%s-files' % domain_tag, total_files, 900) + cache.set('%s-bytecount' % domain_tag, total_bytecount, 900) + return users, total_files, total_bytecount #@periodic_task(run_every=crontab(hour="1", minute="0", day_of_week="*")) diff --git a/meetingtools/apps/content/views.py b/meetingtools/apps/content/views.py index 7bc0792..d300b86 100644 --- a/meetingtools/apps/content/views.py +++ b/meetingtools/apps/content/views.py @@ -1,29 +1,30 @@ # -*- coding: utf-8 -*- __author__ = 'lundberg' -from django.contrib.auth.decorators import login_required, permission_required -from django.db.models import Sum, Count -from django.contrib.auth.models import User -from django.contrib.humanize.templatetags.humanize import naturalday -from django.http import HttpResponseForbidden, HttpResponseBadRequest -from meetingtools.apps.stats.models import UserMeetingTransaction -from iso8601 import iso8601 -from time import mktime -from meetingtools.multiresponse import json_response, respond_to -from meetingtools.apps.stats.forms import StatCaledarForm +from django.contrib.auth.decorators import login_required +from django.db.models import Sum +from django.http import HttpResponseForbidden +from django.core.cache import cache +from meetingtools.multiresponse import respond_to from django.shortcuts import get_object_or_404 -from tagging.models import Tag, TaggedItem +from tagging.models import Tag from meetingtools.apps.content.models import Content from meetingtools.apps.cluster.models import ACCluster +from meetingtools.apps.content import tasks @login_required def user(request, username=None): if username is None: username = request.user.username - content = Content.objects.filter(creator__username=username) - total_bytecount = content.aggregate(Sum('bytecount')) - return respond_to(request,{'text/html': 'apps/content/user.html'}, + content = cache.get('%s-content' % username) + total_bytecount = cache.get('%s-bytecount' % username) + if not content or total_bytecount: + content = Content.objects.filter(creator__username=username) + total_bytecount = content.aggregate(Sum('bytecount')) + cache.set('%s-content' % username, content, 900) + cache.set('%s-bytecount' % username, total_bytecount, 900) + return respond_to(request, {'text/html': 'apps/content/user.html'}, {'username': username, 'content': content, 'total_bytecount': total_bytecount}) @@ -35,20 +36,12 @@ def cluster(request, cluster_name=None): clusters = ACCluster.objects.all().values('name') if cluster_name: - total_bytecount = 0 - domains = [] acc = get_object_or_404(ACCluster, name=cluster_name) - tags = Tag.objects.usage_for_model(Content, filters={'sco__acc': acc}) - for tag in sorted(tags): - if tag.name.startswith('domain:'): - qs = TaggedItem.objects.get_by_model(Content, tag) - d = { - 'domain': tag.name.split('domain:')[1], - 'domain_bytes': qs.aggregate(Sum('bytecount'))['bytecount__sum'], - 'number_of_files': len(qs) - } - total_bytecount += d['domain_bytes'] - domains.append(d) + domains = cache.get('%s-domains' % acc) + total_bytecount = cache.get('%s-bytecount' % acc) + if not domains or not total_bytecount: + domains, total_bytecount = tasks.get_cluster_content(acc) + return respond_to(request, {'text/html': 'apps/content/cluster.html'}, {'clusters': clusters, 'cluster_name': cluster_name, 'domains': domains, 'total_bytecount': total_bytecount}) @@ -63,20 +56,13 @@ def domain(request, domain_name): if not request.user.is_staff: return HttpResponseForbidden - users = [] - tag = get_object_or_404(Tag, name='domain:%s' % domain_name) - qs = TaggedItem.objects.get_by_model(Content, tag) - total_files = len(qs) - total_bytecount = qs.aggregate(Sum('bytecount'))['bytecount__sum'] - creators = qs.values('creator').annotate(num_files=Count('creator')) - for creator in creators: - domain_user = get_object_or_404(User, pk=creator['creator']) - d = { - 'username': domain_user.username, - 'number_of_files': creator['num_files'], - 'bytecount': Content.objects.filter(creator=domain_user).aggregate(Sum('bytecount'))['bytecount__sum'] - } - users.append(d) + domain_tag = get_object_or_404(Tag, name='domain:%s' % domain_name) + users = cache.get('%s-users' % domain_tag) + total_files = cache.get('%s-files' % domain_tag) + total_bytecount = cache.get('%s-bytecount' % domain_tag) + if not users or not total_files or not total_bytecount: + users, total_files, total_bytecount = tasks.get_domain_content(domain_tag) + return respond_to(request, {'text/html': 'apps/content/domain.html'}, {'domain': domain_name, 'total_bytecount': total_bytecount, 'total_files': total_files, 'users': users}) |