Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set reopt_version in response meta data programmatically #593

Merged
merged 16 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Classify the change according to the following categories:
##### Removed
### Patches

## v3.9.3
### Minor Updates
#### Changed
- Set **reopt_version** in **APIMeta** and **ERPMeta** programatically based on actual REopt.jl package version in Julia environment instead of hardcoded so doesn't need to be updated by hand

## v3.9.2
#### Added
- Added attribute `thermal_efficiency` to the arguments of http endpoint `chp_defaults`
Expand Down
17 changes: 9 additions & 8 deletions julia_src/http.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ end
function reopt(req::HTTP.Request)
d = JSON.parse(String(req.body))
error_response = Dict()
settings = d["Settings"]
if !isempty(get(d, "api_key", ""))
ENV["NREL_DEVELOPER_API_KEY"] = pop!(d, "api_key")
else
ENV["NREL_DEVELOPER_API_KEY"] = test_nrel_developer_api_key
delete!(d, "api_key")
end
settings = d["Settings"]
solver_name = get(settings, "solver_name", "HiGHS")
if solver_name == "Xpress" && !(xpress_installed=="True")
solver_name = "HiGHS"
Expand Down Expand Up @@ -140,23 +140,22 @@ function reopt(req::HTTP.Request)

if isempty(error_response)
@info "REopt model solved with status $(results["status"])."
response = Dict(
"results" => results,
"reopt_version" => string(pkgversion(reoptjl))
)
if results["status"] == "error"
response = Dict(
"results" => results
)
if !isempty(inputs_with_defaults_set_in_julia)
response["inputs_with_defaults_set_in_julia"] = inputs_with_defaults_set_in_julia
end
return HTTP.Response(400, JSON.json(response))
else
response = Dict(
"results" => results,
"inputs_with_defaults_set_in_julia" => inputs_with_defaults_set_in_julia
)
response["inputs_with_defaults_set_in_julia"] = inputs_with_defaults_set_in_julia
return HTTP.Response(200, JSON.json(response))
end
else
@info "An error occured in the Julia code."
error_response["reopt_version"] = string(pkgversion(reoptjl))
return HTTP.Response(500, JSON.json(error_response))
end
end
Expand All @@ -169,9 +168,11 @@ function erp(req::HTTP.Request)
results = Dict()
try
results = reoptjl.backup_reliability(erp_inputs)
results["reopt_version"] = string(pkgversion(reoptjl))
catch e
@error "Something went wrong in the ERP Julia code!" exception=(e, catch_backtrace())
error_response["error"] = sprint(showerror, e)
error_response["reopt_version"] = string(pkgversion(reoptjl))
end
GC.gc()
if isempty(error_response)
Expand Down
1 change: 0 additions & 1 deletion reoptjl/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def obj_create(self, bundle, **kwargs):
meta = {
"run_uuid": run_uuid,
"api_version": 3,
"reopt_version": "0.47.2",
"status": "Validating..."
}
bundle.data.update({"APIMeta": meta})
Expand Down
1 change: 1 addition & 0 deletions reoptjl/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class APIMeta(BaseModel, models.Model):
created = models.DateTimeField(auto_now_add=True)
reopt_version = models.TextField(
blank=True,
null=True,
default="",
help_text="Version number of the Julia package for REopt that is used to solve the problem."
)
Expand Down
2 changes: 2 additions & 0 deletions reoptjl/src/run_jump_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def run_jump_model(run_uuid):
if response.status_code == 500:
raise REoptFailedToStartError(task=name, message=response_json["error"], run_uuid=run_uuid, user_uuid=user_uuid)
results = response_json["results"]
reopt_version = response_json["reopt_version"]
if results["status"].strip().lower() != "error":
inputs_with_defaults_set_in_julia = response_json["inputs_with_defaults_set_in_julia"]
time_dict["pyjulia_run_reopt_seconds"] = time.time() - t_start
Expand Down Expand Up @@ -107,6 +108,7 @@ def run_jump_model(run_uuid):

profiler.profileEnd()
# TODO save profile times
APIMeta.objects.filter(run_uuid=run_uuid).update(reopt_version=reopt_version)
if status.strip().lower() != 'error':
update_inputs_in_database(inputs_with_defaults_set_in_julia, run_uuid)
process_results(results, run_uuid)
Expand Down
1 change: 1 addition & 0 deletions reoptjl/test/test_job_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_multiple_outages(self):
run_uuid = r.get('run_uuid')
resp = self.api_client.get(f'/v3/job/{run_uuid}/results')
r = json.loads(resp.content)
self.assertIn("reopt_version", r.keys())
results = r["outputs"]
self.assertEqual(np.array(results["Outages"]["unserved_load_series_kw"]).shape, (1,2,5))
self.assertEqual(np.array(results["Outages"]["generator_fuel_used_per_outage_gal"]).shape, (1,2))
Expand Down
4 changes: 2 additions & 2 deletions resilience_stats/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def obj_create(self, bundle, **kwargs):

meta_dict = {
"run_uuid": erp_run_uuid,
"reopt_version": "0.45.0",
"status": "Validating..."
}

Expand Down Expand Up @@ -450,7 +449,8 @@ def process_erp_results(results: dict, run_uuid: str) -> None:
#TODO: get success or error status from julia
meta = ERPMeta.objects.get(run_uuid=run_uuid)
meta.status = 'Completed' #results.get("status")
meta.save(update_fields=['status'])
meta.reopt_version = results.pop("reopt_version")
meta.save(update_fields=['status','reopt_version'])
ERPOutputs.create(meta=meta, **results).save()


Expand Down
1 change: 1 addition & 0 deletions resilience_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ERPMeta(BaseModel, models.Model):
created = models.DateTimeField(auto_now_add=True)
reopt_version = models.TextField(
blank=True,
null=True,
default="",
help_text="Version number of the REopt Julia package that is used to calculate reliability."
)
Expand Down
4 changes: 3 additions & 1 deletion resilience_stats/tests/test_erp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def test_erp_long_duration_battery(self):
r_sim = json.loads(resp.content)
erp_run_uuid = r_sim.get('run_uuid')
resp = self.get_results_sim(erp_run_uuid)
results_sim = json.loads(resp.content)["outputs"]
r = json.loads(resp.content)
self.assertIn("reopt_version", r.keys())
results_sim = r["outputs"]

expected_result = ([1]*79)+[0.999543,0.994178,0.9871,0.97774,0.965753,0.949429,0.926712,0.899543,0.863584,0.826712,0.785616,0.736416,0.683105,0.626256,0.571005,0.519064,0.47226,0.429909,0.391553,0.357306,0]
#TODO: resolve bug where unlimted fuel markov portion of results goes to zero 1 timestep early
Expand Down