[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.sha.php
# Test files
/scripts/test.php
/cronjobs/test.php
# IDE Settings
/.idea/*
.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->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();
}

View File

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

View File

@ -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);

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
$('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'],
});
}
});
});
</script>
@ -99,7 +49,7 @@ $(function () {
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-signal fa-fw"></i> Stats
</div>
</div>
<div class="panel-body">
<ul class="nav nav-pills" id="hashrategraph">
<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="panel-heading">
Your Hashrate
@ -7,4 +6,3 @@
<div id="mine-area-chart"></div>
</div>
</div>
{/if}

View File

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