Merge pull request #65 from michu2k/statistics-fix

Statistics fix
This commit is contained in:
Paweł Klockiewicz
2020-02-16 15:41:17 +01:00
committed by GitHub
3 changed files with 65 additions and 24 deletions

View File

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

View File

@@ -1,36 +1,66 @@
$(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
}
}]
}});
xAxes: [{
ticks: {
display: false,
maxTicksLimit: 20,
},
}],
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) {

View File

@@ -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'),
];
}