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"
))
) {
$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",

View File

@ -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;
}
}

View File

@ -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",

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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");