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.
This commit is contained in:
parent
b7b0ab9298
commit
91ef2caaab
@ -16,7 +16,7 @@ PIDFILE='/tmp/mmcfe-ng-cron.pid'
|
|||||||
CRONHOME='.'
|
CRONHOME='.'
|
||||||
|
|
||||||
# List of cruns to execute
|
# 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
|
# Additional arguments to pass to cronjobs
|
||||||
CRONARGS="-v"
|
CRONARGS="-v"
|
||||||
|
|||||||
41
cronjobs/statistics.php
Executable file
41
cronjobs/statistics.php
Executable 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");
|
||||||
|
|
||||||
|
?>
|
||||||
@ -13,6 +13,7 @@ if (!defined('SECURITY'))
|
|||||||
class Statistics {
|
class Statistics {
|
||||||
private $sError = '';
|
private $sError = '';
|
||||||
private $table = 'statistics_shares';
|
private $table = 'statistics_shares';
|
||||||
|
private $getcache = true;
|
||||||
|
|
||||||
public function __construct($debug, $mysqli, $config, $share, $user, $block, $memcache) {
|
public function __construct($debug, $mysqli, $config, $share, $user, $block, $memcache) {
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
@ -34,6 +35,14 @@ class Statistics {
|
|||||||
return $this->sError;
|
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) {
|
private function checkStmt($bState) {
|
||||||
if ($bState ===! true) {
|
if ($bState ===! true) {
|
||||||
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
|
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
|
||||||
@ -88,7 +97,7 @@ class Statistics {
|
|||||||
**/
|
**/
|
||||||
public function getCurrentHashrate() {
|
public function getCurrentHashrate() {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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("
|
$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)
|
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() {
|
public function getRoundShares() {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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("
|
$stmt = $this->mysqli->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
( SELECT IFNULL(count(id), 0)
|
( SELECT IFNULL(count(id), 0)
|
||||||
@ -147,7 +156,7 @@ class Statistics {
|
|||||||
**/
|
**/
|
||||||
public function getUserShares($account_id) {
|
public function getUserShares($account_id) {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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("
|
$stmt = $this->mysqli->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
(
|
(
|
||||||
@ -181,7 +190,7 @@ class Statistics {
|
|||||||
**/
|
**/
|
||||||
public function getAllUserStats($filter='%') {
|
public function getAllUserStats($filter='%') {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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("
|
$stmt = $this->mysqli->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
a.id AS id,
|
a.id AS id,
|
||||||
@ -255,7 +264,7 @@ class Statistics {
|
|||||||
**/
|
**/
|
||||||
public function getTopContributors($type='shares', $limit=15) {
|
public function getTopContributors($type='shares', $limit=15) {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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) {
|
switch ($type) {
|
||||||
case 'shares':
|
case 'shares':
|
||||||
$stmt = $this->mysqli->prepare("
|
$stmt = $this->mysqli->prepare("
|
||||||
@ -329,7 +338,7 @@ class Statistics {
|
|||||||
**/
|
**/
|
||||||
public function getHourlyHashrateByPool() {
|
public function getHourlyHashrateByPool() {
|
||||||
$this->debug->append("STA " . __METHOD__, 4);
|
$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("
|
$stmt = $this->mysqli->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate,
|
ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate,
|
||||||
@ -343,7 +352,7 @@ class Statistics {
|
|||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
$aData[$row['hour']] = $row['hashrate'];
|
$aData[$row['hour']] = $row['hashrate'];
|
||||||
}
|
}
|
||||||
return $this->memcache->setCache(__FUNCTION__, $aData);
|
return $this->memcache->setCache(__FUNCTION__, @$aData);
|
||||||
}
|
}
|
||||||
// Catchall
|
// Catchall
|
||||||
$this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error);
|
$this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user