Skip to content

Commit

Permalink
Implement Jared Dillard suggestion to use rrd_info() array to get las…
Browse files Browse the repository at this point in the history
…t updated time.

Better self documenting code with more explicit variable names.
Add logic to not use custom end within 1 minute of now.

Doesn't seem necessary to adjust the 'end' time reference anymore than last update time or '-1min'.  So for now just removing the endLookup array.

Without the datakeys code the time period is one resolution too long.  Maybe there is a way to correct for that, but for now just adding the datakeys code back in.
  • Loading branch information
NOYB committed Jun 7, 2016
1 parent a93f57d commit 32c5735
Showing 1 changed file with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@

$rrd_location = "/var/db/rrd/";

//lookup end time based on resolution (ensure resolution interval)
$endLookup = array(
"60" => "-2min",
"300" => "-5min",
"3600" => "-1hour",
"86400" => "-1day"
);

//TODO security/validation checks
$left = $_POST['left'];
$right = $_POST['right'];
Expand All @@ -82,8 +74,17 @@
$left_pieces = explode("-", $left);
$right_pieces = explode("-", $right);

if ($timePeriod === "custom") {
// Get the last updated time of left and right axis RRD, or if not exist set to a very large value (decades into the future)
$rrd_info_array = rrd_info($rrd_location . $left . ".rrd");
$left_last_updated = ($rrd_info_array['last_update']) ? $rrd_info_array['last_update'] : 99999999999;

$rrd_info_array = rrd_info($rrd_location . $right . ".rrd");
$right_last_updated = ($rrd_info_array['last_update']) ? $rrd_info_array['last_update'] : 99999999999;

// Use the earlier (smaller) of left or right last updated time
$last_updated = ($left_last_updated < $right_last_updated) ? $left_last_updated : $right_last_updated;

if ($timePeriod === "custom") {
//dates validation
if($end < $start) {
die ('{ "error" : "The start date must come before the end date." }');
Expand All @@ -95,14 +96,17 @@
$start = floor($start/$resolution) * $resolution;
$end = floor($end/$resolution) * $resolution;

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', $start, '-e', $end );

// Don't use end that is later than the RRD last updated time, or that is within 1 minute of now, to prevent last value 0 (zero).
$end = ($end > $last_updated) ? $last_updated : ((time() - $end) < 60) ? $end : '-1min';
} else {

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', 'e'.$timePeriod, '-e', $endLookup[$resolution] );

// Use end time reference in 'start' to retain time period length.
$start = 'end' . $timePeriod;
// Use the RRD last updated time or '-1min' as end, to prevent last value 0 (zero).
$end = ($last_updated < 99999999999) ? $last_updated : '-1min';
}

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', $start, '-e', $end );

//Initialze
$left_unit_acronym = $right_unit_acronym = "";

Expand Down Expand Up @@ -279,8 +283,12 @@

$data = array();

$dataKeys = array_keys($data_list);
$lastDataKey = array_pop($dataKeys);
foreach ($data_list as $time => $value) {
if($time != $lastDataKey) {
$data[] = array($time*1000, $value*$multiplier);
}
}

$obj[$ds_key_left_adjusted]['values'] = $data;
Expand Down Expand Up @@ -506,8 +514,12 @@

$data = array();

$dataKeys = array_keys($data_list);
$lastDataKey = array_pop($dataKeys);
foreach ($data_list as $time => $value) {
if($time != $lastDataKey) {
$data[] = array($time*1000, $value*$multiplier);
}
}

$obj[$ds_key_right_adjusted]['values'] = $data;
Expand Down

0 comments on commit 32c5735

Please sign in to comment.