From c09b23b663be505c6b2a4dd1da262b5d242648d9 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Thu, 29 Sep 2022 16:45:26 -0300 Subject: [PATCH 1/4] Adding zeros to avoid line/area graph errors with there are gaps on the data Signed-off-by: RD WebDesign --- api_db.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api_db.php b/api_db.php index ec8eaec98..a86afa935 100644 --- a/api_db.php +++ b/api_db.php @@ -339,6 +339,19 @@ function parseDBData($results, $interval, $from, $until) } } + // It is unpredictable what the first timestamp returned by the database will be. + // This depends on live data. The bar graph can handle "gaps", but the Area graph can't. + // Hence, we filling the "missing" timeslots with 0 to avoid wrong graphic render. + // (https://github.com/pi-hole/AdminLTE/pull/2374#issuecomment-1261865428) + $aligned_from = $from + (($first_db_timestamp - $from) % $interval); + + // Fill gaps in returned data + for ($i = $aligned_from; $i < $until; $i += $interval) { + if (!array_key_exists($i, $data)) { + $data[$i] = 0; + } + } + return $data; } From 169d087eafa4f54e8ed5baa417703bcd284ecc81 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Thu, 29 Sep 2022 23:52:59 -0300 Subject: [PATCH 2/4] Use a single SQL command to retrieve (and synchronize) domains and blocked queries. Signed-off-by: RD WebDesign --- api_db.php | 57 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/api_db.php b/api_db.php index a86afa935..6f3b1b3e8 100644 --- a/api_db.php +++ b/api_db.php @@ -295,11 +295,11 @@ $limit = ''; if (isset($_GET['from'], $_GET['until'])) { - $limit = ' AND timestamp >= :from AND timestamp <= :until'; + $limit = 'timestamp >= :from AND timestamp <= :until'; } elseif (isset($_GET['from']) && !isset($_GET['until'])) { - $limit = ' AND timestamp >= :from'; + $limit = 'timestamp >= :from'; } elseif (!isset($_GET['from']) && isset($_GET['until'])) { - $limit = ' AND timestamp <= :until'; + $limit = 'timestamp <= :until'; } $interval = 600; @@ -315,8 +315,25 @@ $from = intval((intval($_GET['from']) / $interval) * $interval); $until = intval((intval($_GET['until']) / $interval) * $interval); + // Count domains and blocked queries using the same intervals + $sqlcommand = " + SELECT + (timestamp / :interval) * :interval AS interval, + SUM(CASE + WHEN status !=0 THEN 1 + ELSE 0 + END) AS domains, + SUM(CASE + WHEN status IN (1,4,5,6,7,8,9,10,11) THEN 1 + ELSE 0 + END) AS blocked + FROM queries + WHERE $limit + GROUP BY interval + ORDER BY interval"; + // Count permitted queries in intervals - $stmt = $db->prepare('SELECT (timestamp/:interval)*:interval interval, COUNT(*) FROM queries WHERE (status != 0 )'.$limit.' GROUP by interval ORDER by interval'); + $stmt = $db->prepare($sqlcommand); $stmt->bindValue(':from', $from, SQLITE3_INTEGER); $stmt->bindValue(':until', $until, SQLITE3_INTEGER); $stmt->bindValue(':interval', $interval, SQLITE3_INTEGER); @@ -325,14 +342,15 @@ // Parse the DB result into graph data, filling in missing interval sections with zero function parseDBData($results, $interval, $from, $until) { - $data = array(); + $domains = array(); + $blocked = array(); $first_db_timestamp = -1; if (!is_bool($results)) { // Read in the data while ($row = $results->fetchArray()) { - // $data[timestamp] = value_in_this_interval - $data[$row[0]] = intval($row[1]); + $domains[$row['interval']] = intval($row['domains']); + $blocked[$row['interval']] = intval($row['blocked']); if ($first_db_timestamp === -1) { $first_db_timestamp = intval($row[0]); } @@ -347,30 +365,17 @@ function parseDBData($results, $interval, $from, $until) // Fill gaps in returned data for ($i = $aligned_from; $i < $until; $i += $interval) { - if (!array_key_exists($i, $data)) { - $data[$i] = 0; + if (!array_key_exists($i, $domains)) { + $domains[$i] = 0; + $blocked[$i] = 0; } } - return $data; + return array('domains_over_time' => $domains, 'ads_over_time' => $blocked); } - $domains = parseDBData($results, $interval, $from, $until); - - $result = array('domains_over_time' => $domains); - $data = array_merge($data, $result); - - // Count blocked queries in intervals - $stmt = $db->prepare('SELECT (timestamp/:interval)*:interval interval, COUNT(*) FROM queries WHERE status IN (1,4,5,6,7,8,9,10,11)'.$limit.' GROUP by interval ORDER by interval'); - $stmt->bindValue(':from', $from, SQLITE3_INTEGER); - $stmt->bindValue(':until', $until, SQLITE3_INTEGER); - $stmt->bindValue(':interval', $interval, SQLITE3_INTEGER); - $results = $stmt->execute(); - - $addomains = parseDBData($results, $interval, $from, $until); - - $result = array('ads_over_time' => $addomains); - $data = array_merge($data, $result); + $over_time = parseDBData($results, $interval, $from, $until); + $data = array_merge($data, $over_time); } if (isset($_GET['status']) && $auth) { From 7c2dbf46628829c11f8932a11b6d8ce138bd7863 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Fri, 30 Sep 2022 18:48:37 -0300 Subject: [PATCH 3/4] Remove forgotten comment Co-authored-by: yubiuser Signed-off-by: RD WebDesign --- api_db.php | 1 - 1 file changed, 1 deletion(-) diff --git a/api_db.php b/api_db.php index 6f3b1b3e8..0be22b689 100644 --- a/api_db.php +++ b/api_db.php @@ -332,7 +332,6 @@ GROUP BY interval ORDER BY interval"; - // Count permitted queries in intervals $stmt = $db->prepare($sqlcommand); $stmt->bindValue(':from', $from, SQLITE3_INTEGER); $stmt->bindValue(':until', $until, SQLITE3_INTEGER); From d47efd10417383288145231e0c2578ba37da9fa6 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Fri, 30 Sep 2022 19:50:15 -0300 Subject: [PATCH 4/4] Add codes 15 and 16 to the blocked count - 15: Blocked (database is busy) - 16: Blocked (special domain) Signed-off-by: RD WebDesign --- api_db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_db.php b/api_db.php index 0be22b689..fca897a2e 100644 --- a/api_db.php +++ b/api_db.php @@ -324,7 +324,7 @@ ELSE 0 END) AS domains, SUM(CASE - WHEN status IN (1,4,5,6,7,8,9,10,11) THEN 1 + WHEN status IN (1,4,5,6,7,8,9,10,11,15,16) THEN 1 ELSE 0 END) AS blocked FROM queries