From 91ef2caaab7663b744098832b2eeff04d4e37ec9 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 17 Jun 2013 10:07:40 +0200 Subject: [PATCH] Added cronjob to pre-cache statistics This will fix #199 and help on loading times for the website in case caches are empty. Caches are pre-filled by a cron so the website only does it as a fall back. Check Ticket for details. --- cronjobs/run-crons.sh | 2 +- cronjobs/statistics.php | 41 +++++++++++++++++++++ public/include/classes/statistics.class.php | 23 ++++++++---- 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100755 cronjobs/statistics.php diff --git a/cronjobs/run-crons.sh b/cronjobs/run-crons.sh index 5f9179dc..f5cbaee7 100755 --- a/cronjobs/run-crons.sh +++ b/cronjobs/run-crons.sh @@ -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" diff --git a/cronjobs/statistics.php b/cronjobs/statistics.php new file mode 100755 index 00000000..03aad394 --- /dev/null +++ b/cronjobs/statistics.php @@ -0,0 +1,41 @@ +#!/usr/bin/php +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"); + +?> diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 6f56453b..f9f2ea9a 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -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);