-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill-in-gaps.php
executable file
·80 lines (65 loc) · 2.12 KB
/
fill-in-gaps.php
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
#!/usr/bin/php
<?php
include('lib.php');
function curldo($url, $params = false, $opts = []) {
$HOST = 'https://xxxx/externalIncoming/ocpi/cpo/2.1.1';
$ch = curl_init();
$url = '/' . ltrim($url, '/');
if($params) {
$url .= '?' . http_build_query($params);
}
$header[] = "Authorization: Token ";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $HOST . $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
/*
$tolog = json_encode([
'verb' => $verb,
'header' => $header,
'url' => $url,
'params' => $params,
'res' => $res
]);
var_dump(['>>>', curl_getinfo ($ch), json_decode($tolog, true)]);
file_put_contents('/tmp/log.txt', $tolog, FILE_APPEND);
*/
$resJSON = @json_decode($res, true);
if($resJSON) {
return $resJSON;
}
return $res;
}
# We don't really want to worry about timezones
# nor do we want to get into a nasty loop so we
# give a nice baseline that's not too far back
# in time that it may give us some edge condition
# issues but also far back enough so that every session
# we want to see we do see!
$earliest = db_one('select datetime(min(start), "-18 hour") as m from sessions where cost is null');
if(empty($earliest)) {
error_log("Nothing to do!");
exit(0);
}
#
# sqlite doesn't seem to give us the ISO format with
# the T and Z thing (see our notion of timestamps from
# above --- where I objectively just don't give a shit)
#
# so let's fix that here.
#
$earliest = str_replace(' ', 'T', $earliest['m']) . 'Z';
$emptyList = array_map(function($row) { return $row['session']; }, db_all('select session from sessions where cost is null order by id asc'));
$res = curldo('sessions', ['date_from' => $earliest]);
$db = get_db();
if (empty($res['data'])) {
error_log("Nothing to do!");
exit(0);
}
foreach($res['data'] as $session) {
if (array_search($session['id'], $emptyList) !== false) {
$qstr = "update sessions set status='${session['status']}', cost=${session['total_cost']} where session=${session['id']}\n";
$db->exec($qstr);
}
}