-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine.pl
219 lines (163 loc) · 4.79 KB
/
combine.pl
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
#!/usr/bin/perl
use utf8;
use Data::Dumper;
use JSON::XS;
use POSIX qw(strftime);
use open qw/ :std :encoding(utf8) /; # To avoid "Wide character warning"
@dashboards = ({"index"=>"leeds-city-region/leeds.csv","combined"=>"leeds-city-region/combined.csv"});
# Read in the fields from the definition JSON file - available via %fields
getFields("diversity-definition.json");
$lastupdated = "<time datetime=\"".strftime("%F",gmtime())."\">".strftime("%A %d %B %Y %R",gmtime())."</time>";
# Add the replaced fields
foreach $field (keys(%fields)){
if($fields{$field}{'replaces'}){
$fields{$fields{$field}{'replaces'}} = {'replacedby'=>$field};
}
}
@colorder = ("published","organisation","organisation_grouping","organisation_level","employees","link_to_dei_info");
# Loop over the dashboard array
for($d = 0; $d < @dashboards; $d++){
# Get the file name
$file = $dashboards[$d]{'index'};
# If the file exists
if(-e $file){
# Read in the lines of the file
open(FILE,$file);
@lines = <FILE>;
close(FILE);
%data = ('fields'=>{},'rows'=>[]);
$csv = "";
# Loop over lines (ignore header line)
for($i = 1; $i < @lines; $i++){
# Remove new line characters from the line
$lines[$i] =~ s/[\n\r]//g;
# Get the name and URL
($name,$url) = split(",",$lines[$i]);
$cls = "";
readOrganisationURL($url);
}
for($c = 0; $c < @colorder; $c++){
$csv .= ($c > 0 ? ",":"").$colorder[$c];
}
foreach $field (sort(keys(%{$data{'fields'}}))){
$ok = 1;
for($c = 0; $c < @colorder; $c++){
if($field eq $colorder[$c]){ $ok = 0; }
}
if($ok){
$csv .= ",$field";
}
}
$csv .= "\n";
@rows = ();
for($r = 0; $r < @{$data{'rows'}}; $r++){
print "ROW $r\t\n";
$row = "";
for($c = 0; $c < @colorder; $c++){
$row .= ($c > 0 ? ",":"").$data{'rows'}[$r]{$colorder[$c]};
}
foreach $field (sort(keys(%{$data{'fields'}}))){
$ok = 1;
for($c = 0; $c < @colorder; $c++){
if($field eq $colorder[$c]){ $ok = 0; }
}
# print "\t$field\t$ok\n";
if($ok){
$row .= ",$data{'rows'}[$r]{$field}";
}
}
# print "\t$row\n\n";
push(@rows,$row);
}
$csv .= join("\n",reverse(sort(@rows)));
# Save combined results for this dashboard
open(CSV,">",$dashboards[$d]{'combined'});
print CSV $csv;
close(CSV);
}
}
#######################
sub getFields {
my $file = $_[0];
my (@lines,$str,$s);
# Read in the JSON definition
open(FILE,$file);
@lines = <FILE>;
close(FILE);
$str = join("",@lines);
%json = %{JSON::XS->new->decode($str)};
for($s = 0; $s < @{$json{'sections'}}; $s++){
makeRows($json{'sections'}[$s]{'key'},0,%{$json{'sections'}[$s]});
}
return;
}
sub makeRows {
my ($key,$lvl,%rows) = @_;
my ($ps,$p,$name);
$ps = @{$rows{'properties'}};
if($key eq "metadata"){
$key = "";
}
for($p = 0; $p < $ps; $p++){
if($rows{'properties'}[$p]{'key'}){
$name = $key.($key ? "_":"").$rows{'properties'}[$p]{'key'};
if(!$fields{$name}){ $fields{$name} = {}; }
$fields{$name}{'min'} = $rows{'properties'}[$p]{'minimum'}||"";
$fields{$name}{'max'} = $rows{'properties'}[$p]{'maximum'}||"";
$fields{$name}{'pattern'} = $rows{'properties'}[$p]{'pattern'}||"";
$fields{$name}{'patterndesc'} = $rows{'properties'}[$p]{'patterndesc'}||"";
$fields{$name}{'required'} = ($rows{'properties'}[$p]{'required'} ? 1 : 0)||0;
$fields{$name}{'type'} = $rows{'properties'}[$p]{'type'}||"";
if($rows{'properties'}[$p]{'replaces'}){
$fields{$name}{'replaces'} = $rows{'properties'}[$p]{'replaces'};
}
$fields{$name}{'id'} = $name;
if($rows{'properties'}[$p]{'properties'}){
makeRows($name,$lvl+1,%{$rows{'properties'}[$p]});
}
}
}
return;
}
sub readOrganisationURL {
my $url = $_[0];
my (@lines,$i,@cols,@head,$r,$c,@messages,%lookup,$id,$v,$t,$category,$higher,$n,@b,$bad);
if($url){
print "Getting $url...\n";
@lines = `wget -q --no-check-certificate -O- "$url"`;
}else{
print "No URL provided\n";
return;
}
$lines[0] =~ s/^\N{BOM}//; # Remove any BOM character
for($r = 0; $r < @lines; $r++){
$lines[$r] =~ s/[\n\r]//g;
}
(@head) = split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/,$lines[0]);
foreach $id (keys(%fields)){
$lookup{$id} = -1;
}
for($c = 0; $c < @head; $c++){
$lookup{$head[$c]} = $c;
}
for($r = 1; $r < @lines; $r++){
$bad = 0;
%row = ();
# Split the row into columns
(@cols) = split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/,$lines[$r]);
%checksubtotal = ();
for($c = 0; $c < @cols; $c++){
$validcell = 1;
$id = $head[$c];
if($id && $fields{$id}){
if($fields{$id}{'replacedby'}){ $id = $fields{$id}{'replacedby'}; }
$row{$id} = $cols[$c]||"";
$data{'fields'}{$id} = ($data{'fields'}{$id} ? $data{'fields'}{$id}+1 : 0);
}else{
print "Bad col $id\n";
}
}
push(@{$data{'rows'}},{%row});
}
return 0;
}