Commit 75f92077 authored by fred's avatar fred

add filtering options to stats query string

parent ca102b4c
......@@ -3,6 +3,8 @@
{% block appbar %}
<h2>Nonstop - Statistics</h2>
<a href="?from=-7d">during last week</a>
<a href="?from=-30d">during last month</a>
{% endblock %}
{% block content %}
......@@ -12,7 +14,7 @@
<h3>{{ zone.title }} <span>({{zone.start}} → {{zone.end}})</span></h3>
<ul>
<li>Number of tracks: {{zone.stats.count}}
<li>Number of tracks: {{zone.stats.count}}{% if from_date %} (new: {{zone.stats.new_files}}, {{zone.stats.percent_new_files}}){% endif %}
<ul>
<li>Instru: {{zone.stats.instru}} ({{zone.stats.instru_percentage}})</li>
<li>SABAM: {{zone.stats.sabam}} ({{zone.stats.sabam_percentage}})</li>
......
......@@ -36,7 +36,7 @@ class SomaDayArchiveCsvView(SomaDayArchiveView):
else:
writer.writerow([line.play_timestamp.strftime('%Y-%m-%d %H:%M'),
line.filepath.short.encode('utf-8', 'replace')])
return HttpResponse(out.getvalue(), mimetype='text/csv; charset=utf-8')
return HttpResponse(out.getvalue(), content_type='text/csv; charset=utf-8')
class RedirectTodayView(RedirectView):
......@@ -61,11 +61,18 @@ class ArtistListView(ListView):
class ZoneStats(object):
def __init__(self, zone):
def __init__(self, zone, from_date=None, until_date=None, **kwargs):
self.zone = zone
self.qs = Track.objects.filter(nonstop_zones=self.zone, **kwargs)
self.from_date = from_date
if from_date:
self.qs = self.qs.filter(nonstopfile__somalogline__play_timestamp__gte=from_date)
if until_date:
self.qs = self.qs.filter(nonstopfile__somalogline__play_timestamp__lte=until_date)
self.qs = self.qs.distinct()
def count(self, **kwargs):
return Track.objects.filter(nonstop_zones=self.zone, **kwargs).count()
return self.qs.filter(**kwargs).count()
def percentage(self, **kwargs):
total = self.count()
......@@ -100,13 +107,32 @@ class ZoneStats(object):
return '-'
return '%.2f%%' % (100. * self.french() / considered_tracks)
def new_files(self):
return self.count(nonstopfile__creation_timestamp__gte=self.from_date)
def percent_new_files(self):
return self.percentage(nonstopfile__creation_timestamp__gte=self.from_date)
def parse_date(date):
if date.endswith('d'):
return datetime.datetime.today() + datetime.timedelta(int(date.rstrip('d')))
return datetime.datetime.strptime(date, '%Y-%m-%d').date()
class StatisticsView(TemplateView):
template_name = 'nonstop/statistics.html'
def get_context_data(self, **kwargs):
context = super(StatisticsView, self).get_context_data(**kwargs)
context['zones'] = Nonstop.objects.all().order_by('start')
kwargs = {}
if 'from' in self.request.GET:
kwargs['from_date'] = parse_date(self.request.GET['from'])
context['from_date'] = kwargs['from_date']
if 'until' in self.request.GET:
kwargs['until_date'] = parse_date(self.request.GET['until'])
if 'onair' in self.request.GET:
kwargs['nonstopfile__somalogline__on_air'] = True
for zone in context['zones']:
zone.stats = ZoneStats(zone)
zone.stats = ZoneStats(zone, **kwargs)
return context
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment