web: introduce multiple electricity sensors per user

This commit is contained in:
Bart Van Der Meerssche 2010-02-18 21:53:31 +00:00
parent 3dead16060
commit 89dd35a82b
1 changed files with 66 additions and 55 deletions

View File

@ -229,68 +229,71 @@ function _logger_dashboard($type, $function, $interval) {
break; break;
} }
$meter = db_fetch_object(db_query("SELECT lm.meter, lm.unit, lu.private $result_me = db_query("SELECT lm.meter, lm.function, lm.unit, lu.private
FROM {logger_meters} lm FROM {logger_meters} lm
INNER JOIN {logger_users} lu on lm.uid = lu.uid INNER JOIN {logger_users} lu on lm.uid = lu.uid
WHERE lm.uid = %d AND lm.type = '%s' AND lm.function = '%s'", $user->uid, $type, $function)); WHERE lm.uid = %d AND lm.type = '%s'
ORDER BY lm.function", $user->uid, $type);
$meter_me = db_fetch_object($result_me);
$result_sub = db_query("SELECT u.name, lm.meter, lu.private
FROM ((({users} u
INNER JOIN {user_relationships} ur ON u.uid = ur.requestee_id)
INNER JOIN {user_relationship_types} urt ON ur.rtid = urt.rtid)
INNER JOIN {logger_meters} lm ON u.uid = lm.uid)
INNER JOIN {logger_users} lu ON u.uid = lu.uid
WHERE ur.requester_id = %d AND urt.name = '%s' AND type = '%s' AND function = '%s'
ORDER BY ur.rid", $user->uid, 'subscription', $type, $function);
switch ($type) { switch ($type) {
case 'electricity': case 'electricity':
switch ($meter->unit) { switch ($meter_me->unit) {
case 'watt': case 'watt':
$meter->factor = 3600; // 1Wh/s = 3600 W $unit = 'watt';
$factor = 3600; // 1Wh/s = 3600 W
break; break;
case 'kwh': case 'kwh':
$meter->unit = 'kWh/year'; $unit = 'kWh/year';
$meter->factor = 31536; $factor = 31536;
break; break;
case 'eur': case 'eur':
$meter->unit = 'euro/year'; $unit = 'euro/year';
$meter->factor = 5676; // 18 EURcent/kWh $factor = 5676; // 18 EURcent/kWh
break; break;
case 'aud': case 'aud':
$meter->unit = 'aud/year'; $unit = 'aud/year';
$meter->factor = 5991; // 19 AUDcent/kWh $factor = 5991; // 19 AUDcent/kWh
break; break;
} }
} }
$color = array(RED, BLUE, GREEN, YELLOW, PURPLE); $color = array(RED, BLUE, GREEN, YELLOW, PURPLE);
$string->def = ' DEF:data0='. $data_path . $meter->meter .'.rrd:meter:AVERAGE' . $i = 0;
' CDEF:meter0=data0,'. $meter->factor .',*' .
' VDEF:min0=meter0,MINIMUM'.
' VDEF:max0=meter0,MAXIMUM'.
' VDEF:avg0=meter0,AVERAGE'.
' VDEF:last0=meter0,LAST';
$string->line = ' COMMENT:"\s"'. if (!$meter_sub = db_fetch_object($result_sub)) {
' LINE1:meter0#'. $color[0] .':'.'"'. substr($user->name.' ', 0, 15) .'"'. do {
' GPRINT:min0:"min\:%5.0lf"'. $string->def .= _logger_chart_def($i, $data_path, $meter_me->meter, $factor);
' GPRINT:max0:"\tmax\:%5.0lf"'. $string->line .= _logger_chart_line($i, $color[$i], $meter_me->function);
' GPRINT:avg0:"\tavg\:%5.0lf"'. $i++;
' GPRINT:last0:"\tlast\:%5.0lf\l"'; } while ($meter_me = db_fetch_object($result_me));
}
elseif (user_access('logger') && !$meter_me->private) {
$string->def .= _logger_chart_def($i, $data_path, $meter_me->meter, $factor);
$string->line .= _logger_chart_line($i, $color[$i], $user->name);
if (user_access('logger') && !$meter->private) { $i++;
$result = db_query("SELECT u.name, lm.meter, lu.private do {
FROM ((({users} u // check whether the person who's datastream you'd like to superimpose on the graph is in private mode
INNER JOIN {user_relationships} ur ON u.uid = ur.requestee_id) if ($meter_sub->private) {
INNER JOIN {user_relationship_types} urt ON ur.rtid = urt.rtid)
INNER JOIN {logger_meters} lm ON u.uid = lm.uid)
INNER JOIN {logger_users} lu ON u.uid = lu.uid
WHERE ur.requester_id = %d AND urt.name = '%s' AND type = '%s' AND function = '%s'
ORDER BY ur.rid", $user->uid, 'subscription', $type, $function);
$i = 0;
while ($subscription = db_fetch_object($result)) {
$i += 1;
// check whether the person who's datastream you'd like to superimpose on the graph is in private mode
if ($subscription->private) {
// shift the data left by 1 year = 31556926 seconds to prevent the datastream from being displayed // shift the data left by 1 year = 31556926 seconds to prevent the datastream from being displayed
$string->def .= ' DEF:data'. $i .'='. $data_path . $subscription->meter .'.rrd:meter:AVERAGE'. $string->def .= ' DEF:data'. $i .'='. $data_path . $meter_sub->meter .'.rrd:meter:AVERAGE'.
' CDEF:meter'. $i .'=data'. $i .','. $meter->factor .',*'. ' CDEF:meter'. $i .'=data'. $i .','. $factor .',*'.
' SHIFT:meter'. $i .':-31556926'; ' SHIFT:meter'. $i .':-31556926';
// substitute the actual min/max/avg/last values by 'prv' // substitute the actual min/max/avg/last values by 'prv'
$string->line .= ' LINE1:meter'. $i .'#'. $color[$i] .':'.'"'. substr($subscription->name.' ', 0, 15) .'"'. $string->line .= ' LINE1:meter'. $i .'#'. $color[$i] .':'.'"'. substr($meter_sub->name.' ', 0, 15) .'"'.
' COMMENT:"min\: prv"'. ' COMMENT:"min\: prv"'.
' COMMENT:"\tmax\: prv"'. ' COMMENT:"\tmax\: prv"'.
' COMMENT:"\tavg\: prv"'. ' COMMENT:"\tavg\: prv"'.
@ -299,20 +302,11 @@ function _logger_dashboard($type, $function, $interval) {
// the peer has not set his datastream to private so print it! // the peer has not set his datastream to private so print it!
else { else {
$string->def .= ' DEF:data'. $i .'='. $data_path . $subscription->meter .'.rrd:meter:AVERAGE'. $string->def .= _logger_chart_def($i, $data_path, $meter_sub->meter, $factor);
' CDEF:meter'. $i .'=data'. $i .','. $meter->factor .',*'. $string->line .= _logger_chart_line($i, $color[$i], $meter_sub->name);
' VDEF:min'. $i .'=meter'. $i .',MINIMUM'.
' VDEF:max'. $i .'=meter'. $i .',MAXIMUM'.
' VDEF:avg'. $i .'=meter'. $i .',AVERAGE'.
' VDEF:last'. $i .'=meter'. $i .',LAST';
$string->line .= ' LINE1:meter'. $i .'#'. $color[$i] .':'.'"'. substr($subscription->name.' ', 0, 15) .'"'.
' GPRINT:min'. $i .':"min\:%5.0lf"'.
' GPRINT:max'. $i .':"\tmax\:%5.0lf"'.
' GPRINT:avg'. $i .':"\tavg\:%5.0lf"'.
' GPRINT:last'. $i .':"\tlast\:%5.0lf\l"';
} }
} $i++;
} while($meter_sub = db_fetch_object($result_sub));
} }
//construct the TZ=GMT-02:00 format from the $user->timezone object updated by the autotimezone module //construct the TZ=GMT-02:00 format from the $user->timezone object updated by the autotimezone module
@ -323,7 +317,7 @@ function _logger_dashboard($type, $function, $interval) {
$TZ .= gmdate('h:i', abs($user->timezone)) .'" '; $TZ .= gmdate('h:i', abs($user->timezone)) .'" ';
//insert the TZ prior to launching rrdtool to obtain a proper time conversion //insert the TZ prior to launching rrdtool to obtain a proper time conversion
$command = $TZ . $root_path .'/rrdtool graph '. $graph_path . $pngid .'.png -s '. $start . $command = $TZ . $root_path .'/rrdtool graph '. $graph_path . $pngid .'.png -s '. $start .
' --vertical-label '. $meter->unit .' --lower-limit 0 -w 500 -h 350 -E -X 0 --font LEGEND:8:'; ' --vertical-label '. $unit .' --lower-limit 0 -w 500 -h 350 -E -X 0 --font LEGEND:8:';
$command .= $string->def; $command .= $string->def;
$command .= $string->line; $command .= $string->line;
exec($command, $output, $return_var); exec($command, $output, $return_var);
@ -331,6 +325,23 @@ function _logger_dashboard($type, $function, $interval) {
return theme('chart', $graph_path . $pngid .'.png'); return theme('chart', $graph_path . $pngid .'.png');
} }
function _logger_chart_def($i, $path, $meter, $factor) {
return ' DEF:data'. $i .'='. $path . $meter .'.rrd:meter:AVERAGE'.
' CDEF:meter'. $i .'=data'. $i .','. $factor .',*'.
' VDEF:min'. $i .'=meter'. $i .',MINIMUM'.
' VDEF:max'. $i .'=meter'. $i .',MAXIMUM'.
' VDEF:avg'. $i .'=meter'. $i .',AVERAGE'.
' VDEF:last'. $i .'=meter'. $i .',LAST';
}
function _logger_chart_line($i, $color, $caption) {
return ' LINE1:meter'. $i .'#'. $color .':'.'"'. substr($caption.' ', 0, 15) .'"'.
' GPRINT:min'. $i .':"min\:%5.0lf"'.
' GPRINT:max'. $i .':"\tmax\:%5.0lf"'.
' GPRINT:avg'. $i .':"\tavg\:%5.0lf"'.
' GPRINT:last'. $i .':"\tlast\:%5.0lf\l"';
}
function _logger_add($uid) { function _logger_add($uid) {
global $user; global $user;
$rtid = db_result(db_query("SELECT rtid $rtid = db_result(db_query("SELECT rtid