-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.bal
1354 lines (1200 loc) · 61.4 KB
/
main.bal
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 2021 University of Stuttgart
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import ballerina/http;
import ballerina/lang.regexp;
import ballerina/os;
import ballerina/log;
import qhana_backend.database;
// start configuration values
# User configurable os of the host.
# Can also be configured by setting the `OS_TYPE` environment variable.
configurable string os_type = "linux";
# Get the os from the `OS_TYPE` environment variable.
# If not present use the configurable variable `os_type` as fallback.
#
# + return - the configured os
function getOS() returns string {
string os = os:getEnv("OS_TYPE").toLowerAscii();
if os == "" {
return os_type.toLowerAscii();
}
return os;
}
# The final configured os.
final string & readonly configuredOS = getOS().cloneReadOnly();
# List of domains that are allowed CORS requests to the backend.
# Can also be configured by setting the `QHANA_CORS_DOMAINS` environment variable.
configurable string[] corsDomains = ["*"];
# Get the port from the `QHANA_CORS_DOMAINS` environment variable.
# If not present use the configurable variable `corsDomains` as fallback.
#
# + return - the configured cors domains
function getCorsDomains() returns string[] {
string d = os:getEnv("QHANA_CORS_DOMAINS");
if (d.length() > 0) {
string:RegExp r = re `[\s]+`;
return r.split(d);
}
return corsDomains;
}
# The final configured cors domains.
final string[] & readonly configuredCorsDomains = getCorsDomains().cloneReadOnly();
# User configurable port of the backend server.
# Can also be configured by setting the `QHANA_PORT` environment variable.
configurable int port = 9090;
# Get the port from the `QHANA_PORT` environment variable.
# If not present use the configurable variable `port` as fallback.
#
# + return - the configured port number
function getPort() returns int {
string p = os:getEnv("QHANA_PORT");
if (p.matches(re `^[0-9]+$`)) {
do {
return check int:fromString(p);
} on fail {
// error should never happen if regex is correct...
}
}
log:printInfo("Binding to port " + port.toBalString());
return port;
}
# The final configured server port.
final int & readonly serverPort = getPort().cloneReadOnly();
# User specified host ip address of backend server (with protocol and port)
# Can also be specified by setting the `QHANA_HOST` environment variable.
configurable string host = "http://localhost:" + serverPort.toString();
# Determine the base host url from the `QHANA_HOST` environment variable.
# If not present use the configurable variable `host` as fallback.
#
# + return - the configured host base path (including protocol and port)
function getHost() returns string {
string h = os:getEnv("QHANA_HOST");
if (h.matches(re `^https?://.*$`)) {
return h;
}
return host;
}
# The final configured server host.
final string & readonly serverHost = getHost().cloneReadOnly();
# User configurable watcher intervall configuration.
# Can also be configured by setting the `QHANA_WATCHER_INTERVALLS` environment variable.
# The numbers are interpreted as folowing: `[<intervall in seconds>, [<iterations until next intervall>]]*`
# If the list ends with an intervall, i.e., the iterations count is missing, then the intervall
# will be repeated indefinitely.
configurable (decimal|int)[] watcherIntervallConfig = [2, 10, 5, 10, 10, 60, 30, 20, 60, 10, 600];
# Coerce the string input to a positive int or decimal.
#
# + input - the string input to coerce
# + return - the coerced number or the error if coercion failed (or the number was negative)
function coerceToPositiveNumber(string input) returns decimal|int|error {
boolean isDecimal = input.matches(re `^\+?[0-9]+\.[0-9]+$`);
if (isDecimal) {
return decimal:fromString(input);
}
boolean isInt = input.matches(re `^\+?[0-9]+$`);
if (isInt) {
return int:fromString(input);
}
return error(string `Input "${input}" is not a positive number!`);
}
# Get the watcher intervalls from the `QHANA_WATCHER_INTERVALLS` environment variable.
# If not present use the configurable variable `watcherIntervallConfig` as fallback.
#
# + return - the configured watcher intervalls
function getWatcherIntervallConfig() returns (decimal|int)[] {
string intervalls = os:getEnv("QHANA_WATCHER_INTERVALLS");
if (intervalls.length() > 0) {
if (intervalls.startsWith("(") && intervalls.endsWith(")")) {
// Remove enclosing brackets from start/end of string if present
intervalls = intervalls.substring(1, intervalls.length() - 1);
}
do {
string:RegExp r = re `[\s,;]+`;
return from string i in r.split(intervalls)
select check coerceToPositiveNumber(i);
} on fail error err {
log:printError("Failed to parse environment variable QHANA_WATCHER_INTERVALLS!\n", 'error = err, stackTrace = err.stackTrace());
}
}
return watcherIntervallConfig;
}
# The final configured watcher intervalls.
final (decimal|int)[] & readonly configuredWatcherIntervalls = getWatcherIntervallConfig().cloneReadOnly();
# User configurable watcher intervall configuration used when already subscribed to receive webhook updates.
# Can also be configured by setting the `QHANA_SUBSCRIBED_WATCHER_INTERVALLS` environment variable.
# The numbers are interpreted as folowing: `[<intervall in seconds>, [<iterations until next intervall>]]*`
# If the list ends with an intervall, i.e., the iterations count is missing, then the intervall
# will be repeated indefinitely.
configurable (decimal|int)[] subscribedWatcherIntervallConfig = [60, 10, 600];
# Get the watcher intervalls from the `QHANA_SUBSCRIBED_WATCHER_INTERVALLS` environment variable.
# If not present use the configurable variable `watcherIntervallConfig` as fallback.
#
# + return - the configured watcher intervalls
function getSubscribedWatcherIntervallConfig() returns (decimal|int)[] {
string intervalls = os:getEnv("QHANA_SUBSCRIBED_WATCHER_INTERVALLS");
if (intervalls.length() > 0) {
if (intervalls.startsWith("(") && intervalls.endsWith(")")) {
// Remove enclosing brackets from start/end of string if present
intervalls = intervalls.substring(1, intervalls.length() - 1);
}
do {
string:RegExp r = re `[\s,;]+`;
return from string i in r.split(intervalls)
select check coerceToPositiveNumber(i);
} on fail error err {
log:printError("Failed to parse environment variable QHANA_SUBSCRIBED_WATCHER_INTERVALLS!\n", 'error = err, stackTrace = err.stackTrace());
}
}
return subscribedWatcherIntervallConfig;
}
# The final configured watcher intervalls.
final (decimal|int)[] & readonly configuredSubscribedWatcherIntervalls = getSubscribedWatcherIntervallConfig().cloneReadOnly();
# User configurable URL map which is used by the backend to rewrite URLs used by the result watchers.
# Can also be configured by setting the `QHANA_URL_MAPPING` environment variable.
# The keys are regex patterns and the values replacement string.
# All replacements will be applied to an URL.
#
# Intended for use in a dockerized dev setup where localhost is used as outside URL
configurable map<string> & readonly internalUrlMap = {};
# Get the URL map from the `QHANA_URL_MAPPING` environment variable.
# If not present use the configurable variable `internalUrlMap` as fallback.
#
# + return - the configured watcher intervalls
function getInternalUrlMap() returns map<string> {
string mapping = os:getEnv("QHANA_URL_MAPPING");
if (mapping.length() > 0) {
do {
return check mapping.fromJsonStringWithType();
} on fail error err {
log:printError("Failed to parse environment variable QHANA_URL_MAPPING!\n", 'error = err, stackTrace = err.stackTrace());
}
}
map<string> newMapping = {};
foreach var [key, value] in internalUrlMap.entries() {
if key[0] == "\"" || key[0] == "'" {
// remove enclosing quotes if necessary
// FIXME: This is a workaround for a possible bug in Ballerina. Can be removed if the bug is fixed.
var strippedKey = key.substring(1, key.length() - 1);
newMapping[strippedKey] = value;
} else {
newMapping[key] = value;
}
}
return newMapping;
}
# The final configured URL map.
final map<string> & readonly configuredUrlMap = getInternalUrlMap().cloneReadOnly();
# Preset plugins.
# Can also be configured by setting the `QHANA_PLUGINS` environment variable.
configurable string[] plugins = [];
# Preset plugin runners.
# Can also be configured by setting the `QHANA_PLUGIN_RUNNERS` environment variable.
configurable string[] pluginRunners = [];
# Get preset plugin runner from the `QHANA_PLUGIN_RUNNERS` environment variable.
# If not present use the configurable variable `pluginRunners` as fallback.
#
# + return - the configured watcher intervalls
function getPluginRunnersConfig() returns string[] {
string pRunners = os:getEnv("QHANA_PLUGIN_RUNNERS");
if (pRunners.length() > 0) {
do {
return check pRunners.fromJsonStringWithType();
} on fail error err {
log:printError("Failed to parse environment variable QHANA_PLUGIN_RUNNERS!\n", 'error = err, stackTrace = err.stackTrace());
}
}
return pluginRunners;
}
# The final configured plugin runners.
final string[] & readonly preconfiguredPluginRunners = getPluginRunnersConfig().cloneReadOnly();
# Get preset plugins from the `QHANA_PLUGINS` environment variable.
# If not present use the configurable variable `plugins` as fallback.
#
# + return - the configured watcher intervalls
function getPluginsConfig() returns string[] {
string pluginList = os:getEnv("QHANA_PLUGINS");
if (pluginList.length() > 0) {
do {
return check pluginList.fromJsonStringWithType();
} on fail error err {
log:printError("Failed to parse environment variable QHANA_PLUGINS!\n", 'error = err, stackTrace = err.stackTrace());
}
}
return plugins;
}
# The final configured plugins.
final string[] & readonly preconfiguredPlugins = getPluginsConfig().cloneReadOnly();
// end configuration values
# Rewrite the given URL with the rules configured in the variable `configuredUrlMap`.
#
# + url - the input URL
# + return - the rewritten URL
isolated function mapToInternalUrl(string url) returns string {
if configuredUrlMap.length() == 0 {
return url; // fast exit
}
// apply all replacements specified in the url map, keys are interpreted as regex
var replacedUrl = url;
foreach var [pattern, replacement] in configuredUrlMap.entries() {
do {
string:RegExp regex = check regexp:fromString(pattern);
replacedUrl = regex.replace(replacedUrl, replacement);
} on fail error err {
log:printError("Failed to parse regex pattern '" + pattern + "'!\n", 'error = err, stackTrace = err.stackTrace());
}
}
return replacedUrl;
}
# The QHAna backend api service.
@http:ServiceConfig {
cors: {
allowOrigins: configuredCorsDomains,
allowMethods: ["OPTIONS", "GET", "PUT", "POST", "DELETE"],
allowHeaders: ["Content-Type", "Depth", "User-Agent", "range", "X-File-Size", "X-Requested-With", "If-Modified-Since", "X-File-Name", "Cache-Control", "Access-Control-Allow-Origin", "Accept"],
allowCredentials: true,
maxAge: 84900
}
}
service / on new http:Listener(serverPort) {
# The root resource of the QHAna backend API.
#
# All resources contain a `@self` link that is the canonical URL of the resource.
# Resources can contain links to other resources.
#
# + return - the root resource
resource function get .() returns RootResponse {
return {
'\@self: serverHost + "/",
experiments: serverHost + "/experiments/",
pluginRunners: serverHost + "/pluginRunners/",
tasks: serverHost + "/tasks/"
};
}
# Get a list of configured plugin or plugin-runner endpoints.
#
# + return - the list of endpoints as a "ListResponse".
resource function get plugin\-endpoints() returns PluginEndpointsListResponse|http:InternalServerError {
int endpointCount;
database:PluginEndpointFull[] endpoints;
transaction {
endpointCount = check database:getPluginEndpointsCount();
endpoints = check database:getPluginEndpoints();
check commit;
} on fail error err {
log:printError("Could not get plugin endpoints.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
var result = from var endpoint in endpoints
select mapToPluginEndpointResponse(endpoint);
// FIXME load from database...
return {
'\@self: serverHost + "/plugin-endpoints",
items: result,
itemCount: endpointCount
};
}
# Add a new endpoint to the list of plugin(-runner) endpoints.
#
# + return - the created resource
resource function post plugin\-endpoints(@http:Payload PluginEndpointPost endpoint) returns PluginEndpointResponse|http:InternalServerError {
database:PluginEndpointFull result;
transaction {
result = check database:addPluginEndpoint(endpoint);
check commit;
} on fail error err {
log:printError("Could not add new plugin endpoint", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToPluginEndpointResponse(result);
}
# Get a specific plugin(-runner) endpoint resource.
#
# + return - the endpoint resource
resource function get plugin\-endpoints/[int endpointId]() returns PluginEndpointResponse|http:InternalServerError {
database:PluginEndpointFull result;
transaction {
result = check database:getPluginEndpoint(endpointId);
check commit;
} on fail error err {
log:printError("Could not get plugin endpoint.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToPluginEndpointResponse(result);
}
# Update an existing plugin(-runner) endpoint resource.
#
# + return - the updated endpoint resource
resource function put plugin\-endpoints/[int endpointId](@http:Payload PluginEndpointPost endpoint) returns PluginEndpointResponse|http:InternalServerError {
database:PluginEndpointFull result;
transaction {
result = check database:editPluginEndpoint(endpointId, endpoint.'type);
check commit;
} on fail error err {
log:printError("Could not update plugin endpoint", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToPluginEndpointResponse(result);
}
# Remove an existing plugin(-runner) endpoint resource.
#
# + return - an empty response with a 2xx http status code on success
resource function delete plugin\-endpoints/[int endpointId]() returns http:Ok|http:InternalServerError {
transaction {
check database:deletePluginEndpoint(endpointId);
check commit;
} on fail error err {
log:printError("Could not delete plugin endpoint", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return <http:Ok>{};
}
////////////////////////////////////////////////////////////////////////////
// Experiments /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
# Get a list of experiments.
#
# The experiments list resource is paginated
#
# + search - filter by experiment name
# + page - the requested page (starting with page 0)
# + item\-count - the number of items per page (5 <= item-count <= 500)
# + sort - 1 for asc sort, -1 for desc sort by experiment name
# + return - the list resource containing the experiments
resource function get experiments(string? search, int? page = 0, int? item\-count = 10, int? sort = 1) returns ExperimentListResponse|http:InternalServerError|http:BadRequest|http:NotFound {
int intPage = (page is ()) ? 0 : page;
int itemCount = (item\-count is ()) ? 10 : item\-count;
int intSort = (sort is ()) ? 1 : sort;
if (intPage < 0) {
return <http:BadRequest>{body: "Cannot retrieve a negative page number!"};
}
if (itemCount < 5 || itemCount > 500) {
return <http:BadRequest>{body: "Item count must be between 5 and 500 (both inclusive)!"};
}
var offset = intPage * itemCount;
int experimentCount;
database:ExperimentFull[] experiments;
transaction {
experimentCount = check database:getExperimentCount(search = search);
if (offset >= experimentCount) {
// page is out of range!
check commit;
return <http:NotFound>{};
} else {
experiments = check database:getExperiments(search = search, 'limit = itemCount, offset = offset, sort = intSort);
check commit;
}
} on fail error err {
log:printError("Could not get experiments.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
// map to api response(s)
var result = from var exp in experiments
select mapToExperimentResponse(exp);
// TODO include query params in self link
return {'\@self: serverHost + "/experiments/", items: result, itemCount: experimentCount};
}
# Create a new experiment.
#
# + return - the created experiment resource
@http:ResourceConfig {
consumes: ["application/json"]
}
resource function post experiments(@http:Payload database:Experiment experiment) returns ExperimentResponse|http:InternalServerError {
database:ExperimentFull result;
transaction {
result = check database:createExperiment(experiment);
check commit;
} on fail error err {
log:printError("Could not create new experiment", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToExperimentResponse(result);
}
# Get a specific experiment by its id.
#
# + experimentId - the id of the requested experiment
# + return - the experiment resource
resource function get experiments/[int experimentId]() returns ExperimentResponse|http:InternalServerError|error {
database:ExperimentFull result;
transaction {
result = check database:getExperiment(experimentId);
check commit;
} on fail error err {
log:printError("Could not get experiment.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToExperimentResponse(result);
}
# Update an existing experiment.
#
# + experimentId - the id of the experiment to update
# + return - the updated experiment
@http:ResourceConfig {
consumes: ["application/json"]
}
resource function put experiments/[int experimentId](@http:Payload database:Experiment experiment) returns ExperimentResponse|http:InternalServerError {
database:ExperimentFull result;
transaction {
result = check database:updateExperiment(experimentId, experiment);
check commit;
} on fail error err {
log:printError("Could not update experiment.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToExperimentResponse(result);
}
////////////////////////////////////////////////////////////////////////////
// Data ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
# Get summary information about data available in the experiment.
#
# The summary is a map which keys are the available data types.
# The values of the map are lists of content types describing the serialization
# formats available for the specific data type. This summary can be used to
# decide if all input requirements for a plugin can be fulfilled by the
# data available in the experiment.
#
# + experimentId - the id of the experiment
# + return - the summary information of the currently available data
resource function get experiments/[int experimentId]/data\-summary() returns map<string[]>|http:InternalServerError {
map<string[]> data;
transaction {
data = check database:getDataTypesSummary(experimentId);
check commit;
} on fail error err {
log:printError("Could not get data types summary.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
return data;
}
# Get a list of data available in the experiment.
#
# The data is sorted with newer versions appearing before oder versions.
#
# + experimentId - the id of the experiment
# + sort - 1 for asc sort, -1 for desc sort by name and version
# + search - search keyword in name, data type and content type (insensitive)
# + return - the paginated list of data resources
resource function get experiments/[int experimentId]/data(boolean? all\-versions, string? search, string? data\-type, int page = 0, int item\-count = 10, int? sort = 1) returns ExperimentDataListResponse|http:NotFound|http:InternalServerError|http:BadRequest {
boolean includeAllVersions = all\-versions == true || all\-versions == ();
int intSort = (sort is ()) ? 1 : sort;
string searchString = (search is ()) ? "" : search;
if (page < 0) {
return <http:BadRequest>{body: "Cannot retrieve a negative page number!"};
}
if (item\-count < 5 || item\-count > 500) {
return <http:BadRequest>{body: "Item count must be between 5 and 500 (both inclusive)!"};
}
var offset = page * item\-count;
int dataCount;
database:ExperimentDataFull[] data;
transaction {
dataCount = check database:getExperimentDataCount(experimentId, searchString, all = includeAllVersions, dataType = data\-type);
if (offset >= dataCount) {
// page is out of range!
check commit;
return <http:NotFound>{};
} else {
data = check database:getDataList(experimentId, searchString, all = includeAllVersions, dataType = data\-type, 'limit = item\-count, offset = offset, sort = intSort);
check commit;
}
} on fail error err {
log:printError("Could not get experiment data list.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
var dataList = from var d in data
select mapToExperimentDataResponse(d);
// TODO add query params to self URL
return {'\@self: string `${serverHost}/experiments/${experimentId}/data/?allVersions=${includeAllVersions}`, items: dataList, itemCount: dataCount};
}
# Get a specific experiment data resource.
#
# + experimentId - the id of the experiment
# + version - the version of the experiment data resource (optional, defaults to "latest")
# + return - the experiment data resource
resource function get experiments/[int experimentId]/data/[string name](string? 'version) returns ExperimentDataResponse|http:InternalServerError {
database:ExperimentDataFull data;
int? producingStep;
int[]? inputFor;
transaction {
data = check database:getData(experimentId, name, 'version);
producingStep = check database:getProducingStepOfData(data);
inputFor = check database:getStepsUsingData(data);
check commit;
} on fail error err {
log:printError("Could not get experiment data resource.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToExperimentDataResponse(data, producingStep, inputFor);
}
# Download the actual data behind the experiment data resource.
#
# + experimentId - the id of the experiment
# + version - the version of the experiment data resource (optional, defaults to "latest")
# + return - the data of the experiment data resource
resource function get experiments/[int experimentId]/data/[string name]/download(string? 'version, http:Caller caller) returns error? {
database:ExperimentDataFull data;
http:Response resp = new;
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
resp.addHeader("Access-Control-Allow-Headers", "range,Content-Type,Depth,User-Agent,X-File-Size,X-Requested-With,If-Modified-Since,X-File-Name,Cache-Control,Access-Control-Allow-Origin");
transaction {
data = check database:getData(experimentId, name, 'version);
check commit;
} on fail error err {
log:printError("Could not get experiment data for download.", 'error = err, stackTrace = err.stackTrace());
resp.statusCode = http:STATUS_INTERNAL_SERVER_ERROR;
resp.setPayload("Something went wrong. Please try again later.");
check caller->respond(resp);
return;
}
resp.statusCode = http:STATUS_OK;
var cType = data.contentType;
if cType.startsWith("text/") || cType.startsWith("application/json") || cType.startsWith("application/X-lines+json") {
resp.addHeader("Content-Disposition", string `inline; filename="${data.name}"`);
} else {
resp.addHeader("Content-Disposition", string `attachment; filename="${data.name}"`);
}
resp.setFileAsPayload(data.location, contentType = data.contentType);
check caller->respond(resp);
}
////////////////////////////////////////////////////////////////////////////
// Timeline ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
# Get a list of timeline entries of an experiment.
#
# The list resource of timeline entries is paginated
#
# + experimentId - the id of the experiment
# + plugin\-name - filter by plugin name
# + 'version - filter by version (name + version for exact match)
# + status - filter by status (pending/finished)
# + uncleared\-substep - filter by step status (whether there is an uncleared substep that requires user inputs) - If set to 1 (or any positive number), steps must have at least one uncleared substeps. Else, be set to -1 (or any negative number). Set to 0 if not specified.
# + result\-quality - filter by result quality (unknown/neutral/good/bad/error/unusable)
# + page - the requested page (starting with page 0)
# + 'item\-count - the number of items per page (5 <= item-count <= 500)
# + sort - 1 for asc sort, -1 for desc sort by step sequence
# + return - the list resource containing the timeline entries
resource function get experiments/[int experimentId]/timeline(string? plugin\-name, string? 'version, string? status, int? uncleared\-substep, string? result\-quality, int page = 0, int item\-count = 0, int? sort = 1) returns TimelineStepListResponse|http:BadRequest|http:NotFound|http:InternalServerError {
if (page < 0) {
return <http:BadRequest>{body: "Cannot retrieve a negative page number!"};
}
if (item\-count < 1 || item\-count > 500) {
return <http:BadRequest>{body: "Item count must be between 1 and 500 (both inclusive)!"};
}
int intSort = (sort is ()) ? 1 : sort;
var offset = page * item\-count;
int stepCount;
database:TimelineStepFull[] steps;
transaction {
stepCount = check database:getTimelineStepCount(experimentId, plugin\-name, 'version, status, uncleared\-substep, result\-quality);
if (offset >= stepCount) {
// page is out of range!
check commit;
return <http:NotFound>{};
} else {
steps = check database:getTimelineStepList(experimentId, plugin\-name, 'version, status, uncleared\-substep, result\-quality, 'limit = item\-count, offset = offset, sort = intSort);
check commit;
}
} on fail error err {
log:printError("Could not get timeline step list.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
var stepList = from var s in steps
select mapToTimelineStepMinResponse(s);
return {'\@self: string `${serverHost}/experiments/${experimentId}/timeline`, items: stepList, itemCount: stepCount};
}
# Create a new timeline step entry.
#
# This also creates a new result watcher that keeps polling the plugin result
# until the final result is available.
#
# + experimentId - the id of the experiment
# + return - the created timeline step resource
resource function post experiments/[int experimentId]/timeline(@http:Payload TimelineStepPost stepData) returns TimelineStepResponse|http:InternalServerError {
database:TimelineStepWithParams createdStep;
database:ExperimentDataReference[] inputData;
transaction {
inputData = from var inputUrl in stepData.inputData
select check mapFileUrlToDataRef(experimentId, inputUrl);
createdStep = check database:createTimelineStep(
experimentId = experimentId,
parameters = stepData.parameters,
parametersContentType = stepData.parametersContentType,
processorName = stepData.processorName,
processorVersion = stepData.processorVersion,
processorLocation = stepData.processorLocation
);
check database:saveTimelineStepInputData(createdStep.stepId, experimentId, inputData);
check database:createTimelineStepResultWatcher(createdStep.stepId, mapToInternalUrl(stepData.resultLocation));
check commit;
} on fail error err {
log:printError("Could not create new timeline step entry.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
do {
ScheduleResultWatcher watcherScheduler = check new (createdStep.stepId, configuredWatcherIntervalls, configuredSubscribedWatcherIntervalls);
check watcherScheduler.schedule();
} on fail error err {
log:printError("Failed to start watcher.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Failed to start watcher."};
return resultErr;
}
return mapToTimelineStepResponse(createdStep, (), inputData, []);
}
# Get a specific timeline step by its step number.
#
# + experimentId - the id of the experiment
# + timelineStepSequence - the step number of the timeline step
# + return - the requested timeline step resource
resource function get experiments/[int experimentId]/timeline/[int timelineStepSequence]() returns TimelineStepResponse|http:InternalServerError {
database:TimelineStepWithParams step;
database:TypedExperimentDataReference[] inputData;
database:TypedExperimentDataReference[] outputData;
database:TimelineSubstepSQL[] substeps;
transaction {
step = check database:getTimelineStep(experimentId = experimentId, sequence = timelineStepSequence);
inputData = check database:getStepInputData(step);
outputData = check database:getStepOutputData(step);
// duplicates input data for substeps, but overhead is negligible
// TODO: FIXME substep has stepId field that is misinterpreted by ui -> change to sequence
substeps = check database:getTimelineSubstepsWithInputData(step.stepId);
check commit;
} on fail error err {
log:printError("Could not get timeline step.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return mapToTimelineStepResponse(step, substeps, inputData, outputData);
}
# Update the result quality associated with a specific timeline step.
#
# Result quality must be one of the following values: 'UNKNOWN', 'NEUTRAL', 'GOOD', 'BAD', 'ERROR', or 'UNUSABLE'.
#
# + experimentId - the id of the experiment
# + timelineStep - the step number of the timeline step
# + return - an empty response with a 2xx http status code on success
resource function put experiments/[int experimentId]/timeline/[int timelineStep](@http:Payload TimelineStepResultQualityPut resultQuality) returns http:Ok|http:BadRequest|http:InternalServerError {
string rq = resultQuality.resultQuality;
if rq != "UNKNOWN" && rq != "NEUTRAL" && rq != "GOOD" && rq != "BAD" && rq != "ERROR" && rq != "UNUSABLE" {
return <http:BadRequest>{body: "Result quality must be one of the following values: 'UNKNOWN', 'NEUTRAL', 'GOOD', 'BAD', 'ERROR', or 'UNUSABLE'."};
}
transaction {
check database:updateTimelineStepResultQuality(experimentId, timelineStep, resultQuality.resultQuality);
check commit;
} on fail error err {
log:printError("Could not update result quality of timeline step.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return <http:Ok>{};
}
# Get the notes associated with a specific timelin step.
#
# + experimentId - the id of the experiment
# + timelineStepSequence - the step number of the timeline step
# + return - the timline step notes
resource function get experiments/[int experimentId]/timeline/[int timelineStepSequence]/notes() returns TimelineStepNotesResponse|http:InternalServerError {
string result;
transaction {
result = check database:getTimelineStepNotes(experimentId, timelineStepSequence);
check commit;
} on fail error err {
log:printError("Could not get timeline step notes.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return {
'\@self: string `${serverHost}/experiments/${experimentId}/timeline/${timelineStepSequence}/notes`,
notes: result
};
}
# Update the notes associated with a specific timeline step.
#
# + experimentId - the id of the experiment
# + timelineStep - the step number of the timeline step
# + return - the updated timline step notes
resource function put experiments/[int experimentId]/timeline/[int timelineStep]/notes(@http:Payload TimelineStepNotesPost notes) returns http:Ok|http:InternalServerError {
transaction {
check database:updateTimelineStepNotes(experimentId, timelineStep, notes.notes);
check commit;
} on fail error err {
log:printError("Could not update timeline step notes.", 'error = err, stackTrace = err.stackTrace());
return <http:InternalServerError>{body: "Something went wrong. Please try again later."};
}
return <http:Ok>{};
}
resource function get experiments/[int experimentId]/timeline/[int timelineStep]/parameters(http:Caller caller) returns error? {
database:TimelineStepWithParams result;
http:Response resp = new;
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
resp.addHeader("Access-Control-Allow-Headers", "range,Content-Type,Depth,User-Agent,X-File-Size,X-Requested-With,If-Modified-Since,X-File-Name,Cache-Control,Access-Control-Allow-Origin");
transaction {
result = check database:getTimelineStep(experimentId = experimentId, sequence = timelineStep);
check commit;
} on fail error err {
log:printError("Could not get timeline step for parameter retrieval.", 'error = err, stackTrace = err.stackTrace());
resp.statusCode = http:STATUS_INTERNAL_SERVER_ERROR;
resp.setPayload("Something went wrong. Please try again later.");
check caller->respond(resp);
return;
}
resp.statusCode = http:STATUS_OK;
var cType = result.parametersContentType;
if cType.startsWith("text/") || cType.startsWith("application/json") || cType.startsWith("application/X-lines+json") {
resp.addHeader("Content-Disposition", "inline; filename=\"parameters\"");
} else {
resp.addHeader("Content-Disposition", "attachment; filename=\"parameters\"");
}
resp.setTextPayload(result.parameters, contentType = result.parametersContentType);
check caller->respond(resp);
}
# Post the user input data associated with an unfinished timeline substep.
#
# + experimentId - the id of the experiment
# + timelineStepSequence - the step number of the timeline step
# + substepNr - the step number of the timeline substep
# + return - the updated timline substep
resource function post experiments/[int experimentId]/timeline/[int timelineStepSequence]/substeps/[int substepNr](@http:Payload TimelineSubstepPost substepData) returns TimelineSubstepResponseWithParams|http:InternalServerError {
database:TimelineStepWithParams step;
database:TimelineSubstepSQL substep;
database:ExperimentDataReference[] inputData;
transaction {
inputData = check trap from var inputUrl in substepData.inputData
select checkpanic mapFileUrlToDataRef(experimentId, inputUrl); // FIXME move back to check if https://github.com/ballerina-platform/ballerina-lang/issues/34894 is resolved
step = check database:getTimelineStep(experimentId = experimentId, sequence = timelineStepSequence);
// verify that substep is in database
substep = check database:getTimelineSubstep(step.stepId, substepNr);
// save input data and update progress
check database:saveTimelineSubstepParams(step.stepId, substepNr, substepData.parameters, substepData.parametersContentType);
check database:saveTimelineSubstepInputData(step.stepId, substepNr, experimentId, inputData);
check commit;
} on fail error err {
log:printError("Could not save input data for timeline substep.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
do {
// reschedule result watcher (already running for the timeline step the substep is associated with)
var watcher = getResultWatcherFromRegistry(step.stepId);
if !(watcher is error) && !watcher.isSubscribed {
// watcher is the main source of updates, reschedule with initial intervalls for faster updates
check watcher.schedule(configuredWatcherIntervalls);
}
} on fail error err {
log:printError("Failed to restart watcher.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Failed to restart watcher."};
return resultErr;
}
return mapToTimelineSubstepResponse(experimentId, timelineStepSequence, substep, inputData);
}
# Get a list of substeps of a timeline entry.
#
# + experimentId - the id of the experiment
# + timelineStepSequence - the step number of the timeline step
# + return - the list of timline substeps
resource function get experiments/[int experimentId]/timeline/[int timelineStepSequence]/substeps() returns TimelineSubstepListResponse|http:InternalServerError {
// no pagination
database:TimelineSubstepSQL[] substeps;
transaction {
database:TimelineStepWithParams step = check database:getTimelineStep(experimentId = experimentId, sequence = timelineStepSequence);
substeps = check database:getTimelineSubsteps(step.stepId, experimentId);
check commit;
} on fail error err {
log:printError("Could not get list of timeline substeps.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
TimelineSubstepResponseWithoutParams[] substepsResponse = mapToTimelineSubstepListResponse(experimentId, timelineStepSequence, substeps);
return {
'\@self: string `${serverHost}/experiments/${experimentId}/timeline/${timelineStepSequence}/substeps`,
items: substepsResponse
};
}
# Get a specific substep of a timeline entry.
#
# + experimentId - the id of the experiment
# + timelineStepSequence - the step number of the timeline step
# + substepNr - the step number of the timeline substep
# + return - the requested timline substep
resource function get experiments/[int experimentId]/timeline/[int timelineStepSequence]/substeps/[int substepNr]() returns TimelineSubstepResponseWithParams|http:InternalServerError {
database:TimelineSubstepWithParams substep;
database:ExperimentDataReference[] inputData;
transaction {
// FIXME timelineStep != database step id!!!! Requested timelineStepSequence is in fact stepId (ui should not know that id)
substep = check database:getTimelineSubstepWithParams(experimentId, timelineStepSequence, substepNr);
inputData = check database:getSubstepInputData(substep.stepId, substep.substepNr);
check commit;
} on fail error err {
log:printError("Could not get timeline substep entry.", 'error = err, stackTrace = err.stackTrace());
// if with return does not correctly narrow type for rest of function... this does.
http:InternalServerError resultErr = {body: "Something went wrong. Please try again later."};
return resultErr;
}
return mapToTimelineSubstepResponse(experimentId, timelineStepSequence, substep, inputData);
}
resource function get experiments/[int experimentId]/timeline/[int timelineStep]/substeps/[int substepNr]/parameters(http:Caller caller) returns error? {
database:TimelineSubstepWithParams substep;
http:Response resp = new;
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
resp.addHeader("Access-Control-Allow-Headers", "range,Content-Type,Depth,User-Agent,X-File-Size,X-Requested-With,If-Modified-Since,X-File-Name,Cache-Control,Access-Control-Allow-Origin");
transaction {
substep = check database:getTimelineSubstepWithParams(experimentId, timelineStep, substepNr);
check commit;
} on fail error err {
log:printError("Could not get parameters for timeline substep.", 'error = err, stackTrace = err.stackTrace());
resp.statusCode = http:STATUS_INTERNAL_SERVER_ERROR;
resp.setPayload("Something went wrong. Please try again later.");
check caller->respond(resp);
return;
}
resp.statusCode = http:STATUS_OK;
var cType = substep.parametersContentType;
if cType.startsWith("text/") || cType.startsWith("application/json") || cType.startsWith("application/X-lines+json") {