-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatechanges.php
More file actions
346 lines (314 loc) · 9.11 KB
/
statechanges.php
File metadata and controls
346 lines (314 loc) · 9.11 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
declare(strict_types = 1);
class Hour extends DateTimeImmutable {
const DEFAULT_FORMAT = 'Y-m-d H:i:s';
private $string = null;
public $hour = null;
public static $timezone = null;
public function __construct(string $string) {
$this->string = $string;
$this->hourNumber = intval(substr($this->string, 11, 2));
if (is_null(self::$timezone)) {
self::$timezone = new DateTimeZone('UTC');
}
parent::__construct($string, self::$timezone);
}
public static function now(): Hour {
$now = new DateTime();
$nowString = $now->format(self::DEFAULT_FORMAT);
$hour = new static($nowString);
return $hour;
}
public function previousHour(): Hour {
$format = 'Y-m-d H:00:00';
$hourString = $this->format($format);
$hour = new static($hourString);
return $hour;
}
public function nextHour(): Hour {
$hour = $this->previousHour();
$nextHour = $hour->add(new DateInterval('PT1H'));
$nextHourString = $nextHour->format(self::DEFAULT_FORMAT);
$nextHourObject = new static($nextHourString);
return $nextHourObject;
}
public function equalHours($hour): bool {
$equals = (
substr_compare($this->string, $hour->string, 0, 13) === 0
);
return $equals;
}
public function differenceInSeconds(Hour $datetime): int {
$difference = $this->diff($datetime);
$differenceInSeconds =
$difference->h * 3600 + // 60 * 60
$difference->i * 60 +
$difference->s
;
return $differenceInSeconds;
}
public function dayNumber(): string {
$format = 'N';
$dayNumber = $this->format($format);
return $dayNumber;
}
public function __toString(): string {
return $this->string;
}
}
class HourPeriod {
public $hour = null;
public $seconds = null;
public function __construct(Hour $hour, int $seconds) {
$this->hour = $hour;
$this->seconds = $seconds;
}
const TOTAL_NUMBER_OF_SECONDS = 60 * 60;
public static function createFullHour(Hour $hour) {
$seconds = self::TOTAL_NUMBER_OF_SECONDS;
$hourPeriod = new static($hour, $seconds);
return $hourPeriod;
}
public static function createEmptyHour(Hour $hour) {
$seconds = 0;
$hourPeriod = new static($hour, $seconds);
return $hourPeriod;
}
public static function createByDifference(
Hour $hour, Hour $start, Hour $end
) {
$difference = $start->differenceInSeconds($end);
$hourPeriod = new static($hour, $difference);
return $hourPeriod;
}
public function merge(HourPeriod $hourPeriod) {
$this->seconds += $hourPeriod->seconds;
return $this;
}
}
class HourPeriodList extends ArrayObject {
public function seconds() {
$seconds = 0;
$count = $this->count();
if ($count > 0) {
$total = $this->total();
$divisor = $count * HourPeriod::TOTAL_NUMBER_OF_SECONDS;
$seconds = $total / $divisor;
}
return $seconds;
}
public function total(): int {
$seconds = 0;
foreach($this as $hourPeriod) {
$seconds += $hourPeriod->seconds;
}
return $seconds;
}
}
class AverageList extends ArrayObject {
public function average(): float {
$average = array_sum((array) $this) / count($this);
return $average;
}
}
class HourPeriodGroup {
private $dictionary = null;
private $days = null;
private $hours = null;
public function __construct(string $interval) {
$this->dictionary = array();
$this->interval($interval);
}
private function interval(string $interval): void {
$now = Hour::now();
$intervalObject = DateInterval::createFromDateString($interval);
$timestamp = $now->sub($intervalObject)->nextHour();
$rows = Database::rowsFromDatebase($timestamp);
$this->processRows($rows);
}
public function processRows(array $rows): void {
$previousRow = null;
foreach($rows as $row) {
if (!is_null($previousRow)) {
$this->processStartEnd(
$previousRow->created,
$row->created ,
$previousRow->state
);
}
$previousRow = $row;
}
}
private function processStartEnd(
string $startTimestamp, string $endTimestamp, string $stateString
): void {
$start = new Hour($startTimestamp);
$end = new Hour($endTimestamp );
$state = intval($stateString);
if ($start->equalHours($end)) {
$startHour = $start->previousHour();
$this->addHourPeriod($startHour, $state, $start, $end);
} else {
$this->processBeyondMidnight($start, $end, $state);
}
}
private function processBeyondMidnight(
Hour $start, Hour $end, int $state
): void {
$startHour = $start->previousHour();
$startNextHour = $start->nextHour();
$this->addHourPeriod($startHour, $state, $start, $startNextHour);
$endHour = $end->previousHour();
$hour = $startNextHour;
while($hour < $endHour) {
$this->addHourPeriod($hour, $state);
$hour = $hour->nextHour();
}
$this->addHourPeriod($endHour, $state, $end, $endHour);
}
const STATE_CLOSED = 0;
const STATE_OPEN = 1;
private function addHourPeriod(
Hour $hour , int $state,
Hour $start = null, Hour $end = null
): void {
$hourPeriod = (($state === self::STATE_CLOSED)
? HourPeriod::createEmptyHour($hour)
: (is_null($start)
? HourPeriod::createFullHour($hour)
: HourPeriod::createByDifference($hour, $start, $end)
)
);
$hash = (string) $hour;
if (array_key_exists($hash, $this->dictionary)) {
$hourPeriod = $hourPeriod->merge($this->dictionary[$hash]);
}
$this->dictionary[$hash] = $hourPeriod;
}
private function days(): array {
$days = array_fill(1, 7, array_fill(0, 24, null));
foreach($this->dictionary as $hourPeriod) {
$dayNumber = $hourPeriod->hour->dayNumber();
$hourNumber = $hourPeriod->hour->hourNumber;
if (!isset($days[$dayNumber][$hourNumber])) {
$days[$dayNumber][$hourNumber] = new HourPeriodList();
}
$days[$dayNumber][$hourNumber][] = $hourPeriod;
}
return $days;
}
private $statistics = null;
private $totalPercentages = null;
private $hourPercentages = null;
private $dayList = null;
public function statistics(): array {
$this->statistics = array();
$this->totalPercentages = new AverageList();
$this->hourPercentages = array();
$days = $this->days();
foreach($days as $dayNumber => $hours) {
$this->dayList = new AverageList();
foreach($hours as $hourNumber => $hourPeriodList) {
$this->seconds($hourNumber, $hourPeriodList);
}
$this->dayListAverage($dayNumber);
}
$this->hoursAverage();
$this->round();
return $this->statistics;
}
private function seconds(int $hourNumber, HourPeriodList $hourPeriodList) {
$seconds = $hourPeriodList->seconds();
$this->dayList[$hourNumber] = $seconds;
$this->totalPercentages[] = $seconds;
if (!isset($this->hourPercentages[$hourNumber])) {
$this->hourPercentages[$hourNumber] = new AverageList();
}
$this->hourPercentages[$hourNumber][] = $seconds;
}
private function dayListAverage($dayNumber) {
$this->statistics[$dayNumber] = (array) $this->dayList;
$this->statistics[$dayNumber][] = $this->dayList->average();
}
private function hoursAverage() {
$hoursAverage = array();
foreach($this->hourPercentages as $hourNumber => $percentages) {
$hoursAverage[$hourNumber] = $percentages->average();
}
$hoursAverage[] = $this->totalPercentages->average();
$this->statistics[] = $hoursAverage;
}
private function round() {
array_walk_recursive($this->statistics, function(&$value) {
$value = round($value, 4);
});
}
private function json(): void {
$statistics = $this->statistics();
$jsonString = json_encode($statistics, JSON_FORCE_OBJECT);
header('Content-Type: application/json');
echo($jsonString);
}
public static function handleRequest(): void {
$intervalList = array(
'1week' => '1 week' ,
'1month' => '1 month' ,
'3month' => '3 months',
'1year' => '1 year' ,
'2year' => '2 years' ,
);
$intersect = array_intersect(
array_keys($intervalList), array_keys($_GET)
);
$intervalKey = reset($intersect);
if ($intervalKey !== false) {
$interval = $intervalList[$intervalKey];
$group = new static($interval);
$group->json();
}
}
}
class Database extends PDO {
private function __construct() {
include_once($_SERVER['DOCUMENT_ROOT'] . '/../spaceAPI_config.php');
$dsn =
'mysql:dbname=' . $spaceApi_db_dbname .
';host=' . $spaceApi_db_servername
;
parent::__construct(
$dsn, $spaceApi_db_username, $spaceApi_db_password
);
}
public static function rowsFromDatebase(Hour $timestamp): array {
$connection = new static();
$sql = "
select * from (
select created, state
from statechanges
where created = (
select max(created)
from statechanges
where created < ?
)
union all
select created, state
from statechanges
where created >= ?
union all
select now(), null
) as `table`
order by created
";
$bindings = array($timestamp, $timestamp);
$statement = $connection->prepare($sql);
$statement->execute($bindings);
$result = $statement->fetchAll(PDO::FETCH_CLASS);
if (count($result) > 0) {
if ($result[0]->created < $timestamp) {
$result[0]->created = (string) $timestamp;
}
}
return $result;
}
}
HourPeriodGroup::handleRequest();