-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecreate_status_changes.php
103 lines (89 loc) · 2.2 KB
/
recreate_status_changes.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require 'inc/main.inc';
const MIN_DURATION = 60 * 60 * 24;
const MIN_CONFIRMATIONS = 5;
$mongodb_query = new MongoDB\Driver\Query([], ['sort' => ['ts' => 1]]);
$rows = $mongodb_manager->executeQuery('ac.statuses', $mongodb_query);
$apps = [];
foreach($rows as $row) {
if(!isset($apps[$row->id])) {
$apps[$row->id] = ['t' => []];
}
if(!isset($apps[$row->id]['t'][$row->territory])) {
$apps[$row->id]['t'][$row->territory] = [];
}
$apps[$row->id]['t'][$row->territory][$row->ts] = $row->available;
}
global $mongodb_manager;
$mongodb_bulk = new MongoDB\Driver\BulkWrite();
function insertChangeIfSignificant($change) {
if(!$change) {
return false;
}
if(!isset($change->duration)) {
return false;
}
if($change->duration < MIN_DURATION) {
return false;
}
if($change->confirmations < MIN_CONFIRMATIONS) {
return false;
}
if($change->change == -1) {
$margin = 60 * 60 * 24 * 2;
if(!isAppAvailableSomewhereDuring($change->id, $change->ts + $margin, $change->ts + $change->duration)) {
return false;
}
}
global $mongodb_bulk;
$mongodb_bulk->insert($change);
return true;
}
function isAppAvailableSomewhereDuring($id, $ts_start, $ts_end) {
global $apps;
foreach($apps[$id]['t'] as $statuses) {
foreach($statuses as $ts => $status) {
if($ts < $ts_start) {
continue;
}
if($ts > $ts_end) {
break;
}
if($status) {
return true;
}
}
}
return false;
}
foreach($apps as $id => $app) {
foreach($app['t'] as $territory => $statuses) {
$change = null;
$last = new stdClass;
foreach($statuses as $ts => $status) {
if(isset($last->status)) {
if($last->status == $status) {
if($change) {
$change->confirmations++;
$change->duration = $ts - $change->ts;
}
} else {
if(!$change || insertChangeIfSignificant($change)) {
$change = new stdClass;
$change->id = $id;
$change->territory = $territory;
$change->change = $status ? 1 : -1;
$change->ts = $ts;
$change->confirmations = 0;
} else {
$change = null;
}
}
}
$last->status = $status;
$last->ts = $ts;
}
insertChangeIfSignificant($change);
}
}
$mongodb_manager->executeBulkWrite('ac.status_changes_tmp', $mongodb_bulk);