/
create_project.go
619 lines (513 loc) · 14.4 KB
/
create_project.go
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package randomization
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
)
// createProjectStep1 gets the project name from the user.
func createProjectStep1(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
tvals := struct {
User string
LoggedIn bool
}{
User: user.String(),
LoggedIn: user != nil,
}
if err := tmpl.ExecuteTemplate(w, "create_project_step1.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep1 failed to execute template: %v", err)
}
}
// createProjectStep2 asks if the subject-level data are to be logged.
func createProjectStep2(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
projectName := r.FormValue("project_name")
// Check if the project name has already been used.
pkey := user.String() + "::" + projectName
key := datastore.NewKey(ctx, "EncodedProject", pkey, 0, nil)
var pr EncodedProject
err := datastore.Get(ctx, key, &pr)
if err == nil {
msg := fmt.Sprintf("A project named \"%s\" belonging to user %s already exists.", projectName, user.String())
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
return
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
}
if err := tmpl.ExecuteTemplate(w, "create_project_step2.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep2 failed to execute template: %v", err)
}
}
// createProjectStep3 gets the number of treatment groups.
func createProjectStep3(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
StoreRawData bool
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
StoreRawData: r.FormValue("store_rawdata") == "yes",
}
if err := tmpl.ExecuteTemplate(w, "create_project_step3.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep3 failed to execute template: %v", err)
}
}
// createProjectStep4
func createProjectStep4(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
numgroups, _ := strconv.Atoi(r.FormValue("numgroups"))
// Group numbers (they don't have names yet)
IX := make([]int, numgroups)
for i := 0; i < numgroups; i++ {
IX[i] = i + 1
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
StoreRawData bool
NumGroups int
IX []int
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
StoreRawData: r.FormValue("store_rawdata") == "true",
IX: IX,
NumGroups: numgroups,
}
if err := tmpl.ExecuteTemplate(w, "create_project_step4.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep4 failed to execute template: %v", err)
}
}
// createProjectStep5
func createProjectStep5(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
numgroups, _ := strconv.Atoi(r.FormValue("numgroups"))
// Indices for the groups
ix := make([]int, numgroups)
for i := 0; i < numgroups; i++ {
ix[i] = i
}
// Get the group names from the previous page
GroupNames := make([]string, numgroups)
for i := 0; i < numgroups; i++ {
GroupNames[i] = r.FormValue(fmt.Sprintf("name%d", i+1))
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
GroupNames string
GroupNames_arr []string
StoreRawData bool
NumGroups int
IX []int
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
GroupNames: strings.Join(GroupNames, ","),
GroupNames_arr: GroupNames,
NumGroups: len(GroupNames),
StoreRawData: r.FormValue("store_rawdata") == "true",
IX: ix,
}
if err := tmpl.ExecuteTemplate(w, "create_project_step5.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep5 failed to execute template: %v", err)
}
}
// createProjectStep6
func createProjectStep6(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
numgroups, err := strconv.Atoi(r.FormValue("numgroups"))
if err != nil {
msg := "Unable to parse project, the number of groups must be a number."
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
log.Errorf(ctx, "createProjectStep6: %v", err)
}
// Get the sampling rates from the previous page
groupNamesArr := cleanSplit(r.FormValue("group_names"), ",")
samplingRates := make([]string, numgroups)
for i := 0; i < numgroups; i++ {
samplingRates[i] = r.FormValue(fmt.Sprintf("rate%s", groupNamesArr[i]))
x, err := strconv.ParseFloat(samplingRates[i], 64)
if err != nil {
log.Errorf(ctx, "createProjectStep6: %v", err)
}
if (err != nil) || (x <= 0) {
msg := "The sampling rates must be positive numbers."
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
return
}
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
GroupNames string
StoreRawData bool
SamplingRates string
NumGroups int
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
GroupNames: r.FormValue("group_names"),
StoreRawData: r.FormValue("store_rawdata") == "true",
SamplingRates: strings.Join(samplingRates, ","),
NumGroups: numgroups,
}
if err := tmpl.ExecuteTemplate(w, "create_project_step6.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep6 failed to execute template: %v", err)
}
}
// createProjectStep7
func createProjectStep7(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
log.Errorf(ctx, "createProjectStep7: %v", err)
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
numgroups, _ := strconv.Atoi(r.FormValue("numgroups"))
numvar, _ := strconv.Atoi(r.FormValue("numvar"))
ix := make([]int, numvar)
for i := 0; i < numvar; i++ {
ix[i] = i + 1
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
IX []int
GroupNames string
StoreRawData bool
NumGroups int
NumVar int
Any_vars bool
SamplingRates string
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
GroupNames: r.FormValue("group_names"),
IX: ix,
NumGroups: numgroups,
NumVar: numvar,
Any_vars: numvar > 0,
StoreRawData: r.FormValue("store_rawdata") == "true",
SamplingRates: r.FormValue("rates"),
}
if err := tmpl.ExecuteTemplate(w, "create_project_step7.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep7 failed to execute template: %v", err)
}
}
func processVariableInfo(r *http.Request, numvar int) (string, bool) {
variables := make([]string, numvar)
for i := 0; i < numvar; i++ {
vec := make([]string, 4)
vname := fmt.Sprintf("name%d", i+1)
vec[0] = strings.TrimSpace(r.FormValue(vname))
if len(vec[0]) == 0 {
return "", false
}
vname = fmt.Sprintf("levels%d", i+1)
vec[1] = r.FormValue(vname)
levels := cleanSplit(vec[1], ",")
if len(levels) < 2 {
return "", false
}
for _, x := range levels {
if len(x) == 0 {
return "", false
}
}
vec[2] = r.FormValue(fmt.Sprintf("weight%d", i+1))
vec[3] = r.FormValue(fmt.Sprintf("func%d", i+1))
variables[i] = strings.Join(vec, ";")
}
return strings.Join(variables, ":"), true
}
// createProjectStep8
func createProjectStep8(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
numgroups, err := strconv.Atoi(r.FormValue("numgroups"))
if err != nil {
log.Errorf(ctx, "createrProjectStep8: %v", err)
}
numvar, err := strconv.Atoi(r.FormValue("numvar"))
if err != nil {
log.Errorf(ctx, "createrProjectStep8: %v", err)
}
variables, ok := processVariableInfo(r, numvar)
if !ok {
validationErrorStep8(w, r)
return
}
ix := make([]int, numvar)
for i := 0; i < numvar; i++ {
ix[i] = i + 1
}
tvals := struct {
User string
LoggedIn bool
Name string
Pkey string
IX []int
GroupNames string
StoreRawData bool
NumGroups int
Numvar int
Variables string
SamplingRates string
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
GroupNames: r.FormValue("group_names"),
IX: ix,
NumGroups: numgroups,
Numvar: numvar,
Variables: variables,
StoreRawData: r.FormValue("store_rawdata") == "true",
SamplingRates: r.FormValue("rates"),
}
if err := tmpl.ExecuteTemplate(w, "create_project_step8.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep8 failed to execute template: %v", err)
}
}
// createProjectStep9 creates the project using all supplied
// information, and stores the project in the database.
func createProjectStep9(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
user := user.Current(ctx)
numvar, err := strconv.Atoi(r.FormValue("numvar"))
if err != nil {
log.Errorf(ctx, "createProjectStep9: %v", err)
}
GroupNames := r.FormValue("group_names")
projectName := r.FormValue("project_name")
variables := r.FormValue("variables")
VL := cleanSplit(variables, ":")
bias, err := strconv.Atoi(r.FormValue("bias"))
if err != nil {
log.Errorf(ctx, "createProjectStep9: %v", err)
}
// Parse and validate the variable information.
VA := make([]Variable, numvar)
for i, vl := range VL {
vx := cleanSplit(vl, ";")
var va Variable
va.Name = vx[0]
va.Levels = cleanSplit(vx[1], ",")
va.Weight, err = strconv.ParseFloat(vx[2], 64)
if err != nil {
log.Errorf(ctx, "createProjectStep9: %v", err)
}
va.Func = vx[3]
VA[i] = va
}
var project Project
project.Owner = user.String()
project.Created = time.Now()
project.Name = projectName
project.Variables = VA
project.Bias = bias
project.GroupNames = cleanSplit(GroupNames, ",")
project.Assignments = make([]int, len(project.GroupNames))
project.StoreRawData = r.FormValue("store_rawdata") == "true"
project.Open = true
// Convert the rates to numbers
rates := r.FormValue("rates")
ratesArr := cleanSplit(rates, ",")
ratesNum := make([]float64, len(ratesArr))
for i, x := range ratesArr {
ratesNum[i], err = strconv.ParseFloat(x, 64)
if err != nil {
log.Errorf(ctx, "createProjectStep9: %v", err)
}
}
project.SamplingRates = ratesNum
// Set up the data.
numgroups := len(project.GroupNames)
data0 := make([][][]float64, len(project.Variables))
for j, va := range project.Variables {
data0[j] = make([][]float64, len(va.Levels))
for k := range va.Levels {
data0[j][k] = make([]float64, numgroups)
}
}
project.Data = data0
pkey := user.String() + "::" + projectName
dkey := datastore.NewKey(ctx, "EncodedProject", pkey, 0, nil)
eproj, err := encodeProject(&project)
if err != nil {
log.Errorf(ctx, "Create_project_step9 [2]: %v", err)
}
_, err = datastore.Put(ctx, dkey, eproj)
if err != nil {
msg := "A datastore error occured, the project was not created."
log.Errorf(ctx, "Create_project_step9: %v", err)
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
return
}
// Remove any stale SharingByProject entities
dkey = datastore.NewKey(ctx, "SharingByProject", pkey, 0, nil)
err = datastore.Delete(ctx, dkey)
if err != nil {
log.Errorf(ctx, "Create_project_step9 [3]: %v", err)
}
tvals := struct {
User string
LoggedIn bool
}{
User: user.String(),
LoggedIn: user != nil,
}
if err := tmpl.ExecuteTemplate(w, "create_project_step9.html", tvals); err != nil {
log.Errorf(ctx, "createProjectStep9 failed to execute template: %v", err)
}
}
func validationErrorStep8(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
if err := r.ParseForm(); err != nil {
ServeError(ctx, w, err)
return
}
numgroups, err := strconv.Atoi(r.FormValue("numgroups"))
if err != nil {
log.Errorf(ctx, "validationErrorStep8: %v", err)
}
numvar, err := strconv.Atoi(r.FormValue("numvar"))
if err != nil {
log.Errorf(ctx, "validationErrorStep8: %v", err)
}
tvals := struct {
User string
LoggedIn bool
Name string
NumGroups int
Pkey string
GroupNames string
StoreRawData bool
Numvar int
SamplingRates string
}{
User: user.String(),
LoggedIn: user != nil,
Name: r.FormValue("project_name"),
GroupNames: r.FormValue("group_names"),
NumGroups: numgroups,
Numvar: numvar,
StoreRawData: r.FormValue("store_rawdata") == "true",
SamplingRates: r.FormValue("rates"),
}
if err := tmpl.ExecuteTemplate(w, "validation_error_step8.html", tvals); err != nil {
log.Errorf(ctx, "Failed to execute template: %v", err)
}
}