From 4e19de4c7a92f872d6dffbce52fdf325815aaeb1 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sat, 14 Sep 2013 17:03:19 +0200 Subject: [PATCH] Unified gauges Ajax * Single call for all data * Use proper hash units as configured in pool * New API call for dashboard data that has adjusted output --- .../pages/api/getdashboarddata.inc.php | 69 +++++++++++ public/templates/test/dashboard/gauges.tpl | 109 +++--------------- 2 files changed, 85 insertions(+), 93 deletions(-) create mode 100644 public/include/pages/api/getdashboarddata.inc.php diff --git a/public/include/pages/api/getdashboarddata.inc.php b/public/include/pages/api/getdashboarddata.inc.php new file mode 100644 index 00000000..a36b4bb2 --- /dev/null +++ b/public/include/pages/api/getdashboarddata.inc.php @@ -0,0 +1,69 @@ +isActive(); + +// Check user token +$user_id = $user->checkApiKey($_REQUEST['api_key']); + +/** + * This check will ensure the user can do the following: + * Admin: Check any user via request id + * Regular: Check your own status + * Other: Deny access via checkApiKey + **/ +if ( ! $user->isAdmin($user_id) && ($_REQUEST['id'] != $user_id && !empty($_REQUEST['id']))) { + // User is admin and tries to access an ID that is not their own + header("HTTP/1.1 401 Unauthorized"); + die("Access denied"); +} else if ($user->isAdmin($user_id)) { + // Admin, so allow any ID passed in request + $id = $_REQUEST['id']; + // Is it a username or a user ID + ctype_digit($_REQUEST['id']) ? $username = $user->getUserName($_REQUEST['id']) : $username = $_REQUEST['id']; + ctype_digit($_REQUEST['id']) ? $id = $_REQUEST['id'] : $id = $user->getUserId($_REQUEST['id']); +} else { + // Not admin, only allow own user ID + $id = $user_id; + $username = $user->getUserName($id); +} + +// Fetch raw RPC data +$bitcoin->can_connect() === true ? $dNetworkHashrate = $bitcoin->query('getnetworkhashps') : $dNetworkHashrate = 0; + +// Some settings +$start = microtime(true); +$interval = 300; +if ( ! $dPoolHashrateModifier = $setting->getValue('statistics_pool_hashrate_modifier') ) $dPoolHashrateModifier = 1; +if ( ! $dPersonalHashrateModifier = $setting->getValue('statistics_personal_hashrate_modifier') ) $dPersonalHashrateModifier = 1; +if ( ! $dNetworkHashrateModifier = $setting->getValue('statistics_network_hashrate_modifier') ) $dNetworkHashrateModifier = 1; + +// Fetch raw data +$statistics->setGetCache(false); +$dPoolHashrate = $statistics->getCurrentHashrate($interval); +if ($dPoolHashrate > $dNetworkHashrate) $dNetworkHashrate = $dPoolHashrate; +$dPersonalHashrate = $statistics->getUserHashrate($id, $interval); +$dPersonalSharerate = $statistics->getUserSharerate($id, $interval); +$statistics->setGetCache(true); + +$runtime = (microtime(true) - $start) * 1000; + +// Apply pool modifiers +$dPersonalHashrate = $dPersonalHashrate * $dPersonalHashrateModifier; +$dPoolHashrate = $dPoolHashrate * $dPoolHashrateModifier; +$dNetworkHashrate = $dNetworkHashrate / 1000 * $dNetworkHashrateModifier; + +// Output JSON format +echo json_encode(array($_REQUEST['action'] => array( + 'datatime' => $runtime, + 'personal' => array ( 'hashrate' => $dPersonalHashrate, 'sharerate' => $dPersonalSharerate ), + 'pool' => array( 'hashrate' => $dPoolHashrate ), + 'network' => array( 'hashrate' => $dNetworkHashrate ), +))); + +// Supress master template +$supress_master = 1; +?> diff --git a/public/templates/test/dashboard/gauges.tpl b/public/templates/test/dashboard/gauges.tpl index e1b340fd..a279060e 100644 --- a/public/templates/test/dashboard/gauges.tpl +++ b/public/templates/test/dashboard/gauges.tpl @@ -11,102 +11,38 @@