Commit 6e48764c authored by fred's avatar fred

add stats module

parent a94071fc
......@@ -136,6 +136,7 @@ INSTALLED_APPS = (
'nonstop',
'panikdb.aa',
'panikdb.customtags',
'panikdb.stats',
'gadjo',
'combo.data',
'combo.manager',
......
import datetime
import json
import urllib2
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from panikdb.stats.models import SoundFile, DailyStat
class Command(BaseCommand):
def handle(self, verbosity, **kwargs):
self.verbose = (verbosity > 1)
piwik_api_href = settings.PIWIK_API_HREF
piwik_token_auth = settings.PIWIK_TOKEN_AUTH
piwik_site_id = settings.PIWIK_SITE_ID
url = '%(piwik_api_href)s?module=API&method=Events.getAction&'\
'secondaryDimension=eventName&idSite=%(piwik_site_id)s&'\
'period=day&date=last2&format=json&'\
'token_auth=%(piwik_token_auth)s&expanded=1' % locals()
request = urllib2.Request(url)
request.add_header('Accept', 'application/json')
if self.verbose:
print 'calling piwik'
result = json.load(urllib2.urlopen(request))
if self.verbose:
print 'collecting results'
for day in result:
day_datetime = datetime.datetime.strptime(day, '%Y-%m-%d')
for stat_segment in result.get(day):
if stat_segment.get('segment') != 'eventAction==Play':
continue
for entry in stat_segment.get('subtable'):
sound_id = entry.get('label').split(':')[0]
try:
soundfile = SoundFile.objects.get(id=sound_id)
except (SoundFile.DoesNotExist, ValueError):
if self.verbose:
print 'failed to process', entry.get('label')
continue
try:
stat = DailyStat.objects.get(soundfile=soundfile,
day=day_datetime)
except DailyStat.DoesNotExist:
stat = DailyStat(soundfile=soundfile, day=day_datetime)
for attribute in ('nb_events', 'nb_visits'):
setattr(stat, attribute, entry.get(attribute))
stat.save()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('emissions', '0003_newsitem_event_date'),
]
operations = [
migrations.CreateModel(
name='DailyStat',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('day', models.DateField()),
('nb_events', models.IntegerField()),
('nb_visits', models.IntegerField()),
('soundfile', models.ForeignKey(to='emissions.SoundFile')),
],
options={
},
bases=(models.Model,),
),
]
from django.db import models
from emissions.models import SoundFile
class DailyStat(models.Model):
soundfile = models.ForeignKey(SoundFile)
day = models.DateField()
nb_events = models.IntegerField()
nb_visits = models.IntegerField()
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