]> git.0d.be Git - django-panik-nonstop.git/commitdiff
stats: include total duration
authorFrédéric Péters <fpeters@0d.be>
Sun, 12 Jul 2020 15:41:26 +0000 (17:41 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sun, 12 Jul 2020 15:41:26 +0000 (17:41 +0200)
nonstop/templates/nonstop/statistics.html
nonstop/views.py

index 7987976b759476e6ce7c48006a9a513d5c1ca10e..19770e6d4331a30d825b791485fdf7171c1057fd 100644 (file)
@@ -23,6 +23,7 @@
 
 <ul>
   <li>Number of tracks: {{zone.stats.count}}{% if from_date %} (new: {{zone.stats.new_files}}, {{zone.stats.percent_new_files}}){% endif %}
+         (duration: {{zone.stats.total_duration}})
   <ul>
   <li>Instru: {{zone.stats.instru}} ({{zone.stats.instru_percentage}})</li>
   <li>SABAM: {{zone.stats.sabam}} ({{zone.stats.sabam_percentage}})</li>
index 20314407ec13dfed273629e3b86ae70baae24676..f30846edf75e686f2b6b1365702aff83739ff6e0 100644 (file)
@@ -10,7 +10,7 @@ from django.core.files.storage import default_storage
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 from django.core.urlresolvers import reverse
 from django.contrib import messages
-from django.db.models import Q
+from django.db.models import Q, Sum
 from django.http import HttpResponse, HttpResponseRedirect, FileResponse
 from django.utils.six import StringIO
 from django.utils.translation import ugettext_lazy as _
@@ -106,6 +106,18 @@ class ZoneStats(object):
             self.qs = self.qs.filter(nonstopfile__somalogline__play_timestamp__lte=until_date)
         self.qs = self.qs.distinct()
 
+    def total_duration(self, **kwargs):
+        total = self.qs.filter(**kwargs).aggregate(Sum('duration'))['duration__sum'].total_seconds()
+        if total > 3600 * 2:
+            duration = _('%d hours') % (total / 3600)
+        else:
+            duration = _('%d minutes') % (total / 60)
+        start = datetime.datetime(2000, 1, 1, self.zone.start.hour, self.zone.start.minute)
+        end = datetime.datetime(2000, 1, 1, self.zone.end.hour, self.zone.end.minute)
+        if end < start:
+            end = end + datetime.timedelta(days=1)
+        return duration + _(', → %d days') % (total // (end - start).total_seconds())
+
     def count(self, **kwargs):
         return self.qs.filter(**kwargs).count()