[REFACTOR] Use raw statistical data

This commit is contained in:
Sebastian Grewe 2014-07-13 13:55:37 +02:00
parent bf2429ab2f
commit 77c6abed05
7 changed files with 44 additions and 111 deletions

4
.gitignore vendored
View File

@ -18,6 +18,10 @@
/include/config/global.inc.scrypt.php /include/config/global.inc.scrypt.php
/include/config/global.inc.sha.php /include/config/global.inc.sha.php
# Test files
/scripts/test.php
/cronjobs/test.php
# IDE Settings # IDE Settings
/.idea/* /.idea/*
.buildpath .buildpath

View File

@ -192,7 +192,7 @@ class Statistics extends Base {
return $this->memcache->setCache(__FUNCTION__ . $account_id . $limit, $result->fetch_all(MYSQLI_ASSOC), 5); return $this->memcache->setCache(__FUNCTION__ . $account_id . $limit, $result->fetch_all(MYSQLI_ASSOC), 5);
return $this->sqlError(); return $this->sqlError();
} }
/** /**
* Currently the only function writing to the database * Currently the only function writing to the database
* Stored per block user statistics of valid and invalid shares * Stored per block user statistics of valid and invalid shares
@ -698,61 +698,44 @@ class Statistics extends Base {
* @param $account_id int account id * @param $account_id int account id
* @return data array NOT FINISHED YET * @return data array NOT FINISHED YET
**/ **/
public function getHourlyHashrateByAccount($account_id) { public function getHashrateByAccount($account_id, $format='array') {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
account_id AS id, timestamp,
AVG(hashrate) AS hashrate, hashrate
HOUR(FROM_UNIXTIME(timestamp)) AS hour
FROM " . $this->getUserStatsTableName() . " FROM " . $this->getUserStatsTableName() . "
WHERE WHERE
account_id = ? 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))");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) { if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
$iStartHour = date('G'); $aData = $result->fetch_all(MYSQLI_ASSOC);
// Initilize array if ($format == 'json') $aData = json_encode($aData);
for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; return $this->memcache->setCache(__FUNCTION__ . $account_id . $format, $aData);
// 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);
} }
return $this->sqlError(); return $this->sqlError();
} }
/** /**
* get Hourly hashrate for the pool * get Hourly hashrate for the pool
* @param none * @param none
* @return data array NOT FINISHED YET * @return data array NOT FINISHED YET
**/ **/
public function getHourlyHashrateByPool() { public function getHashrateForPool($format='array') {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
SUM(hashrate) / ( timestamp,
SELECT SUM(hashrate) AS hashrate
COUNT(DISTINCT timestamp) FROM " . $this->getUserStatsTableName() . "
FROM " . $this->getUserStatsTableName() . " GROUP BY timestamp");
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))");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) {
$iStartHour = date('G'); $aData = $result->fetch_all(MYSQLI_ASSOC);
// Initilize array if ($format == 'json') $aData = json_encode($aData);
for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; return $this->memcache->setCache(__FUNCTION__ . $format, $aData);
// Fill data
while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int)$row['hashrate'];
return $this->memcache->setCache(__FUNCTION__, $aData);
} }
var_dump($this->mysqli->error);
return $this->sqlError(); return $this->sqlError();
} }

View File

@ -5,7 +5,7 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
$api->isActive(); $api->isActive();
// Check for valid API key // Check for valid API key
$id = $user->checkApiKey($_REQUEST['api_key']); $id = $user->checkApiKey(@$_REQUEST['api_key']);
header('HTTP/1.1 400 Bad Request'); header('HTTP/1.1 400 Bad Request');
die('400 Bad Request'); die('400 Bad Request');

View File

@ -4,8 +4,8 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { if (!$smarty->isCached('master.tpl', $smarty_cache_key)) {
$debug->append('No cached version available, fetching from backend', 3); $debug->append('No cached version available, fetching from backend', 3);
if ($user->isAuthenticated()) { if ($user->isAuthenticated()) {
$aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); $aHourlyHashRates = $statistics->getHashrateByAccount($_SESSION['USERDATA']['id'], 'json');
$aPoolHourlyHashRates = $statistics->getHourlyHashrateByPool(); $aPoolHourlyHashRates = $statistics->getHashrateForPool('json');
} }
$smarty->assign("YOURHASHRATES", @$aHourlyHashRates); $smarty->assign("YOURHASHRATES", @$aHourlyHashRates);
$smarty->assign("POOLHASHRATES", @$aPoolHourlyHashRates); $smarty->assign("POOLHASHRATES", @$aPoolHourlyHashRates);

View File

@ -8,88 +8,38 @@ $(function () {
// You can't draw here chart directly, because it's on hidden tab, instead let's do the workaround // 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) { $('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') == '#mine' && $('#mine-area-chart').html().length == 0) {
if ($(e.target).attr('href') == '#pool' && $('#pool-area-chart').html().length == 0) { var chart = Morris.Line({
Morris.Area({ // ID of the element in which to draw the chart.
element: 'pool-area-chart', element: 'mine-area-chart',
data: [ data: {$YOURHASHRATES},
{foreach $POOLHASHRATES as $hour=>$hashrate} xkey: 'timestamp',
{ ykeys: ['hashrate'],
period: '{$hour|default:"0"}:00',
Pool: '{$hashrate|default:"0"}',
},
{/foreach}
],
parseTime: false,
behaveLikeLine: true,
xkey: 'period',
ykeys: ['Pool'],
labels: ['Hashrate'], labels: ['Hashrate'],
pointSize: 2, pointSize: 2,
hideHover: 'auto', hideHover: 'auto',
lineColors: ['#0b62a4'],
pointFillColors: ['#FFFFFF'],
resize: true, resize: true,
fillOpacity: 1.00, 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'], lineColors: ['#24A665'],
pointFillColors: ['#FFFFFF'], pointFillColors: ['#FFFFFF'],
resize: true,
fillOpacity: 1.00,
postUnits: ' KH/s'
}); });
} }
if ($(e.target).attr('href') == '#pool' && $('#pool-area-chart').html().length == 0) {
if ($(e.target).attr('href') == '#both' && $('#both-area-chart').html().length == 0) { var chart = Morris.Line({
Morris.Area({ // ID of the element in which to draw the chart.
element: 'both-area-chart', element: 'pool-area-chart',
data: [ data: {$POOLHASHRATES},
{foreach $YOURHASHRATES as $yourhour=>$yourhashrate} xkey: 'timestamp',
{ ykeys: ['hashrate'],
period: '{$yourhour|default:"0"}:00', labels: ['Hashrate'],
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'],
pointSize: 2, pointSize: 2,
hideHover: 'auto', hideHover: 'auto',
resize: true, resize: true,
fillOpacity: 0.1, fillOpacity: 1.00,
postUnits: ' KH/s' lineColors: ['#24A665'],
pointFillColors: ['#FFFFFF'],
}); });
} }
}); });
}); });
</script> </script>
@ -99,7 +49,7 @@ $(function () {
<div class="panel panel-info"> <div class="panel panel-info">
<div class="panel-heading"> <div class="panel-heading">
<i class="fa fa-signal fa-fw"></i> Stats <i class="fa fa-signal fa-fw"></i> Stats
</div> </div>
<div class="panel-body"> <div class="panel-body">
<ul class="nav nav-pills" id="hashrategraph"> <ul class="nav nav-pills" id="hashrategraph">
<li><a href="#mine" data-toggle="tab">Mine</a></li> <li><a href="#mine" data-toggle="tab">Mine</a></li>

View File

@ -1,4 +1,3 @@
{if is_array($YOURHASHRATES)}
<div class="tab-pane fade in" id="mine"> <div class="tab-pane fade in" id="mine">
<div class="panel-heading"> <div class="panel-heading">
Your Hashrate Your Hashrate
@ -7,4 +6,3 @@
<div id="mine-area-chart"></div> <div id="mine-area-chart"></div>
</div> </div>
</div> </div>
{/if}

View File

@ -1,4 +1,3 @@
{if is_array($POOLHASHRATES)}
<div class="tab-pane fade in" id="pool"> <div class="tab-pane fade in" id="pool">
<div class="panel-heading"> <div class="panel-heading">
Pool Hashrate Pool Hashrate
@ -7,4 +6,3 @@
<div id="pool-area-chart"></div> <div id="pool-area-chart"></div>
</div> </div>
</div> </div>
{/if}