Merge pull request #200 from TheSerapher/issue-199

Added cronjob to pre-cache statistics
This commit is contained in:
Sebastian Grewe 2013-06-17 01:08:49 -07:00
commit 32cbab45d3
3 changed files with 58 additions and 8 deletions

View File

@ -16,7 +16,7 @@ PIDFILE='/tmp/mmcfe-ng-cron.pid'
CRONHOME='.'
# List of cruns to execute
CRONS="findblock.php proportional_payout.php blockupdate.php auto_payout.php tickerupdate.php notifications.php"
CRONS="findblock.php proportional_payout.php blockupdate.php auto_payout.php tickerupdate.php notifications.php statistics.php"
# Additional arguments to pass to cronjobs
CRONARGS="-v"

41
cronjobs/statistics.php Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/php
<?php
/*
Copyright:: 2013, Sebastian Grewe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Include all settings and classes
require_once('shared.inc.php');
// Fetch all cachable values but disable fetching from cache
$statistics->setGetCache(false);
// Since fetching from cache is disabled, overwrite our stats
if (!$statistics->getRoundShares())
verbose("Unable to fetch and store current round shares\n");
if (!$statistics->getTopContributors('shares'))
verbose("Unable to fetch and store top share contributors\n");
if (!$statistics->getTopContributors('hashes'))
verbose("Unable to fetch and store top hashrate contributors\n");
if (!$statistics->getCurrentHashrate())
verbose("Unable to fetch and store pool hashrate\n");
// Admin specific statistics, we cache the global query due to slowness
if (!$statistics->getAllUserStats('%'))
verbose("Unable to fetch and store admin panel full user list\n");
?>

View File

@ -13,6 +13,7 @@ if (!defined('SECURITY'))
class Statistics {
private $sError = '';
private $table = 'statistics_shares';
private $getcache = true;
public function __construct($debug, $mysqli, $config, $share, $user, $block, $memcache) {
$this->debug = $debug;
@ -34,6 +35,14 @@ class Statistics {
return $this->sError;
}
// Disable fetching values from cache
public function setGetCache($set=false) {
$this->getcache = $set;
}
public function getGetCache() {
return $this->getcache;
}
private function checkStmt($bState) {
if ($bState ===! true) {
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
@ -88,7 +97,7 @@ class Statistics {
**/
public function getCurrentHashrate() {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
");
@ -122,7 +131,7 @@ class Statistics {
**/
public function getRoundShares() {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
( SELECT IFNULL(count(id), 0)
@ -147,7 +156,7 @@ class Statistics {
**/
public function getUserShares($account_id) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
(
@ -181,7 +190,7 @@ class Statistics {
**/
public function getAllUserStats($filter='%') {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $filter)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $filter)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
a.id AS id,
@ -255,7 +264,7 @@ class Statistics {
**/
public function getTopContributors($type='shares', $limit=15) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data;
switch ($type) {
case 'shares':
$stmt = $this->mysqli->prepare("
@ -329,7 +338,7 @@ class Statistics {
**/
public function getHourlyHashrateByPool() {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate,
@ -343,7 +352,7 @@ class Statistics {
while ($row = $result->fetch_assoc()) {
$aData[$row['hour']] = $row['hashrate'];
}
return $this->memcache->setCache(__FUNCTION__, $aData);
return $this->memcache->setCache(__FUNCTION__, @$aData);
}
// Catchall
$this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error);