-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
201 lines (177 loc) · 6.35 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Govdirectory quantity tracker</title>
<style>
body {
font-family: -apple-system, system-ui, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th,
td {
padding: 10px;
border: 1px solid #ddd;
text-align: left;
}
th {
background-color: #f5f5f5;
}
.status-mismatch {
color: red;
}
#downloadBtn {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 10px 0;
}
#downloadBtn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
#loading {
display: none;
margin: 20px 0;
}
</style>
</head>
<body>
<h1>Govdirectory quantity tracker</h1>
<button id="downloadBtn" disabled>Download CSV</button>
<div id="loading">Loading data...</div>
<div id="results"></div>
<script>
const HEADERS = {
"Accept": "application/json",
"User-Agent": "WikidataAgencyChecker/1.0"
};
const DETECT_QUERY = `
SELECT ?item ?itemLabel ?quantity ?countryLabel WHERE {
?item wdt:P279/wdt:P279 wd:Q327333 ;
wdt:P1114 ?quantity .
OPTIONAL {
?item wdt:P17 ?country .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
`;
const COUNT_QUERY_TEMPLATE = `
SELECT (COUNT(?item) AS ?count) WHERE {
?item wdt:P31/wdt:P279* wd:{{.}} .
MINUS { ?item wdt:P3999 [] }
MINUS { ?item wdt:P576 [] }
}
`;
async function runSparqlQuery(query) {
const url = new URL("https://query.wikidata.org/sparql");
url.searchParams.append("format", "json");
url.searchParams.append("query", query);
const response = await fetch(url, {headers: HEADERS});
if (!response.ok) {
throw new Error(`SPARQL query failed: ${response.statusText}`);
}
return await response.json();
}
async function getExpectedQuantities() {
const results = await runSparqlQuery(DETECT_QUERY);
return results.results.bindings.map(binding => ({
itemId: binding.item.value.split("/").pop(),
label: binding.itemLabel.value,
quantity: parseInt(binding.quantity.value),
country: binding.countryLabel?.value || "-"
}));
}
async function getActualCount(itemId) {
const query = COUNT_QUERY_TEMPLATE.replace("{{.}}", itemId);
const results = await runSparqlQuery(query);
return parseInt(results.results.bindings[0].count.value);
}
function displayResults(results) {
const table = document.createElement("table");
table.innerHTML = `
<thead>
<tr>
<th>Type</th>
<th>Expected</th>
<th>Actual</th>
<th>Country</th>
<th>Status</th>
<th>Item ID</th>
</tr>
</thead>
<tbody>
${results.map(r => `
<tr>
<td>${r.label}</td>
<td>${r.expected}</td>
<td>${r.actual}</td>
<td>${r.country}</td>
<td class="${r.expected !== r.actual ? 'status-mismatch' : ''}">${r.expected === r.actual ? '✓' : '!'}</td>
<td>${r.itemId}</td>
</tr>
`).join('')}
</tbody>
`;
document.getElementById("results").appendChild(table);
}
function downloadCSV(results) {
const csvContent = [
["Type", "Expected", "Actual", "Country", "Status", "Item ID"],
...results.map(r => [
r.label,
r.expected,
r.actual,
r.country,
r.expected === r.actual ? "✓" : "!",
r.itemId
])
].map(row => row.join(",")).join("\n");
const blob = new Blob([csvContent], {type: "text/csv"});
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
const timestamp = new Date().toISOString().replace(/[:.]/g, "");
a.href = url;
a.download = `agency_report_${timestamp}.csv`;
a.click();
window.URL.revokeObjectURL(url);
}
async function generateReport() {
const loading = document.getElementById("loading");
const downloadBtn = document.getElementById("downloadBtn");
loading.style.display = "block";
try {
const quantities = await getExpectedQuantities();
const results = await Promise.all(
quantities.map(async q => ({
...q,
expected: q.quantity,
actual: await getActualCount(q.itemId)
}))
);
displayResults(results);
downloadBtn.onclick = () => downloadCSV(results);
downloadBtn.disabled = false;
} catch (error) {
console.error("Error generating report:", error);
document.getElementById("results").innerHTML = `
<p style="color: red">Error generating report: ${error.message}</p>
`;
} finally {
loading.style.display = "none";
}
}
generateReport();
</script>
</body>
</html>