From e15cf498f5bad3b168dbb0b122c60199ebb8c506 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Sun, 1 Jan 2023 23:45:56 +0100
Subject: [PATCH] Datovy model pro statistiku

---
 lib/PZ/Schema/Result/Shortcut.pm   | 11 +++++++++++
 lib/PZ/Schema/Result/StatDaily.pm  | 22 ++++++++++++++++++++++
 lib/PZ/Schema/Result/StatHourly.pm | 22 ++++++++++++++++++++++
 sql/migrations.sql                 | 21 +++++++++++++++++++++
 4 files changed, 76 insertions(+)
 create mode 100644 lib/PZ/Schema/Result/StatDaily.pm
 create mode 100644 lib/PZ/Schema/Result/StatHourly.pm

diff --git a/lib/PZ/Schema/Result/Shortcut.pm b/lib/PZ/Schema/Result/Shortcut.pm
index 11d3022..372b6f8 100644
--- a/lib/PZ/Schema/Result/Shortcut.pm
+++ b/lib/PZ/Schema/Result/Shortcut.pm
@@ -41,5 +41,16 @@ __PACKAGE__->has_many(
     log_items => 'PZ::Schema::Result::Log',
     { 'foreign.shortcut_id' => 'self.id', },
 );
+
+__PACKAGE__->has_many(
+    stat_hourly_items => 'PZ::Schema::Result::StatHourly',
+    { 'foreign.shortcut_id' => 'self.id', },
+);
+
+__PACKAGE__->has_many(
+    stat_dayly_items => 'PZ::Schema::Result::StatDayly',
+    { 'foreign.shortcut_id' => 'self.id', },
+);
+
 1;
 
diff --git a/lib/PZ/Schema/Result/StatDaily.pm b/lib/PZ/Schema/Result/StatDaily.pm
new file mode 100644
index 0000000..3eb6f61
--- /dev/null
+++ b/lib/PZ/Schema/Result/StatDaily.pm
@@ -0,0 +1,22 @@
+package PZ::Schema::Result::StatDaily;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+our $VERSION = 1;
+
+__PACKAGE__->table('stat_daily');
+
+__PACKAGE__->add_columns(
+    qw(
+        shortcut_id
+        period
+        count
+    ),
+);
+
+__PACKAGE__->set_primary_key('shortcut_id', 'period');
+
+1;
diff --git a/lib/PZ/Schema/Result/StatHourly.pm b/lib/PZ/Schema/Result/StatHourly.pm
new file mode 100644
index 0000000..e73c206
--- /dev/null
+++ b/lib/PZ/Schema/Result/StatHourly.pm
@@ -0,0 +1,22 @@
+package PZ::Schema::Result::StatHourly;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+our $VERSION = 1;
+
+__PACKAGE__->table('stat_hourly');
+
+__PACKAGE__->add_columns(
+    qw(
+        shortcut_id
+        period
+        count
+    ),
+);
+
+__PACKAGE__->set_primary_key('shortcut_id', 'period');
+
+1;
diff --git a/sql/migrations.sql b/sql/migrations.sql
index 9dcaaff..0a31e8d 100644
--- a/sql/migrations.sql
+++ b/sql/migrations.sql
@@ -50,3 +50,24 @@ alter table "log" add column "ua" text;
 
 -- 4 up
 alter table "log" add column "referrer" text;
+
+-- 5 up
+create view "stat_hourly" as
+select
+    "shortcut_id",
+    date_trunc('hour', "time") as "period",
+    count(*)
+from "log"
+    group by "shortcut_id", date_trunc('hour', "time")
+    order by date_trunc('hour', "time")
+;
+
+create view "stat_daily" as
+select
+    "shortcut_id",
+    date_trunc('day', "time") as "period",
+    count(*)
+from "log"
+    group by "shortcut_id", date_trunc('day', "time")
+    order by date_trunc('day', "time")
+;
-- 
GitLab