Fixed refresh interval functions and memory leaks

* [FIX] Memory leak fixed in Hashrate Graph
* [FIX] Potential memory leak in Gauges
* [FIX] Properly refresh data via setTimeout
 * Ensures Ajax calls are completed before re-calling them
This commit is contained in:
Sebastian Grewe 2013-09-15 17:40:24 +02:00
parent 7a73fbf099
commit 286c1d5110
2 changed files with 36 additions and 34 deletions

View File

@ -17,33 +17,43 @@
$(document).ready(function(){
var g1, g2, g3, g4, g5;
// Helper to initilize gauges
function initGauges(data) {
g1 = new JustGage({id: "nethashrate", value: parseFloat(data.getdashboarddata.network.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.network.hashrate * 2), title: "Net Hashrate", label: "{/literal}{$GLOBAL.hashunits.network}{literal}"});
g2 = new JustGage({id: "poolhashrate", value: parseFloat(data.getdashboarddata.pool.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.pool.hashrate * 2), title: "Pool Hashrate", label: "{/literal}{$GLOBAL.hashunits.pool}{literal}"});
g3 = new JustGage({id: "hashrate", value: parseFloat(data.getdashboarddata.personal.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.personal.hashrate * 2), title: "Hashrate", label: "{/literal}{$GLOBAL.hashunits.personal}{literal}"});
g4 = new JustGage({id: "sharerate", value: parseFloat(data.getdashboarddata.personal.sharerate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.personal.sharerate * 2), title: "Sharerate", label: "shares/s"});
g5 = new JustGage({id: "sharerate", value: parseFloat(data.getdashboarddata.runtime).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.runtime * 3), title: "Querytime", label: "ms"});
}
// Helper to refresh gauges
function refreshGauges(data) {
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));
g5.refresh(parseFloat(data.getdashboarddata.runtime).toFixed(2));
}
// Fetch initial data via Ajax
$.ajax({
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getdashboarddata&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}',
async: false, // Run all others requests after this only if it's done
dataType: 'json',
success: function (data) {
g1 = new JustGage({id: "nethashrate", value: parseFloat(data.getdashboarddata.network.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.network.hashrate * 2), title: "Net Hashrate", label: "{/literal}{$GLOBAL.hashunits.network}{literal}"});
g2 = new JustGage({id: "poolhashrate", value: parseFloat(data.getdashboarddata.pool.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.pool.hashrate * 2), title: "Pool Hashrate", label: "{/literal}{$GLOBAL.hashunits.pool}{literal}"});
g3 = new JustGage({id: "hashrate", value: parseFloat(data.getdashboarddata.personal.hashrate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.personal.hashrate * 2), title: "Hashrate", label: "{/literal}{$GLOBAL.hashunits.personal}{literal}"});
g4 = new JustGage({id: "sharerate", value: parseFloat(data.getdashboarddata.personal.sharerate).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.personal.sharerate * 2), title: "Sharerate", label: "shares/s"});
g5 = new JustGage({id: "sharerate", value: parseFloat(data.getdashboarddata.datatime).toFixed(2), min: 0, max: Math.round(data.getdashboarddata.datatime * 3), title: "Querytime", label: "ms"});
}
success: function (data) { initGauges(data); }
});
// Our reload and refresh gauges handler
setInterval(function() {
// Our worker process to keep gauges updated
(function worker() {
$.ajax({
url: '{/literal}{$smarty.server.PHP_SELF}?page=api&action=getdashboarddata&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}',
dataType: 'json',
success: function (data) {
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));
g5.refresh(parseFloat(data.getdashboarddata.datatime).toFixed(2));
success: function(data) { refreshGauges(data); },
complete: function() {
setTimeout(worker, {/literal}{($GLOBAL.config.statistics_ajax_refresh_interval * 1000)|default:"10000"}{literal})
}
});
}, {/literal}{($GLOBAL.config.statistics_ajax_refresh_interval * 1000)|default:"10000"}{literal});
})();
});
{/literal}
</script>

View File

@ -17,11 +17,11 @@
<script>{literal}
$(document).ready(function(){
$.jqplot.config.enablePlugins = true;
// $.jqplot.config.enablePlugins = true;
// Ajax API URL
var url = "{/literal}{$smarty.server.PHP_SELF}?page=api&action=getuserhashrate&api_key={$GLOBAL.userdata.api_key}&id={$GLOBAL.userdata.id}{literal}";
// jqPlit defaults
var jqPlotOptions = {
grid: { drawBorder: false, background: '#fbfbfb', shadow: false },
@ -47,23 +47,13 @@ $(document).ready(function(){
};
// Init empty graph with 0 data
$.jqplot('hashrategraph', [[[]]], jqPlotOptions);
var plot1 = $.jqplot('hashrategraph', [[[]]], jqPlotOptions);
// Store our data globally
var storedData = Array();
// Fetch current datapoint as initial data
$.ajax({
url: url,
dataType: "json",
success: function(data) {
storedData[storedData.length] = [new Date().getTime(), data.getuserhashrate.hashrate];
$.jqplot('hashrategraph', [storedData], jqPlotOptions).replot();
}
});
// Update graph
setInterval(function() {
// Our refresh worker process, updated at intervals
(function workerHashrateGraph() {
$.ajax({
url: url,
dataType: "json",
@ -71,9 +61,11 @@ $(document).ready(function(){
// Start dropping out elements
if (storedData.length > 20) { storedData.shift(); }
storedData[storedData.length] = [new Date().getTime(), data.getuserhashrate.hashrate];
$.jqplot('hashrategraph', [storedData], jqPlotOptions).replot();
}
console.log(storedData);
if (typeof(plot1) != "undefined") plot1.replot({data: [storedData]});
},
complete: function() { setTimeout(workerHashrateGraph, {/literal}{($GLOBAL.config.statistics_ajax_refresh_interval * 1000)|default:"10000"}{literal}) }
});
}, {/literal}{($GLOBAL.config.statistics_ajax_refresh_interval * 1000)|default:"10000"}{literal});
})();
});
{/literal}</script>