forked from alltheplaces/alltheplaces.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata.html
226 lines (216 loc) · 9.47 KB
/
wikidata.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.datatables.net/fixedcolumns/4.1.0/css/fixedColumns.dataTables.min.css" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
<title>ATP / NSI / OSM</title>
<style>
body {
font-size: 12px;
padding: 10px 100px;
}
.insight-label {
margin-left: 10px;
}
.insight-label>select {
height: 25px;
}
.insight-files {
display: inline-block;
}
</style>
</head>
<body>
<table id="spider-table" class="display" style="width:100%">
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="./shared.js"></script>
<script type=module>
async function loadInsights(url) {
dataTable.clear()
dataTable.draw()
const insightsResponse = await window.fetch(url)
const insightsJson = await insightsResponse.json()
let data = insightsJson["data"]
if (!("atp_country_count" in data[0])) {
// If we encounter an insights JSON without the newer fields then we add them dynamically
// so that the new DataTables config does not error i.e. backwards compatability for now.
for (let i = 0; i < data.length; i++) {
data[i]["atp_country_count"] = ""
data[i]["atp_supplier_count"] = ""
}
}
else {
// Re-calculate the "atp_count" (number of POIs) based on the sum across all countries
// of the largest contributing spider in each area.
// It might be that we move this calculation to the "insights" command at some point
// but for now as it is here we preserve backwards compatibility.
for (let i = 0; i < data.length; i++) {
if (data[i]["atp_count"]) {
let new_atp_count = 0
Object.keys(data[i]["atp_splits"]).forEach(function (country) {
let country_max = 0
Object.keys(data[i]["atp_splits"][country]).forEach(function (spider) {
let count = data[i]["atp_splits"][country][spider]
if (count > country_max) {
country_max = count
}
})
new_atp_count += country_max
})
data[i]["atp_count"] = new_atp_count
}
}
}
// Old insights.json had attributes "nsi_brand" and "nsi_description".
// These were misnamed and should in fact be references to Wikidata values.
// Fix these, leaving an "nsi_brand" field which will be incorrect but
// will be good in later insights.json.
if ("nsi_description" in data[0]) {
for (let i = 0; i < data.length; i++) {
data[i]["q_title"] = data[i]["nsi_brand"]
data[i]["q_description"] = data[i]["nsi_description"]
}
}
dataTable.rows.add(insightsJson["data"])
dataTable.draw()
}
function newInsightsJsonSelected() {
loadInsights(document.getElementById('insight-select').value)
}
const historyList = await fetchHistoryList()
console.log(historyList)
function isInt(value) {
if (isNaN(value)) {
return false;
}
let x = parseFloat(value);
return (x | 0) === x;
}
let dataTable = $("#spider-table").DataTable({
lengthMenu: [10, 15, 20, 25, 50, 75, 100],
pageLength: 10,
dom: 'l<"insight-files">frtip',
order: [[1, 'desc']],
columnDefs: [
{ className: 'dt-center', targets: [0,5,6,7] },
{ className: 'dt-right', targets: [1,2,3,4] },
],
columns: [
{
"title": "Q-code",
"data": "code",
"render": function (data, type, row, meta) {
if (type === 'display' && data.startsWith("Q")) {
data = '<a href="https://www.wikidata.org/wiki/' + data + '">' + data + '</a>';
}
return data;
}
},
{"title": "OSM count", "data": "osm_count"},
{"title": "ATP count", "data": "atp_count"},
{
"title": "Countries",
"data": "atp_country_count",
"render": function (data, type, row, meta) {
if (type === 'display' && row["atp_country_count"]) {
let dataUrl = URL.createObjectURL(
new Blob([JSON.stringify(row["atp_splits"], null, 6)], { type: "application/json" })
);
data = "<a target='_blank' href='"+dataUrl+"'>" + data + "</a>";
}
return data;
}
},
{"title": "Spiders", "data": "atp_supplier_count"},
{"title": "ATP brand", "data": "atp_brand"},
{
"title": "NSI brand",
"data": "nsi_brand",
"render": function (data, type, row, meta) {
if (type === 'display' && data) {
data = '<a href="https://nsi.guide/index.html?t=brands&tt=' + row["code"] + '">' + data + '</a>';
}
return data;
}
},
{"title": "Q-title", "data": "q_title"},
{
"title": "Q-description",
"data": "q_description",
"render": function (data, type, row, meta) {
if (type === 'display') {
data = '<a href="https://www.wikidata.org/wiki/' + row["code"] + '">' + data + '</a>';
}
return data;
}
},
],
"footerCallback": function (row, data, start, end, display) {
let api = this.api()
// Total up the numeric columns (all next to each)
let columns = [1,2,3,4]
for (let i in columns) {
let total = api
.column(columns[i], {filter: "applied"})
.data()
.reduce(function (a, b) {
return a + b;
}, 0);
// Update total in footer
$(api.column(columns[i]).footer()).html(total.toLocaleString("us-US"));
}
},
"createdRow": function (row, data, dataIndex, cells) {
// TODO: have an issue column and mark it if anything triggers in this method?
if (!data['code'].startsWith("Q")) {
// For sure we do not have a valid wikidata Q-code, highlight this line.
$(row).css('background-color', 'Pink')
}
if (isInt(data['atp_count']) && data['osm_count'] > data['atp_count']) {
// More data in OSM than ATP, highlight this line and the ATP count cell.
$(cells[2]).css('color', 'Red')
$(row).css('background-color', 'Pink')
}
if (data['atp_brand'] && data['nsi_brand'] && !data['nsi_brand'].startsWith(data['atp_brand'])) {
// ATP brand name is only allowed to be a strict or leading match of the NSI brand name
// If not then highlight the ATP brand name cell and the whole line.
$(cells[5]).css('color', 'Red')
$(row).css('background-color', 'Pink')
}
if (data['atp_count'] > 0 && !data['nsi_brand']) {
// Something is in ATP but not in NSI, if it's valid then perhaps add to NSI?
// Highlight the line as worthy of possible attention.
$(row).css('background-color', 'Pink')
}
},
});
$('div.insight-files').html('<label class="insight-label">Select <select id="insight-select" aria-controls="spider-table" class></select> run</label>');
let selectElement = document.getElementById('insight-select');
for (let i = 0; i < historyList.length; i++) {
selectElement.add(new Option(historyList[i]["name"], historyList[i]["insights_url"]));
}
selectElement.onchange = newInsightsJsonSelected;
// Select the most recent run to display initially.
await loadInsights(historyList[0]["insights_url"])
</script>
</body>
</html>