-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathopenBallot.ur
456 lines (418 loc) · 23.8 KB
/
openBallot.ur
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
open Bootstrap
functor Make(M : sig
con voterKey1 :: Name
type voterKeyT
con voterKeyR :: {Type}
constraint [voterKey1] ~ voterKeyR
con voterKey = [voterKey1 = voterKeyT] ++ voterKeyR
con voterRest :: {Type}
constraint voterKey ~ voterRest
con voterKeyName :: Name
con voterOtherConstraints :: {{Unit}}
constraint [voterKeyName] ~ voterOtherConstraints
val voter : sql_table (voterKey ++ voterRest) ([voterKeyName = map (fn _ => ()) voterKey] ++ voterOtherConstraints)
val voterKeyInj : $(map sql_injectable_prim voterKey)
val voterKeyFl : folder voterKey
val voterKeyShow : show $voterKey
val voterKeyEq : $(map eq voterKey)
val voterKeyOrd : $(map ord voterKey)
con choiceBallot :: {Type} (* Identifies subsets of the choices that should be considered together *)
con choiceKey1 :: Name
type choiceKeyT
con choiceKeyR :: {Type}
constraint [choiceKey1] ~ choiceKeyR
con choiceKey = [choiceKey1 = choiceKeyT] ++ choiceKeyR
con choiceRest :: {Type}
constraint choiceBallot ~ choiceKey
constraint (choiceKey ++ choiceBallot) ~ choiceRest
con choiceKeyName :: Name
con choiceOtherConstraints :: {{Unit}}
constraint [choiceKeyName] ~ choiceOtherConstraints
val choice : sql_table (choiceBallot ++ choiceKey ++ choiceRest) ([choiceKeyName = map (fn _ => ()) (choiceBallot ++ choiceKey)] ++ choiceOtherConstraints)
val choiceKeyInj : $(map sql_injectable_prim choiceKey)
val choiceKeyFl : folder choiceKey
val choiceKeyShow : show $choiceKey
val choiceKeyEq : $(map eq choiceKey)
val choiceBallotInj : $(map sql_injectable_prim choiceBallot)
val choiceBallotFl : folder choiceBallot
constraint voterKey ~ (choiceBallot ++ choiceKey)
constraint (voterKey ++ choiceBallot ++ choiceKey) ~ [Votes]
constraint choiceBallot ~ [Channel]
val amVoter : transaction (option $voterKey)
val maxVotesPerVoter : option int
val keyFilter : sql_exp [Choice = choiceBallot ++ choiceKey ++ choiceRest] [] [] bool
val alwaysShowVotes : bool
end) = struct
open M
val voterKeyEq : eq $voterKey = @@Record.eq [voterKey] voterKeyEq voterKeyFl
val voterKeyOrd : ord $voterKey = @@Record.ord [voterKey] voterKeyOrd voterKeyFl
val choiceKeyEq : eq $choiceKey = @@Record.eq [choiceKey] choiceKeyEq choiceKeyFl
val voterKeyInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim voterKeyFl voterKeyInj
val choiceKeyInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim choiceKeyFl choiceKeyInj
val choiceBallotInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim choiceBallotFl choiceBallotInj
open Option
table vote : (choiceBallot ++ choiceKey ++ voterKey ++ [Votes = int])
PRIMARY KEY {{@primary_key [choiceKey1] [choiceKeyR ++ choiceBallot ++ voterKey] ! !
(choiceKeyInj ++ choiceBallotInj ++ voterKeyInj)}},
{{one_constraint [#Choice] (@Sql.easy_foreign ! ! ! ! ! ! (@Folder.concat ! choiceBallotFl choiceKeyFl) choice)}},
{{one_constraint [#Voter] (@Sql.easy_foreign ! ! ! ! ! ! voterKeyFl voter)}}
type choice = {Key : $choiceKey,
Votes : source (list ($voterKey * int)),
ShowVoters : source bool}
datatype operation = Vote | Unvote
type action = { Operation : operation, Voter : $voterKey, Choice : $choiceKey }
table listeners : (choiceBallot ++ [Channel = channel action])
type input = _
type a = {Voter : $voterKey,
Ballot : $choiceBallot,
Choices : source (list choice),
Channel : channel action}
fun create r =
let
fun doVotes (ls : list { Choice : $choiceKey, Vote : $(map option voterKey ++ [Votes = option int]) })
(acc : list choice)
(lastChoice : option $choiceKey)
(votes : list ($voterKey * int)) =
case ls of
[] =>
(case lastChoice of
None => return acc
| Some ch =>
votes <- source votes;
sv <- source False;
return ({Key = ch, Votes = votes, ShowVoters = sv} :: acc))
| {Choice = ch, Vote = v} :: ls =>
case @Sql.unnull (@Folder.concat ! (_ : folder [Votes = _]) voterKeyFl) v of
None =>
(* This is a choice with no votes yet. *)
(case lastChoice of
None =>
votes <- source [];
sv <- source False;
doVotes ls ({Key = ch, Votes = votes, ShowVoters = sv} :: acc) None []
| Some ch' =>
votes' <- source votes;
sv' <- source False;
votes <- source [];
sv <- source False;
doVotes ls ({Key = ch, Votes = votes, ShowVoters = sv} :: {Key = ch', Votes = votes', ShowVoters = sv'} :: acc) None [])
| Some v =>
(* This is a vote for the current choice. *)
if lastChoice = Some ch then
(* We're continuing with the last choice processed. *)
doVotes ls acc lastChoice ((v -- #Votes, v.Votes) :: votes)
else
case lastChoice of
None =>
(* There was no last choice needing further processing. *)
doVotes ls acc (Some ch) ((v -- #Votes, v.Votes) :: [])
| Some lastChoice =>
votes <- source votes;
sv <- source False;
doVotes ls ({Key = lastChoice, Votes = votes, ShowVoters = sv} :: acc)
(Some ch) ((v -- #Votes, v.Votes) :: [])
in
votes <- queryL ({{{sql_query1 [[]]
{Distinct = False,
From = @@sql_left_join [[]]
[[Choice = choiceBallot ++ choiceKey ++ choiceRest]]
[[Vote = map (fn t => (t, option t)) (choiceBallot ++ choiceKey ++ [Votes = int] ++ voterKey)]]
! ! !
{Vote = @Top.mp [sql_injectable_prim] [fn t => nullify t (option t)]
@@nullify_prim (@Folder.concat ! (_ : folder [Votes = _])
(@Folder.concat !
(@Folder.concat ! choiceBallotFl choiceKeyFl)
voterKeyFl))
(_ ++ voterKeyInj
++ choiceBallotInj ++ choiceKeyInj)}
(FROM choice) (FROM vote)
(WHERE {@@Sql.easy_join [#Choice] [#Vote]
[choiceKey] [choiceBallot ++ choiceRest]
[choiceBallot ++ [Votes = _] ++ voterKey]
[[]] [[]] [[]]
! ! ! ! choiceKeyFl}),
Where = (WHERE {@@Sql.easy_where [#Choice] [choiceBallot] [_] [_] [_] [_]
! ! choiceBallotInj' choiceBallotFl r.Ballot}
AND {sql_exp_weaken keyFilter}),
GroupBy = sql_subset_all [_],
Having = (WHERE TRUE),
SelectFields = sql_subset [[Choice = (choiceKey, _),
Vote = ([Votes = _] ++ map option voterKey, _)]],
SelectExps = {}} }}}
ORDER BY {{{@Sql.order_by (@Folder.concat ! choiceKeyFl (@Folder.mp voterKeyFl))
(@Sql.some_fields [#Choice] [choiceKey] ! ! choiceKeyFl
++ @Sql.some_fields [#Vote] [map option voterKey] ! ! (@Folder.mp voterKeyFl))
sql_asc}}});
choices <- doVotes votes [] None [];
choices <- source choices;
chan <- channel;
@@Sql.easy_insert [[Channel = channel action] ++ choiceBallot] [_]
({Channel = _ : sql_injectable (channel action)} ++ choiceBallotInj')
(@Folder.cons [#Channel] [_] ! choiceBallotFl)
listeners ({Channel = chan} ++ r.Ballot);
return (r ++ {Choices = choices, Channel = chan})
end
fun onload a =
let
fun loop () =
act <- recv a.Channel;
(case act.Operation of
Vote =>
chs <- Basis.get a.Choices;
List.app (fn ch =>
if ch.Key <> act.Choice then
return ()
else
votes <- Basis.get ch.Votes;
oldcount <- return (Option.get 0 (List.assoc act.Voter votes));
votes <- return ((act.Voter, oldcount+1)
:: List.filter (fn (v, _) =>
v <> act.Voter) votes);
set ch.Votes (List.sort (fn (v1, _) (v2, _) => v1 > v2) votes)) chs
| Unvote =>
chs <- Basis.get a.Choices;
List.app (fn ch =>
if ch.Key <> act.Choice then
return ()
else
votes <- Basis.get ch.Votes;
oldcount <- return (Option.get 0 (List.assoc act.Voter votes));
votes <- return (List.filter (fn (v, _) =>
v <> act.Voter) votes);
votes <- return (if oldcount > 1 then
(act.Voter, oldcount-1) :: votes
else
votes);
set ch.Votes (List.sort (fn (v1, _) (v2, _) => v1 > v2) votes)) chs);
loop ()
in
spawn (loop ())
end
fun del r =
v <- amVoter;
case v of
None => error <xml>Not authenticated as a voter</xml>
| Some v =>
oldCount <- oneOrNoRowsE1 (SELECT (vote.Votes)
FROM vote
WHERE {@@Sql.easy_where [#Vote] [choiceBallot ++ choiceKey ++ voterKey]
[[Votes = _]] [[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl))
(r.Ballot ++ r.Choice ++ v)});
case oldCount of
None => return ()
| Some 1 =>
dml (DELETE FROM vote
WHERE {@@Sql.easy_where [#T] [choiceBallot ++ choiceKey ++ voterKey]
[[Votes = _]] [[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl))
(r.Ballot ++ r.Choice ++ v)});
queryI1 (SELECT listeners.Channel
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Unvote, Voter = v, Choice = r.Choice})
| Some oldCount =>
dml (UPDATE vote
SET Votes = Votes - 1
WHERE {@@Sql.easy_where [#T] [choiceBallot ++ choiceKey ++ voterKey]
[[Votes = _]] [[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl))
(r.Ballot ++ r.Choice ++ v)});
queryI1 (SELECT listeners.Channel
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Unvote, Voter = v, Choice = r.Choice})
fun add r =
v <- amVoter;
case v of
None => error <xml>Not authenticated as a voter</xml>
| Some v =>
oldCount <- oneOrNoRowsE1 (SELECT (vote.Votes)
FROM vote
WHERE {@@Sql.easy_where [#Vote] [choiceBallot ++ choiceKey ++ voterKey]
[[Votes = _]] [[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl))
(r.Ballot ++ r.Choice ++ v)});
case oldCount of
None =>
@@Sql.easy_insert [choiceBallot ++ choiceKey ++ voterKey ++ [Votes = int]] [_]
({Votes = _} ++ choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.cons [#Votes] [_] ! (@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl)))
vote
(r.Ballot ++ r.Choice ++ v ++ {Votes = 1});
queryI1 (SELECT listeners.Channel
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Vote, Voter = v, Choice = r.Choice})
| Some n =>
if (case maxVotesPerVoter of
None => False
| Some m => n >= m) then
return ()
else
dml (UPDATE vote
SET Votes = Votes + 1
WHERE {@@Sql.easy_where [#T] [choiceBallot ++ choiceKey ++ voterKey]
[[Votes = _]] [[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj' ++ voterKeyInj')
(@Folder.concat ! choiceBallotFl
(@Folder.concat ! choiceKeyFl voterKeyFl))
(r.Ballot ++ r.Choice ++ v)});
queryI1 (SELECT listeners.Channel
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Vote, Voter = v, Choice = r.Choice})
fun oneChoice showKey showVotes a ch = <xml>
<tr>
<td>
<dyn signal={votes <- signal ch.Votes;
myVotes <- return (Option.get 0 (List.assoc a.Voter votes));
return <xml>
<button class={if myVotes = 0 then
CLASS "disabled btn"
else
CLASS "btn btn-secondary"}
onclick={fn _ => rpc (del {Ballot = a.Ballot,
Choice = ch.Key})}>
<span class="glyphicon glyphicon-minus"/>
</button>
<button class={if (case maxVotesPerVoter of
None => False
| Some n => myVotes >= n) then
CLASS "disabled btn"
else
CLASS "btn btn-secondary"}
onclick={fn _ => rpc (add {Ballot = a.Ballot,
Choice = ch.Key})}>
<span class="glyphicon glyphicon-plus"/>
</button>
</xml>}/>
</td>
{if showKey then
<xml><td>{[ch.Key]}</td></xml>
else
<xml/>}
<td>
<dyn signal={votes <- signal ch.Votes;
return <xml>{[List.foldl (fn (v, n) m => if v = a.Voter then n + m else m) 0 votes]}</xml>}/>
</td>
<td>
<dyn signal={votes <- signal ch.Votes;
return <xml>
{[List.foldl (fn (_, n) m => n + m) 0 votes]}
<dyn signal={sv <- (if showVotes then
return True
else
signal ch.ShowVoters);
return (if sv then <xml>
{if showVotes then
<xml/>
else
<xml><button class="btn btn-secondary"
onclick={fn _ => set ch.ShowVoters False}>
<span class="glyphicon glyphicon-chevron-left"/>
</button></xml>}
{List.mapX (fn (k, n) => <xml><br/>{[k]}{case maxVotesPerVoter of
Some 1 => <xml/>
| _ => <xml> ({[n]})</xml>}</xml>) votes}
</xml> else <xml>
<button class="btn btn-secondary"
onclick={fn _ => set ch.ShowVoters True}>
<span class="glyphicon glyphicon-chevron-right"/>
</button>
</xml>)}/>
</xml>}/>
</td>
</tr>
</xml>
fun render _ a = <xml>
<table class="bs-table">
<thead><tr>
<th/>
<th>Choice</th>
<th>Your Vote</th>
<th>Votes</th>
</tr></thead>
<tbody>
<dyn signal={choices <- signal a.Choices;
return (List.mapX (oneChoice True alwaysShowVotes a) choices)}/>
</tbody>
</table>
<dyn signal={choices <- signal a.Choices;
(count, choices) <- List.foldlM (fn ch (count, choices) =>
votes <- signal ch.Votes;
thisCount <- return (List.foldl (fn (_, n) m => n + m) 0 votes);
if thisCount > count then
return (thisCount, ch.Key :: [])
else if thisCount = count then
return (count, ch.Key :: choices)
else
return (count, choices))
(0, []) choices;
if count = 0 then
return <xml/>
else
return <xml>
<h3>Current leader{case choices of
_ :: _ :: _ => <xml>s</xml>
| _ => <xml/>}, with <i>{[count]}</i> vote{case count of
1 => <xml/>
| _ => <xml>s</xml>}:</h3>
{List.mapX (fn ch => <xml>{[ch]}<br/></xml>) choices}
</xml>}/>
</xml>
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
fun ui r = {Create = create r,
Onload = onload,
Render = render,
Notification = notification,
Buttons = buttons}
fun removeVotesFor r =
dml (DELETE FROM vote
WHERE {@@Sql.easy_where [#T] [choiceBallot ++ choiceKey] [voterKey ++ [Votes = _]]
[[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj')
(@Folder.concat ! choiceBallotFl choiceKeyFl)
r})
structure OneChoice = struct
type input = _
type a = {Base : a,
Choice : $choiceKey}
val create r =
base <- create (r -- #Choice);
return {Base = base, Choice = r.Choice}
val onload a = onload a.Base
fun render _ a = <xml>
<table class="bs-table">
<tr>
<th/>
<th>Your Vote</th>
<th>Votes</th>
</tr>
<dyn signal={choices <- signal a.Base.Choices;
return (List.mapX (fn ch =>
if ch.Key <> a.Choice then
<xml/>
else
oneChoice False True a.Base ch) choices)}/>
</table>
</xml>
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
fun ui x = {Create = create x,
Onload = onload,
Render = render,
Notification = notification,
Buttons = buttons}
end
end