forked from KateMorley/grid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
executable file
·105 lines (83 loc) · 2.65 KB
/
update.php
File metadata and controls
executable file
·105 lines (83 loc) · 2.65 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
<?php
// Updates the site
use KateMorley\Grid\Database;
use KateMorley\Grid\Environment;
use KateMorley\Grid\Data\DataException;
use KateMorley\Grid\Data\Demand;
use KateMorley\Grid\Data\Emissions;
use KateMorley\Grid\Data\Generation;
use KateMorley\Grid\Data\Pricing;
use KateMorley\Grid\Data\Visits;
use KateMorley\Grid\UI\Favicon;
use KateMorley\Grid\UI\UI;
// IONOS servers set to CET which causes time to
// display wrongly if a tz is not specified.
date_default_timezone_set("Europe/London");
spl_autoload_register(function ($class) {
require_once __DIR__ .
"/classes/" .
strtr(substr($class, 16), "\\", "/") .
".php";
});
Environment::load(__DIR__ . "/.env");
$database = new Database();
foreach (
[
"Updating generation… " => function ($database) {
Generation::update($database);
},
"Updating emissions… " => function ($database) {
Emissions::update($database);
},
"Updating pricing… " => function ($database) {
Pricing::update($database);
},
// demand must be updated after other half-hourly data to exclude future data
"Updating demand… " => function ($database) {
Demand::update($database);
},
"Updating visits… " => function ($database) {
Visits::update($database);
},
"Finishing update… " => function ($database) {
$database->finishUpdate();
},
"Outputting files… " => function ($database) {
$state = $database->getState();
ob_start();
UI::output($state);
file_put_contents(
__DIR__ . "/public/index.html",
ob_get_clean(),
LOCK_EX
);
file_put_contents(
__DIR__ . "/public/favicon.svg",
Favicon::create($state->latest->types),
LOCK_EX
);
},
]
as $action => $callback
) {
echo $action;
$start = microtime(true);
try {
$callback($database);
echo "OK";
$database->clearErrors($action);
} catch (DataException $e) {
$error = $e->getMessage();
echo "ERROR: " . $error;
if (
$database->getErrorCount($action, $error) >=
(int) getenv("ERROR_REPORTING_THRESHOLD")
) {
$database->clearErrors($action);
if ((int) getenv("ERROR_REPORTING_THRESHOLD") > 0) {
trigger_error(trim($action) . " " . $error);
}
}
}
echo " (" . sprintf("%0.3f", microtime(true) - $start) . " seconds)\n";
}