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

refactor: make api available outside flask context #130

Merged
merged 4 commits into from
May 18, 2021
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
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.git
.github
.pytest_cache
**/__pycache__
.tox
.env
.venv
Dockerfile
.dockerignore
39 changes: 29 additions & 10 deletions pyreisejl/utility/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
from pathlib import Path
from subprocess import PIPE, Popen

Expand Down Expand Up @@ -26,27 +28,44 @@ def get_script_path():
return str(path_to_script)


@app.route("/launch/<int:scenario_id>", methods=["POST"])
def launch_simulation(scenario_id):
cmd_call = ["python3", "-u", get_script_path(), str(scenario_id), "--extract-data"]
threads = request.args.get("threads", None)
solver = request.args.get("solver", None)
def launch_simulation(scenario_id, threads=None, solver=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to be using this to launch simulations from powersimdata, should we add a check for available solvers here just so that it fails faster? i.e. from pyreisejl.utility.launchers import get_available_solvers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have this validation in powersimdata, not sure if we'd want to move it here at some point but that gets into a bit of a design decision so thinking we can save that for later.

cmd = [
sys.executable,
"-u",
get_script_path(),
str(scenario_id),
"--extract-data",
]

if threads is not None:
cmd_call.extend(["--threads", str(threads)])
cmd.extend(["--threads", str(threads)])

if solver is not None:
cmd_call.extend(["--solver", solver])
cmd.extend(["--solver", solver])

proc = Popen(cmd_call, stdout=PIPE, stderr=PIPE, start_new_session=True)
new_env = os.environ.copy()
new_env["PYTHONPATH"] = str(Path(__file__).parent.parent.parent.absolute())
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, start_new_session=True, env=new_env)
entry = SimulationState(scenario_id, proc)
state.add(entry)
return jsonify(entry.as_dict())
return entry.as_dict()


def check_progress():
return state.as_dict()


@app.route("/launch/<int:scenario_id>", methods=["POST"])
def handle_launch(scenario_id):
threads = request.args.get("threads", None)
solver = request.args.get("solver", None)
entry = launch_simulation(scenario_id, threads, solver)
return jsonify(entry)


@app.route("/list")
def list_ongoing():
return jsonify(state.as_dict())
return jsonify(check_progress())


@app.route("/status/<int:scenario_id>")
Expand Down
18 changes: 11 additions & 7 deletions pyreisejl/utility/const.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import posixpath
import os
from pathlib import Path

DATA_ROOT_DIR = "/mnt/bes/pcm"
if os.getenv("DEPLOYMENT_MODE") is not None:
DATA_ROOT_DIR = os.path.join(Path.home(), "ScenarioData", "")
else:
DATA_ROOT_DIR = "/mnt/bes/pcm"

SCENARIO_LIST = posixpath.join(DATA_ROOT_DIR, "ScenarioList.csv")
EXECUTE_LIST = posixpath.join(DATA_ROOT_DIR, "ExecuteList.csv")
EXECUTE_DIR = posixpath.join(DATA_ROOT_DIR, "tmp")
INPUT_DIR = posixpath.join(DATA_ROOT_DIR, "data", "input")
OUTPUT_DIR = posixpath.join(DATA_ROOT_DIR, "data", "output")
SCENARIO_LIST = os.path.join(DATA_ROOT_DIR, "ScenarioList.csv")
EXECUTE_LIST = os.path.join(DATA_ROOT_DIR, "ExecuteList.csv")
EXECUTE_DIR = os.path.join(DATA_ROOT_DIR, "tmp")
INPUT_DIR = os.path.join(DATA_ROOT_DIR, "data", "input")
OUTPUT_DIR = os.path.join(DATA_ROOT_DIR, "data", "output")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One day, it will be nice to find a ways to not set these variables when we use PowerSimData to launch REISE.jl. What is particularly ugly is that the path needs to be identical to the ones set in PowerSimData

6 changes: 2 additions & 4 deletions pyreisejl/utility/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,8 @@ def get_scenario(scenario_id):
scenario_info = scenario.to_dict("records", into=OrderedDict)[0]

# Determine input and execute directory for data
input_dir = os.path.join(const.EXECUTE_DIR, "scenario_%s" % scenario_info["id"])
execute_dir = os.path.join(
const.EXECUTE_DIR, f"scenario_{str(scenario_id)}", "output"
)
input_dir = os.path.join(const.EXECUTE_DIR, f"scenario_{scenario_id}")
execute_dir = os.path.join(input_dir, "output")

# Grab start and end date for scenario
start_date = scenario_info["start_date"]
Expand Down