From 4686a096a2cee5b133dae4f24038d52c45b82fcf Mon Sep 17 00:00:00 2001 From: michu2k Date: Sat, 15 Feb 2020 16:29:19 +0100 Subject: [PATCH 1/3] Changed statistics API --- inc/modules/statistics/Site.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/modules/statistics/Site.php b/inc/modules/statistics/Site.php index 5933d82..4a9a6f2 100644 --- a/inc/modules/statistics/Site.php +++ b/inc/modules/statistics/Site.php @@ -34,8 +34,10 @@ class Site extends SiteModule // Get latest country or fetch new $country = 'Unknown'; $latest = $this->db('statistics')->where('ip', $ip)->desc('created_at')->limit(1)->oneArray(); + if (!$latest) { - $details = json_decode(HttpRequest::get('http://freegeoip.net/json/'.$ip), true); + $details = json_decode(HttpRequest::get('https://freegeoip.app/json/'.$ip), true); + if (!empty($details['country_code'])) { $country = $details['country_code']; } From 5d36c79081fe40ab2472f88636949509f139741c Mon Sep 17 00:00:00 2001 From: michu2k Date: Sun, 16 Feb 2020 15:08:18 +0100 Subject: [PATCH 2/3] statistics: Fixed pages stats error, Changed data order in pages stats --- inc/modules/statistics/assets/js/app.js | 60 +++++++++++++++++-------- inc/modules/statistics/src/Chart.php | 19 +++++--- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/inc/modules/statistics/assets/js/app.js b/inc/modules/statistics/assets/js/app.js index 490260b..18d49e1 100644 --- a/inc/modules/statistics/assets/js/app.js +++ b/inc/modules/statistics/assets/js/app.js @@ -1,36 +1,60 @@ $(document).ready(function() { var defaultColors = [ - "rgba(131, 58, 163, 0.5)", "rgba(201, 216, 88, 0.5)", "rgba(5, 183, 196, 0.5)", "rgba(139, 20, 229, 0.5)", "rgba(85, 150, 219, 0.5)", "rgba(46, 151, 155, 0.5)", "rgba(169, 99, 226, 0.5)", "rgba(90, 27, 209, 0.5)", "rgba(123, 160, 3, 0.5)", "rgba(161, 95, 226, 0.5)", "rgba(201, 59, 214, 0.5)", "rgba(9, 102, 104, 0.5)", "rgba(81, 118, 186, 0.5)", "rgba(220, 63, 252, 0.5)", "rgba(252, 63, 82, 0.5)", "rgba(97, 249, 176, 0.5)", "rgba(232, 30, 154, 0.5)", "rgba(239, 7, 231, 0.5)", "rgba(107, 239, 211, 0.5)", "rgba(168, 10, 23, 0.5)", "rgba(221, 90, 99, 0.5)", "rgba(35, 102, 237, 0.5)", "rgba(15, 226, 216, 0.5)", "rgba(63, 122, 211, 0.5)", "rgba(226, 88, 86, 0.5)", "rgba(232, 98, 85, 0.5)", "rgba(168, 6, 226, 0.5)" + "rgba(131, 58, 163, 0.5)", + "rgba(201, 216, 88, 0.5)", + "rgba(5, 183, 196, 0.5)", + "rgba(139, 20, 229, 0.5)", + "rgba(85, 150, 219, 0.5)", + "rgba(46, 151, 155, 0.5)", + "rgba(169, 99, 226, 0.5)", + "rgba(90, 27, 209, 0.5)", + "rgba(123, 160, 3, 0.5)", + "rgba(161, 95, 226, 0.5)", + "rgba(201, 59, 214, 0.5)", + "rgba(9, 102, 104, 0.5)", + "rgba(81, 118, 186, 0.5)", + "rgba(220, 63, 252, 0.5)", + "rgba(252, 63, 82, 0.5)", + "rgba(97, 249, 176, 0.5)", + "rgba(232, 30, 154, 0.5)", + "rgba(239, 7, 231, 0.5)", + "rgba(107, 239, 211, 0.5)", + "rgba(168, 10, 23, 0.5)", + "rgba(221, 90, 99, 0.5)", + "rgba(35, 102, 237, 0.5)", + "rgba(15, 226, 216, 0.5)", + "rgba(63, 122, 211, 0.5)", + "rgba(226, 88, 86, 0.5)", + "rgba(232, 98, 85, 0.5)", + "rgba(168, 6, 226, 0.5)" ]; - var charts = []; + $('[data-chart]').each(function() { var name = $(this).attr('id') || false; - - if(name === false) - return; + + if (name === false) return; var type = $(this).data('chart'); var labels = $(this).data('labels'); var data = $(this).data('datasets'); var options = {}; - if(type == 'bar') - { + + if (type == 'bar') { options = Object.assign(options, {scales: { - yAxes: [{ - ticks: { - beginAtZero:true - } - }] - }}); + yAxes: [{ + ticks: { + beginAtZero: true + }, + }] + }}); } - + var backgroundColor = function() { - if(type == 'pie') - return defaultColors; - else - return 'rgba(248, 190, 18, 0.2)'; + if (type == 'pie') return defaultColors; + return 'rgba(248, 190, 18, 0.2)'; } + var datasets = []; data = eval(data); data.forEach(function(e) { diff --git a/inc/modules/statistics/src/Chart.php b/inc/modules/statistics/src/Chart.php index c76cf4d..f9b8b6e 100644 --- a/inc/modules/statistics/src/Chart.php +++ b/inc/modules/statistics/src/Chart.php @@ -76,7 +76,7 @@ class Chart public function getPages($url = null, $referrer = null) { - return $this->getPopularBy('url', $url, $referrer); + return $this->getPopularBy('url', $url, $referrer, 'desc'); } public function getReferrers($url = null, $referrer = null) @@ -84,7 +84,7 @@ class Chart return $this->getPopularBy('referrer', $url, $referrer); } - protected function getPopularBy($group, $url = null, $referrer = null) + protected function getPopularBy($group, $url = null, $referrer = null, $order = 'asc') { $data = $this->db('statistics') ->select([ @@ -98,16 +98,25 @@ class Chart if (!empty($url)) { $data->where('url', $url); } + if (!empty($referrer)) { $data->where('referrer', $referrer); } $data = $data->toArray(); + + if ($order == 'desc') { + $data = array_reverse($data); + } + + $labels = array_map(function (&$value) { + $value = preg_replace('/[^a-zA-Z0-9\/]/', '', $value); + return '"'.$value.'"'; + }, array_column($data, $group)); + return [ - 'labels' => array_map(function (&$value) { - return '"'.$value.'"'; - }, array_column($data, $group)), + 'labels' => $labels, 'data' => array_column($data, 'count'), ]; } From 841963bc6c34ae3fbd670ae2f8230bef5b5b6a07 Mon Sep 17 00:00:00 2001 From: michu2k Date: Sun, 16 Feb 2020 15:19:35 +0100 Subject: [PATCH 3/3] statistics: Removed labels from pages statistics due to a visibility issue --- inc/modules/statistics/assets/js/app.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/inc/modules/statistics/assets/js/app.js b/inc/modules/statistics/assets/js/app.js index 18d49e1..d657b16 100644 --- a/inc/modules/statistics/assets/js/app.js +++ b/inc/modules/statistics/assets/js/app.js @@ -42,6 +42,12 @@ $(document).ready(function() { if (type == 'bar') { options = Object.assign(options, {scales: { + xAxes: [{ + ticks: { + display: false, + maxTicksLimit: 20, + }, + }], yAxes: [{ ticks: { beginAtZero: true