forked from caprenter/IATI-Transparency_Indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory_assesment.php
201 lines (170 loc) · 5.42 KB
/
history_assesment.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
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
<?php
/*
go through a history directory and work out timelyness
[timestamp] => 2012-03-17T00:35:09.496097
[message] => REST API: Update object aa-activity
[author] => iati-archiver
*/
echo "Provider,Number of files who's most recent update was in the last [days],,,,,,Number of updates in last 180 days- breakdown,,,,,,Assessment" . PHP_EOL;
//ksort($updated);
//print_r($updated);
echo ",";
$intervals = array(30,60,90,180,370,"other");
foreach ($intervals as $interval) {
echo $interval; echo ",";
}
foreach ($intervals as $interval) {
echo $interval; echo ",";
}
echo PHP_EOL;
$dir = "history";
$dirs = scandir($dir); //all the folders in our directory holding the data
unset($dirs[0]); // unset the . value
unset($dirs[1]); //unset the .. value
$groups = $dirs;
//print_r($groups); die;
foreach ($groups as $group) {
if (is_dir($dir . "/" . $group)) {
if ($handle = opendir($dir . "/" . $group)) {
//echo "Directory handle: $handle\n";
//echo "Files:\n";
$days = array();
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") { //ignore these system files
//echo $file . PHP_EOL;
$data = file_get_contents($dir . "/" . $group . "/" .$file);
$data = json_decode($data,TRUE);
foreach ($data["result"] as $record) {
$timestamp = $record["timestamp"];
$message = $record["message"];
$author = $record["author"];
if ($author == "iati-archiver" && strstr($message,"update dataset")) {
$last_updated = time() - strtotime($timestamp);
$days[$file][] = round($last_updated/(60*60*24),0);
}
}
//echo $group;
//print_r($data);
//die;
} //if file
} //while files in dir
//echo "Number of files: " . count($days) . PHP_EOL;
$monthly_count = 0;
$number_of_updates = 0;
$number_of_updates_array = array();
foreach ($days as $file) {
foreach ($file as $updates) {
if ($updates < 30) {
$number_of_updates_array[30]++;
}
if ($updates < 60) {
$number_of_updates_array[60]++;
}
if ($updates < 90) {
$number_of_updates_array[90]++;
}
//if ($updates < 120) {
// $number_of_updates_array[120]++;
//}
if ($updates < 180) {
$number_of_updates_array[180]++;
}
if ($updates < 370) {
$number_of_updates_array[370]++;
}
if ($updates > 370) {
$number_of_updates_array["other"]++;
}
//if ($updates < 180) {
// $number_of_updates++;
//}
}
if ($file[0] < 30) {
$updated[30]++;
continue;
} elseif ($file[0] < 60) {
$updated[60]++;
continue;
} elseif ($file[0] < 90) {
$updated[90]++;
continue;
//} elseif ($file[0] < 120) {
// $updated[120]++;
// continue;
} elseif ($file[0] < 180) {
$updated[180]++;
continue;
} elseif ($file[0] < 370) {
$updated[370]++;
continue;
} elseif ($file[0] > 370) {
$updated["other"]++;
continue;
}
}
echo $group; echo ",";
foreach ($intervals as $interval) {
if (isset($updated[$interval])) {
echo $updated[$interval]; echo ",";
} else {
echo ",";
}
}
//echo "Number of updates in last 180 days: " . $number_of_updates . PHP_EOL;
//echo PHP_EOL;
//echo "Number of updates in last 180 days- breakdown: " . PHP_EOL;
//ksort($number_of_updates_array);
// print_r($number_of_updates_array);
foreach ($intervals as $interval) {
if (isset($number_of_updates_array[$interval])) {
echo $number_of_updates_array[$interval]; echo ",";
} else {
echo ",";
}
}
//Assessment
if ( isset($number_of_updates_array[30]) ) {
$monthly_count++;
}
if ( isset($number_of_updates_array[60]) ) {
$monthly_count++;
}
if ( isset($number_of_updates_array[90]) ) {
$monthly_count++;
}
if ($monthly_count > 1) {
$assessment = "Monthly";
}
if ($monthly_count == 1) {
$assessment = "Quarterly";
}
if (!isset($assessment)) {
if (isset($number_of_updates_array[180])) {
$assessment = "Six-monthly";
}
}
if (!isset($assessment)) {
if (isset($number_of_updates_array[370])) {
$assessment = "Annually";
}
}
if (!isset($assessment)) {
if (isset($number_of_updates_array["other"])) {
$assessment = "Beyond one year";
}
}
if (!isset($assessment)) {
$assessment = "Can not calculate";
}
echo $assessment; echo ",";
echo PHP_EOL;
//echo "Raw Data" . PHP_EOL;
//print_r($days);
unset($assessment);
unset($number_of_updates_array);
unset($updated);
//die;
} //if $handle
} //if is_dir($group)
} //foreach group