/
handlerRead.go
1175 lines (993 loc) · 48.3 KB
/
handlerRead.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// worksetParameterPageReadHandler read a "page" of parameter values from workset.
// POST /api/model/:model/workset/:set/parameter/value
// Dimension(s) and enum-based parameters returned as enum codes.
func worksetParameterPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadParameterPageHandler(w, r, "set", true, true)
}
// worksetParameterIdPageReadHandler read a "page" of parameter values from workset.
// POST /api/model/:model/workset/:set/parameter/value-id
// Dimension(s) and enum-based parameters returned as enum id, not enum codes.
func worksetParameterIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadParameterPageHandler(w, r, "set", true, false)
}
// runParameterPageReadHandler read a "page" of parameter values from model run.
// POST /api/model/:model/run/:run/parameter/value
// Dimension(s) and enum-based parameters returned as enum codes.
func runParameterPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadParameterPageHandler(w, r, "run", false, true)
}
// runParameterIdPageReadHandler read a "page" of parameter values from model run.
// POST /api/model/:model/run/:run/parameter/value-id
// Dimension(s) and enum-based parameters returned as enum id, not enum codes.
func runParameterIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadParameterPageHandler(w, r, "run", false, false)
}
// doReadParameterPageHandler read a "page" of parameter values from workset or model run.
// Json is posted to specify parameter name, "page" size and other read arguments,
// see db.ReadParamLayout for more details.
// Page is part of parameter values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Dimension(s) and enum-based parameters returned as enum codes or enum id's
func doReadParameterPageHandler(w http.ResponseWriter, r *http.Request, srcArg string, isSet, isCode bool) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
src := getRequestParam(r, srcArg) // workset name or run digest-or-name
// decode json request body
var layout db.ReadParamLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
layout.IsFromSet = isSet // overwrite json value, it was likely default
// get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
ok := false
cvtCell, ok = theCatalog.ParameterCellConverter(false, dn, layout.Name)
if !ok {
http.Error(w, "Error at parameter read: "+layout.Name, http.StatusBadRequest)
return
}
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read parameter page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadParameterTo(dn, src, &layout, cvtWr)
if !ok {
http.Error(w, "Error at parameter read "+src+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err := json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// runTablePageReadHandler read a "page" of output table values
// from expression table, accumulator table or "all-accumulators" view of model run.
// POST /api/model/:model/run/:run/table/value
// Dimension items returned as enum codes or, if dimension type simple as string values
func runTablePageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTablePageHandler(w, r, true)
}
// runTableIdPageReadHandler read a "page" of output table values
// from expression table, accumulator table or "all-accumulators" view of model run.
// POST /api/model/:model/run/:run/table/value-id
// Dimension(s) returned as enum id, not enum codes.
func runTableIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTablePageHandler(w, r, false)
}
// doReadTablePageHandler read a "page" of output table values
// from expression table, accumulator table or "all-accumulators" view of model run.
// Json is posted to specify table name, "page" size and other read arguments,
// see db.ReadTableLayout for more details.
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Dimension items returned enum id's or as enum codes and for dimension type simple as string values.
func doReadTablePageHandler(w http.ResponseWriter, r *http.Request, isCode bool) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// decode json request body
var layout db.ReadTableLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
// if required get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
ok := false
cvtCell, ok = theCatalog.TableToCodeCellConverter(dn, layout.Name, layout.IsAccum, layout.IsAllAccum)
if !ok {
http.Error(w, "Error at output table read: "+layout.Name, http.StatusBadRequest)
return
}
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read output table page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadOutTableTo(dn, rdsn, &layout, cvtWr)
if !ok {
http.Error(w, "Error at run output table read "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err := json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// runTableCalcPageReadHandler read a "page" of output table expressions and calculate of additional measures.
// POST /api/model/:model/run/:run/table/calc
// Dimension items returned as enum codes or, if dimension type simple as string values
func runTableCalcPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTableCalcPageHandler(w, r, true)
}
// runTableCalcIdPageReadHandler read a "page" of output table expressions and calculate of additional measures.
// POST /api/model/:model/run/:run/table/calc-id
// Dimension(s) returned as enum id, not enum codes.
func runTableCalcIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTableCalcPageHandler(w, r, false)
}
// doTableCalcGetPageHandler calculate a "page" of additional measures for output table using expressions or by aggregating accumulators.
// Json is posted to specify table name, "page" size and additional measures calculations,
// see db.ReadCalculteTableLayout for more details.
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Dimension items returned enum id's or as enum codes and for dimension type simple as string values.
func doReadTableCalcPageHandler(w http.ResponseWriter, r *http.Request, isCode bool) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// decode json request body
var layout db.ReadCalculteTableLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
// if required get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
runIds := []int{}
ok := false
if isCode {
cvtCell, _, runIds, ok = theCatalog.TableToCodeCalcCellConverter(dn, rdsn, layout.Name, layout.Calculation, nil)
if !ok {
http.Error(w, "Error at run output table calculate: "+layout.Name, http.StatusBadRequest)
return
}
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// calculate output table measure and read measure page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadOutTableCalculateTo(
dn, rdsn, &db.ReadTableLayout{ReadLayout: layout.ReadLayout}, layout.Calculation, runIds, cvtWr,
)
if !ok {
http.Error(w, "Error at run output table calculate "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err := json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// runTableComparePageReadHandler compare model runs and return a "page" of comparison expressions and/or calculated additional measures.
// POST /api/model/:model/run/:run/table/compare
// Dimension items returned as enum codes or, if dimension type simple as string values
func runTableComparePageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTableComparePageHandler(w, r, true)
}
// runTableCompareIdPageReadHandler compare model runs and return a "page" of comparison expressions and/or calculated additional measures.
// POST /api/model/:model/run/:run/table/compare-id
// Dimension(s) returned as enum id, not enum codes.
func runTableCompareIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadTableComparePageHandler(w, r, false)
}
// doReadTableComparePageHandler compare model runs with base run and return a "page" of comparison values or calculated additional measures.
// Json is posted to specify table name, "page" size and additional measures calculations,
// see db.ReadCompareTableLayout for more details.
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Dimension items returned enum id's or as enum codes and for dimension type simple as string values.
func doReadTableComparePageHandler(w http.ResponseWriter, r *http.Request, isCode bool) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // base run digest-or-stamp-or-name
// decode json request body
var layout db.ReadCompareTableLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
// check if base run compeleted successfully
rBase, ok := theCatalog.CompletedRunByDigestOrStampOrName(dn, rdsn)
if !ok {
omppLog.Log("Error at table compare: base run not found or not completed successfully: ", rdsn)
http.Error(w, "Error at run output table compare: "+layout.Name+": "+"base run must be completed successfully: "+rdsn, http.StatusBadRequest)
return
}
if rBase.Status != db.DoneRunStatus {
omppLog.Log("Error at table compare: base run not completed successfully: ", rdsn, ": ", rBase.Status)
http.Error(w, "Error at run output table compare: "+layout.Name+": "+"base run must be completed successfully: "+rdsn, http.StatusBadRequest)
return
}
layout.FromId = rBase.RunId // set base run
// if required get converter from id's cell into code cell
// validate all runs: it must be completed successfully
var cvtCell func(interface{}) (interface{}, error)
var runIds []int
ok = false
if isCode {
cvtCell, _, runIds, ok = theCatalog.TableToCodeCalcCellConverter(dn, rdsn, layout.Name, layout.Calculation, layout.Runs)
if !ok {
http.Error(w, "Error at run output table compare: "+layout.Name, http.StatusBadRequest)
return
}
} else {
runIds, ok = isSuccessAllRuns(dn, layout.Runs)
if !ok {
omppLog.Log("Error at table compare: all runs must be completed successfully: ", dn, ": ", layout.Name)
http.Error(w, "Error at run output table compare: "+layout.Name+": "+"all runs must be completed successfully: "+rdsn, http.StatusBadRequest)
return
}
}
if len(runIds) <= 0 {
omppLog.Log("Warning at table compare: only base run found, no runs to comparte with: ", dn, ": ", layout.Name)
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// calculate output table measure and read measure page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadOutTableCalculateTo(
dn, rdsn, &db.ReadTableLayout{ReadLayout: layout.ReadLayout}, layout.Calculation, runIds, cvtWr,
)
if !ok {
http.Error(w, "Error at run output table compare "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err := json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// check if all runs completed successfully and return run id's for all existing runs, skip runs which do exist.
func isSuccessAllRuns(digest string, runLst []string) ([]int, bool) {
// check if all additional model runs completed successfully
runIds := []int{}
if len(runLst) > 0 {
rLst, _ := theCatalog.RunRowListByModel(digest)
for _, rn := range runLst {
rId := 0
for k := 0; rId <= 0 && k < len(rLst); k++ {
if rn == rLst[k].RunDigest || rn == rLst[k].RunStamp || rn == rLst[k].Name {
rId = rLst[k].RunId
}
if rId > 0 {
if rLst[k].Status != db.DoneRunStatus {
omppLog.Log("Warning: model run not completed successfully: ", rLst[k].RunDigest, ": ", rLst[k].Status)
return []int{}, false
}
}
}
if rId <= 0 {
omppLog.Log("Warning: model run not found: ", rn)
continue
}
// else: model run completed successfully, include run id into the list of additional runs
isFound := false
for k := 0; !isFound && k < len(runIds); k++ {
isFound = rId == runIds[k]
}
if !isFound {
runIds = append(runIds, rId)
}
}
}
return runIds, true
}
// worksetParameterPageGetHandler read a "page" of parameter values from workset.
//
// GET /api/model/:model/workset/:set/parameter/:name/value
// GET /api/model/:model/workset/:set/parameter/:name/value/start/:start
// GET /api/model/:model/workset/:set/parameter/:name/value/start/:start/count/:count
//
// Dimension(s) and enum-based parameters returned as enum codes.
func worksetParameterPageGetHandler(w http.ResponseWriter, r *http.Request) {
doParameterGetPageHandler(w, r, "set", true, true)
}
// runParameterPageGetHandler read a "page" of parameter values from model run results.
//
// GET /api/model/:model/run/:run/parameter/:name/value
// GET /api/model/:model/run/:run/parameter/:name/value/start/:start
// GET /api/model/:model/run/:run/parameter/:name/value/start/:start/count/:count
//
// Dimension(s) and enum-based parameters returned as enum codes.
func runParameterPageGetHandler(w http.ResponseWriter, r *http.Request) {
doParameterGetPageHandler(w, r, "run", false, true)
}
// doParameterGetPageHandler read a "page" of parameter values from workset or model run.
// Page is part of parameter values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Dimension(s) and enum-based parameters returned as enum codes or enum id's.
func doParameterGetPageHandler(w http.ResponseWriter, r *http.Request, srcArg string, isSet, isCode bool) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
src := getRequestParam(r, srcArg) // workset name or run digest-or-stamp-or-name
name := getRequestParam(r, "name") // parameter name
// url or query parameters: page offset and page size
start, ok := getInt64RequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start row number to read "+name, http.StatusBadRequest)
return
}
count, ok := getInt64RequestParam(r, "count", 0)
if !ok {
http.Error(w, "Invalid value of max row count to read "+name, http.StatusBadRequest)
return
}
// setup read layout
layout := db.ReadParamLayout{
ReadLayout: db.ReadLayout{
Name: name,
ReadPageLayout: db.ReadPageLayout{Offset: start, Size: count},
},
IsFromSet: isSet,
}
// if required get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
cvtCell, ok = theCatalog.ParameterCellConverter(false, dn, name)
if !ok {
http.Error(w, "Error at parameter read: "+name, http.StatusBadRequest)
return
}
}
// write to response
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte{'['}) // start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read parameter page into json array response, convert enum id's to code if requested
_, ok = theCatalog.ReadParameterTo(dn, src, &layout, cvtWr)
if !ok {
http.Error(w, "Error at parameter read "+src+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of json output array
}
// runTableExprPageGetHandler read a "page" of output table expression(s) values from model run results.
// GET /api/model/:model/run/:run/table/:name/expr
// GET /api/model/:model/run/:run/table/:name/expr/start/:start
// GET /api/model/:model/run/:run/table/:name/expr/start/:start/count/:count
// Enum-based dimension items returned as enum codes.
func runTableExprPageGetHandler(w http.ResponseWriter, r *http.Request) {
doTableGetPageHandler(w, r, false, false, true)
}
// runTableAccPageGetHandler read a "page" of output table accumulator(s) values from model run results.
// GET /api/model/:model/run/:run/table/:name/acc/start/:start
// GET /api/model/:model/run/:run/table/:name/acc/start/:start/count/:count
// Enum-based dimension items returned as enum codes.
func runTableAccPageGetHandler(w http.ResponseWriter, r *http.Request) {
doTableGetPageHandler(w, r, true, false, true)
}
// runTableAllAccPageGetHandler read a "page" of output table accumulator(s) values
// from "all-accumulators" view of model run results.
// GET /api/model/:model/run/:run/table/:name/all-acc
// GET /api/model/:model/run/:run/table/:name/all-acc/start/:start
// GET /api/model/:model/run/:run/table/:name/all-acc/start/:start/count/:count
// Enum-based dimension items returned as enum codes.
func runTableAllAccPageGetHandler(w http.ResponseWriter, r *http.Request) {
doTableGetPageHandler(w, r, true, true, true)
}
// doTableGetPageHandler read a "page" of values from
// output table expressions, accumulators or "all-accumulators" views.
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Enum-based dimension items returned as enum id or as enum codes.
func doTableGetPageHandler(w http.ResponseWriter, r *http.Request, isAcc, isAllAcc, isCode bool) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
name := getRequestParam(r, "name") // output table name
// url or query parameters: page offset and page size
start, ok := getInt64RequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start row number to read "+name, http.StatusBadRequest)
return
}
count, ok := getInt64RequestParam(r, "count", 0)
if !ok {
http.Error(w, "Invalid value of max row count to read "+name, http.StatusBadRequest)
return
}
// setup read layout
layout := db.ReadTableLayout{
ReadLayout: db.ReadLayout{
Name: name,
ReadPageLayout: db.ReadPageLayout{Offset: start, Size: count},
},
IsAccum: isAcc,
IsAllAccum: isAllAcc,
}
// if required get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
cvtCell, ok = theCatalog.TableToCodeCellConverter(dn, layout.Name, layout.IsAccum, layout.IsAllAccum)
if !ok {
http.Error(w, "Error at run output table read: "+name, http.StatusBadRequest)
return
}
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte{'['}) // start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read output table page into json array response, convert enum id's to code if requested
_, ok = theCatalog.ReadOutTableTo(dn, rdsn, &layout, cvtWr)
if !ok {
http.Error(w, "Error at run output table read "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of json output array
}
// runTableCalcPageGetHandler for all output table expressions calculate a "page" of additional measures.
//
// Measures calculated either as aggregation for each expresion: SUM AVG COUNT MIN MAX VAR SD SE CV
// or as comma separated list of arbitrary calcialtion expressions, for example: OM_AVG(acc0),OM_SD(acc1)
//
// GET /api/model/:model/run/:run/table/:name/calc/:calc
// GET /api/model/:model/run/:run/table/:name/calc/:calc/start/:start
// GET /api/model/:model/run/:run/table/:name/calc/:calc/start/:start/count/:count
//
// Calculation applied to derived accumulator with the same name as expression name.
//
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Enum-based dimension items returned as enum codes.
func runTableCalcPageGetHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
name := getRequestParam(r, "name") // output table name
calc := getRequestParam(r, "calc") // calculation function name: sum avg count min max var sd se cv
// validate parameters: page offset, page size and calculation expression
if calc == "" {
http.Error(w, "Invalid (empty) calculation expression "+calc, http.StatusBadRequest)
return
}
start, ok := getInt64RequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start row number to read "+name, http.StatusBadRequest)
return
}
count, ok := getInt64RequestParam(r, "count", 0)
if !ok {
http.Error(w, "Invalid value of max row count to read "+name, http.StatusBadRequest)
return
}
// setup read layout and calculate layout
tableLt := db.ReadTableLayout{
ReadLayout: db.ReadLayout{
Name: name,
ReadPageLayout: db.ReadPageLayout{Offset: start, Size: count},
},
}
calcLt, ok := theCatalog.TableAggrExprCalculateLayout(dn, name, calc)
if !ok {
http.Error(w, "Invalid calculation expression "+calc, http.StatusBadRequest)
return
}
// get converter from id's cell into code cell
cvtCell, _, runIds, ok := theCatalog.TableToCodeCalcCellConverter(dn, rdsn, tableLt.Name, calcLt, nil)
if !ok {
http.Error(w, "Failed to create output table csv converter: "+name, http.StatusBadRequest)
return
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte{'['}) // start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// calculate output table measure and read measure page into json array response, convert enum id's to code if requested
_, ok = theCatalog.ReadOutTableCalculateTo(dn, rdsn, &tableLt, calcLt, runIds, cvtWr)
if !ok {
http.Error(w, "Error at run output table read "+rdsn+": "+name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of json output array
}
// runTableComparePageGetHandler compare model runs and return a "page" of comparison measures.
//
// It is either calculation for each expression: DIFF RATIO PERCENT or multiple arbitrary calculations.
// For example, RATIO is: expr0[variant] / expr0[base], expr1[variant] / expr1[base],....
// Or arbitrary comma separated expression(s): expr0 , expr1[variant] + expr2[base] , ....
// Variant runs can be a comma separated list of run digests or run stamps or run names.
// If run name contains comma then name must be "double quoted" or 'single quoted'.
// For example: "Year 1995, 1996", 'Age [30, 40]'
//
// GET /api/model/:model/run/:run/table/:name/compare/:compare/variant/:variant
// GET /api/model/:model/run/:run/table/:name/compare/:compare/variant/:variant/start/:start
// GET /api/model/:model/run/:run/table/:name/compare/:compare/variant/:variant/start/:start/count/:count
//
// Page is part of output table values defined by zero-based "start" row number and row count.
// If row count <= 0 then all rows returned.
// Enum-based dimension items returned as enum codes.
func runTableComparePageGetHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // base run digest-or-stamp-or-name
name := getRequestParam(r, "name") // output table name
compare := getRequestParam(r, "compare") // comparison function name: diff ratio percent
vr := getRequestParam(r, "variant") // variant run digest-or-stamp-or-name
// validate parameters: page offset, page size and calculation expression
if compare == "" {
http.Error(w, "Invalid (empty) comparison expression", http.StatusBadRequest)
return
}
vRdsn := helper.ParseCsvLine(vr, 0)
if len(vRdsn) <= 0 {
http.Error(w, "Invalid or empty list runs to compare", http.StatusBadRequest)
return
}
start, ok := getInt64RequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start row number to read "+name, http.StatusBadRequest)
return
}
count, ok := getInt64RequestParam(r, "count", 0)
if !ok {
http.Error(w, "Invalid value of max row count to read "+name, http.StatusBadRequest)
return
}
// setup read layout and calculate layout
tableLt := db.ReadTableLayout{
ReadLayout: db.ReadLayout{
Name: name,
ReadPageLayout: db.ReadPageLayout{Offset: start, Size: count},
},
}
calcLt, ok := theCatalog.TableExprCompareLayout(dn, name, compare)
if !ok {
http.Error(w, "Invalid comparison expression "+compare, http.StatusBadRequest)
return
}
// get converter from id's cell into code cell
cvtCell, _, runIds, ok := theCatalog.TableToCodeCalcCellConverter(dn, rdsn, tableLt.Name, calcLt, vRdsn)
if !ok {
http.Error(w, "Failed to create output table converter: "+name, http.StatusBadRequest)
return
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte{'['}) // start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// calculate output table measure and read measure page into json array response, convert enum id's to code if requested
_, ok = theCatalog.ReadOutTableCalculateTo(dn, rdsn, &tableLt, calcLt, runIds, cvtWr)
if !ok {
http.Error(w, "Error at run output table read "+rdsn+": "+name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of json output array
}
// runMicrodataPageReadHandler read a "page" of microdata values from model run.
// POST /api/model/:model/run/:run/microdata/value
// Enum-based microdata attributes returned as enum codes.
func runMicrodataPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadMicrodataPageHandler(w, r, true)
}
// runMicrodataIdPageReadHandler read a "page" of microdata values from model run.
// POST /api/model/:model/run/:run/microdata/value-id
// Enum-based microdata attributes returned as enum id, not enum codes.
func runMicrodataIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
doReadMicrodataPageHandler(w, r, false)
}
// doReadMicrodataPageHandler read a "page" of microdata values from model run.
// Json is posted to specify entity name, "page" size and other read arguments,
// see db.ReadMicroLayout for more details.
// If generation digest not specified in ReadMicroLayout then it found by entity name and run digest.
// Page of values is a rows from microdata value table started at zero based offset row
// and up to max page size rows, if page size <= 0 then all values returned.
// Enum-based microdata attributes returned as enum codes or enum id's.
func doReadMicrodataPageHandler(w http.ResponseWriter, r *http.Request, isCode bool) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// return error if microdata disabled
if !theCfg.isMicrodata {
http.Error(w, "Error: microdata not allowed: "+dn+" "+rdsn, http.StatusBadRequest)
return
}
// decode json request body
var layout db.ReadMicroLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
// get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
ok := false
genDigest := ""
_, genDigest, cvtCell, ok = theCatalog.MicrodataCellConverter(false, dn, rdsn, layout.Name)
if !ok {
http.Error(w, "Error at run microdata read: "+layout.Name, http.StatusBadRequest)
return
}
if layout.GenDigest != "" && layout.GenDigest != genDigest {
http.Error(w, "Error at run microdata read, generation digest not found: "+layout.GenDigest+" expected: "+genDigest+": "+layout.Name, http.StatusBadRequest)
return
}
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read microdata page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadMicrodataTo(dn, rdsn, &layout, cvtWr)
if !ok {
http.Error(w, "Error at run microdata read "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err := json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// runMicrodataPageGetHandler read a "page" of microdata values from model run results.
// GET /api/model/:model/run/:run/microdata/:name/value
// GET /api/model/:model/run/:run/microdata/:name/value/start/:start
// GET /api/model/:model/run/:run/microdata/:name/value/start/:start/count/:count
// Enum-based microdata attributes returned as enum codes.
func runMicrodataPageGetHandler(w http.ResponseWriter, r *http.Request) {
doMicrodataGetPageHandler(w, r, true)
}
// doMicrodataGetPageHandler read a "page" of microdata values from model run.
// Page of values is a rows from microdata value table started at zero based offset row
// and up to max page size rows, if page size <= 0 then all values returned.
// Enum-based microdata attributes returned as enum codes or enum id's.
func doMicrodataGetPageHandler(w http.ResponseWriter, r *http.Request, isCode bool) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
name := getRequestParam(r, "name") // entity name
// return error if microdata disabled
if !theCfg.isMicrodata {
http.Error(w, "Error: microdata not allowed: "+dn+" "+rdsn, http.StatusBadRequest)
return
}
// url or query parameters: page offset and page size
start, ok := getInt64RequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start row number to read "+name, http.StatusBadRequest)
return
}
count, ok := getInt64RequestParam(r, "count", 0)
if !ok {
http.Error(w, "Invalid value of max row count to read "+name, http.StatusBadRequest)
return
}
// setup read layout
layout := db.ReadMicroLayout{
ReadLayout: db.ReadLayout{
Name: name,
ReadPageLayout: db.ReadPageLayout{Offset: start, Size: count},
},
}
// get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
ok := false
genDigest := ""
_, genDigest, cvtCell, ok = theCatalog.MicrodataCellConverter(false, dn, rdsn, layout.Name)
if !ok {
http.Error(w, "Error at run microdata read: "+name, http.StatusBadRequest)
return
}
layout.GenDigest = genDigest
}
// write to response
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte{'['}) // start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read microdata page into json array response, convert enum id's to code if requested
_, ok = theCatalog.ReadMicrodataTo(dn, rdsn, &layout, cvtWr)
if !ok {
http.Error(w, "Error at run microdata read "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of json output array
}
// runMicrodataCalcPageReadHandler read a "page" of microdata values from model run.
// POST /api/model/:model/run/:run/microdata/calc
// It can be multiple aggregations of value attributes (float of integer type), group by dimension attributes (enum-based or bool type).
// For example: GroupBy: [AgeGroup, Sex] and Calculation: [OM_AVG(Income), OM_MAX(Salary+Pension)]
// Enum-based microdata attributes returned as enum codes.
func runMicrodataCalcPageReadHandler(w http.ResponseWriter, r *http.Request) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// return error if microdata disabled
if !theCfg.isMicrodata {
http.Error(w, "Error: microdata not allowed: "+dn+" "+rdsn, http.StatusBadRequest)
return
}
// decode json request body
var layout db.ReadCalculteMicroLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
doReadMicrodataCalcPageHandler(w, r, dn, rdsn, &layout, []string{}, true)
}
// runMicrodataIdPageReadHandler read a "page" of microdata values from model run.
// POST /api/model/:model/run/:run/microdata/calc-id
// It can be multiple aggregations of value attributes (float of integer type), group by dimension attributes (enum-based or bool type).
// For example: GroupBy: [AgeGroup, Sex] and Calculation: [OM_AVG(Income), OM_MAX(Salary+Pension)]
// Enum-based microdata attributes returned as enum id, not enum codes.
func runMicrodataCalcIdPageReadHandler(w http.ResponseWriter, r *http.Request) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// return error if microdata disabled
if !theCfg.isMicrodata {
http.Error(w, "Error: microdata not allowed: "+dn+" "+rdsn, http.StatusBadRequest)
return
}
// decode json request body
var layout db.ReadCalculteMicroLayout
if !jsonRequestDecode(w, r, true, &layout) {
return // error at json decode, response done with http error
}
doReadMicrodataCalcPageHandler(w, r, dn, rdsn, &layout, []string{}, false)
}
// doReadMicrodataCalcPageHandler read a "page" of microdata values from model run.
// Json is posted to specify entity name, "page" size and other read arguments,
// see db.ReadCalculteMicroLayout for more details.
// If generation digest not specified in ReadCalculteMicroLayout then it found by entity name and run digest.
// Page of values is a rows from microdata value table started at zero based offset row
// and up to max page size rows, if page size <= 0 then all values returned.
// Enum-based microdata attributes returned as enum codes or enum id's.
func doReadMicrodataCalcPageHandler(w http.ResponseWriter, r *http.Request, dn string, rdsn string, layout *db.ReadCalculteMicroLayout, varLst []string, isCode bool) {
// get base run id, run variants, entity generation digest and microdata cell converter
baseRunId, runIds, genDigest, cvtMicro, err := theCatalog.MicrodataCalcToCsvConverter(dn, isCode, rdsn, varLst, layout.Name, &layout.CalculateMicroLayout)
if err != nil {
omppLog.Log("Failed to create microdata csv converter: ", rdsn, ": ", layout.Name, ": ", err.Error())
http.Error(w, "Failed to create microdata csv converter: "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
layout.FromId = baseRunId
// get converter from id's cell into code cell
var cvtCell func(interface{}) (interface{}, error)
if isCode {
cvtCell, err = cvtMicro.IdToCodeCell(cvtMicro.ModelDef, layout.Name)
if err != nil {
omppLog.Log("Failed to create microdata cell value converter: ", dn, ": ", layout.Name, ": ", err.Error())
http.Error(w, "Failed to create microdata cell value converter: "+rdsn+": "+layout.Name, http.StatusBadRequest)
}
}
// read microdata values, page size =0: read all values
microLt := db.ReadMicroLayout{
ReadLayout: layout.ReadLayout,
GenDigest: genDigest,
}
// write to response: page layout and page data
jsonSetHeaders(w, r) // start response with set json headers, i.e. content type
w.Write([]byte("{\"Page\":[")) // start of data page and start of json output array
enc := json.NewEncoder(w)
cvtWr := jsonCellWriter(w, enc, cvtCell)
// read microdata page into json array response, convert enum id's to code if requested
lt, ok := theCatalog.ReadMicrodataCalculateTo(dn, rdsn, µLt, &layout.CalculateMicroLayout, runIds, cvtWr)
if !ok {
http.Error(w, "Error at run microdata read "+rdsn+": "+layout.Name, http.StatusBadRequest)
return
}
w.Write([]byte{']'}) // end of data page array
// continue response with output page layout: offset, size, last page flag
w.Write([]byte(",\"Layout\":"))
err = json.NewEncoder(w).Encode(lt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("}")) // end of data page and end of json
}
// runMicrodataComparePageReadHandler read a "page" of microdata comparison between base and variant(s) model runs.
// POST /api/model/:model/run/:run/microdata/compare
// It can be multiple comparison of value attributes (float of integer type) and / or aggregation of value attributes.
// All comparisons and aggregations grouped by dimension attributes (enum-based or bool type).
// It can be multiple aggregations of value attributes (float of integer type), group by dimension attributes (enum-based or bool type).
// For example: GroupBy: [AgeGroup, Sex] and Calculation: [OM_AVG(Income[variant]-Income[base]) , OM_MAX(Salary+Pension)]
// Enum-based microdata attributes returned as enum codes.
func runMicrodataComparePageReadHandler(w http.ResponseWriter, r *http.Request) {
// url parameters
dn := getRequestParam(r, "model") // model digest-or-name
rdsn := getRequestParam(r, "run") // run digest-or-stamp-or-name
// return error if microdata disabled