Fill empty hours with proper data

* Properly sort the array prior to sending it back
 * Sort from current to one hour earlier for proper data display
 * Adjusted templates to use new sorted arrays

Fixes #606
This commit is contained in:
Sebastian Grewe 2013-08-15 09:58:33 +02:00
parent 50f380c25e
commit 14c0535b10
4 changed files with 32 additions and 61 deletions

View File

@ -402,21 +402,11 @@ class Statistics {
AND a.id = ?
GROUP BY HOUR(time)");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = array();
while ($row = $result->fetch_assoc()) {
$aData[$row['hour']] = $row['hashrate'];
}
// We have no data, so we fill with 0
if (empty($aData)) {
for ($i = 0; $i <= 24; $i++) $aData[$i] = 0;
} else {
$count = -1;
foreach ($aData as $hour => $hashrate) {
$count++;
if ($count == $hour) continue;
$aData[$count] = 0;
}
}
$iStartHour = date('G');
for ($i = $iStartHour; $i <= 24; $i++) $aData[$i] = 0;
while ($row = $result->fetch_assoc()) $aData[$row['hour']] = $row['hashrate'];
// Fill any non-existing hours with 0 hashrate
for ($i = 0; $i <= 24; $i++) if (!array_key_exists($i, $aData)) $aData[$i] = 0;
return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData);
}
// Catchall
@ -449,10 +439,12 @@ class Statistics {
AND time > NOW() - INTERVAL 25 HOUR
GROUP BY HOUR(time)");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) {
while ($row = $result->fetch_assoc()) {
$aData[$row['hour']] = $row['hashrate'];
}
return $this->memcache->setCache(__FUNCTION__, @$aData);
$iStartHour = date('G');
for ($i = $iStartHour; $i <= 24; $i++) $aData[$i] = 0;
while ($row = $result->fetch_assoc()) $aData[$row['hour']] = (int) $row['hashrate'];
// Fill any non-existing hours with 0 hashrate
for ($i = 0; $i <= 24; $i++) if (!array_key_exists($i, $aData)) $aData[$i] = 0;
return $this->memcache->setCache(__FUNCTION__, $aData);
}
// Catchall
$this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error);

View File

@ -5,32 +5,23 @@
<thead>
<tr>
<td></td>
{for $i=date('G') to 23}
<th scope="col">{$i}:00</th>
{/for}
{for $i=0 to date('G', time () - 60 * 60)}
<th scope="col">{$i}:00</th>
{/for}
{foreach $YOURHASHRATES as $hour=>$hashrate}
<th scope="col">{$hour|default:"0"}:00</th>
{/foreach}
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{$smarty.session.USERDATA.username}</th>
{for $i=date('G') to 23}
<td>{$YOURHASHRATES.$i|default:"0"}</td>
{/for}
{for $i=0 to date('G', time() - 60 * 60)}
<td>{$YOURHASHRATES.$i|default:"0"}</td>
{/for}
{foreach $YOURHASHRATES as $hour=>$hashrate}
<td>{$hashrate|default:"0"}</td>
{/foreach}
</tr>
<tr>
<th scope="row">Pool</th>
{for $i=date('G') to 23}
<td>{$POOLHASHRATES.$i|default:"0"}</td>
{/for}
{for $i=0 to date('G', time() - 60 * 60)}
<td>{$POOLHASHRATES.$i|default:"0"}</td>
{/for}
{foreach $POOLHASHRATES as $hour=>$hashrate}
<td>{$hashrate|default:"0"}</td>
{/foreach}
</tr>
</tbody>
</table>

View File

@ -5,23 +5,17 @@
<thead>
<tr>
<td></td>
{for $i=date('G') to 23}
<th scope="col">{$i}:00</th>
{/for}
{for $i=0 to date('G', time () - 60 * 60)}
<th scope="col">{$i}:00</th>
{/for}
{foreach $YOURHASHRATES as $hour=>$hashrate}
<th scope="col">{$hour|default:"0"}:00</th>
{/foreach}
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{$smarty.session.USERDATA.username}</th>
{for $i=date('G') to 23}
<td>{$YOURHASHRATES.$i|default:"0"}</td>
{/for}
{for $i=0 to date('G', time() - 60 * 60)}
<td>{$YOURHASHRATES.$i|default:"0"}</td>
{/for}
{foreach $YOURHASHRATES as $hour=>$hashrate}
<td>{$hashrate|default:"0"}</td>
{/foreach}
</tr>
</tbody>
</table>

View File

@ -5,23 +5,17 @@
<thead>
<tr>
<td></td>
{for $i=date('G') to 23}
<th scope="col">{$i}:00</th>
{/for}
{for $i=0 to date('G', time () - 60 * 60)}
<th scope="col">{$i}:00</th>
{/for}
{foreach $POOLHASHRATES as $hour=>$hashrate}
<th scope="col">{$hour|default:"0"}:00</th>
{/foreach}
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Pool</th>
{for $i=date('G') to 23}
<td>{$POOLHASHRATES.$i|default:"0"}</td>
{/for}
{for $i=0 to date('G', time() - 60 * 60)}
<td>{$POOLHASHRATES.$i|default:"0"}</td>
{/for}
{foreach $POOLHASHRATES as $hour=>$hashrate}
<td>{$hashrate|default:"0"}</td>
{/foreach}
</tr>
</tbody>
</table>