feat(analytics): add 'other' group to pie charts in order to display more accurate data

This commit is contained in:
Benjamin Bellamy 2020-11-03 14:14:30 +00:00
parent ba89fd590d
commit 73acef933f
7 changed files with 44 additions and 75 deletions

View File

@ -37,7 +37,7 @@ class AnalyticsPodcastByCountryModel extends Model
"{$podcastId}_analytics_podcast_by_country_weekly" "{$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`') $found = $this->select('`country_code` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
@ -46,24 +46,7 @@ class AnalyticsPodcastByCountryModel extends Model
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(9); ->findAll();
$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);
cache()->save( cache()->save(
"{$podcastId}_analytics_podcast_by_country_weekly", "{$podcastId}_analytics_podcast_by_country_weekly",
@ -97,24 +80,7 @@ class AnalyticsPodcastByCountryModel extends Model
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
$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);
cache()->save( cache()->save(
"{$podcastId}_analytics_podcast_by_country_yearly", "{$podcastId}_analytics_podcast_by_country_yearly",

View File

@ -33,7 +33,9 @@ class AnalyticsPodcastByHourModel extends Model
public function getData(int $podcastId): array public function getData(int $podcastId): array
{ {
if (!($found = cache("{$podcastId}_analytics_podcasts_by_hour"))) { 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`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
@ -51,9 +53,4 @@ class AnalyticsPodcastByHourModel extends Model
} }
return $found; return $found;
} }
} }

View File

@ -37,18 +37,18 @@ class AnalyticsPodcastByPlayerModel extends Model
"{$podcastId}_analytics_podcasts_by_player_by_service_weekly" "{$podcastId}_analytics_podcasts_by_player_by_service_weekly"
)) ))
) { ) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`service` as `labels`') $found = $this->select('`service` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`service` !=' => '', '`service` !=' => '',
'`is_bot`' => 0, '`is_bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_service_weekly", "{$podcastId}_analytics_podcasts_by_player_by_service_weekly",
$found, $found,
@ -72,18 +72,18 @@ class AnalyticsPodcastByPlayerModel extends Model
"{$podcastId}_analytics_podcasts_by_player_by_app_weekly" "{$podcastId}_analytics_podcasts_by_player_by_app_weekly"
)) ))
) { ) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`app` as `labels`') $found = $this->select('`app` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`app` !=' => '', '`app` !=' => '',
'`is_bot`' => 0, '`is_bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_app_weekly", "{$podcastId}_analytics_podcasts_by_player_by_app_weekly",
$found, $found,
@ -107,18 +107,18 @@ class AnalyticsPodcastByPlayerModel extends Model
"{$podcastId}_analytics_podcasts_by_player_by_app_yearly" "{$podcastId}_analytics_podcasts_by_player_by_app_yearly"
)) ))
) { ) {
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));
$found = $this->select('`app` as `labels`') $found = $this->select('`app` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`app` !=' => '', '`app` !=' => '',
'`is_bot`' => 0, '`is_bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 year')), '`date` >' => $oneYearAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_app_yearly", "{$podcastId}_analytics_podcasts_by_player_by_app_yearly",
$found, $found,
@ -142,6 +142,7 @@ class AnalyticsPodcastByPlayerModel extends Model
"{$podcastId}_analytics_podcasts_by_player_by_os_weekly" "{$podcastId}_analytics_podcasts_by_player_by_os_weekly"
)) ))
) { ) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`os` as `labels`') $found = $this->select('`os` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
@ -149,12 +150,11 @@ class AnalyticsPodcastByPlayerModel extends Model
'`app` !=' => '', '`app` !=' => '',
'`os` !=' => '', '`os` !=' => '',
'`is_bot`' => 0, '`is_bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_os_weekly", "{$podcastId}_analytics_podcasts_by_player_by_os_weekly",
$found, $found,
@ -178,18 +178,18 @@ class AnalyticsPodcastByPlayerModel extends Model
"{$podcastId}_analytics_podcasts_by_player_by_device_weekly" "{$podcastId}_analytics_podcasts_by_player_by_device_weekly"
)) ))
) { ) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`device` as `labels`') $found = $this->select('`device` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`device` !=' => '', '`device` !=' => '',
'`is_bot`' => 0, '`is_bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_device_weekly", "{$podcastId}_analytics_podcasts_by_player_by_device_weekly",
$found, $found,
@ -211,16 +211,17 @@ class AnalyticsPodcastByPlayerModel extends Model
if ( if (
!($found = cache("{$podcastId}_analytics_podcasts_by_player_bots")) !($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`') $found = $this->select('DATE_FORMAT(`date`,"%Y-%m-01") as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`is_bot`' => 1, '`is_bot`' => 1,
'`date` >' => date('Y-m-d', strtotime('-1 year')), '`date` >' => $oneYearAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`labels`', 'ASC') ->orderBy('`labels`', 'ASC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_podcasts_by_player_bots", "{$podcastId}_analytics_podcasts_by_player_bots",

View File

@ -33,16 +33,16 @@ class AnalyticsWebsiteByBrowserModel extends Model
public function getData(int $podcastId): array public function getData(int $podcastId): array
{ {
if (!($found = cache("{$podcastId}_analytics_website_by_browser"))) { if (!($found = cache("{$podcastId}_analytics_website_by_browser"))) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`browser` as `labels`') $found = $this->select('`browser` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_website_by_browser", "{$podcastId}_analytics_website_by_browser",
$found, $found,

View File

@ -33,18 +33,18 @@ class AnalyticsWebsiteByEntryPageModel extends Model
public function getData(int $podcastId): array public function getData(int $podcastId): array
{ {
if (!($found = cache("{$podcastId}_analytics_website_by_entry_page"))) { if (!($found = cache("{$podcastId}_analytics_website_by_entry_page"))) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select( $found = $this->select(
'IF(`entry_page_url`=\'/\',\'/\',SUBSTRING_INDEX(`entry_page_url`,\'/\',-1)) as `labels`' 'IF(`entry_page_url`=\'/\',\'/\',SUBSTRING_INDEX(`entry_page_url`,\'/\',-1)) as `labels`'
) )
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_website_by_entry_page", "{$podcastId}_analytics_website_by_entry_page",
$found, $found,

View File

@ -33,16 +33,16 @@ class AnalyticsWebsiteByRefererModel extends Model
public function getData(int $podcastId): array public function getData(int $podcastId): array
{ {
if (!($found = cache("{$podcastId}_analytics_website_by_referer"))) { if (!($found = cache("{$podcastId}_analytics_website_by_referer"))) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`referer_url` as `labels`') $found = $this->select('`referer_url` as `labels`')
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_website_by_referer", "{$podcastId}_analytics_website_by_referer",
$found, $found,
@ -64,18 +64,18 @@ class AnalyticsWebsiteByRefererModel extends Model
if ( if (
!($found = cache("{$podcastId}_analytics_website_by_domain_weekly")) !($found = cache("{$podcastId}_analytics_website_by_domain_weekly"))
) { ) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select( $found = $this->select(
'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`' 'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`'
) )
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 week')), '`date` >' => $oneWeekAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_website_by_domain_weekly", "{$podcastId}_analytics_website_by_domain_weekly",
$found, $found,
@ -97,18 +97,18 @@ class AnalyticsWebsiteByRefererModel extends Model
if ( if (
!($found = cache("{$podcastId}_analytics_website_by_domain_yearly")) !($found = cache("{$podcastId}_analytics_website_by_domain_yearly"))
) { ) {
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));
$found = $this->select( $found = $this->select(
'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`' 'SUBSTRING_INDEX(`domain`, \'.\', -2) as `labels`'
) )
->selectSum('`hits`', '`values`') ->selectSum('`hits`', '`values`')
->where([ ->where([
'`podcast_id`' => $podcastId, '`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 year')), '`date` >' => $oneYearAgo,
]) ])
->groupBy('`labels`') ->groupBy('`labels`')
->orderBy('`values`', 'DESC') ->orderBy('`values`', 'DESC')
->findAll(10); ->findAll();
cache()->save( cache()->save(
"{$podcastId}_analytics_website_by_domain_yearly", "{$podcastId}_analytics_website_by_domain_yearly",
$found, $found,

View File

@ -3,6 +3,7 @@ import am4geodata_worldLow from "@amcharts/amcharts4-geodata/worldLow";
import * as am4charts from "@amcharts/amcharts4/charts"; import * as am4charts from "@amcharts/amcharts4/charts";
import * as am4core from "@amcharts/amcharts4/core"; import * as am4core from "@amcharts/amcharts4/core";
import * as am4maps from "@amcharts/amcharts4/maps"; import * as am4maps from "@amcharts/amcharts4/maps";
import * as am4plugins_sliceGrouper from "@amcharts/amcharts4/plugins/sliceGrouper";
import am4themes_material from "@amcharts/amcharts4/themes/material"; import am4themes_material from "@amcharts/amcharts4/themes/material";
const drawPieChart = (chartDivId: string, dataUrl: string | null): void => { 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; chart.dataSource.parser.options.emptyAs = 0;
// Add and configure Series // Add and configure Series
const pieSeries = chart.series.push(new am4charts.PieSeries()); 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.value = "values";
pieSeries.dataFields.category = "labels"; pieSeries.dataFields.category = "labels";
pieSeries.slices.template.stroke = am4core.color("#ffffff"); pieSeries.slices.template.stroke = am4core.color("#ffffff");