Skip to content

Commit

Permalink
Performance enhancement for dashboard statistics 2 #51
Browse files Browse the repository at this point in the history
  • Loading branch information
ssl committed Oct 22, 2020
1 parent a3ff011 commit 6ee8c1b
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 130 deletions.
138 changes: 137 additions & 1 deletion assets/css/classic.css
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,140 @@ th{color:#ffffff}
@font-face{font-family:'Roboto Mono';font-style:normal;font-weight:500;src:url(https://fonts.gstatic.com/s/robotomono/v12/L0x5DF4xlVMF-BfR8bXMIjhIq3-cXbKDO1w.woff2) format('woff2');unicode-range:U+0370-03FF}
@font-face{font-family:'Roboto Mono';font-style:normal;font-weight:500;src:url(https://fonts.gstatic.com/s/robotomono/v12/L0x5DF4xlVMF-BfR8bXMIjhEq3-cXbKDO1w.woff2) format('woff2');unicode-range:U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB}
@font-face{font-family:'Roboto Mono';font-style:normal;font-weight:500;src:url(https://fonts.gstatic.com/s/robotomono/v12/L0x5DF4xlVMF-BfR8bXMIjhFq3-cXbKDO1w.woff2) format('woff2');unicode-range:U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF}
@font-face{font-family:'Roboto Mono';font-style:normal;font-weight:500;src:url(https://fonts.gstatic.com/s/robotomono/v12/L0x5DF4xlVMF-BfR8bXMIjhLq3-cXbKD.woff2) format('woff2');unicode-range:U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD}
@font-face{font-family:'Roboto Mono';font-style:normal;font-weight:500;src:url(https://fonts.gstatic.com/s/robotomono/v12/L0x5DF4xlVMF-BfR8bXMIjhLq3-cXbKD.woff2) format('woff2');unicode-range:U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD}

.sk-fading-circle {
width: 30px;
height: 30px;
position: relative;
margin-bottom:3px;
}

.sk-fading-circle .load {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}

.sk-fading-circle .load:before {
content: '';
display: block;
margin: 0 auto;
width: 15%;
height: 15%;
background-color: #fff;
border-radius: 100%;
-webkit-animation: loadFadeDelay 1.2s infinite ease-in-out both;
animation: loadFadeDelay 1.2s infinite ease-in-out both;
}
.sk-fading-circle .load2 {
-webkit-transform: rotate(30deg);
-ms-transform: rotate(30deg);
transform: rotate(30deg);
}
.sk-fading-circle .load3 {
-webkit-transform: rotate(60deg);
-ms-transform: rotate(60deg);
transform: rotate(60deg);
}
.sk-fading-circle .load4 {
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.sk-fading-circle .load5 {
-webkit-transform: rotate(120deg);
-ms-transform: rotate(120deg);
transform: rotate(120deg);
}
.sk-fading-circle .load6 {
-webkit-transform: rotate(150deg);
-ms-transform: rotate(150deg);
transform: rotate(150deg);
}
.sk-fading-circle .load7 {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
}
.sk-fading-circle .load8 {
-webkit-transform: rotate(210deg);
-ms-transform: rotate(210deg);
transform: rotate(210deg);
}
.sk-fading-circle .load9 {
-webkit-transform: rotate(240deg);
-ms-transform: rotate(240deg);
transform: rotate(240deg);
}
.sk-fading-circle .load10 {
-webkit-transform: rotate(270deg);
-ms-transform: rotate(270deg);
transform: rotate(270deg);
}
.sk-fading-circle .load11 {
-webkit-transform: rotate(300deg);
-ms-transform: rotate(300deg);
transform: rotate(300deg);
}
.sk-fading-circle .load12 {
-webkit-transform: rotate(330deg);
-ms-transform: rotate(330deg);
transform: rotate(330deg);
}
.sk-fading-circle .load2:before {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.sk-fading-circle .load3:before {
-webkit-animation-delay: -1s;
animation-delay: -1s;
}
.sk-fading-circle .load4:before {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.sk-fading-circle .load5:before {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
.sk-fading-circle .load6:before {
-webkit-animation-delay: -0.7s;
animation-delay: -0.7s;
}
.sk-fading-circle .load7:before {
-webkit-animation-delay: -0.6s;
animation-delay: -0.6s;
}
.sk-fading-circle .load8:before {
-webkit-animation-delay: -0.5s;
animation-delay: -0.5s;
}
.sk-fading-circle .load9:before {
-webkit-animation-delay: -0.4s;
animation-delay: -0.4s;
}
.sk-fading-circle .load10:before {
-webkit-animation-delay: -0.3s;
animation-delay: -0.3s;
}
.sk-fading-circle .load11:before {
-webkit-animation-delay: -0.2s;
animation-delay: -0.2s;
}
.sk-fading-circle .load12:before {
-webkit-animation-delay: -0.1s;
animation-delay: -0.1s;
}

@-webkit-keyframes loadFadeDelay {
0%, 39%, 100% { opacity: 0; }
40% { opacity: 1; }
}

@keyframes loadFadeDelay {
0%, 39%, 100% { opacity: 0; }
40% { opacity: 1; }
}
8 changes: 8 additions & 0 deletions assets/js/ezxss.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ function request(action, data) {

$(document).ready(function() {

if(location.toString().split('/').pop() == 'dashboard') {
request('statistics', {csrf:csrf}).then(function(r) {
$.each( r, function( key, value ) {
$("#"+key).html(value);
});
});
}

$("form.form").submit(function(Form) {
$("#alert").slideUp();
Form.preventDefault();
Expand Down
82 changes: 5 additions & 77 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ public function twofactorLogin()
*/
public function setting($name)
{
if ($name == 'temp-secret') {
if ($name === 'temp-secret') {
return $this->secret;
}

if ($this->settings === []) {
foreach ($this->database->fetchAll('SELECT setting,value FROM settings', []) as $setting) {
$this->settings[$setting['setting']] = $setting['value'];
}
}

return htmlspecialchars($this->settings[$name], ENT_QUOTES);
return htmlspecialchars($this->database->fetchSetting($name), ENT_QUOTES);
}

/**
Expand All @@ -84,73 +78,6 @@ public function timezones() {
return $html;
}

/**
* Get statistics of amount of reports
* @method statistics
* @param string $branch which branch
* @return string count
*/
public function statistics($branch)
{
if($this->statisticsCache === []) {
$this->statisticsCache = ['total' => 0, 'week' => 0, 'totaldomains' => 0, 'weekdomains' => 0, 'totalshared' => 0, 'last' => 'never'];

$allReports = $this->database->fetchAll('SELECT origin,time,referer FROM reports', []);

$this->statisticsCache['total'] = count($allReports);

$uniqueDomains = [];
$uniqueDomainsWeek = [];
foreach($allReports as $report) {

// Counts report from last week
if($report['time'] > time() - 604800) {
$this->statisticsCache['week']++;

// Counts unique domains from last week
if(!in_array($report['origin'], $uniqueDomainsWeek, true)) {
$uniqueDomainsWeek[] = $report['origin'];
$this->statisticsCache['weekdomains']++;
}
}

// Counts unique domains
if(!in_array($report['origin'], $uniqueDomains, true)) {
$uniqueDomains[] = $report['origin'];
$this->statisticsCache['totaldomains']++;
}

// Counts amount of shared reports
if(strpos($report['referer'], "Shared via ") === 0) {
$this->statisticsCache['totalshared']++;
}
}

$lastReport = end($allReports);
if(isset($lastReport['time'])) {
$time = time() - $lastReport['time'];
$syntaxText = 's';
if ($time > 60) {
$time /= 60;
$syntaxText = 'm';
}
if ($time > 60) {
$time /= 60;
$syntaxText = 'h';
}
if ($time > 24) {
$time /= 24;
$syntaxText = 'd';
}
$this->statisticsCache['last'] = floor($time) . $syntaxText;
} else {
$this->statisticsCache['last'] = 'never';
}
}

return $this->statisticsCache[$branch];
}

/**
* Get ezXSS version
* @method version
Expand Down Expand Up @@ -328,6 +255,7 @@ public function report($key)
}

if (isset($this->reportInfo[$key])) {
date_default_timezone_set($this->setting('timezone'));
return ($key == 'time') ? date('F j, Y, g:i a', $this->reportInfo[$key]) : htmlspecialchars(
$this->reportInfo[$key]
);
Expand Down Expand Up @@ -374,9 +302,9 @@ public function searchQuery($navigation)
*/
public function twofactorSettings()
{
$secretCheck = $this->database->fetch('SELECT value FROM settings WHERE setting = "secret"');
$secretCheck = $this->setting('secret');

if (strlen($secretCheck['value']) !== 16) {
if (strlen($secretCheck) !== 16) {
if ($this->secret == '') {
for ($i = 0; $i < 16; $i++) {
$this->secret .= $this->base32Characters[rand(0, 31)];
Expand Down
18 changes: 14 additions & 4 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Database
{
private $DB;
private $settingsCache = [];

/**
* Try to connect to database
Expand Down Expand Up @@ -81,13 +82,17 @@ public function rowCount($query, $array = [])
/**
* Return value of setting
* @method fetchSetting
* @param string $setting Setting name
* @param string $name Setting name
* @return string Setting value
*/
public function fetchSetting($setting)
public function fetchSetting($name)
{
$query = $this->fetch('SELECT value FROM settings WHERE setting = :setting LIMIT 1', [':setting' => $setting]);
return $query[0];
if($this->settingsCache === []) {
foreach($this->fetchAll('SELECT setting,value FROM settings', []) as $setting) {
$this->settingsCache[$setting['setting']] = $setting['value'];
}
}
return $this->settingsCache[$name];
}

/**
Expand All @@ -104,4 +109,9 @@ public function fetch($query, $array = [])
return $fetch->fetch();
}

public function isInstalled() {
$rowCount = $this->rowCount('SELECT id FROM settings');
return $rowCount > 0;
}

}
6 changes: 4 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public function __construct()
*/
public function json()
{
if ($this->user->getCsrf() != $this->post('csrf')) {
if ($this->user->getCsrf() !== $this->post('csrf')) {
return $this->convert('CSRF token is not valid');
}

if (!$this->user->isLoggedIn() && $this->post('action') != 'login' && $this->post('action') != 'install' && $this->post('action') != 'update') {
if (!$this->user->isLoggedIn() && $this->post('action') !== 'login' && $this->post('action') !== 'install' && $this->post('action') !== 'update') {
return $this->convert('You need to be logged in to perform this action.');
}

Expand Down Expand Up @@ -89,6 +89,8 @@ public function json()
case 'collecting' :
return $this->convert($this->user->collecting($this->post('select')));
break;
case 'statistics':
return $this->user->statistics();
default :
return $this->convert('This action does not exists.');
break;
Expand Down

0 comments on commit 6ee8c1b

Please sign in to comment.