Commit 75f92077 authored by fred's avatar fred

add filtering options to stats query string

parent ca102b4c
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
{% block appbar %} {% block appbar %}
<h2>Nonstop - Statistics</h2> <h2>Nonstop - Statistics</h2>
<a href="?from=-7d">during last week</a>
<a href="?from=-30d">during last month</a>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
...@@ -12,7 +14,7 @@ ...@@ -12,7 +14,7 @@
<h3>{{ zone.title }} <span>({{zone.start}} → {{zone.end}})</span></h3> <h3>{{ zone.title }} <span>({{zone.start}} → {{zone.end}})</span></h3>
<ul> <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> <ul>
<li>Instru: {{zone.stats.instru}} ({{zone.stats.instru_percentage}})</li> <li>Instru: {{zone.stats.instru}} ({{zone.stats.instru_percentage}})</li>
<li>SABAM: {{zone.stats.sabam}} ({{zone.stats.sabam_percentage}})</li> <li>SABAM: {{zone.stats.sabam}} ({{zone.stats.sabam_percentage}})</li>
......
...@@ -36,7 +36,7 @@ class SomaDayArchiveCsvView(SomaDayArchiveView): ...@@ -36,7 +36,7 @@ class SomaDayArchiveCsvView(SomaDayArchiveView):
else: else:
writer.writerow([line.play_timestamp.strftime('%Y-%m-%d %H:%M'), writer.writerow([line.play_timestamp.strftime('%Y-%m-%d %H:%M'),
line.filepath.short.encode('utf-8', 'replace')]) 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): class RedirectTodayView(RedirectView):
...@@ -61,11 +61,18 @@ class ArtistListView(ListView): ...@@ -61,11 +61,18 @@ class ArtistListView(ListView):
class ZoneStats(object): class ZoneStats(object):
def __init__(self, zone): def __init__(self, zone, from_date=None, until_date=None, **kwargs):
self.zone = zone 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): def count(self, **kwargs):
return Track.objects.filter(nonstop_zones=self.zone, **kwargs).count() return self.qs.filter(**kwargs).count()
def percentage(self, **kwargs): def percentage(self, **kwargs):
total = self.count() total = self.count()
...@@ -100,13 +107,32 @@ class ZoneStats(object): ...@@ -100,13 +107,32 @@ class ZoneStats(object):
return '-' return '-'
return '%.2f%%' % (100. * self.french() / considered_tracks) 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): class StatisticsView(TemplateView):
template_name = 'nonstop/statistics.html' template_name = 'nonstop/statistics.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(StatisticsView, self).get_context_data(**kwargs) context = super(StatisticsView, self).get_context_data(**kwargs)
context['zones'] = Nonstop.objects.all().order_by('start') 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']: for zone in context['zones']:
zone.stats = ZoneStats(zone) zone.stats = ZoneStats(zone, **kwargs)
return context 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