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..1bf21a15 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -29,39 +29,15 @@ function objectToArray(p){ // Functions to update data in page - -function updateSummaryData(runOnce) { - var setTimer = function(timeInSeconds) { - if (!runOnce) { - setTimeout(updateSummaryData, timeInSeconds * 1000); - } - }; - $.getJSON("api.php?summary", function LoadSummaryData(data) { - - ["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] + "%" : data[header]; - $("h3#" + header).text(textData); - }); - $("h3.statistic.glow").removeClass("glow"); - }, 500); - - updateSessionTimer(); - }).done(function() { - setTimer(10); - }).fail(function() { - setTimer(300); - }); -} - var failures = 0; function updateQueriesOverTime() { $.getJSON("api.php?overTimeData10mins", function(data) { + + if("FTLnotrunning" in data) + { + 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 +69,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 +88,12 @@ function updateQueriesOverTime() { function updateForwardedOverTime() { $.getJSON("api.php?overTimeDataForwards&getForwardDestinationNames", function(data) { + + if("FTLnotrunning" in data) + { + return; + } + // convert received objects to arrays data.over_time = objectToArray(data.over_time); var timestamps = data.over_time[0]; @@ -177,7 +159,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 +178,12 @@ function updateForwardedOverTime() { function updateQueryTypes() { $.getJSON("api.php?getQueryTypes", function(data) { + + if("FTLnotrunning" in data) + { + return; + } + var colors = []; // Get colors from AdminLTE $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); @@ -219,7 +207,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 +233,12 @@ function escapeHtml(text) { function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { + + if("FTLnotrunning" in data) + { + return; + } + // Clear tables before filling them with data $("#client-frequency td").parent().remove(); var clienttable = $("#client-frequency").find("tbody:last"); @@ -275,14 +269,20 @@ function updateTopClientsChart() { } - $("#client-frequency .overlay").remove(); - // Update top clients list data every 10 seconds + $("#client-frequency .overlay").hide(); + // Update top clients list data every ten seconds setTimeout(updateTopClientsChart, 10000); }); } function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { + + if("FTLnotrunning" in data) + { + return; + } + // Clear tables before filling them with data $("#domain-frequency td").parent().remove(); $("#ad-frequency td").parent().remove(); @@ -325,13 +325,77 @@ 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); }); } +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 = { 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'; } ?>