From ab31284f263d095527b36c56ff2c2a565dc71730 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 20 Jan 2019 11:54:15 +0100 Subject: [PATCH 1/4] Always show the full requested range. Fill data with zero at the end when the daterange is extending into the future. Similarly, fill data wil zero at the beginning if the requested range extends into a region without recorded queries. Signed-off-by: DL6ER --- api_db.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/api_db.php b/api_db.php index 7687fb6e..1fe77449 100644 --- a/api_db.php +++ b/api_db.php @@ -378,15 +378,18 @@ if (isset($_GET['getGraphData']) && $auth) $interval = $q; } + $from = intval($_GET['from']); + $until = intval($_GET['until']); + // 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->bindValue(":from", intval($_GET['from']), SQLITE3_INTEGER); - $stmt->bindValue(":until", intval($_GET['until']), SQLITE3_INTEGER); + $stmt->bindValue(":from", $from, SQLITE3_INTEGER); + $stmt->bindValue(":until", $until, SQLITE3_INTEGER); $stmt->bindValue(":interval", $interval, SQLITE3_INTEGER); $results = $stmt->execute(); // Parse the DB result into graph data, filling in missing sections with zero - function parseDBData($results, $interval) { + function parseDBData($results, $interval, $from, $until) { $data = array(); $min = null; $max = null; @@ -406,7 +409,7 @@ if (isset($_GET['getGraphData']) && $auth) } // Fill the missing intervals with zero - for($i = $min; $i < $max; $i += $interval) { + for($i = min($min,$from); $i < max($max,$until); $i += $interval) { if(!array_key_exists($i, $data)) $data[$i] = 0; } @@ -415,19 +418,19 @@ if (isset($_GET['getGraphData']) && $auth) return $data; } - $domains = parseDBData($results, $interval); + $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 == 1 OR status == 4 OR status == 5)'.$limit.' GROUP by interval ORDER by interval'); - $stmt->bindValue(":from", intval($_GET['from']), SQLITE3_INTEGER); - $stmt->bindValue(":until", intval($_GET['until']), SQLITE3_INTEGER); + $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); + $addomains = parseDBData($results, $interval, $from, $until); $result = array('ads_over_time' => $addomains); $data = array_merge($data, $result); From 1096179245b580b90c73243183395645724693a0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 15 Jun 2019 16:24:00 +0200 Subject: [PATCH 2/4] Fix (almost) endless loop when there are no (or no blocked) queries in the selected interval. Signed-off-by: DL6ER --- api_db.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/api_db.php b/api_db.php index 1fe77449..06f7b9a3 100644 --- a/api_db.php +++ b/api_db.php @@ -404,10 +404,17 @@ if (isset($_GET['getGraphData']) && $auth) if($max === null || $max < $row[0]) $max = $row[0]; - // Get the non-zero graph data + // $data[timestamp] = value_in_this_interval $data[$row[0]] = intval($row[1]); } + // Fall back to $from and $until if we read + // no data in the fetchArray while loop above + if($min === null) + $min = $from; + if($max === null) + $max = $until; + // Fill the missing intervals with zero for($i = min($min,$from); $i < max($max,$until); $i += $interval) { if(!array_key_exists($i, $data)) From 7c4dee6f2d2bbaf57adab317c5c05398537be982 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 16 Jun 2019 14:06:52 +0200 Subject: [PATCH 3/4] Simplify getGraphData and round and to match the requested . Signed-off-by: DL6ER --- api_db.php | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/api_db.php b/api_db.php index 06f7b9a3..a5d91b62 100644 --- a/api_db.php +++ b/api_db.php @@ -378,45 +378,24 @@ if (isset($_GET['getGraphData']) && $auth) $interval = $q; } - $from = intval($_GET['from']); - $until = intval($_GET['until']); + // Round $from and $until to match the requested $interval + $from = intval((intval($_GET['from'])/$interval)*$interval); + $until = intval((intval($_GET['until'])/$interval)*$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->bindValue(":from", $from, SQLITE3_INTEGER); - $stmt->bindValue(":until", $until, SQLITE3_INTEGER); - $stmt->bindValue(":interval", $interval, SQLITE3_INTEGER); - $results = $stmt->execute(); - - // Parse the DB result into graph data, filling in missing sections with zero + // Parse the DB result into graph data, filling in missing interval sections with zero function parseDBData($results, $interval, $from, $until) { $data = array(); - $min = null; - $max = null; if(!is_bool($results)) { // Read in the data while($row = $results->fetchArray()) { - // Get min and max timestamps - if($min === null || $min > $row[0]) - $min = $row[0]; - - if($max === null || $max < $row[0]) - $max = $row[0]; - // $data[timestamp] = value_in_this_interval $data[$row[0]] = intval($row[1]); } - // Fall back to $from and $until if we read - // no data in the fetchArray while loop above - if($min === null) - $min = $from; - if($max === null) - $max = $until; - // Fill the missing intervals with zero - for($i = min($min,$from); $i < max($max,$until); $i += $interval) { + // Advance in steps of interval + for($i = $from; $i < $until; $i += $interval) { if(!array_key_exists($i, $data)) $data[$i] = 0; } @@ -425,6 +404,13 @@ if (isset($_GET['getGraphData']) && $auth) return $data; } + // 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->bindValue(":from", $from, SQLITE3_INTEGER); + $stmt->bindValue(":until", $until, SQLITE3_INTEGER); + $stmt->bindValue(":interval", $interval, SQLITE3_INTEGER); + $results = $stmt->execute(); + $domains = parseDBData($results, $interval, $from, $until); $result = array('domains_over_time' => $domains); From fb96e6ff21e0d55bfe0606ba56d14bb302669f79 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 16 Jun 2019 14:08:26 +0200 Subject: [PATCH 4/4] Reposition parseDBData() to simplify diff output Signed-off-by: DL6ER --- api_db.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api_db.php b/api_db.php index a5d91b62..5195114e 100644 --- a/api_db.php +++ b/api_db.php @@ -382,6 +382,13 @@ if (isset($_GET['getGraphData']) && $auth) $from = intval((intval($_GET['from'])/$interval)*$interval); $until = intval((intval($_GET['until'])/$interval)*$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->bindValue(":from", $from, SQLITE3_INTEGER); + $stmt->bindValue(":until", $until, SQLITE3_INTEGER); + $stmt->bindValue(":interval", $interval, SQLITE3_INTEGER); + $results = $stmt->execute(); + // Parse the DB result into graph data, filling in missing interval sections with zero function parseDBData($results, $interval, $from, $until) { $data = array(); @@ -404,13 +411,6 @@ if (isset($_GET['getGraphData']) && $auth) return $data; } - // 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->bindValue(":from", $from, SQLITE3_INTEGER); - $stmt->bindValue(":until", $until, SQLITE3_INTEGER); - $stmt->bindValue(":interval", $interval, SQLITE3_INTEGER); - $results = $stmt->execute(); - $domains = parseDBData($results, $interval, $from, $until); $result = array('domains_over_time' => $domains);