-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharts.php
More file actions
106 lines (88 loc) · 4.6 KB
/
Copy pathcharts.php
File metadata and controls
106 lines (88 loc) · 4.6 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
<?php
set_error_handler(function($severity, $message, $file, $line) {
throw new ErrorException($message, 0, $severity, $file, $line);
});
require('api/log.php');
require('api/http.php');
require('api/charts/dataset.php');
require('api/charts/handler.php');
$root = realpath(__DIR__);
$base = getenv('BASE');
$metrics[] = new Persistence\SQLiteDataset('metrics.sqlite', <<<'SQL'
SELECT "address", "metric", "value", datetime("timestamp", 'unixepoch', 'localtime') as "timestamp" FROM "log"
WHERE "log"."address"="54:ef:44:10:00:ee:16:c6"
AND "log"."metric"="temperature.measured_value"
AND "log"."timestamp">=unixepoch(date('now', '-7 days'))
ORDER BY "log"."timestamp" DESC
SQL);
$metrics[] = new Persistence\SQLiteDataset('metrics.sqlite', <<<'SQL'
SELECT "address", "metric", "value", datetime("timestamp", 'unixepoch', 'localtime') as "timestamp" FROM "log"
WHERE "log"."address"="54:ef:44:10:00:ed:fd:04"
AND "log"."metric"="temperature.measured_value"
AND "log"."timestamp">=unixepoch(date('now', '-7 days'))
ORDER BY "log"."timestamp" DESC
SQL);
$metrics[] = new Persistence\SQLiteDataset('metrics.sqlite', <<<'SQL'
SELECT "address", "metric", "value", datetime("timestamp", 'unixepoch', 'localtime') as "timestamp" FROM "log"
WHERE "log"."address"="54:ef:44:10:00:ee:16:c6"
AND "log"."metric"="humidity.measured_value"
AND "log"."timestamp">=unixepoch(date('now', '-7 days'))
ORDER BY "log"."timestamp" DESC
SQL);
$metrics[] = new Persistence\SQLiteDataset('metrics.sqlite', <<<'SQL'
SELECT "address", "metric", "value", datetime("timestamp", 'unixepoch', 'localtime') as "timestamp" FROM "log"
WHERE "log"."address"="54:ef:44:10:00:ed:fd:04"
AND "log"."metric"="humidity.measured_value"
AND "log"."timestamp">=unixepoch(date('now', '-7 days'))
ORDER BY "log"."timestamp" DESC
SQL);
$metrics[] = new Persistence\LogfileDataset('current.log', ''
.'/[0-9:\- ]+ \[vpstun\] (?P<timestamp>[0-9:\-+T]+) (?P<forwarded>[0-9., ]+[0-9]) '
.'"(?P<method>[A-Z]+) (?P<uri>[^"]+) HTTP\/(?P<version>[0-9.]+)" '
.'(?P<status>\d{3}) "(?P<referer>[^"]+)" "(?P<user_agent>[^"]+)"/');
$metrics[] = new Persistence\GpxDataset('tracks/', [
'/gpx/trk/name' => ['name', 'strval'],
'/gpx/trk/type' => ['type', 'strval'],
'/gpx/trk/trkseg/trkpt/time' => ['timestamp', 'strval'],
'/gpx/trk/extensions/gpxtrkx:trackstatsextension/gpxtrkx:distance' => ['distance', 'floatval'],
'/gpx/trk/extensions/gpxtrkx:trackstatsextension/gpxtrkx:maxspeed' => ['max_speed', 'floatval'],
'/gpx/trk/extensions/gpxtrkx:trackstatsextension/gpxtrkx:timertime' => ['total_time', 'intval'],
'/gpx/trk/extensions/gpxtrkx:trackstatsextension/gpxtrkx:movingtime' => ['moving_time', 'intval'],
]);
$rootLogger = Log\ConsoleLogger::for('RootLogger');
$httpLogger = Log\ConsoleLogger::for('HttpLogger');
$request = Http\newRequest();
$response = Http\newResponse();
$router = new Http\Router($base, $httpLogger);
$router->add('GET', '/', Http\serveRedirect("{$base}/index.html"));
foreach($metrics as $index => $metric) {
$router->add('GET', '/api/metrics/' . ($index + 1), new Http\Handler\Dataset($metric));
}
foreach(array(
'/index.html' => 'app/charts/index.html',
'/app/styles.css' => 'app/charts/styles.css',
'/app/dom.js' => 'app/dom.js',
'/app/chart.js' => 'app/charts/chart.js',
'/app/dataset.js' => 'app/charts/dataset.js',
) as $path => $file) {
$router->add('GET', $path, Http\serveFile("{$root}/{$file}"));
}
foreach(array(
'/app/vendor/chart.js' => ['https://unpkg.com/chart.js@4.5.1/dist/chart.umd.min.js', '48444a82d4edcb5bec0f1965faacdde18d9c17db3063d042abada2f705c9f54a'],
'/app/vendor/moment.js' => ['https://unpkg.com/moment@2.30.1/min/moment.min.js', '845c524969edd5b3af9aa6d8718d29fe92e8dbe25b955214a8e064a05a9a5027'],
'/app/vendor/hammer.js' => ['https://unpkg.com/hammerjs@2.0.8/hammer.min.js', '7953631f0e54794d2352a3cfa591c0914d73e14f90141058e3cf16bee7939bcf'],
'/app/vendor/chartjs-adapter-moment.js' => ['https://unpkg.com/chartjs-adapter-moment@1.0.1/dist/chartjs-adapter-moment.min.js', '4ca6ddbc16c438c7decc60f16fbee9639d37277af609390f7794eb2729addb55'],
'/app/vendor/chartjs-plugin-zoom.js' => ['https://unpkg.com/chartjs-plugin-zoom@2.2.0/dist/chartjs-plugin-zoom.min.js', 'e4a088e5bab93be6ee47c939eeb9ebaa80e0b39156d4bdfd1af9c844be81b6c4'],
) as $path => list($url, $hash)) {
$router->add('GET', $path, Http\serveUrl($url, $hash));
}
try {
if (!$router->apply($request, $response)) {
$response->setStatus(404);
}
} catch (Throwable $e) {
$rootLogger->error('uncaught {exception}', ['exception' => $e]);
$response->setStatus(500);
$response->setBodyAsText($e->getMessage());
}
?>