-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_charts.php
More file actions
executable file
·179 lines (162 loc) · 5.46 KB
/
Copy pathgenerate_charts.php
File metadata and controls
executable file
·179 lines (162 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/php
<?php
error_reporting( -1 );
date_default_timezone_set( 'Europe/Berlin' );
require_once 'SVGGraph/autoloader.php'; # draw SVG charts
require_once __DIR__ . '/php-svg/autoloader.php'; # merge SVG files
use SVG\SVG;
use SVG\Nodes\Structures\SVGGroup;
$sensors = [ 'bedroom', 'guestroom', 'livingroom', 'balcony', 'bathroom' ];
$metrics = [ 'temperature', 'humidity' ];
#$sensors = [ 'balcony' ];
#$metrics = [ 'temperature' ];
$hours = 3;
$step = 300; # resolution in seconds
$trend_latest_count = 5; # number of latest values to calculate trend from
$trend_threshold = 0.00035 ; # if slope is greater than this, draw an arrow
$base_url = 'http://grafana:9090/api/v1';
$end = time();
$start = $end - ( 3600 * $hours );
foreach ( $sensors as $sensor ) {
echo "\n=== $sensor ===\n";
foreach ( $metrics as $metric ) {
echo "--- $metric ---\n";
$url = "$base_url/query_range?query=sensor_$metric{name=\"$sensor\"}&start=$start&end=$end&step=$step";
$json = getJSONfromURL( $url );
$aRawValues = $json["data"]["result"][0]["values"];
$values = [];
foreach ( $aRawValues as $aRawValue ) {
$ts = $aRawValue[0];
$value = $aRawValue[1];
#echo "\$ts: $ts, \$value: $value\n";
$values[$ts] = $value;
}
$trend_values = array_slice( $values, -$trend_latest_count, $trend_latest_count, true );
$x_trend_values = array_keys( $trend_values );
$y_trend_values = array_values( $trend_values );
$first_trend_timestamp = $x_trend_values[0];
echo "first_trend_timestamp: $first_trend_timestamp (" . date( "Y-m-d\TH:i:s\Z", $first_trend_timestamp ) . ")\n";
# Ersten Timestamp als Basis verwenden und Differenz in Sekunden berechnen
$start_time = $x_trend_values[0];
$times_from_zero = array_map( function( $ts ) use ( $start_time ) {
return ( $ts - $start_time ); }, $x_trend_values );
var_dump( $times_from_zero );
var_dump( $y_trend_values );
list( $m, $b ) = linearRegression( $times_from_zero, $y_trend_values );
$m = round( $m, 4 );
echo "slope (m): " . $m . "\n";
#echo "Achsenabschnitt (b): " . $b . "\n";
# generate SVG files
$y_values_min = min( array_values( $values ) ); # get min value
$values = array_map( function( $y_val ) use ( $y_values_min ) {
return ( $y_val - $y_values_min ); }, $values ); # rebase to min
#echo "values:\n";
#var_dump( $values );
$svg = renderSVG( $values );
if ( abs( $m ) > $trend_threshold ) {
echo "steep slope detected! drawing an arrow!\n";
$arrow_file = ( $m < 0 ) ? "arrow-down.svg" : "arrow-up.svg";
$svg = SVG::fromString($svg);
$doc = $svg->getDocument();
$arrow = SVG::fromFile($arrow_file);
$doc2 = $arrow->getDocument();
$group = new SVGGroup();
$group->setAttribute('transform', 'translate(84, 8)');
$group->addChild($doc2);
$doc->addChild($group);
}
$svgfile = "chart-$sensor-$metric.svg";
file_put_contents( $svgfile, $svg );
}
}
function renderSVG( $values ) {
global $first_trend_timestamp;
$settings = [
'guideline' => [
[ $first_trend_timestamp, NULL, 'x', 'stroke_width' => 3, 'colour' => 'white' ],
],
'guideline_above' => true,
'guideline_dash' => "5,5",
'auto_fit' => false, # auto fit on page
'back_colour' => '#eee',
'back_stroke_width' => 0,
'back_stroke_colour' => '#eee',
'stroke_colour' => '#000',
'axis_colour' => '#333',
#'axis_overlap' => 2,
'grid_colour' => '#666',
'grid_division_v' => 1,
'label_colour' => '#000',
'axis_font' => 'Arial',
'axis_font_size' => 10,
'fill_under' => true,
'pad_right' => 0,
'pad_left' => 0,
'marker_type' => 'circle',
# marker_size 0 disables them
'marker_size' => 0,
'marker_colour' => 'blue',
'link_base' => '/',
'link_target' => '_top',
'minimum_grid_spacing' => 1,
#'show_subdivisions' => true,
#'show_grid_subdivisions' => true,
#'grid_subdivision_colour' => '#ccc',
#'best_fit' => 'straight',
#'best_fit_colour' => 'red',
#'best_fit_dash' => '2,2',
'show_axes' => false,
'show_axis_text_h' => false,
'show_axis_text_v' => false,
'show_grid' => false,
'show_divisions' => false,
'line_curve' => 0.75,
'line_breaks' => true,
'datetime_keys' => true,
'datetime_key_format' => 'U',
];
#$width = 400;
#$height = 200;
$width = 218;
$height = 66;
$type = 'LineGraph';
$colours = [ [ 'red', 'yellow' ] ];
$graph = new Goat1000\SVGGraph\SVGGraph($width, $height, $settings);
$graph->colours($colours);
$graph->values($values);
#$graph->render($type);
return $graph->fetch($type); # don't output to browser
}
function getJSONfromURL( $url ) {
#echo "URL: $url\n";
$result = file_get_contents( $url );
#echo "$result\n";
$json = (json_decode($result, true));
#var_dump( $json ); return;
if ( $json["status"] == "success") {
return $json;
}
else {
return false;
}
}
function linearRegression($x, $y) {
$n = count($x);
if ($n != count($y)) {
throw new Exception("Die Arrays müssen gleich lang sein.");
}
// Mittelwerte der Arrays berechnen
$mean_x = array_sum($x) / $n;
$mean_y = array_sum($y) / $n;
// Variablen zur Berechnung der Summe der Abweichungen
$numerator = 0; // Zähler für die Steigung (m)
$denominator = 0; // Nenner für die Steigung (m)
for ($i = 0; $i < $n; $i++) {
$numerator += ($x[$i] - $mean_x) * ($y[$i] - $mean_y);
$denominator += ($x[$i] - $mean_x) ** 2;
}
// Berechnung der Steigung (m) und des Achsenabschnitts (b)
$m = $numerator / $denominator;
$b = $mean_y - ($m * $mean_x);
return [$m, $b];
}