web: backport the handling of timezones for night.rrd to xmlrpc alpha API

This commit is contained in:
Bart Van Der Meerssche 2009-09-15 19:46:26 +00:00
parent 2b9d98771e
commit c9ab068642
2 changed files with 19 additions and 7 deletions

View File

@ -2,7 +2,7 @@
// //
// xmlrpc.inc : data and management plane xmlrpc methods for logging metering data // xmlrpc.inc : data and management plane xmlrpc methods for logging metering data
// aplha version API // alpha version API
// //
// Copyright (c) 2008-2009 jokamajo.org // Copyright (c) 2008-2009 jokamajo.org
// //
@ -77,11 +77,11 @@ function _logger_measurement_add($logs) {
} }
system($command, $return); system($command, $return);
if ($return == 0) { if ($return == 0) {
// update the night rrd every day at 6AM UTC // update the night rrd every day at 6AM local time
if (time() > $meterdata->night) { if (time() > $meterdata->night) {
$timestamp = floor(time()/86400)*86400; $midnight = _logger_midnight(time(), $auth['device']);
$start = $timestamp + 3600; $start = $midnight + 7200; // 2AM local time
$end = $start + 10800; //3h time interval $end = $start + 10800; // 3h time interval
$command = $path->root ."/rrdtool fetch ". $path->base . $meter .".rrd AVERAGE -r 900 -s ". $start ." -e ". $end ." | tail -n 12 | awk -F': ' '{SUM += $2} END {print SUM/12}'"; $command = $path->root ."/rrdtool fetch ". $path->base . $meter .".rrd AVERAGE -r 900 -s ". $start ." -e ". $end ." | tail -n 12 | awk -F': ' '{SUM += $2} END {print SUM/12}'";
$night = (float)shell_exec($command); //test shell_exec iso system $night = (float)shell_exec($command); //test shell_exec iso system
$command = $path->root .'/rrdtool update '. $path->night . $meter .'.rrd '. $timestamp .':'. $night; $command = $path->root .'/rrdtool update '. $path->night . $meter .'.rrd '. $timestamp .':'. $night;
@ -92,7 +92,7 @@ function _logger_measurement_add($logs) {
else { else {
watchdog_xmlrpc('logger.measurementAdd', 'error updating night rrd: %command', array('%command' => $command), WATCHDOG_ERROR); //debugging watchdog_xmlrpc('logger.measurementAdd', 'error updating night rrd: %command', array('%command' => $command), WATCHDOG_ERROR); //debugging
} }
$meterdata->night = $timestamp + 104400; //add an offset of 29h, i.e. 5AM UTC next day $meterdata->night = $midnight + 108000; //add an offset of 30h = 6AM local time
} }
// {logger_meters} is updated with the true metervalue $value, NOT $value*$meterdata->factor since we're not normalising this entry! // {logger_meters} is updated with the true metervalue $value, NOT $value*$meterdata->factor since we're not normalising this entry!
db_query("UPDATE {logger_meters} SET access = %d, night = %d, value = %d WHERE meter = '%s'", time(), $meterdata->night, $value, $meter); db_query("UPDATE {logger_meters} SET access = %d, night = %d, value = %d WHERE meter = '%s'", time(), $meterdata->night, $value, $meter);
@ -104,3 +104,15 @@ function _logger_measurement_add($logs) {
} }
return $command; //using $command for testing purposes, replace by $info afterwards return $command; //using $command for testing purposes, replace by $info afterwards
} }
/**
* Calculate the most recent midnight for this device based on the user's timezone
*/
function _logger_midnight($timestamp, $device) {
$timezone = db_result(db_query("SELECT u.timezone FROM {logger_devices} ld INNER JOIN {users} u ON ld.uid = u.uid WHERE ld.device = '%s'", $device));
// add one day to make sure $midnight > $timestamp
$midnight = floor($timestamp/86400 + 1)*86400 - $timezone;
// search for first $midnight < $timestamp
while ($midnight > $timestamp) $midnight -= 86400;
return $midnight;
}

View File

@ -103,7 +103,7 @@ function _logger_measurement_add($auth, $logs) {
system($command, $return); system($command, $return);
if ($return == 0) { if ($return == 0) {
// update the night rrd every day at 6AM UTC // update the night rrd every day at 6AM local time
if (time() > $meterdata->night) { if (time() > $meterdata->night) {
$midnight = _logger_midnight(time(), $auth['device']); $midnight = _logger_midnight(time(), $auth['device']);
$start = $midnight + 7200; // 2AM local time $start = $midnight + 7200; // 2AM local time