Skip to content

Commit

Permalink
Fill in empty db graph data intervals with zero
Browse files Browse the repository at this point in the history
Signed-off-by: Mcat12 <[email protected]>
  • Loading branch information
AzureMarker committed Jul 22, 2018
1 parent 97967f6 commit 34f89a8
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions api_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,38 @@ function SQLite3_connect($trytoreconnect)
$stmt->bindValue(":interval", $interval, SQLITE3_INTEGER);
$results = $stmt->execute();

$domains = array();
// Parse the DB result into graph data, filling in missing sections with zero
function parseDBData($results, $interval) {
$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[$row[0]] = intval($row[1]);
}

if(!is_bool($results))
while ($row = $results->fetchArray())
{
$domains[$row[0]] = intval($row[1]);
// Fill the missing intervals with zero
for($i = $min; $i < $max; $i += $interval) {
if(!array_key_exists($i, $data))
$data[$i] = 0;
}
}

return $data;
}

$domains = parseDBData($results, $interval);

$result = array('domains_over_time' => $domains);
$data = array_merge($data, $result);

Expand All @@ -342,13 +367,8 @@ function SQLite3_connect($trytoreconnect)
$stmt->bindValue(":interval", $interval, SQLITE3_INTEGER);
$results = $stmt->execute();

$addomains = array();
$addomains = parseDBData($results, $interval);

if(!is_bool($results))
while ($row = $results->fetchArray())
{
$addomains[$row[0]] = intval($row[1]);
}
$result = array('ads_over_time' => $addomains);
$data = array_merge($data, $result);
}
Expand Down

0 comments on commit 34f89a8

Please sign in to comment.