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

feat: Added a new GCBM status endpoint #180

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion local/rest_api_gcbm/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from flask import jsonify
from config_table import rename_columns
import sqlite3
import docker
from preprocess import get_config_templates, get_modules_cbm_config, get_provider_config

flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
Expand Down Expand Up @@ -490,7 +491,7 @@ def gcbm_list_simulations():
}, 200


@app.route("/gcbm/status", methods=["POST"])
@app.route("/gcbm/report", methods=["POST"])
def status():
"""
Get status of a simulation
Expand Down Expand Up @@ -730,5 +731,35 @@ def gcbm_db():
return {"data": "db file uploaded succesfully. Proceed to the next step."}


def checkDockerContainerStatus(container):
client = docker.from_env()
# cli = docker.APIClient()
if client.containers.list(filters={"name": container}):
response = client.containers.list(filters={"name": container})
return str(response[0].id)[:12]
else:
return None


@app.route("/gcbm/status", methods=["GET"])
def get_simulation_status():
"""
If the container gcbm-api is running, the associated logs are returned
"""
container_id = checkDockerContainerStatus("flint.gcbm")
logs_file_name = container_id + ".txt"
if container_id != "":
logs_cmd = "docker logs " + container_id + " > " + logs_file_name + " 2>&1"
os.system(logs_cmd)
logs = []
with open(logs_file_name) as file_handle:
logs = file_handle.readlines()
# os.system('rm ' + logs_file_name)
return {"container_running": True, "logs": logs}, 200
else:
logs = ["Error No Running Container"]
return {"container_running": False, "logs": logs}, 400


if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
1 change: 1 addition & 0 deletions local/rest_api_gcbm/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ psutil==5.9.0
pandas
matplotlib
rasterio
docker==6.0.0
3 changes: 2 additions & 1 deletion local/tests/test_rest_gcbm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from distutils.command.upload import upload
import re
from pkg_resources import yield_lines
import pytest
import requests
Expand Down Expand Up @@ -296,7 +297,7 @@ def test_status(self, gcbm_endpoint, yield_title):
yield_title fixture. It returns the status of a simulation which \
we already uploaded the files under the title by \
yield_title fixutre. """
status_endpoint = gcbm_endpoint + "status"
status_endpoint = gcbm_endpoint + "report"
data = {"title": yield_title}
status_response = requests.post(status_endpoint, json=data)
assert status_response.status_code == 200
Expand Down