From 286c1d511029368ec7155544246a7fa6a3c9d6ee Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 15 Sep 2013 17:40:24 +0200 Subject: [PATCH] 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 --- public/templates/test/dashboard/gauges.tpl | 42 +++++++++++++--------- public/templates/test/dashboard/graph.tpl | 28 ++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/public/templates/test/dashboard/gauges.tpl b/public/templates/test/dashboard/gauges.tpl index 210b4228..77d4fef1 100644 --- a/public/templates/test/dashboard/gauges.tpl +++ b/public/templates/test/dashboard/gauges.tpl @@ -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} diff --git a/public/templates/test/dashboard/graph.tpl b/public/templates/test/dashboard/graph.tpl index f7e3dc74..60421a6d 100644 --- a/public/templates/test/dashboard/graph.tpl +++ b/public/templates/test/dashboard/graph.tpl @@ -17,11 +17,11 @@