forked from Open-EO/openeo-r-udf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.R
471 lines (401 loc) · 16.3 KB
/
api.R
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
library(stars)
library(abind)
library(lubridate)
library(xts)
api_version = "0.1.0"
r_udf_version = "0.3.0"
DEBUG = FALSE
#TODO define float maximum digits
source("data_transformation.R")
.measure_time = function(fun,message,envir=parent.frame()) {
if (!DEBUG) return(eval(fun,envir = envir))
tryCatch({
start = Sys.time()
return(eval(fun,envir=envir))
},error=function(e){
stop(e$message)
},finally = {
cat(message)
cat("\n")
print(Sys.time()-start)
})
}
.read_data_requirement = function(code) {
require_annotation = "@require"
require_regex = paste0("[#]+\\s*",require_annotation,"\\s*(\\w+):(\\w+)\\s*\\n")
selection = unlist(regmatches(code,regexec(require_regex,code,perl=TRUE)))
if (length(selection) > 0) {
return(list(
variable_name = selection[2],
target_class = selection[3]
))
} else {
return(list())
}
}
#* Interprete JSON, divide code and data and assign classes
#* @filter check-data
check_data = function(req, res) {
if (req$PATH_INFO != "/udf") return(plumber::forward())
if (DEBUG) {
cat("=== Started executing at endpoint /udf ===\n")
}
if (length(req$postBody) > 0) {
if (DEBUG) {
cat("Upload data:\n")
print(Sys.time()-as_datetime(as.numeric(req$HEADERS["date"]),tz=Sys.timezone()))
}
if (grepl(x = req$postBody,pattern = "structured_data_list")) {
json_in = .measure_time(quote(jsonlite::fromJSON(req$postBody,simplifyVector=FALSE)),"Read json. Runtime:")
} else {
json_in = .measure_time(quote(jsonlite::fromJSON(req$postBody)),"Read json. Runtime:")
}
req$postBody = NULL
if (is.null(json_in$code$language) || !tolower(json_in$code$language)=="r") {
res$status = 422
return(list(error = "Cannot interprete code source, due to missing programming language."))
}
req$code = json_in$code
req$data = json_in$data # data is the UDF data model
# split user_context and server_context and append also to req
req$user_context = if (length(json_in$data$user_context) == 0) list() else json_in$data$user_context
json_in$data$user_context = NULL
req$server_context = if (length(json_in$data$server_context) == 0) list() else json_in$data$server_context
json_in$data$server_context = NULL
if (length(req$data$structured_data_list) > 0) {
class(req$data) = "StructuredData"
} else if (length(req$data$data_collection$object_collections$data_cubes) > 0) {
class(req$data) = "DataCube"
} else {
res$status = 422
return(list(error = "Data other than 'StructuredData' and 'DataCube' as a special case of a DataCollection are not supported right now."))
}
}
plumber::forward()
}
#* Interprete JSON, divide code and data and assign classes
#* @filter check-data-legacy
check_data_legacy = function(req, res) {
if (req$PATH_INFO != "/udf_legacy") return(plumber::forward())
# TODO check the endpoint called /udf -> ok, /udf_legacy -> another filter should be called
if (DEBUG) {
cat("=== Started executing at endpoint /udf ===\n")
}
if (length(req$postBody) > 0) {
if (DEBUG) {
cat("Upload data:\n")
print(Sys.time()-as_datetime(as.numeric(req$HEADERS["date"]),tz=Sys.timezone()))
}
json_in = .measure_time(quote(jsonlite::fromJSON(req$postBody)),"Read json. Runtime:")
req$postBody = NULL
if (is.null(json_in$code$language) || !tolower(json_in$code$language)=="r") {
res$status = 400 #maybe 422
return(list(error = "Cannot interprete code source, due to missing programming language."))
}
req$code = json_in$code
req$data = json_in$data
if (length(req$data$raster_collection_tiles) > 0) {
class(req$data) = "RasterCollectionTile"
} else if (length(req$data$hypercubes) > 0) {
class(req$data) = "HyperCube"
} else if (length(req$data$structured_data) > 0) {
class(req$data) = "StructuredDataLegacy"
} else {
res$status = 400
return(list(error = "Data other than RasterCollectionTile, Hypercube and StructuredData are not supported yet."))
}
}
plumber::forward()
}
#* @apiTitle R UDF API
#*
#* Takes a UDFRequest containing data and code and runs the code on the data
#*
#* @post /udf_legacy
post_udf_legacy.json = function(req,res, debug=FALSE) {
if (!is.null(debug) && isTRUE(debug)) {
DEBUG = debug
}
# prepare the executable code
fun = .prepare_udf_function(code = req$code$source)
# if data requirements states something else than stars we need to convert it
data_requirement = .read_data_requirement(req$code$source)
if (length(data_requirement) > 0) {
if (length(data_requirement$variable_name) > 0) {
#replace variable name in fun
names(formals(fun)) = data_requirement$variable_name
# TODO when we use the context this needs to be accounted for!
}
}
# transform data into stars or simple data
data_in = .translate_input_data_legacy(data = req$data, data_requirement)
# run the UDF
results = .measure_time(quote(lapply(data_in, fun)),"Executed script. Runtime:")
# map to stars or keep simple data types
results = lapply(1:length(results), function(index) {
if (any(class(results[[index]]) %in% "stars")) {
return(results[[index]])
} else if (any(class(results[[index]]) %in% "xts")) { # TODO check later only xts to go in here (actual xts of results)
return(st_as_stars(results[[index]]))
} else if (any(class(results[[index]]) %in%
c("list","numeric","integer","character","factor","logical","matrix","data.frame"))) {
return(results[[index]])
} else {
stop("UDF data return is not a simple type, xts or stars.")
}
})
# transform stars into HyperCube, simple data types into StructuredData
if (any("stars" %in% class(results[[1]]))) {
json_out = .measure_time(quote(lapply(results,function(obj) as(obj,"HyperCube"))),"Translated from stars to Hypercube Runtime:")
} else {
json_out = .measure_time(quote(lapply(results,function(obj) as(obj,"StructuredDataLegacy"))),"Translated from simple data to StructuredData. Runtime:")
}
rm(results)
# Merge multiple data chunks
if (length(json_out) == 1) {
json_out = json_out[[1]]
} else {
shell = json_out[[1]]
shell$hypercubes = lapply(unname(json_out),function(obj) obj$hypercubes[[1]])
shell$structured_data = lapply(unname(json_out),function(obj) obj$structured_data[[1]])
json_out = shell
rm(shell)
}
# Create the JSON structure
json = .measure_time(quote(jsonlite::toJSON(json_out,auto_unbox = TRUE)),"Prepared JSON from list. Runtime:")
res$setHeader(name = "CONTENT-TYPE",value = "application/json")
res$setHeader(name = "date", value = Sys.time())
res$body = json
return(res)
}
#* @apiTitle R UDF API
#*
#* Takes a UDFRequest containing data and code and runs the code on the data
#*
#* @post /udf
post_udf.json = function(req,res, debug=FALSE) {
tryCatch({
if (!is.null(debug) && isTRUE(debug)) {
DEBUG = debug
}
# prepare the executable code
fun = .prepare_udf_function(code = req$code$source)
# if data requirements states something else than stars we need to convert it
data_requirement = .read_data_requirement(req$code$source)
if (length(data_requirement) > 0) {
if (length(data_requirement$variable_name) > 0) {
#replace variable name in fun, but keep the variable "context" which describes the user context
names(formals(fun)) = c(data_requirement$variable_name,"context")
}
}
# transform data into stars or simple data
data_in = .translate_input_data(data = req$data, data_requirement)
# run the UDF
if (length(data_in) == 0) {
stop("error while reading the input data")
} else if (class(data_in) == "stars" || (is.list(data_in) && class(data_in[[1]]) == "stars")) {
results = list(.measure_time(quote(do.call(fun,args = list(data_in,req$user_context))),"Executed script. Runtime:"))
} else {
# mainly for a multitude of structured data (e.g. multiple timeseries)
results = list(.measure_time(quote(lapply(data_in, fun, context = req$user_context)),"Executed script. Runtime:"))
}
# map to stars or keep simple data types
results = lapply(1:length(results), function(index) {
if (any(class(results[[index]]) %in% "stars")) {
return(results[[index]])
} else if (any(class(results[[index]]) %in% "xts")) { # TODO check later only xts to go in here (actual xts of results)
return(st_as_stars(results[[index]]))
} else if (any(class(results[[index]]) %in%
c("list","numeric","integer","character","factor","logical","matrix","data.frame"))) {
return(results[[index]])
} else {
stop("UDF data return is not a simple type, xts or stars.")
}
})
# transform stars into HyperCube, simple data types into StructuredData
if ("stars" %in% class(results[[1]])) { # only if all results are stars objects
if (! all(sapply(results,function(res)"stars"==class(res)))) {
stop("All data outputs have to be of class 'stars' or any structured data. Mixed types not supported, yet.")
}
json_out = list(
user_context = if (length(req$user_context) == 0) NA else length(req$user_context),
server_context = if (length(req$server_context) == 0) NA else req$server_context,
data_collection=.measure_time(quote(as(results,"DataCube")),"Translated from stars to DataCollection. Runtime:"))
} else {
json_out = .measure_time(quote(lapply(results,function(obj) as(obj,"StructuredData"))),"Translated from simple data to StructuredData. Runtime:")
json_out = list(
user_context = if (length(req$user_context) == 0) NA else length(req$user_context),
server_context = if (length(req$server_context) == 0) NA else req$server_context,
structured_data_list = json_out
)
}
rm(results)
# Create the JSON structure
json = .measure_time(quote(jsonlite::toJSON(json_out,
auto_unbox = TRUE,
force=TRUE,
digits = if (length(req$server_context$export_digits) == 0) 4 else req$server_context$export_digits)),
"Prepared JSON from list. Runtime:")
}, error = function(e) {
json = jsonlite::toJSON(e,auto_unbox = T,force=T)
res$status = 500
})
res$setHeader(name = "Content-Type",value = "application/json")
res$setHeader(name = "date", value = Sys.time())
res$body = json
return(res)
}
#* @get /
#* @serializer unboxedJSON
#* preempt check-data
udf_version = function(){
return(list(
runtime=list(
language="R",
service_name="r-udf-service",
service_version = r_udf_version,
api_version = api_version
)
))
}
#* Gets the library configuration of this udf service
#* @get /packages
#* @serializer unboxedJSON
#* @preempt check-data
get_installed_libraries = function() {
libs = as.data.frame(installed.packages()[,c("Package","Version")])
rownames(libs) = NULL
return(libs)
}
.prepare_udf_function = function(code) {
fun = function() {}
formals(fun) = alist(data=,context=) #TODO also metadata from run_udf (processes api)
tryCatch({
if (!startsWith(code,"{")) {
# if a starting bracket is missing set opening and closing ones, otherwise we assume that the
# provided code is clean
code = paste0("{\n",code,"\n}")
}
body(fun) = parse(text=code)
},
error = function(e) {
stop(paste0("Provided R code is not valid. Please check code syntax, parenthesis and spelling. Message: ",e$message))
})
return(fun)
}
.translate_input_data_legacy = function(data,data_requirement=NULL) {
if ("HyperCube" %in% class(data)) {
data_in = .measure_time(quote(as(data,"stars")),"Translated list into stars. Runtime:")
} else if ("StructuredDataLegacy" %in% class(data)) {
data_in = .measure_time(quote(as.StructuredDataLegacy.base(data)),"Translated into simple data. Runtime:")
}
if (length(data_requirement) > 0) {
if (length(data_requirement$target_class) > 0) {
switch(data_requirement$target_class,
xts = {
# coerce stars_in into the target class
data_in = lapply(data_in, function(stars) {
if (! "stars" %in% class(stars)) stop("Coercion into xts failed. Input data is no stars object.")
if (! "t" %in% names(st_dimensions(stars))) {
stop("No temporal dimension 't' found.")
}
as.xts(stars)
})
},
list = {
data_in = lapply(data_in,as.list)
},
data.frame = {
data_in = lapply(data_in,as.data.frame)
},
matrix = {
data_in = lapply(data_in,as.matrix)
},
tibble = {
data_in = lapply(data_in,tibble::as_tibble)
},
numeric = {
data_in = lapply(data_in,as.numeric)
},
character = {
data_in = lapply(data_in,as.character)
},
integer = {
data_in = lapply(data_in,as.integer)
},
logical = {
data_in = lapply(data_in,as.logical)
},
factor = {
data_in = lapply(data_in,as.factor)
},
{
# default behavior
if (!(length(data_requirement$target_class) > 0 &&
data_requirement$target_class == "stars")) {
stop("Not supported variable class. Use 'stars' or 'xts'")
}
}
)
}
}
return(data_in)
}
.translate_input_data = function(data,data_requirement=NULL) {
if ("DataCube" == class(data)) {
data_in = .measure_time(quote(as(data,"stars")),"Translated list into stars. Runtime:")
} else if ("StructuredData" %in% class(data)) {
data_in = .measure_time(quote(as.StructuredData.base(data)),"Translated into simple data. Runtime:")
}
if (length(data_requirement) > 0) {
if (length(data_requirement$target_class) > 0) {
switch(data_requirement$target_class,
xts = {
# coerce stars_in into the target class
data_in = lapply(data_in, function(stars) {
if (! "stars" %in% class(stars)) stop("Coercion into xts failed. Input data is no stars object.")
if (! "t" %in% names(st_dimensions(stars))) {
stop("No temporal dimension 't' found.")
}
as.xts(stars)
})
},
list = {
data_in = lapply(data_in,as.list)
},
data.frame = {
data_in = lapply(data_in,as.data.frame)
},
matrix = {
data_in = lapply(data_in,as.matrix)
},
tibble = {
data_in = lapply(data_in,tibble::as_tibble)
},
numeric = {
data_in = lapply(data_in,as.numeric)
},
character = {
data_in = lapply(data_in,as.character)
},
integer = {
data_in = lapply(data_in,as.integer)
},
logical = {
data_in = lapply(data_in,as.logical)
},
factor = {
data_in = lapply(data_in,as.factor)
},
{
# default behavior
if (!(length(data_requirement$target_class) > 0 &&
data_requirement$target_class == "stars")) {
stop("Not supported variable class. Use 'stars' or 'xts'")
}
}
)
}
}
return(data_in)
}