mirror of
https://github.com/pi-hole/web.git
synced 2026-04-27 12:15:00 +01:00
Merge pull request #900 from pi-hole/tweak/long_term_data_padding
Always show the full requested range in the long-term API results
This commit is contained in:
34
api_db.php
34
api_db.php
@@ -349,35 +349,31 @@ if (isset($_GET['getGraphData']) && $auth)
|
||||
$interval = $q;
|
||||
}
|
||||
|
||||
// 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", 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) {
|
||||
// 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];
|
||||
|
||||
// Get the non-zero graph data
|
||||
// $data[timestamp] = value_in_this_interval
|
||||
$data[$row[0]] = intval($row[1]);
|
||||
}
|
||||
|
||||
// Fill the missing intervals with zero
|
||||
for($i = $min; $i < $max; $i += $interval) {
|
||||
// Advance in steps of interval
|
||||
for($i = $from; $i < $until; $i += $interval) {
|
||||
if(!array_key_exists($i, $data))
|
||||
$data[$i] = 0;
|
||||
}
|
||||
@@ -386,19 +382,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);
|
||||
|
||||
Reference in New Issue
Block a user