Skip to content

Commit c08a5a2

Browse files
committed
Modify helper function to fetch results on mvs server
1 parent 0138a02 commit c08a5a2

1 file changed

Lines changed: 56 additions & 3 deletions

File tree

app/projects/requests.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import json
44

55
# from requests.exceptions import HTTPError
6-
from epa.settings import PROXY_CONFIG, MVS_POST_URL, MVS_GET_URL, MVS_SA_POST_URL
6+
from epa.settings import (
7+
PROXY_CONFIG,
8+
MVS_POST_URL,
9+
MVS_GET_URL,
10+
MVS_SA_POST_URL,
11+
MVS_SA_GET_URL,
12+
)
713
from dashboard.models import AssetsResults, KPICostsMatrixResults, KPIScalarResults
814
from projects.constants import DONE, PENDING, ERROR
915
import logging
@@ -53,7 +59,24 @@ def mvs_simulation_check_status(token):
5359
return json.loads(response.text)
5460

5561

56-
def fetch_mvs_simulation_status(simulation):
62+
def mvs_sa_check_status(token):
63+
try:
64+
response = requests.get(
65+
MVS_SA_GET_URL + token, proxies=PROXY_CONFIG, verify=False
66+
)
67+
response.raise_for_status()
68+
except requests.HTTPError as http_err:
69+
logger.error(f"HTTP error occurred: {http_err}")
70+
return None
71+
except Exception as err:
72+
logger.error(f"Other error occurred: {err}")
73+
return None
74+
else:
75+
logger.info("Success!")
76+
return json.loads(response.text)
77+
78+
79+
def fetch_mvs_simulation_results(simulation):
5780
if simulation.status == PENDING:
5881
response = mvs_simulation_check_status(token=simulation.mvs_token)
5982
try:
@@ -79,6 +102,36 @@ def fetch_mvs_simulation_status(simulation):
79102
)
80103
simulation.save()
81104

105+
return simulation.status != PENDING
106+
107+
108+
def fetch_mvs_sa_results(simulation):
109+
if simulation.status == PENDING:
110+
response = mvs_sa_check_status(token=simulation.mvs_token)
111+
try:
112+
simulation.status = response["status"]
113+
simulation.errors = (
114+
json.dumps(response["results"][ERROR])
115+
if simulation.status == ERROR
116+
else None
117+
)
118+
# TODO here fetch the results of the reference scenario in a separate Simulation instance
119+
# and parse the
120+
simulation.results = (
121+
response["results"] if simulation.status == DONE else None
122+
)
123+
logger.info(f"The simulation {simulation.id} is finished")
124+
except:
125+
simulation.status = ERROR
126+
simulation.results = None
127+
128+
simulation.elapsed_seconds = (datetime.now() - simulation.start_date).seconds
129+
simulation.end_date = (
130+
datetime.now() if response["status"] in [ERROR, DONE] else None
131+
)
132+
simulation.save()
133+
return simulation.status != PENDING
134+
82135

83136
def get_mvs_simulation_results(simulation):
84137
# TODO do not repeat if the simulation is not on the server anymore, or if the results are already loaded
@@ -99,7 +152,7 @@ def get_mvs_simulation_results(simulation):
99152

100153
simulation.save()
101154
else:
102-
fetch_mvs_simulation_status(simulation)
155+
fetch_mvs_simulation_results(simulation)
103156

104157

105158
def parse_mvs_results(simulation, response_results):

0 commit comments

Comments
 (0)