From 73df3bddb6a23eb79fea80e93e9c71dc46eeff82 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 10 May 2018 15:11:22 +0200 Subject: [PATCH] Add "Top Clients (blocked only)" table Signed-off-by: DL6ER --- index.php | 34 +++++++++++++++++++++++++--- scripts/pi-hole/js/index.js | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index fe028232..b75edec5 100644 --- a/index.php +++ b/index.php @@ -7,7 +7,7 @@ * Please see LICENSE file for your rights under this license. */ $indexpage = true; require "scripts/pi-hole/php/header.php"; - require_once("scripts/pi-hole/php/gravity.php"); + require_once("scripts/pi-hole/php/gravity.php"); ?>
@@ -217,7 +217,7 @@ if($boxedlayout) } else { - $tablelayout = "col-md-6 col-lg-4"; + $tablelayout = "col-md-6 col-lg-6"; } ?>
@@ -278,7 +278,35 @@ else
-

Top Clients

+

Top Clients (total)

+
+ +
+
+ + + + + + + + +
ClientRequestsFrequency
+
+
+
+ +
+ +
+ +
+ + +
+
+
+

Top Clients (blocked only)

diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index 390c19b7..0fd513c4 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -360,7 +360,6 @@ function updateForwardedOverTime() { }); } - function updateClientsOverTime() { $.getJSON("api.php?overTimeDataClients&getClientNames", function(data) { @@ -527,7 +526,7 @@ function escapeHtml(text) { } function updateTopClientsChart() { - $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { + $.getJSON("api.php?summaryRaw&getQuerySources&topClientsBlocked", function(data) { if("FTLnotrunning" in data) { @@ -566,17 +565,56 @@ function updateTopClientsChart() { " " + data.top_sources[client] + "
"); } + } + // Clear tables before filling them with data + $("#client-frequency-blocked td").parent().remove(); + var clientblockedtable = $("#client-frequency-blocked").find("tbody:last"); + var client, percentage, clientname, clientip; + for (client in data.top_sources_blocked) { + + if ({}.hasOwnProperty.call(data.top_sources_blocked, client)){ + // Sanitize client + if(escapeHtml(client) !== client) + { + // Make a copy with the escaped index if necessary + data.top_sources_blocked[escapeHtml(client)] = data.top_sources_blocked[client]; + } + client = escapeHtml(client); + if(client.indexOf("|") > -1) + { + var idx = client.indexOf("|"); + clientname = client.substr(0, idx); + clientip = client.substr(idx+1, client.length-idx); + } + else + { + clientname = client; + clientip = client; + } + + var url = ""+clientname+""; + percentage = data.top_sources_blocked[client] / data.dns_queries_today * 100; + clientblockedtable.append(" " + url + + " " + data.top_sources_blocked[client] + "
"); + } } // Remove table if there are no results (e.g. privacy mode enabled) if(jQuery.isEmptyObject(data.top_sources)) { $("#client-frequency").parent().remove(); - return; + } + + // Remove table if there are no results (e.g. privacy mode enabled) + if(jQuery.isEmptyObject(data.top_sources_blocked)) + { + $("#client-frequency-blocked").parent().remove(); } $("#client-frequency .overlay").hide(); + $("#client-frequency-blocked .overlay").hide(); // Update top clients list data every ten seconds setTimeout(updateTopClientsChart, 10000); });