From c9ab0686426cfe0161ee20a718c1f172a7809728 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Tue, 15 Sep 2009 19:46:26 +0000 Subject: [PATCH] web: backport the handling of timezones for night.rrd to xmlrpc alpha API --- web/drupal/modules/logger/xmlrpc.inc | 24 ++++++++++++++++++------ web/drupal/modules/logger/xmlrpc1.inc | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/web/drupal/modules/logger/xmlrpc.inc b/web/drupal/modules/logger/xmlrpc.inc index a2a4229..54f5f38 100644 --- a/web/drupal/modules/logger/xmlrpc.inc +++ b/web/drupal/modules/logger/xmlrpc.inc @@ -2,7 +2,7 @@ // // xmlrpc.inc : data and management plane xmlrpc methods for logging metering data -// aplha version API +// alpha version API // // Copyright (c) 2008-2009 jokamajo.org // @@ -77,11 +77,11 @@ function _logger_measurement_add($logs) { } system($command, $return); 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) { - $timestamp = floor(time()/86400)*86400; - $start = $timestamp + 3600; - $end = $start + 10800; //3h time interval + $midnight = _logger_midnight(time(), $auth['device']); + $start = $midnight + 7200; // 2AM local time + $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}'"; $night = (float)shell_exec($command); //test shell_exec iso system $command = $path->root .'/rrdtool update '. $path->night . $meter .'.rrd '. $timestamp .':'. $night; @@ -92,7 +92,7 @@ function _logger_measurement_add($logs) { else { 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! 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 } + +/** + * 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; +} \ No newline at end of file diff --git a/web/drupal/modules/logger/xmlrpc1.inc b/web/drupal/modules/logger/xmlrpc1.inc index fa373d9..f930bc9 100644 --- a/web/drupal/modules/logger/xmlrpc1.inc +++ b/web/drupal/modules/logger/xmlrpc1.inc @@ -103,7 +103,7 @@ function _logger_measurement_add($auth, $logs) { system($command, $return); 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) { $midnight = _logger_midnight(time(), $auth['device']); $start = $midnight + 7200; // 2AM local time