-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor sim service to include TDS integration (#24)
- Loading branch information
Showing
17 changed files
with
337 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name = "Scheduler" | ||
name = "SimulationService" | ||
uuid = "e66378d9-a322-4933-8764-0ce0bcab4993" | ||
authors = ["Five Grant <[email protected]>"] | ||
version = "0.2.0" | ||
|
@@ -11,6 +11,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" | |
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" | ||
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" | ||
EasyModelAnalysis = "ef4b24a4-a090-4686-a932-e7e56a5a83bd" | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" | ||
|
@@ -21,6 +22,7 @@ JobSchedulers = "eeff360b-c02d-44d3-ab26-4013c616a17e" | |
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" | ||
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" | ||
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50" | ||
OpenAPI = "d5e62ea6-ddf3-4d43-8e4c-ad5e6c8bfd7d" | ||
OpenBLAS32_jll = "656ef2d0-ae68-5445-9ca0-591084a874a2" | ||
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" | ||
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
FROM julia:1.8 | ||
WORKDIR /scheduler | ||
WORKDIR /simulation-service | ||
|
||
# Install dependencies | ||
COPY Manifest.toml /scheduler/ | ||
COPY Project.toml /scheduler/ | ||
COPY Manifest.toml /simulation-service/ | ||
COPY Project.toml /simulation-service/ | ||
ENV JULIA_PROJECT=. | ||
RUN julia -e 'using Pkg; Pkg.instantiate();' | ||
|
||
# Install Scheduler source | ||
COPY src/ /scheduler/src/ | ||
# Install simulation-service source | ||
COPY src/ /simulation-service/src/ | ||
RUN julia -e 'using Pkg; Pkg.resolve();' | ||
|
||
# Launch Scheduler | ||
# Launch simulation-service | ||
EXPOSE 8080 | ||
CMD [ "julia", "--threads", "4", "-e", "using Scheduler; Scheduler.run!();" ] | ||
CMD [ "julia", "--threads", "4", "-e", "using SimulationService; SimulationService.run!();" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
examples/request-forecast.json → examples/request-simulate.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Provide external awareness / service-related side-effects to SciML operations | ||
""" | ||
module ArgIO | ||
|
||
import Symbolics | ||
import DataFrames: DataFrame, rename! | ||
import CSV | ||
import HTTP: Request | ||
import Oxygen: serveparallel, serve, resetstate, json, setschema, @post, @get | ||
|
||
include("./Settings.jl"); import .Settings: settings | ||
include("./AssetManager.jl"); import .AssetManager: fetch_dataset, fetch_model, upload | ||
|
||
export prepare_input, prepare_output | ||
|
||
|
||
""" | ||
Transform requests into arguments to be used by operation | ||
Optionally, IDs are hydrated with the corresponding entity from TDS. | ||
""" | ||
function prepare_input(req::Request) | ||
args = json(req, Dict{Symbol,Any}) | ||
if settings["ENABLE_TDS"] | ||
if in(:model, keys(args)) | ||
args[:model] = fetch_model(args[:model]) | ||
end | ||
if in(:dataset, keys(args)) | ||
args[:dataset] = fetch_dataset(args[:dataset]) | ||
end | ||
end | ||
args | ||
end | ||
|
||
""" | ||
Normalize the header of the resulting dataframe and return a CSV | ||
Optionally, the CSV is saved to TDS instead an the coreresponding ID is returned. | ||
""" | ||
function prepare_output(dataframe::DataFrame) | ||
stripped_names = names(dataframe) .=> (r -> replace(r, "(t)"=>"")).(names(dataframe)) | ||
rename!(dataframe, stripped_names) | ||
rename!(dataframe, "timestamp" => "timestep") | ||
if !settings["ENABLE_TDS"] | ||
io = IOBuffer() | ||
# TODO(five): Write to remote server | ||
CSV.write(io, dataframe) | ||
return String(take!(io)) | ||
else | ||
return upload(dataframe) | ||
end | ||
end | ||
|
||
""" | ||
Coerces NaN values to nothing for each parameter. | ||
""" | ||
function prepare_output(params::Vector{Pair{Symbolics.Num, Float64}}) | ||
nan_to_nothing(value) = isnan(value) ? nothing : value | ||
Dict(key => nan_to_nothing(value) for (key, value) in params) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Asset fetching from TDS | ||
""" | ||
module AssetManager | ||
|
||
import DataFrames: DataFrame | ||
import CSV, Downloads, HTTP | ||
import OpenAPI.Clients: Client | ||
import JSON3 as JSON | ||
|
||
include("./Settings.jl"); import .Settings: settings | ||
|
||
export fetch_dataset, fetch_model, upload | ||
|
||
""" | ||
Return model JSON as string from TDS by ID | ||
""" | ||
function fetch_model(model_id::Int64) | ||
response = HTTP.get("$(settings["TDS_URL"])/models/$model_id", ["Content-Type" => "application/json"]) | ||
body = response.body |> JSON.read ∘ String | ||
body.content | ||
end | ||
|
||
""" | ||
Return csv from TDS by ID | ||
""" | ||
function fetch_dataset(dataset_id::Int64) | ||
url = "$(settings["TDS_URL"])/datasets/$dataset_id/files" | ||
io = IOBuffer() | ||
Downloads.download(url, io) | ||
seekstart(io) | ||
CSV.read(io, DataFrame) | ||
end | ||
|
||
""" | ||
Upload a CSV to TDS | ||
""" | ||
function upload(output::DataFrame) | ||
# TODO(five): Stream so there isn't duplication | ||
io = IOBuffer() | ||
CSV.write(io, output) | ||
seekstart(io) | ||
|
||
payload = JSON.write(Dict( | ||
"name" => "autogenerated sim run", | ||
"description" => "autogenerated sim run", | ||
"url" => "", | ||
"simulation_run" => true | ||
)) | ||
response = HTTP.post("$(settings["TDS_URL"])/datasets", ["Content-Type" => "application/json"], payload) | ||
body = response.body |> JSON.read ∘ String | ||
|
||
|
||
# TODO(five): Handle 4xx from TDS | ||
url = "$(settings["TDS_URL"])/datasets/$(body["id"])/files" | ||
HTTP.post(url, [], HTTP.Form(Dict("filename" => "generated.csv", "file" => HTTP.Multipart("file.csv", io, "text/plain")))) | ||
|
||
body["id"] | ||
end | ||
|
||
end # module AssetManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.