From 73acef933ff3485987afc5157de022910876fc12 Mon Sep 17 00:00:00 2001
From: Benjamin Bellamy <ben@podlibre.org>
Date: Tue, 3 Nov 2020 14:14:30 +0000
Subject: [PATCH] feat(analytics): add 'other' group to pie charts in order to
 display more accurate data

---
 app/Models/AnalyticsPodcastByCountryModel.php | 40 ++-----------------
 app/Models/AnalyticsPodcastByHourModel.php    |  9 ++---
 app/Models/AnalyticsPodcastByPlayerModel.php  | 35 ++++++++--------
 app/Models/AnalyticsWebsiteByBrowserModel.php |  6 +--
 .../AnalyticsWebsiteByEntryPageModel.php      |  6 +--
 app/Models/AnalyticsWebsiteByRefererModel.php | 18 ++++-----
 app/Views/_assets/modules/Charts.ts           |  5 +++
 7 files changed, 44 insertions(+), 75 deletions(-)

diff --git a/app/Models/AnalyticsPodcastByCountryModel.php b/app/Models/AnalyticsPodcastByCountryModel.php
index ad9bb39585..413da94348 100644
--- a/app/Models/AnalyticsPodcastByCountryModel.php
+++ b/app/Models/AnalyticsPodcastByCountryModel.php
@@ -37,7 +37,7 @@ class AnalyticsPodcastByCountryModel extends Model
                 "{$podcastId}_analytics_podcast_by_country_weekly"
             ))
         ) {
-            $oneWeekAgo=date('Y-m-d', strtotime('-1 week'));
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`country_code` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
@@ -46,24 +46,7 @@ class AnalyticsPodcastByCountryModel extends Model
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(9);
-
-            $found[] = $this->db
-                ->query(
-                    "SELECT
-                        \"Other\" AS `labels`,
-                        SUM(`others`.`values`) AS `values`
-                    FROM
-                        (SELECT SUM(`hits`) AS `values`
-                            FROM {$this->db->prefixTable($this->table)}
-                            WHERE `date` > $oneWeekAgo
-                            GROUP BY `country_code`
-                            ORDER BY `values` DESC
-                            LIMIT 18446744073709551610 OFFSET 9
-                        ) AS `others`
-                    GROUP BY `labels`"
-                )
-                ->getRow(0);
+                ->findAll();
 
             cache()->save(
                 "{$podcastId}_analytics_podcast_by_country_weekly",
@@ -97,24 +80,7 @@ class AnalyticsPodcastByCountryModel extends Model
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
-            $found[] = $this->db
-                ->query(
-                    "SELECT
-                        \"Other\" AS `labels`,
-                        SUM(`others`.`values`) AS `values`
-                    FROM
-                        (SELECT SUM(`hits`) AS `values`
-                            FROM {$this->db->prefixTable($this->table)}
-                            WHERE `date` > $oneyearago
-                            GROUP BY `country_code`
-                            ORDER BY `values` DESC
-                            LIMIT 18446744073709551610 OFFSET 9
-                        ) AS `others`
-                    GROUP BY `labels`"
-                )
-                ->getRow(0);
+                ->findAll();
 
             cache()->save(
                 "{$podcastId}_analytics_podcast_by_country_yearly",
diff --git a/app/Models/AnalyticsPodcastByHourModel.php b/app/Models/AnalyticsPodcastByHourModel.php
index 1960e7836e..2d65209db0 100644
--- a/app/Models/AnalyticsPodcastByHourModel.php
+++ b/app/Models/AnalyticsPodcastByHourModel.php
@@ -33,7 +33,9 @@ class AnalyticsPodcastByHourModel extends Model
     public function getData(int $podcastId): array
     {
         if (!($found = cache("{$podcastId}_analytics_podcasts_by_hour"))) {
-            $found = $this->select('`hour` as `labels`')
+            $found = $this->select(
+                'right(concat(\'0\',`hour`,\'h\'),3) as `labels`'
+            )
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
@@ -51,9 +53,4 @@ class AnalyticsPodcastByHourModel extends Model
         }
         return $found;
     }
-
-
-
-
-    
 }
diff --git a/app/Models/AnalyticsPodcastByPlayerModel.php b/app/Models/AnalyticsPodcastByPlayerModel.php
index 32dc29d5af..0652c8fbd4 100644
--- a/app/Models/AnalyticsPodcastByPlayerModel.php
+++ b/app/Models/AnalyticsPodcastByPlayerModel.php
@@ -37,18 +37,18 @@ class AnalyticsPodcastByPlayerModel extends Model
                 "{$podcastId}_analytics_podcasts_by_player_by_service_weekly"
             ))
         ) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`service` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
                     '`service` !=' => '',
                     '`is_bot`' => 0,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_by_service_weekly",
                 $found,
@@ -72,18 +72,18 @@ class AnalyticsPodcastByPlayerModel extends Model
                 "{$podcastId}_analytics_podcasts_by_player_by_app_weekly"
             ))
         ) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`app` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
                     '`app` !=' => '',
                     '`is_bot`' => 0,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_by_app_weekly",
                 $found,
@@ -107,18 +107,18 @@ class AnalyticsPodcastByPlayerModel extends Model
                 "{$podcastId}_analytics_podcasts_by_player_by_app_yearly"
             ))
         ) {
+            $oneYearAgo = date('Y-m-d', strtotime('-1 year'));
             $found = $this->select('`app` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
                     '`app` !=' => '',
                     '`is_bot`' => 0,
-                    '`date` >' => date('Y-m-d', strtotime('-1 year')),
+                    '`date` >' => $oneYearAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_by_app_yearly",
                 $found,
@@ -142,6 +142,7 @@ class AnalyticsPodcastByPlayerModel extends Model
                 "{$podcastId}_analytics_podcasts_by_player_by_os_weekly"
             ))
         ) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`os` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
@@ -149,12 +150,11 @@ class AnalyticsPodcastByPlayerModel extends Model
                     '`app` !=' => '',
                     '`os` !=' => '',
                     '`is_bot`' => 0,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_by_os_weekly",
                 $found,
@@ -178,18 +178,18 @@ class AnalyticsPodcastByPlayerModel extends Model
                 "{$podcastId}_analytics_podcasts_by_player_by_device_weekly"
             ))
         ) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`device` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
                     '`device` !=' => '',
                     '`is_bot`' => 0,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_by_device_weekly",
                 $found,
@@ -211,16 +211,17 @@ class AnalyticsPodcastByPlayerModel extends Model
         if (
             !($found = cache("{$podcastId}_analytics_podcasts_by_player_bots"))
         ) {
+            $oneYearAgo = date('Y-m-d', strtotime('-1 year'));
             $found = $this->select('DATE_FORMAT(`date`,"%Y-%m-01") as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
                     '`is_bot`' => 1,
-                    '`date` >' => date('Y-m-d', strtotime('-1 year')),
+                    '`date` >' => $oneYearAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`labels`', 'ASC')
-                ->findAll(10);
+                ->findAll();
 
             cache()->save(
                 "{$podcastId}_analytics_podcasts_by_player_bots",
diff --git a/app/Models/AnalyticsWebsiteByBrowserModel.php b/app/Models/AnalyticsWebsiteByBrowserModel.php
index 701818e090..d2da9b3ac8 100644
--- a/app/Models/AnalyticsWebsiteByBrowserModel.php
+++ b/app/Models/AnalyticsWebsiteByBrowserModel.php
@@ -33,16 +33,16 @@ class AnalyticsWebsiteByBrowserModel extends Model
     public function getData(int $podcastId): array
     {
         if (!($found = cache("{$podcastId}_analytics_website_by_browser"))) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`browser` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_website_by_browser",
                 $found,
diff --git a/app/Models/AnalyticsWebsiteByEntryPageModel.php b/app/Models/AnalyticsWebsiteByEntryPageModel.php
index 4365a9de41..ad19f6a698 100644
--- a/app/Models/AnalyticsWebsiteByEntryPageModel.php
+++ b/app/Models/AnalyticsWebsiteByEntryPageModel.php
@@ -33,18 +33,18 @@ class AnalyticsWebsiteByEntryPageModel extends Model
     public function getData(int $podcastId): array
     {
         if (!($found = cache("{$podcastId}_analytics_website_by_entry_page"))) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select(
                 'IF(`entry_page_url`=\'/\',\'/\',SUBSTRING_INDEX(`entry_page_url`,\'/\',-1)) as `labels`'
             )
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_website_by_entry_page",
                 $found,
diff --git a/app/Models/AnalyticsWebsiteByRefererModel.php b/app/Models/AnalyticsWebsiteByRefererModel.php
index 045b85142a..570f2ec8e5 100644
--- a/app/Models/AnalyticsWebsiteByRefererModel.php
+++ b/app/Models/AnalyticsWebsiteByRefererModel.php
@@ -33,16 +33,16 @@ class AnalyticsWebsiteByRefererModel extends Model
     public function getData(int $podcastId): array
     {
         if (!($found = cache("{$podcastId}_analytics_website_by_referer"))) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select('`referer_url` as `labels`')
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_website_by_referer",
                 $found,
@@ -64,18 +64,18 @@ class AnalyticsWebsiteByRefererModel extends Model
         if (
             !($found = cache("{$podcastId}_analytics_website_by_domain_weekly"))
         ) {
+            $oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
             $found = $this->select(
                 'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`'
             )
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
-                    '`date` >' => date('Y-m-d', strtotime('-1 week')),
+                    '`date` >' => $oneWeekAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_website_by_domain_weekly",
                 $found,
@@ -97,18 +97,18 @@ class AnalyticsWebsiteByRefererModel extends Model
         if (
             !($found = cache("{$podcastId}_analytics_website_by_domain_yearly"))
         ) {
+            $oneYearAgo = date('Y-m-d', strtotime('-1 year'));
             $found = $this->select(
                 'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`'
             )
                 ->selectSum('`hits`', '`values`')
                 ->where([
                     '`podcast_id`' => $podcastId,
-                    '`date` >' => date('Y-m-d', strtotime('-1 year')),
+                    '`date` >' => $oneYearAgo,
                 ])
                 ->groupBy('`labels`')
                 ->orderBy('`values`', 'DESC')
-                ->findAll(10);
-
+                ->findAll();
             cache()->save(
                 "{$podcastId}_analytics_website_by_domain_yearly",
                 $found,
diff --git a/app/Views/_assets/modules/Charts.ts b/app/Views/_assets/modules/Charts.ts
index 9e2f754e94..777a0d8e1a 100644
--- a/app/Views/_assets/modules/Charts.ts
+++ b/app/Views/_assets/modules/Charts.ts
@@ -3,6 +3,7 @@ import am4geodata_worldLow from "@amcharts/amcharts4-geodata/worldLow";
 import * as am4charts from "@amcharts/amcharts4/charts";
 import * as am4core from "@amcharts/amcharts4/core";
 import * as am4maps from "@amcharts/amcharts4/maps";
+import * as am4plugins_sliceGrouper from "@amcharts/amcharts4/plugins/sliceGrouper"; 
 import am4themes_material from "@amcharts/amcharts4/themes/material";
 
 const drawPieChart = (chartDivId: string, dataUrl: string | null): void => {
@@ -20,6 +21,10 @@ const drawPieChart = (chartDivId: string, dataUrl: string | null): void => {
   chart.dataSource.parser.options.emptyAs = 0;
   // Add and configure Series
   const pieSeries = chart.series.push(new am4charts.PieSeries());
+  const grouper = pieSeries.plugins.push(new am4plugins_sliceGrouper.SliceGrouper());
+  grouper.limit = 9;
+  grouper.groupName = "- Other -";
+  grouper.clickBehavior = "break";
   pieSeries.dataFields.value = "values";
   pieSeries.dataFields.category = "labels";
   pieSeries.slices.template.stroke = am4core.color("#ffffff");
-- 
GitLab