-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosedBallot.ur
354 lines (328 loc) · 20.8 KB
/
closedBallot.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
open Bootstrap3
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 (voterKey ++ choiceBallot) ~ [Client, Channel]
val amVoter : transaction (option $voterKey)
val maxVotesPerVoter : option int
val keyFilter : sql_exp [Choice = choiceBallot ++ choiceKey ++ choiceRest] [] [] 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 int,
MyVotes : source int}
datatype operation = Vote | Unvote
type action = { Operation : operation, IsItYou : bool, Choice : $choiceKey }
table listeners : (choiceBallot ++ voterKey ++ [Channel = channel action])
type input = _
type a = {Voter : $voterKey,
Ballot : $choiceBallot,
Choices : list choice,
Channel : channel action}
fun create r =
choices <- List.mapQueryM ({{{sql_query1 [[]]
{Distinct = False,
From = @@sql_left_join [[]]
[[Choice = choiceBallot ++ choiceKey ++ choiceRest,
Vote = map option (choiceBallot ++ choiceKey ++ [Votes = int] ++ voterKey)]]
[[YourVote = map (fn t => (t, option t)) (choiceBallot ++ choiceKey ++ [Votes = int] ++ voterKey)]]
! ! !
{YourVote = @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)}
(@@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}))
(FROM vote AS YourVote)
(WHERE {@@Sql.easy_join [#Choice] [#YourVote]
[choiceKey] [choiceBallot ++ choiceRest]
[choiceBallot ++ [Votes = _] ++ voterKey]
[[Vote = map option (choiceBallot ++ choiceKey ++ [Votes = int] ++ voterKey)]] [[]] [[]]
! ! ! ! choiceKeyFl}
AND {@@Sql.easy_where [#YourVote] [voterKey] [_]
[[Choice = _, Vote = map option (choiceBallot ++ choiceKey ++ [Votes = int] ++ voterKey)]] [_] [_]
! ! voterKeyInj' voterKeyFl r.Voter}),
Where = (WHERE {@@Sql.easy_where [#Choice] [choiceBallot] [_] [_] [_] [_]
! ! choiceBallotInj' choiceBallotFl r.Ballot}
AND {sql_exp_weaken keyFilter}),
GroupBy = sql_subset [[Choice = (choiceKey, _),
Vote = ([], _),
YourVote = ([], _)]],
Having = (WHERE TRUE),
SelectFields = sql_subset [[Choice = (choiceKey, _),
Vote = ([], _),
YourVote = ([], _)]],
SelectExps = {Votes = sql_window (SQL SUM(vote.Votes)),
MyVotes = sql_window (SQL SUM(YourVote.Votes))}} }}}
ORDER BY {{{@Sql.order_by choiceKeyFl
(@Sql.some_fields [#Choice] [choiceKey] ! ! choiceKeyFl)
sql_asc}}})
(fn {Choice = k, Votes = n, MyVotes = yn, ...} =>
s <- source (Option.get 0 n);
ys <- source (Option.get 0 yn);
return {Key = k, Votes = s, MyVotes = ys});
chan <- channel;
@@Sql.easy_insert [[Channel = channel action] ++ choiceBallot ++ voterKey] [_]
({Channel = _ : sql_injectable (channel action)} ++ choiceBallotInj' ++ voterKeyInj')
(@Folder.cons [#Channel] [_] ! (@Folder.concat ! choiceBallotFl voterKeyFl))
listeners ({Channel = chan} ++ r.Voter ++ r.Ballot);
return (r ++ {Choices = choices, Channel = chan})
fun onload a =
let
fun loop () =
act <- recv a.Channel;
(case act.Operation of
Vote =>
List.app (fn ch =>
if ch.Key <> act.Choice then
return ()
else
(votes <- Basis.get ch.Votes;
set ch.Votes (votes + 1);
if act.IsItYou then
(yourVotes <- Basis.get ch.MyVotes;
set ch.MyVotes (yourVotes + 1))
else
return ())) a.Choices
| Unvote =>
List.app (fn ch =>
if ch.Key <> act.Choice then
return ()
else
(votes <- Basis.get ch.Votes;
set ch.Votes (votes - 1);
if act.IsItYou then
(yourVotes <- Basis.get ch.MyVotes;
set ch.MyVotes (yourVotes - 1))
else
return ())) a.Choices);
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, listeners.{{voterKey}}
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Unvote, IsItYou = (l -- #Channel = 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, listeners.{{voterKey}}
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Unvote, IsItYou = (l -- #Channel = 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, listeners.{{voterKey}}
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Vote, IsItYou = (l -- #Channel = 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, listeners.{{voterKey}}
FROM listeners
WHERE {@Sql.easy_where [#Listeners] ! ! choiceBallotInj' choiceBallotFl r.Ballot})
(fn l => send l.Channel {Operation = Vote, IsItYou = (l -- #Channel = v), Choice = r.Choice})
fun oneChoice showKey a ch = <xml>
<tr>
<td>
<dyn signal={votes <- signal ch.Votes;
myVotes <- signal ch.MyVotes;
return <xml>
<button class={if myVotes = 0 then
CLASS "disabled btn glyphicon glyphicon-minus"
else
CLASS "btn glyphicon glyphicon-minus"}
onclick={fn _ => rpc (del {Ballot = a.Ballot,
Choice = ch.Key})}/>
<button class={if (case maxVotesPerVoter of
None => False
| Some n => myVotes >= n) then
CLASS "disabled btn glyphicon glyphicon-plus"
else
CLASS "btn glyphicon glyphicon-plus"}
onclick={fn _ => rpc (add {Ballot = a.Ballot,
Choice = ch.Key})}/>
</xml>}/>
</td>
{if showKey then
<xml><td>{[ch.Key]}</td></xml>
else
<xml/>}
<td>
<dyn signal={votes <- signal ch.MyVotes;
return <xml>{[votes]}</xml>}/>
</td>
<td>
<dyn signal={votes <- signal ch.Votes;
return <xml>{[votes]}</xml>}/>
</td>
</tr>
</xml>
fun render _ a = <xml>
<table class="bs3-table table-striped">
<tr>
<th/>
<th>Choice</th>
<th>Your Vote</th>
<th>Total Votes</th>
</tr>
{List.mapX (oneChoice True a) a.Choices}
</table>
<dyn signal={(count, choices) <- List.foldlM (fn ch (count, choices) =>
thisCount <- signal ch.Votes;
if thisCount > count then
return (thisCount, ch.Key :: [])
else if thisCount = count then
return (count, ch.Key :: choices)
else
return (count, choices))
(0, []) a.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 ui r = {Create = create r,
Onload = onload,
Render = render}
fun removeVotesFor r =
dml (DELETE FROM vote
WHERE {@@Sql.easy_where [#T] [choiceBallot ++ choiceKey] [voterKey ++ [Votes = _]]
[[]] [[]] [[]] ! !
(choiceBallotInj' ++ choiceKeyInj')
(@Folder.concat ! choiceBallotFl choiceKeyFl)
r})
end