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
This commit is contained in:
Sebastian Grewe 2013-09-14 17:03:19 +02:00
parent c7ca211532
commit 4e19de4c7a
2 changed files with 85 additions and 93 deletions

View File

@ -0,0 +1,69 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY')) die('Hacking attempt');
// Check if the API is activated
$api->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;
?>

View File

@ -11,102 +11,38 @@
<script>
{literal}
window.onload = function(){
// Auto-adjust max value
if ({/literal}{$GLOBAL.nethashrate}{literal} < 20000000000) {
maxVal = 20;
} else if ({/literal}{$GLOBAL.nethashrate}{literal} < 40000000000) {
maxVal = 40;
} else if ({/literal}{$GLOBAL.nethashrate}{literal} < 80000000000) {
maxVal = 80;
} else if ({/literal}{$GLOBAL.nethashrate}{literal} < 160000000000) {
maxVal = 160;
} else if ({/literal}{$GLOBAL.nethashrate}{literal} < 320000000000) {
maxVal = 320;
} else {
maxVal = 1000;
}
var g1 = new JustGage({
id: "nethashrate",
value: {/literal}{($GLOBAL.nethashrate / 1000 / 1000 / 1000)|number_format:"2"}{literal},
value: parseFloat({/literal}{$GLOBAL.nethashrate}{literal}).toFixed(2),
min: 0,
max: maxVal,
max: Math.round({/literal}{$GLOBAL.nethashrate}{literal} * 2),
title: "Net Hashrate",
label: "ghash/s"
label: "{/literal}{$GLOBAL.hashunits.network}{literal}"
});
// Auto-adjust max value
if ({/literal}{$GLOBAL.hashrate}{literal} < 5000) {
maxVal = 5;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 10000) {
maxVal = 10;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 20000) {
maxVal = 20;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 40000) {
maxVal = 40;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 80000) {
maxVal = 80;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 160000) {
maxVal = 160;
} else if ({/literal}{$GLOBAL.hashrate}{literal} < 320000) {
maxVal = 320;
} else {
maxVal = 1000;
}
var g2 = new JustGage({
id: "poolhashrate",
value: {/literal}{$GLOBAL.hashrate / 1000}{literal},
value: parseFloat({/literal}{$GLOBAL.hashrate}{literal}).toFixed(2),
min: 0,
max: maxVal,
max: Math.round({/literal}{$GLOBAL.hashrate}{literal} * 2),
title: "Pool Hashrate",
label: "mhash/s"
label: "{/literal}{$GLOBAL.hashunits.pool}{literal}"
});
// Auto-adjust max value
if ({/literal}{$GLOBAL.userdata.hashrate}{literal} < 1000) {
maxVal = 1;
} else if ({/literal}{$GLOBAL.userdata.hashrate}{literal} < 2000) {
maxVal = 2;
} else if ({/literal}{$GLOBAL.userdata.hashrate}{literal} < 5000) {
maxVal = 5;
} else if ({/literal}{$GLOBAL.userdata.hashrate}{literal} < 10000) {
maxVal = 10;
} else if ({/literal}{$GLOBAL.userdata.hashrate}{literal} < 20000) {
maxVal = 20;
} else {
maxVal = 150;
}
var g3 = new JustGage({
id: "hashrate",
value: {/literal}{$GLOBAL.userdata.hashrate / 1000}{literal},
value: parseFloat({/literal}{$GLOBAL.userdata.hashrate}{literal}).toFixed(2),
min: 0,
max: maxVal,
max: Math.round({/literal}{$GLOBAL.userdata.hashrate}{literal} * 2),
title: "Hashrate",
label: "mhash/s"
label: "{/literal}{$GLOBAL.hashunits.personal}{literal}"
});
// Auto-adjust max value
function findShareMax(val) {
if ( val < 1.0 ) {
maxVal = 1.0;
} else if ( val < 2.0 ) {
maxVal = 2.0;
} else if ( val < 5.0 ) {
maxVal = 5.0;
} else if ( val < 10.0 ) {
maxVal = 10.0;
} else if ( val < 20.0 ) {
maxVal = 20.0;
} else {
maxVal = 100.0;
}
return maxVal;
};
var g4 = new JustGage({
id: "sharerate",
value: {/literal}{$GLOBAL.userdata.sharerate|number_format:"2"}{literal},
value: parseFloat({/literal}{$GLOBAL.userdata.sharerate}{literal}).toFixed(2),
min: 0,
max: findShareMax({/literal}{$GLOBAL.userdata.sharerate|number_format:"2"}{literal}),
max: Math.round({/literal}{$GLOBAL.userdata.sharerate}{literal} * 2),
title: "Sharerate",
label: "shares/s"
});
@ -114,29 +50,16 @@ window.onload = function(){
// Our reload and refresh gauges handler
setInterval(function() {
$.ajax({
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getpoolhashrate&api_key={$GLOBAL.userdata.api_key}{literal}',
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getdashboarddata&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}',
dataType: 'json',
async: false,
success: function (data) {
g2.refresh(parseFloat(data.getpoolhashrate.hashrate / 1000).toFixed(3));
g1.refresh(parseFloat(data.getdashboarddata.network.hashrate).toFixed(2));
g2.refresh(parseFloat(data.getdashboarddata.pool.hashrate).toFixed(2));
g3.refresh(parseFloat(data.getdashboarddata.personal.hashrate).toFixed(2));
g4.refresh(parseFloat(data.getdashboarddata.personal.sharerate).toFixed(2));
}
});
$.ajax({
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getuserhashrate&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}',
dataType: 'json',
async: false,
success: function (data) {
g3.refresh(parseFloat(data.getuserhashrate.hashrate / 1000).toFixed(3));
},
});
$.ajax({
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getusersharerate&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}',
dataType: 'json',
async: false,
success: function (data) {
g4.refresh(parseFloat(data.getusersharerate.sharerate).toFixed(2));
},
});
}, 2000);
};
{/literal}