From 77c6abed05891a0553d0a00c253f7a3616006648 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 13 Jul 2014 13:55:37 +0200 Subject: [PATCH] [REFACTOR] Use raw statistical data --- .gitignore | 4 + include/classes/statistics.class.php | 53 ++++------- include/pages/api.inc.php | 2 +- include/pages/statistics/graphs.inc.php | 4 +- .../bootstrap/statistics/graphs/default.tpl | 88 ++++--------------- .../bootstrap/statistics/graphs/mine.tpl | 2 - .../bootstrap/statistics/graphs/pool.tpl | 2 - 7 files changed, 44 insertions(+), 111 deletions(-) diff --git a/.gitignore b/.gitignore index b4976dfc..6c1d5b6f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,10 @@ /include/config/global.inc.scrypt.php /include/config/global.inc.sha.php +# Test files +/scripts/test.php +/cronjobs/test.php + # IDE Settings /.idea/* .buildpath diff --git a/include/classes/statistics.class.php b/include/classes/statistics.class.php index b7a65782..c7b2c593 100644 --- a/include/classes/statistics.class.php +++ b/include/classes/statistics.class.php @@ -192,7 +192,7 @@ class Statistics extends Base { return $this->memcache->setCache(__FUNCTION__ . $account_id . $limit, $result->fetch_all(MYSQLI_ASSOC), 5); return $this->sqlError(); } - + /** * Currently the only function writing to the database * Stored per block user statistics of valid and invalid shares @@ -698,61 +698,44 @@ class Statistics extends Base { * @param $account_id int account id * @return data array NOT FINISHED YET **/ - public function getHourlyHashrateByAccount($account_id) { + public function getHashrateByAccount($account_id, $format='array') { $this->debug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT - account_id AS id, - AVG(hashrate) AS hashrate, - HOUR(FROM_UNIXTIME(timestamp)) AS hour + timestamp, + hashrate FROM " . $this->getUserStatsTableName() . " WHERE - account_id = ? - AND timestamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) - AND timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 HOUR)) - GROUP BY HOUR(FROM_UNIXTIME(timestamp))"); + account_id = ?"); if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) { - $iStartHour = date('G'); - // Initilize array - for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; - // Fill data in proper hour order, result in SQL was ordered 0 - 23 - while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int)$row['hashrate']; - return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData); + $aData = $result->fetch_all(MYSQLI_ASSOC); + if ($format == 'json') $aData = json_encode($aData); + return $this->memcache->setCache(__FUNCTION__ . $account_id . $format, $aData); } return $this->sqlError(); } /** - * get Hourly hashrate for the pool + * get Hourly hashrate for the pool * @param none * @return data array NOT FINISHED YET **/ - public function getHourlyHashrateByPool() { + public function getHashrateForPool($format='array') { $this->debug->append("STA " . __METHOD__, 4); if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT - SUM(hashrate) / ( - SELECT - COUNT(DISTINCT timestamp) - FROM " . $this->getUserStatsTableName() . " - WHERE timestamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) - AND timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 HOUR)) - ) AS hashrate, - HOUR(FROM_UNIXTIME(timestamp)) AS hour - FROM " . $this->getUserStatsTableName() . " - WHERE timestamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) - AND timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 HOUR)) - GROUP BY HOUR(FROM_UNIXTIME(timestamp))"); + timestamp, + SUM(hashrate) AS hashrate + FROM " . $this->getUserStatsTableName() . " + GROUP BY timestamp"); if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { - $iStartHour = date('G'); - // Initilize array - for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; - // Fill data - while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int)$row['hashrate']; - return $this->memcache->setCache(__FUNCTION__, $aData); + $aData = $result->fetch_all(MYSQLI_ASSOC); + if ($format == 'json') $aData = json_encode($aData); + return $this->memcache->setCache(__FUNCTION__ . $format, $aData); } + var_dump($this->mysqli->error); return $this->sqlError(); } diff --git a/include/pages/api.inc.php b/include/pages/api.inc.php index 01cd20f5..32fd0a34 100644 --- a/include/pages/api.inc.php +++ b/include/pages/api.inc.php @@ -5,7 +5,7 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; $api->isActive(); // Check for valid API key -$id = $user->checkApiKey($_REQUEST['api_key']); +$id = $user->checkApiKey(@$_REQUEST['api_key']); header('HTTP/1.1 400 Bad Request'); die('400 Bad Request'); diff --git a/include/pages/statistics/graphs.inc.php b/include/pages/statistics/graphs.inc.php index 291229cf..c970b8d8 100644 --- a/include/pages/statistics/graphs.inc.php +++ b/include/pages/statistics/graphs.inc.php @@ -4,8 +4,8 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $debug->append('No cached version available, fetching from backend', 3); if ($user->isAuthenticated()) { - $aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); - $aPoolHourlyHashRates = $statistics->getHourlyHashrateByPool(); + $aHourlyHashRates = $statistics->getHashrateByAccount($_SESSION['USERDATA']['id'], 'json'); + $aPoolHourlyHashRates = $statistics->getHashrateForPool('json'); } $smarty->assign("YOURHASHRATES", @$aHourlyHashRates); $smarty->assign("POOLHASHRATES", @$aPoolHourlyHashRates); diff --git a/templates/bootstrap/statistics/graphs/default.tpl b/templates/bootstrap/statistics/graphs/default.tpl index 5de1b86b..65d03503 100644 --- a/templates/bootstrap/statistics/graphs/default.tpl +++ b/templates/bootstrap/statistics/graphs/default.tpl @@ -8,88 +8,38 @@ $(function () { // You can't draw here chart directly, because it's on hidden tab, instead let's do the workaround $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { - // this ain't pretty, but you should get the idea - if ($(e.target).attr('href') == '#pool' && $('#pool-area-chart').html().length == 0) { - Morris.Area({ - element: 'pool-area-chart', - data: [ - {foreach $POOLHASHRATES as $hour=>$hashrate} - { - period: '{$hour|default:"0"}:00', - Pool: '{$hashrate|default:"0"}', - }, - {/foreach} - ], - parseTime: false, - behaveLikeLine: true, - xkey: 'period', - ykeys: ['Pool'], + if ($(e.target).attr('href') == '#mine' && $('#mine-area-chart').html().length == 0) { + var chart = Morris.Line({ + // ID of the element in which to draw the chart. + element: 'mine-area-chart', + data: {$YOURHASHRATES}, + xkey: 'timestamp', + ykeys: ['hashrate'], labels: ['Hashrate'], pointSize: 2, hideHover: 'auto', - lineColors: ['#0b62a4'], - pointFillColors: ['#FFFFFF'], resize: true, fillOpacity: 1.00, - postUnits: ' KH/s' - }); - } - - if ($(e.target).attr('href') == '#mine' && $('#mine-area-chart').html().length == 0) { - Morris.Area({ - element: 'mine-area-chart', - data: [ - {foreach $YOURHASHRATES as $yourhour=>$yourhashrate} - { - period: '{$yourhour|default:"0"}:00', - Mine: '{$yourhashrate|default:"0"}', - }, - {/foreach} - ], - parseTime: false, - behaveLikeLine: true, - xkey: 'period', - ykeys: ['Mine'], - labels: ['Hashrate'], - pointSize: 2, - hideHover: 'auto', lineColors: ['#24A665'], pointFillColors: ['#FFFFFF'], - resize: true, - fillOpacity: 1.00, - postUnits: ' KH/s' }); } - - if ($(e.target).attr('href') == '#both' && $('#both-area-chart').html().length == 0) { - Morris.Area({ - element: 'both-area-chart', - data: [ - {foreach $YOURHASHRATES as $yourhour=>$yourhashrate} - { - period: '{$yourhour|default:"0"}:00', - Mine: '{$yourhashrate|default:"0"}', - {foreach $POOLHASHRATES as $poolhour=>$poolhashrate} - {if $yourhour eq $poolhour} - Pool: '{$poolhashrate|default:"0"}', - {/if} - {/foreach} - }, - {/foreach} - ], - parseTime: false, - behaveLikeLine: true, - xkey: 'period', - ykeys: ['Mine', 'Pool'], - labels: ['Your Hashrate', 'Pool Hashrate'], + if ($(e.target).attr('href') == '#pool' && $('#pool-area-chart').html().length == 0) { + var chart = Morris.Line({ + // ID of the element in which to draw the chart. + element: 'pool-area-chart', + data: {$POOLHASHRATES}, + xkey: 'timestamp', + ykeys: ['hashrate'], + labels: ['Hashrate'], pointSize: 2, hideHover: 'auto', resize: true, - fillOpacity: 0.1, - postUnits: ' KH/s' + fillOpacity: 1.00, + lineColors: ['#24A665'], + pointFillColors: ['#FFFFFF'], }); } - }); }); @@ -99,7 +49,7 @@ $(function () {
Stats -
+