add view to generate munin data
authorFrédéric Péters <fpeters@0d.be>
Sat, 18 Jul 2020 16:15:55 +0000 (18:15 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sat, 18 Jul 2020 16:16:36 +0000 (18:16 +0200)
nonstop/templates/nonstop/munin_tracks.txt [new file with mode: 0644]
nonstop/urls.py
nonstop/views.py

diff --git a/nonstop/templates/nonstop/munin_tracks.txt b/nonstop/templates/nonstop/munin_tracks.txt
new file mode 100644 (file)
index 0000000..015b4a4
--- /dev/null
@@ -0,0 +1,53 @@
+{% load l10n %}{% localize off %}
+{% if view.kwargs.config %}
+multigraph nonstop_general
+graph_title Nonstop Tracks
+graph_category Nonstop
+nonstop_general_total.label Total
+nonstop_general_active.label Active
+
+multigraph nonstop_percentages
+graph_title Nonstop Tracks Percentages
+graph_category Nonstop
+graph_args --upper-limit 100 -l 0
+graph_vlabel %
+graph_scale no
+nonstop_percentages_instru.label (%instru)
+nonstop_percentages_cfwb.label (%cfwb)
+nonstop_percentages_french.label (%french)
+nonstop_percentages_langset.label (%language set)
+
+{% for zone in zones %}
+multigraph nonstop_{{zone.slug|cut:"-"}}
+graph_title Nonstop Tracks ({{zone.title|safe}})
+graph_category Nonstop
+nonstop_{{zone.slug|cut:"-"}}_total.label (total)
+nonstop_{{zone.slug|cut:"-"}}_instru.label (instru)
+nonstop_{{zone.slug|cut:"-"}}_cfwb.label (cfwb)
+nonstop_{{zone.slug|cut:"-"}}_french.label (french)
+nonstop_{{zone.slug|cut:"-"}}_langset.label (language set)
+{% endfor %}
+
+{% else %}{# actual stats #}
+
+multigraph nonstop_general
+nonstop_general_total.value {{nonstop_general_total}}
+nonstop_general_active.value {{nonstop_general_active}}
+
+multigraph nonstop_percentages
+nonstop_percentages_instru.value {{nonstop_percentages_instru}}
+nonstop_percentages_cfwb.value {{nonstop_percentages_cfwb}}
+nonstop_percentages_french.value {{nonstop_percentages_french}}
+nonstop_percentages_langset.value {{nonstop_percentages_langset}}
+
+{% for zone in zones %}
+multigraph nonstop_{{zone.slug|cut:"-"}}
+nonstop_{{zone.slug|cut:"-"}}_total.value {{zone.stats.count}}
+nonstop_{{zone.slug|cut:"-"}}_instru.value {{zone.stats.instru}}
+nonstop_{{zone.slug|cut:"-"}}_cfwb.value {{zone.stats.cfwb}}
+nonstop_{{zone.slug|cut:"-"}}_french.value {{zone.stats.french}}
+nonstop_{{zone.slug|cut:"-"}}_langset.value {{zone.stats.language_set}}
+{% endfor %}
+
+{% endif %}
+{% endlocalize %}
index 390f604..b34c3b9 100644 (file)
@@ -8,7 +8,8 @@ from .views import (SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView
         QuickLinksView, SearchView, CleanupView, ArtistTracksMetadataView,
         SearchCsvView, AddSomaDiffusionView, DelSomaDiffusionView,
         DiffusionPropertiesView, AjaxProgram, ZoneSettings,
-        jingle_audio_view)
+        jingle_audio_view,
+        MuninTracks)
 
 urlpatterns = [
     url(r'^$', QuickLinksView.as_view(), name='nonstop-quick-links'),
@@ -48,3 +49,7 @@ urlpatterns = [
     # ajax parts
     url(r'^ajax/program/(?P<date>[\d-]*)$', AjaxProgram.as_view(), name='nonstop-ajax-program'),
 ]
+
+public_urlpatterns = [
+    url(r'^api/munin/tracks/(?P<config>config/)?$', MuninTracks.as_view()),
+]
index 481ed13..c8218cf 100644 (file)
@@ -151,7 +151,10 @@ class ZoneStats(object):
     def cfwb_percentage(self):
         return self.percentage(cfwb=True)
 
-    def unset_language(self):
+    def language_set(self):
+        return self.count() - self.language_unset()
+
+    def language_unset(self):
         return self.count(language='')
 
     def unset_language_percentage(self):
@@ -197,6 +200,7 @@ def parse_date(date):
         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'
 
@@ -578,3 +582,27 @@ class ZoneSettings(FormView):
         zone.save()
         zone_settings.save()
         return super().form_valid(form)
+
+
+class MuninTracks(StatisticsView):
+    template_name = 'nonstop/munin_tracks.txt'
+    content_type = 'text/plain; charset=utf-8'
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+        context['nonstop_general_total'] = Track.objects.count()
+        active_tracks_qs = Track.objects.filter(nonstop_zones__isnull=False).distinct()
+        context['nonstop_general_active'] = active_tracks_qs.count()
+        context['nonstop_percentages_instru'] = 100 * (
+                active_tracks_qs.filter(instru=True).count() /
+                context['nonstop_general_active'])
+        context['nonstop_percentages_cfwb'] = 100 * (
+                active_tracks_qs.filter(cfwb=True).count() /
+                context['nonstop_general_active'])
+        context['nonstop_percentages_langset'] = 100 * (
+                active_tracks_qs.exclude(language='').count() /
+                context['nonstop_general_active'])
+        context['nonstop_percentages_french'] = 100 * (
+                active_tracks_qs.filter(language='fr').count() /
+                active_tracks_qs.exclude(language__isnull=True).exclude(language__in=('', 'na')).count())
+        return context