-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovQuery.links
426 lines (371 loc) · 16.9 KB
/
provQuery.links
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# 202105??: tidied up, documented work-arounds, separated out server functions as much as possible,
# 20210616: mostly finished, lookup for context is slow
# 20210618: jump to top fixed, classification of mods added to multimod display
# 20220815: further code improvement
# what's missing
# fix for hardcoded initial values in pulldown menus in updt
# see comments inline, if any
open import DatabaseTables;
open import Utilities;
open import Mvu;
open import MvuHTML;
open import MvuAttrs;
import Template;
typename FatalitySummary = (subcat:Int,
weekdate:String,
count:Int,
file_id:Int,
subcat_name:String,
cat:String,
mods:[(count:Int,decision_time:DateTime,file_id:Int,weekmod:String,
valid_from:DateTime,valid_to:DateTime)]);
typename Model = (results_text: String, results_focus: Bool, inContext: Bool,
summary: [FatalitySummary], details: [FatalitySummary],
subcatDropdown:String, weekdateDropdown: String);
typename Message =
[| MoreDetail:(String,String) | Continue | Cancel | Context | GetSubcat:String | GetWeek:String |
SeeHistory:(String,String) |];
#============= FUNS
fun classifyMods (l) {
fun classifyMod (m) {
if (m.valid_from==beginningOfTime && m.valid_from==m.valid_to) {
(acc=0,rej=1)
} else {
(acc=1,rej=0)
}
}
var b = fold_left (fun (x,y) {(acc=x.acc+y.acc,rej=x.rej+y.rej)},
(acc=0,rej=0), map(classifyMod,l));
if (b.acc==0 && b.rej==1) {
"(rejected)"
} else if (b.acc==0 && b.rej>1) {
"(all rejected)"
} else if (b.acc==1 && b.rej==0) {
"(accepted)"
} else if (b.acc>1 && b.rej==0) {
"(all accepted)"
} else {
"(accepted and rejected)"
}
}
fun sortMultiMods (mms) server {
for (x <- mms) orderby (x.weekdate,x.subcat) [x]
}
#============= DB FUNS
fun getFromSubcats () server {
query {for (x <-- subcategory ) [x]}
}
fun getSubcatDetails (i) server {
query {for (x <-- subcategory ) where (x.subcat==stringToInt(i)) [x]}
}
fun getFromWeeks () server {
for (x <- ttCurrent(week))
where (not(x.all_zero))
orderby (x.weekdate)
[(weekdate=x.weekdate,file_id=x.file_id)]
}
fun getContext (d) server {
(weekmod=d.weekmod,
othermods=
query {for (x <-v- covid_data) for (y <-- subcategory) for (z <-- csv_file)
where (vtData(x).file_id == d.file_id && z.file_id == vtData(x).file_id &&
vtData(x).subcat == y.subcat && z.last_weekdate<>vtData(x).weekdate)
orderby (vtData(x).weekdate,vtData(x).subcat)
[(subcat=vtData(x).subcat,weekdate=vtData(x).weekdate,count=vtData(x).count,file_id=vtData(x).file_id,
decision_time=vtData(x).decision_time,weekmod=z.last_weekdate,
valid_from=vtFrom(x),valid_to=vtTo(x),subcat_name=y.subcat_name,cat=y.cat)]})
}
fun getFatalities () server {
query nested {for (x <- vtCurrent(covid_data)) for (z <-- subcategory)
where (x.subcat==z.subcat)
[(subcat=x.subcat,weekdate=x.weekdate,file_id=x.file_id,
count=x.count,cat=z.cat,subcat_name=z.subcat_name, mods=
for (y <-v- covid_data) for (w <-- csv_file)
where (x.weekdate==vtData(y).weekdate && x.subcat==vtData(y).subcat &&
vtData(y).file_id==w.file_id)
orderby (w.last_weekdate)
[(valid_from=vtFrom(y),valid_to=vtTo(y),decision_time=vtData(y).decision_time,
file_id=vtData(y).file_id,count=vtData(y).count,weekmod=w.last_weekdate)]
)]}
}
#============= UPDT
sig updt : (Message, Model) ~> Model
fun updt(msg, model) {
switch(msg) {
case Cancel -> model
case Continue -> (results_text = "", results_focus = false, inContext = false,
subcatDropdown="1", weekdateDropdown="2020-03-16", summary = model.summary, details=[])
# hard-coding of initial values should obviously be done differently!
case Context -> (model with inContext=true)
case GetSubcat(c) -> (model with subcatDropdown = c)
case GetWeek(w) -> (model with weekdateDropdown = w)
case MoreDetail(subcat,weekdate) -> (((model with results_text = "")
with results_focus = true)
with details = for (x <- model.summary)
where (x.weekdate==model.weekdateDropdown && x.subcat==stringToInt(model.subcatDropdown)) [x])
case SeeHistory(subcat,weekdate) -> var m = ((((model with results_text = "")
with results_focus = true)
with weekdateDropdown = weekdate)
with subcatDropdown = subcat);
(m with details = for (x <- model.summary)
where (x.weekdate==m.weekdateDropdown && x.subcat==stringToInt(m.subcatDropdown)) [x])
}
}
#var catDetails = hd(getSubcatDetails(model.subcatDropdown));
#============= VIEW
sig view : (Model) ~> MvuHTML.HTML(Message)
fun view(model) {
var enabledAttr =
if (not(model.results_focus)) {
MvuAttrs.empty
} else {
MvuAttrs.attr("disabled", "disabled")
};
fun renderModel(model) {
var ms = for (x <- model.summary) where (length(x.mods)>1) [x];
var multimods = sortMultiMods(ms); # need to do this on server for correct
# sorting by Ocaml, rather than JS
fun panel(name, contents) {
div(class("row mt-3"),
div(class("col-sm"),
div(class("card"),
div(class("card-header"), textNode(name)) +*
div(class("card-body"), contents))))
}
## ========== Context for data item/modification
fun renderContextPanels () {
fun renderContext (wc) {
fun renderContextTable (r) {
fun highlightText (x,s) {
if (x.weekdate==model.weekdateDropdown && x.subcat==stringToInt(model.subcatDropdown)) {
"<b>"^^s^^"</b>"
} else {
s
}
}
fun renderContextRow (x) {
tr(a0,
td(a0, unescapedTextNode(highlightText(x,x.weekdate))) +*
td(a0, unescapedTextNode(highlightText(x,x.cat))) +*
td(a0, unescapedTextNode(highlightText(x,x.subcat_name))) +*
td(a0, textNode({if (x.valid_from==beginningOfTime && x.valid_from==x.valid_to) {"Rejected"} else {"Accepted"}})) +*
td(a0,
button(type("button") +@ class("btn btn-primary") +@
#onClick(fun() { Continue }), textNode("See history"))))
onClick(fun() { SeeHistory(intToString(x.subcat),x.weekdate) }), textNode("See history"))))
}
var rows = MvuHTML.concatMap(renderContextRow, r);
#
h5(class("card-title"),h0) +*
table_(class("table table-bordered"),
thead(a0,
tr(a0,
th(style("width: 15%"), textNode("Week")) +*
th(a0, textNode("Category")) +*
th(a0, textNode("Subcategory")) +*
th(a0, textNode("Decision")) +*
th(style("width: 14%"), textNode(""))
)) +*
tbody(a0, rows))
}
panel("Potential modifications arising in the week of "^^wc.weekmod,
renderContextTable(wc.othermods))
}
#
if (length(model.details)<>1) {
error("Error: zero or more than one item in result")
} else if (length(hd(model.details).mods)==1) {
panel("No modifications",
textNode("There have been no modifications to the value for the subcategory '"
^^hd(getSubcatDetails(model.subcatDropdown)).subcat_name^^"' and the week of "^^model.weekdateDropdown^^".") +*
br(a0,h0)) # +*
#textNode(" The death count is "^^intToString(hd(model.details).count)^^"."))
} else {
var c = map(getContext,tl(hd(model.details).mods)); # exclude upload record, details ordered by weekdate and subcat
panel("Other data items with accepted/rejected modifications by week of modification",
MvuHTML.concatMap(renderContext,c))
}
}
## ========== List of data items with at least one modification
fun renderModPanel (d) {
fun renderModRow(x) {
tr(a0,
td(a0, textNode(x.weekdate)) +*
td(a0, textNode(x.cat)) +*
td(a0, textNode(x.subcat_name)) +*
td(a0, textNode(intToString(length(x.mods)-1)^^" "^^classifyMods(tl(x.mods)))) +*
td(a0, button(type("button") +@ class("btn btn-primary") +@
onClick(fun() { SeeHistory(intToString(x.subcat),x.weekdate) }), textNode("See history")))
)
}
var rows = MvuHTML.concatMap(renderModRow, d);
var modTable = {
h5(class("card-title"),h0) +*
table_(class("table table-bordered"),
thead(a0,
tr(a0,
th(style("width: 14%"), textNode("Week")) +*
th(a0, textNode("Category")) +*
th(a0, textNode("Subcategory")) +*
th(a0, textNode("Number of updates seen")) +*
th(a0, h0)
)) +*
tbody(a0, rows))
};
panel("Data items with at least one accepted/rejected modification",modTable)
}
## =========== Item form with dropdowns for displaying item and details (once requested)
var itemForm= {
## =========== Setup of dropdown menus
var catDropdown = {
fun renderDropdownItem(x) {
option(value(intToString(x.subcat)), textNode(x.cat^^": "^^x.subcat_name))
}
var options = MvuHTML.concatMap(renderDropdownItem, getFromSubcats());
select_(
id("catDropdown") +@
class("form-control") +@
onChange(fun(x) { GetSubcat(x) } ), options)
};
var weekDropdown = {
fun renderDropdownItem(x) {
option(value(x.weekdate), textNode(x.weekdate))
}
var options = MvuHTML.concatMap(renderDropdownItem, getFromWeeks());
select_(
id("weekDropdown") +@
class("form-control") +@
onChange(fun(x) { GetWeek(x) } ), options)
};
## ============ Select button for item specified by dropdowns
var itemSelect = {
form(a0,
div(class("form-group"),
label(for_("catDropdown"), textNode("Select subcategory")) +*
catDropdown) +*
div(class("form-group"),
label(for_("weekDropdown"), textNode("Select week")) +*
weekDropdown) +*
div(class("form-group"),
button(type("button") +@ class("btn btn-primary mr-2 mt-2") +@
enabledAttr +@
onClick(fun() { MoreDetail(model.subcatDropdown,model.weekdateDropdown) }), textNode("Lookup"))))
};
## =========== Detail table (only displayed after lookup selected)
# not currently used, calculated change between updates incorrectly,
# it may be better to store the change in the covid_Fatalities table
fun processDetailsAndCalcChange(details,c) {
fun newMods (d,c) {
(decision_time=if (d.valid_from==beginningOfTime) {d.decision_time} else {d.valid_from},
weekmod=d.weekmod,
count=d.count,
change=if (c==0) { "-" } else { intToString(d.count-c) },
dec=if (c==0) { "New value" } else if (d.valid_from==d.valid_to && d.valid_from==beginningOfTime)
{"Reject"} else { "Accept" })
}
switch (details) {
case [] -> []
case x::xs -> newMods(x,c)::processDetailsAndCalcChange(xs,x.count)
}
}
fun processNewDetails (d) {
(weekmod=d.weekmod, count=d.count, change=0,
decision_time=d.valid_from,dec="New data item stored")
}
fun processModDetails (d) {
(weekmod=d.weekmod, count=d.count, change=0,
decision_time=if (d.valid_from==beginningOfTime) {d.decision_time} else {d.valid_from},
dec=if (d.valid_from==d.valid_to && d.valid_from==beginningOfTime) {"Modification rejected"} else { "Modification accepted" })
}
fun renderDetailsTable(details) {
fun renderDetailsRow(x) {
tr(a0,
td(a0, textNode(show(x.decision_time))) +*
td(a0, textNode(intToString(x.count))) +*
td(a0, textNode(x.dec)) +*
#td(a0, textNode(x.change)) +*
td(a0, textNode(x.weekmod))
)
}
var rows = MvuHTML.concatMap(renderDetailsRow, details);
{
h5(class("card-title"),h0) +*
table_(class("table table-bordered"),
thead(a0,
tr(a0,
th(a0,textNode("Date")) +*
th(a0,textNode("Value")) +*
th(a0,textNode("Decision")) +*
#th(a0,textNode("Change")) +*
th(a0,textNode("Week of modification"))
)) +*
tbody(a0, rows))
}
}
var itemDetails = if (model.results_focus) {
if (length(model.details)>1) {
error("Error: two items match selection criteria")
} else if (length(model.details)==1) {
var d = hd(model.details);
var detailsTable = renderDetailsTable(processNewDetails(hd(d.mods))::map(processModDetails,tl(d.mods)));
var s1 = "The current value for subcategory <b>"^^d.subcat_name^^
"</b> and week <b>"^^d.weekdate^^"</b> is <b>"^^intToString(d.count)^^"</b>.<br />
This data item has the following history.";
form(a0,
div(class("form-group"),
label(for_("cont"), unescapedTextNode(s1) +* detailsTable +*
button(type("button") +@ class("btn btn-primary mr-2 mt-2") +@
onClick(fun() { Context }) +@ id("context"),
textNode("See modifications in context")) +*
button(type("button") +@ class("btn btn-primary mr-2 mt-2") +@
onClick(fun() { Continue }) +@ id("cont"), textNode("Continue")))))
} else {
var s = "There is no data item for subcategory <b>"^^hd(getSubcatDetails(model.subcatDropdown)).subcat_name^^
"</b> and week <b>"^^model.weekdateDropdown^^"</b>.<br /><br />";
form(a0,
div(class("form-group"),
label(for_("cont"), unescapedTextNode(s) +*
button(type("button") +@ class("btn btn-primary mr-2 mt-2") +@
onClick(fun() { Continue }) +@ id("cont"), textNode("Continue")))))
}
} else {h0};
if (model.results_focus) {
panel("Data item update history",itemDetails)
} else {
panel("Data item update history",itemSelect +* itemDetails)
}
};
## ========== Selection of panels to display
if (model.inContext) {
var contextPanels = renderContextPanels();
div(class("container"),
h5(class("title"), textNode("Provenance: individual data items")) +* itemForm +* contextPanels)
} else {
var modPanel = renderModPanel(multimods);
div(class("container"),
h5(class("title"), textNode("Provenance: individual data items")) +* itemForm +* modPanel)
}
}
var topLink = a(name("top"), h0); # sufficient to create anchor, no need to put topLink
# elsewhere in code
redirect("#top");
renderModel(model)
}
#============= MAIN
var week_info = getFromWeeks();
var init_weekdateDropdown = if (length(week_info)<>0) hd(week_info).weekdate else "None";
var init_subcatDropdown = intToString(hd(getFromSubcats()).subcat);
fun mainPage() {
var initialModel = (results_text = "", results_focus = false, inContext = false,
subcatDropdown=init_subcatDropdown, weekdateDropdown=init_weekdateDropdown, summary = getFatalities(), details=[]);
runSimple("placeholder", initialModel, view, updt);
Template.template()
}
fun main() {
addRoute("/", fun(_) { mainPage() });
addStaticRoute("/css", "css", [("css", "text/css")]);
addStaticRoute("/js", "js", [("js", "text/javascript")]);
serveWebsockets();
servePages()
}
main()