<commit_before><?php

require_once('config.php');

$id    = filter_get("id", "%^([0-9a-fA-F]+)$%", "");
$filename = $config["rrddir"] . "/" . $id . "-TS.rrd";
if( ! is_readable($filename) ) {
	fail("Could not read file `$filename`");
}

$id_relay = filter_get("id_relay", "%^([0-9a-fA-F]+.[0-9])$%", "");
if( $id_relay != "" ) {
	$filename_relay = $config["rrddir"] . "/" . $id_relay . "-RELAY.rrd";
	if( ! is_readable($filename_relay) ) {
		fail("Could not read file `$filename_relay`");
	}
} else {
	$filename_relay = FALSE;
}

$title   = filter_get("title",   "%^([a-zA-Z0-9 _-]*)$%", "Temperature sensor $id");
$start  = strtotime(filter_get("start",  "%^([0-9a-zA-Z /:-]+)$%", "-1 day"));
if( $start === FALSE ) { fail("Couldn't parse start date"); }
$end    = strtotime(filter_get("end",    "%^([0-9a-zA-Z /:-]+)$%", "now"));
if( $end === FALSE ) { fail("Couldn't parse end date"); }
$width  = filter_get("width",  "%^([0-9]+)$%", "400");
$height = filter_get("height", "%^([0-9]+)$%", "100");


header('Content-type: image/png');
passthru("rrdtool graph - --imgformat PNG" .
	" --width $width --height $height" .
	" --start $start --end $end" .
	" --title '$title'" .
	" --vertical-label '°C'" .
	" --alt-autoscale" . 
	" 'DEF:temp=$filename:temperature:AVERAGE'" .
	" 'DEF:tt=$filename:set_temperature:AVERAGE'" .
	" 'DEF:heater=$filename:heater:AVERAGE'" .
	( $filename_relay ? " 'DEF:relay=$filename_relay:state:AVERAGE'" : "" ) .
	( $filename_relay ? " 'TICK:relay#a0a00030:1'" : "" ) .
	" TICK:heater#ff000030:1" .
	" LINE1:temp#ff0000:'Actual temperature'" .
	" VDEF:temp_avg=temp,AVERAGE" .
	" VDEF:temp_min=temp,MINIMUM" .
	" VDEF:temp_max=temp,MAXIMUM" .
	" 'GPRINT:temp_avg:avg=%4.1lf°C'" .
	" 'GPRINT:temp_min:min=%4.1lf°C'" .
	" 'GPRINT:temp_max:max=%4.1lf°C'" .
	" COMMENT:\\\\n" .
	" LINE1:tt#0000ff:'Target temperature'" .
	" VDEF:tt_avg=tt,AVERAGE" .
	" 'GPRINT:tt_avg:avg=%4.1lf°C'" .
	" COMMENT:\\\\n" .
	" CDEF:empty=UNKN,temp,POP" .
	" LINE1:empty#ffa00060:'Heat requested here '" .
	" CDEF:heater_100=heater,100,*" .<commit_msg>Added option to suppress legend<commit_after><?php

require_once('config.php');

$id    = filter_get("id", "%^([0-9a-fA-F]+)$%", "");
$filename = $config["rrddir"] . "/" . $id . "-TS.rrd";
if( ! is_readable($filename) ) {
	fail("Could not read file `$filename`");
}

$id_relay = filter_get("id_relay", "%^([0-9a-fA-F]+.[0-9])$%", "");
if( $id_relay != "" ) {
	$filename_relay = $config["rrddir"] . "/" . $id_relay . "-RELAY.rrd";
	if( ! is_readable($filename_relay) ) {
		fail("Could not read file `$filename_relay`");
	}
} else {
	$filename_relay = FALSE;
}

$title   = filter_get("title",   "%^([a-zA-Z0-9 _-]*)$%", "Temperature sensor $id");
$start  = strtotime(filter_get("start",  "%^([0-9a-zA-Z /:-]+)$%", "-1 day"));
if( $start === FALSE ) { fail("Couldn't parse start date"); }
$end    = strtotime(filter_get("end",    "%^([0-9a-zA-Z /:-]+)$%", "now"));
if( $end === FALSE ) { fail("Couldn't parse end date"); }
$width  = filter_get("width",  "%^([0-9]+)$%", "400");
$height = filter_get("height", "%^([0-9]+)$%", "100");
$graph_only = filter_get("graph_only", "%(1)%", 0);


header('Content-type: image/png');
passthru("rrdtool graph - --imgformat PNG" .
	" --width $width --height $height" .
	( $graph_only ? " -g" : "" ) .
	" --start $start --end $end" .
	" --title '$title'" .
	" --vertical-label '°C'" .
	" --alt-autoscale" . 
	" 'DEF:temp=$filename:temperature:AVERAGE'" .
	" 'DEF:tt=$filename:set_temperature:AVERAGE'" .
	" 'DEF:heater=$filename:heater:AVERAGE'" .
	( $filename_relay ? " 'DEF:relay=$filename_relay:state:AVERAGE'" : "" ) .
	( $filename_relay ? " 'TICK:relay#a0a00030:1'" : "" ) .
	" TICK:heater#ff000030:1" .
	" LINE1:temp#ff0000:'Actual temperature'" .
	" VDEF:temp_avg=temp,AVERAGE" .
	" VDEF:temp_min=temp,MINIMUM" .
	" VDEF:temp_max=temp,MAXIMUM" .
	" 'GPRINT:temp_avg:avg=%4.1lf°C'" .
	" 'GPRINT:temp_min:min=%4.1lf°C'" .
	" 'GPRINT:temp_max:max=%4.1lf°C'" .
	" COMMENT:\\\\n" .
	" LINE1:tt#0000ff:'Target temperature'" .
	" VDEF:tt_avg=tt,AVERAGE" .
	" 'GPRINT:tt_avg:avg=%4.1lf°C'" .
	" COMMENT:\\\\n" .
	" CDEF:empty=UNKN,temp,POP" .
	" LINE1:empty#ffa00060:'Heat requested here '" .
	" CDEF:heater_100=heater,100,*" .
{'fraction_non_alphanumeric': 0.2710014238253441, 'fraction_numerical': 0.0322733744660655, 'mean_word_length': 4.348984771573604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4706222', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1862, 'n_words': 246}