Add daily visits stats.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 14 Jun 2021 08:56:49 +0000 (10:56 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 14 Jun 2021 08:56:49 +0000 (10:56 +0200)
src/stats/management/commands/populate_visits.py [new file with mode: 0644]
src/stats/migrations/0005_dayvisits.py [new file with mode: 0644]
src/stats/models.py

diff --git a/src/stats/management/commands/populate_visits.py b/src/stats/management/commands/populate_visits.py
new file mode 100644 (file)
index 0000000..efe2500
--- /dev/null
@@ -0,0 +1,25 @@
+from datetime import date, timedelta
+from django.core.management.base import BaseCommand
+from stats.models import Visits, DayVisits
+
+
+class Command(BaseCommand):
+    def add_arguments(self, parser):
+        parser.add_argument(
+            '-s' ,'--since', metavar='YYYY-MM-DD',
+            required=True
+        )
+        parser.add_argument(
+            '-u' ,'--until', metavar='YYYY-MM-DD',
+            required=True
+        )
+
+        
+    def handle(self, **options):
+        since = date(*[int(p) for p in options['since'].split('-')])
+        until = date(*[int(p) for p in options['until'].split('-')])
+        while since < until:
+            print(since)
+            DayVisits.build_day(since)
+            since += timedelta(1)
+
diff --git a/src/stats/migrations/0005_dayvisits.py b/src/stats/migrations/0005_dayvisits.py
new file mode 100644 (file)
index 0000000..0ebf03a
--- /dev/null
@@ -0,0 +1,28 @@
+# Generated by Django 2.2.19 on 2021-06-14 08:56
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0032_collection_listed'),
+        ('stats', '0004_auto_20210601_1303'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='DayVisits',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('date', models.DateField()),
+                ('views', models.IntegerField()),
+                ('unique_views', models.IntegerField()),
+                ('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catalogue.Book')),
+            ],
+            options={
+                'abstract': False,
+            },
+        ),
+    ]
index e95e759..ca0b55d 100644 (file)
@@ -5,18 +5,21 @@ from django.conf import settings
 from django.db import models
 
 
-class Visits(models.Model):
+class VisitsBase(models.Model):
     book = models.ForeignKey('catalogue.Book', models.CASCADE)
     date = models.DateField()
     views = models.IntegerField()
     unique_views = models.IntegerField()
 
+    class Meta:
+        abstract = True
+
     @classmethod
-    def build_month(cls, year, month):
+    def build_for_date(cls, date, period):
         Book = apps.get_model('catalogue', 'Book')
 
-        date = f'{year}-{month:02d}-01'
-        url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period=month&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1'
+        date = date.isoformat()
+        url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period={period}&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1'
         data = urlopen(url).read().decode('utf-16')
         lines = data.split('\n')[1:]
         for line in lines:
@@ -35,3 +38,16 @@ class Visits(models.Model):
                         book=book, date=date,
                         defaults={'views': views, 'unique_views': uviews}
                     )
+
+
+class Visits(VisitsBase):
+    @classmethod
+    def build_month(cls, date):
+        cls.build_for_date(date.replace(day=1), 'month')
+
+
+class DayVisits(VisitsBase):
+    @classmethod
+    def build_day(cls, date):
+        cls.build_for_date(date, 'day')
+