From 35dcb9b5ed7c6c6c200008b07f718bef1ee1a9e0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 21:30:03 +0100 Subject: [PATCH 1/6] Improve fallback handling of the dashboard for when FTL is not running --- api.php | 13 +++++-- scripts/pi-hole/js/index.js | 76 ++++++++++++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/api.php b/api.php index d75b55ff..d9fe1433 100644 --- a/api.php +++ b/api.php @@ -62,13 +62,20 @@ elseif (isset($_GET['disable']) && $auth) } // Other API functions -if(testFTL() && !isset($_GET["PHP"])) +if(!testFTL() && !isset($_GET["PHP"])) { - require("api_FTL.php"); + $data = array_merge($data, array("FTLnotrunning" => true)); } else { - require("api_PHP.php"); + if(!isset($_GET["PHP"])) + { + require("api_FTL.php"); + } + else + { + require("api_PHP.php"); + } } if(isset($_GET["jsonForceObject"])) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 8fdbde5d..75a45112 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -38,6 +38,18 @@ function updateSummaryData(runOnce) { }; $.getJSON("api.php?summary", function LoadSummaryData(data) { + updateSessionTimer(); + + if("FTLnotrunning" in data) + { + // Try again in ten seconds + setTimer(10); + data["ads_blocked_today"] = "---"; + data["dns_queries_today"] = "---"; + data["domains_being_blocked"] = "---"; + data["ads_percentage_today"] = "---"; + } + ["ads_blocked_today", "dns_queries_today", "ads_percentage_today"].forEach(function(today) { var todayElement = $("h3#" + today); todayElement.text() !== data[today] && todayElement.addClass("glow"); @@ -45,13 +57,12 @@ function updateSummaryData(runOnce) { window.setTimeout(function() { ["ads_blocked_today", "dns_queries_today", "domains_being_blocked", "ads_percentage_today"].forEach(function(header, idx) { - var textData = idx === 3 ? data[header] + "%" : data[header]; + var textData = (idx === 3 && data[header] !== "---") ? data[header] + "%" : data[header]; $("h3#" + header).text(textData); }); $("h3.statistic.glow").removeClass("glow"); }, 500); - updateSessionTimer(); }).done(function() { setTimer(10); }).fail(function() { @@ -62,6 +73,15 @@ function updateSummaryData(runOnce) { var failures = 0; function updateQueriesOverTime() { $.getJSON("api.php?overTimeData10mins", function(data) { + + if("FTLnotrunning" in data) + { + // Show spinner + $("#queries-over-time .overlay").show(); + // Try again in ten seconds + setTimeout(updateQueriesOverTime, 10000); + return; + } // convert received objects to arrays data.domains_over_time = objectToArray(data.domains_over_time); data.ads_over_time = objectToArray(data.ads_over_time); @@ -93,7 +113,7 @@ function updateQueriesOverTime() { timeLineChart.data.datasets[1].data.push(data.ads_over_time[1][hour]); } } - $("#queries-over-time .overlay").remove(); + $("#queries-over-time .overlay").hide(); timeLineChart.update(); }).done(function() { // Reload graph after 10 minutes @@ -112,6 +132,15 @@ function updateQueriesOverTime() { function updateForwardedOverTime() { $.getJSON("api.php?overTimeDataForwards&getForwardDestinationNames", function(data) { + + if("FTLnotrunning" in data) + { + // Show spinner + $("#forward-destinations .overlay").show(); + // Try again in ten seconds + setTimeout(updateForwardedOverTime, 10000); + return; + } // convert received objects to arrays data.over_time = objectToArray(data.over_time); var timestamps = data.over_time[0]; @@ -177,7 +206,7 @@ function updateForwardedOverTime() { var d = new Date(1000*parseInt(timestamps[j])); forwardDestinationChart.data.labels.push(d); } - $("#forward-destinations .overlay").remove(); + $("#forward-destinations .overlay").hide(); forwardDestinationChart.update(); }).done(function() { // Reload graph after 10 minutes @@ -196,6 +225,15 @@ function updateForwardedOverTime() { function updateQueryTypes() { $.getJSON("api.php?getQueryTypes", function(data) { + + if("FTLnotrunning" in data) + { + // Show spinner + $("#query-types .overlay").show(); + // Try again in ten seconds + setTimeout(updateQueryTypes, 10000); + return; + } var colors = []; // Get colors from AdminLTE $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); @@ -219,7 +257,7 @@ function updateQueryTypes() { // and push it at once queryTypeChart.data.datasets[0] = dd; queryTypeChart.data.labels = k; - $("#query-types .overlay").remove(); + $("#query-types .overlay").hide(); queryTypeChart.update(); queryTypeChart.chart.config.options.cutoutPercentage=50; queryTypeChart.update(); @@ -245,6 +283,15 @@ function escapeHtml(text) { function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { + + if("FTLnotrunning" in data) + { + // Show spinner + $("#client-frequency .overlay").show(); + // Try again in ten seconds + setTimeout(updateTopClientsChart, 10000); + return; + } // Clear tables before filling them with data $("#client-frequency td").parent().remove(); var clienttable = $("#client-frequency").find("tbody:last"); @@ -275,14 +322,23 @@ function updateTopClientsChart() { } - $("#client-frequency .overlay").remove(); - // Update top clients list data every 10 seconds - setTimeout(updateTopClientsChart, 10000); + $("#client-frequency .overlay").hide(); + // Update top clients list data every second + setTimeout(updateTopClientsChart, 1000); }); } function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { + if("FTLnotrunning" in data) + { + // Show spinner + $("#domain-frequency .overlay").show(); + $("#ad-frequency .overlay").show(); + // Try again in ten seconds + setTimeout(updateTopLists, 10000); + return; + } // Clear tables before filling them with data $("#domain-frequency td").parent().remove(); $("#ad-frequency td").parent().remove(); @@ -325,8 +381,8 @@ function updateTopLists() { $("#ad-frequency").parent().remove(); } - $("#domain-frequency .overlay").remove(); - $("#ad-frequency .overlay").remove(); + $("#domain-frequency .overlay").hide(); + $("#ad-frequency .overlay").hide(); // Update top lists data every 10 seconds setTimeout(updateTopLists, 10000); }); From 17fb960a011e0c1f23182d7aea41e68d3994069c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 21:35:16 +0100 Subject: [PATCH 2/6] Update statistics every second --- scripts/pi-hole/js/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 75a45112..647e07cb 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -64,7 +64,7 @@ function updateSummaryData(runOnce) { }, 500); }).done(function() { - setTimer(10); + setTimer(1); }).fail(function() { setTimer(300); }); @@ -323,8 +323,8 @@ function updateTopClientsChart() { } $("#client-frequency .overlay").hide(); - // Update top clients list data every second - setTimeout(updateTopClientsChart, 1000); + // Update top clients list data every ten seconds + setTimeout(updateTopClientsChart, 10000); }); } From ebef61614d46ab8f3bef0967f5ee7590decee02d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 21:57:44 +0100 Subject: [PATCH 3/6] Handle FTL offline notification and instant refresh when it becomes available --- scripts/pi-hole/js/index.js | 59 +++++++++++++++++++--------------- scripts/pi-hole/php/header.php | 2 +- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 647e07cb..366e4773 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -42,12 +42,34 @@ function updateSummaryData(runOnce) { if("FTLnotrunning" in data) { + data["ads_blocked_today"] = "No"; + data["dns_queries_today"] = "FTL"; + data["ads_percentage_today"] = "power"; + data["domains_being_blocked"] = "available"; + // Adjust text + $("#temperature").html(" FTL offline"); + // Show spinner + $("#queries-over-time .overlay").show(); + $("#forward-destinations .overlay").show(); + $("#query-types .overlay").show(); + $("#client-frequency .overlay").show(); + $("#domain-frequency .overlay").show(); + $("#ad-frequency .overlay").show(); // Try again in ten seconds setTimer(10); - data["ads_blocked_today"] = "---"; - data["dns_queries_today"] = "---"; - data["domains_being_blocked"] = "---"; - data["ads_percentage_today"] = "---"; + } + else + { + if($("#temperature").text().search("FTL offline") > -1) + { + // FTL was previously offline + $("#temperature").text(" "); + updateQueriesOverTime(); + updateForwardedOverTime(); + updateQueryTypes(); + updateTopClientsChart(); + updateTopLists(); + } } ["ads_blocked_today", "dns_queries_today", "ads_percentage_today"].forEach(function(today) { @@ -57,7 +79,7 @@ function updateSummaryData(runOnce) { window.setTimeout(function() { ["ads_blocked_today", "dns_queries_today", "domains_being_blocked", "ads_percentage_today"].forEach(function(header, idx) { - var textData = (idx === 3 && data[header] !== "---") ? data[header] + "%" : data[header]; + var textData = (idx === 3 && data[header] !== "power") ? data[header] + "%" : data[header]; $("h3#" + header).text(textData); }); $("h3.statistic.glow").removeClass("glow"); @@ -76,12 +98,9 @@ function updateQueriesOverTime() { if("FTLnotrunning" in data) { - // Show spinner - $("#queries-over-time .overlay").show(); - // Try again in ten seconds - setTimeout(updateQueriesOverTime, 10000); return; } + // convert received objects to arrays data.domains_over_time = objectToArray(data.domains_over_time); data.ads_over_time = objectToArray(data.ads_over_time); @@ -135,12 +154,9 @@ function updateForwardedOverTime() { if("FTLnotrunning" in data) { - // Show spinner - $("#forward-destinations .overlay").show(); - // Try again in ten seconds - setTimeout(updateForwardedOverTime, 10000); return; } + // convert received objects to arrays data.over_time = objectToArray(data.over_time); var timestamps = data.over_time[0]; @@ -228,12 +244,9 @@ function updateQueryTypes() { if("FTLnotrunning" in data) { - // Show spinner - $("#query-types .overlay").show(); - // Try again in ten seconds - setTimeout(updateQueryTypes, 10000); return; } + var colors = []; // Get colors from AdminLTE $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); @@ -286,12 +299,9 @@ function updateTopClientsChart() { if("FTLnotrunning" in data) { - // Show spinner - $("#client-frequency .overlay").show(); - // Try again in ten seconds - setTimeout(updateTopClientsChart, 10000); return; } + // Clear tables before filling them with data $("#client-frequency td").parent().remove(); var clienttable = $("#client-frequency").find("tbody:last"); @@ -330,15 +340,12 @@ function updateTopClientsChart() { function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { + if("FTLnotrunning" in data) { - // Show spinner - $("#domain-frequency .overlay").show(); - $("#ad-frequency .overlay").show(); - // Try again in ten seconds - setTimeout(updateTopLists, 10000); return; } + // Clear tables before filling them with data $("#domain-frequency td").parent().remove(); $("#ad-frequency td").parent().remove(); diff --git a/scripts/pi-hole/php/header.php b/scripts/pi-hole/php/header.php index 4c4d6e25..cb56f1e5 100644 --- a/scripts/pi-hole/php/header.php +++ b/scripts/pi-hole/php/header.php @@ -323,7 +323,7 @@ } else { - echo ' FTL offline'; + echo ' FTL offline'; } ?>
From 8d4864eb17949c3ee3d8396c58723ee8743fd3be Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 22:01:58 +0100 Subject: [PATCH 4/6] Add delay of 0.5 sec before trying to load all data --- scripts/pi-hole/js/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 366e4773..ca060818 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -64,11 +64,13 @@ function updateSummaryData(runOnce) { { // FTL was previously offline $("#temperature").text(" "); - updateQueriesOverTime(); - updateForwardedOverTime(); - updateQueryTypes(); - updateTopClientsChart(); - updateTopLists(); + window.setTimeout(function() { + updateQueriesOverTime(); + updateForwardedOverTime(); + updateQueryTypes(); + updateTopClientsChart(); + updateTopLists(); + }, 500); } } From 0043c229d56eeaef721e902914d401c5828f2a2e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 22:21:30 +0100 Subject: [PATCH 5/6] Change logic --- scripts/pi-hole/js/index.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index ca060818..27260f1b 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -29,7 +29,7 @@ function objectToArray(p){ // Functions to update data in page - +var FTLoffline = false; function updateSummaryData(runOnce) { var setTimer = function(timeInSeconds) { if (!runOnce) { @@ -55,22 +55,21 @@ function updateSummaryData(runOnce) { $("#client-frequency .overlay").show(); $("#domain-frequency .overlay").show(); $("#ad-frequency .overlay").show(); - // Try again in ten seconds - setTimer(10); + + FTLoffline = true; } else { - if($("#temperature").text().search("FTL offline") > -1) + if(FTLoffline) { // FTL was previously offline + FTLoffline = false; $("#temperature").text(" "); - window.setTimeout(function() { - updateQueriesOverTime(); - updateForwardedOverTime(); - updateQueryTypes(); - updateTopClientsChart(); - updateTopLists(); - }, 500); + updateQueriesOverTime(); + updateForwardedOverTime(); + updateQueryTypes(); + updateTopClientsChart(); + updateTopLists(); } } From 8b91c8c12e29b97d70d2297907adef30f62ddabb Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Mar 2017 23:21:15 +0100 Subject: [PATCH 6/6] Codacy and changed text --- scripts/pi-hole/js/index.js | 128 ++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 27260f1b..1bf21a15 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -29,70 +29,6 @@ function objectToArray(p){ // Functions to update data in page -var FTLoffline = false; -function updateSummaryData(runOnce) { - var setTimer = function(timeInSeconds) { - if (!runOnce) { - setTimeout(updateSummaryData, timeInSeconds * 1000); - } - }; - $.getJSON("api.php?summary", function LoadSummaryData(data) { - - updateSessionTimer(); - - if("FTLnotrunning" in data) - { - data["ads_blocked_today"] = "No"; - data["dns_queries_today"] = "FTL"; - data["ads_percentage_today"] = "power"; - data["domains_being_blocked"] = "available"; - // Adjust text - $("#temperature").html(" FTL offline"); - // Show spinner - $("#queries-over-time .overlay").show(); - $("#forward-destinations .overlay").show(); - $("#query-types .overlay").show(); - $("#client-frequency .overlay").show(); - $("#domain-frequency .overlay").show(); - $("#ad-frequency .overlay").show(); - - FTLoffline = true; - } - else - { - if(FTLoffline) - { - // FTL was previously offline - FTLoffline = false; - $("#temperature").text(" "); - updateQueriesOverTime(); - updateForwardedOverTime(); - updateQueryTypes(); - updateTopClientsChart(); - updateTopLists(); - } - } - - ["ads_blocked_today", "dns_queries_today", "ads_percentage_today"].forEach(function(today) { - var todayElement = $("h3#" + today); - todayElement.text() !== data[today] && todayElement.addClass("glow"); - }); - - window.setTimeout(function() { - ["ads_blocked_today", "dns_queries_today", "domains_being_blocked", "ads_percentage_today"].forEach(function(header, idx) { - var textData = (idx === 3 && data[header] !== "power") ? data[header] + "%" : data[header]; - $("h3#" + header).text(textData); - }); - $("h3.statistic.glow").removeClass("glow"); - }, 500); - - }).done(function() { - setTimer(1); - }).fail(function() { - setTimer(300); - }); -} - var failures = 0; function updateQueriesOverTime() { $.getJSON("api.php?overTimeData10mins", function(data) { @@ -396,6 +332,70 @@ function updateTopLists() { }); } +var FTLoffline = false; +function updateSummaryData(runOnce) { + var setTimer = function(timeInSeconds) { + if (!runOnce) { + setTimeout(updateSummaryData, timeInSeconds * 1000); + } + }; + $.getJSON("api.php?summary", function LoadSummaryData(data) { + + updateSessionTimer(); + + if("FTLnotrunning" in data) + { + data["ads_blocked_today"] = "Lost"; + data["dns_queries_today"] = "connection"; + data["ads_percentage_today"] = "to"; + data["domains_being_blocked"] = "API"; + // Adjust text + $("#temperature").html(" FTL offline"); + // Show spinner + $("#queries-over-time .overlay").show(); + $("#forward-destinations .overlay").show(); + $("#query-types .overlay").show(); + $("#client-frequency .overlay").show(); + $("#domain-frequency .overlay").show(); + $("#ad-frequency .overlay").show(); + + FTLoffline = true; + } + else + { + if(FTLoffline) + { + // FTL was previously offline + FTLoffline = false; + $("#temperature").text(" "); + updateQueriesOverTime(); + updateForwardedOverTime(); + updateQueryTypes(); + updateTopClientsChart(); + updateTopLists(); + } + } + + ["ads_blocked_today", "dns_queries_today", "ads_percentage_today"].forEach(function(today) { + var todayElement = $("h3#" + today); + todayElement.text() !== data[today] && todayElement.addClass("glow"); + }); + + window.setTimeout(function() { + ["ads_blocked_today", "dns_queries_today", "domains_being_blocked", "ads_percentage_today"].forEach(function(header, idx) { + var textData = (idx === 3 && data[header] !== "to") ? data[header] + "%" : data[header]; + $("h3#" + header).text(textData); + }); + $("h3.statistic.glow").removeClass("glow"); + }, 500); + + }).done(function() { + setTimer(1); + }).fail(function() { + setTimer(300); + }); +} + $(document).ready(function() { var isMobile = {