Skip to content

Commit

Permalink
Support reporting expected models (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger authored Feb 18, 2020
1 parent 8102387 commit 0e7549c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions gordo/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import subprocess
from functools import wraps


import yaml
from flask import Flask, g, request, current_app, make_response, jsonify
from gordo.server import views
from gordo import __version__
Expand All @@ -25,7 +25,9 @@
class Config:
"""Server config"""

MODEL_COLLECTION_DIR_ENV_VAR = "MODEL_COLLECTION_DIR"
def __init__(self):
self.MODEL_COLLECTION_DIR_ENV_VAR = "MODEL_COLLECTION_DIR"
self.EXPECTED_MODELS = yaml.safe_load(os.getenv("EXPECTED_MODELS", "[]"))


def adapt_proxy_deployment(wsgi_app: typing.Callable) -> typing.Callable:
Expand Down
7 changes: 7 additions & 0 deletions gordo/server/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,14 @@ def get(self, gordo_project: str):
)


class ExpectedModels(Resource):
@api.doc(description="Models that the server expects to be able to serve.")
def get(self, gordo_project: str):
return jsonify({"expected-models": current_app.config["EXPECTED_MODELS"]})


api.add_resource(ModelListView, "/gordo/v0/<gordo_project>/models")
api.add_resource(ExpectedModels, "/gordo/v0/<gordo_project>/expected-models")
api.add_resource(BaseModelView, "/gordo/v0/<gordo_project>/<gordo_name>/prediction")
api.add_resource(
MetaDataView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,8 @@ spec:
value: /gordo/models/{{project_name}}/{{project_revision}}
- name: GORDO_LOG_LEVEL
value: "{{log_level}}"
- name: EXPECTED_MODELS
value: [{% for machine in machines %}"{{ machine.name }}",{% endfor %}]

resources:
requests:
Expand Down
17 changes: 17 additions & 0 deletions tests/gordo/server/test_gordo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,20 @@ def test_non_existant_model_metadata(tmpdir, gordo_project, api_version):
f"/gordo/{api_version}/{gordo_project}/model-does-not-exist/metadata"
)
assert resp.status_code == 404


def test_expected_models_route(tmpdir):
"""
Route that gives back the expected models names, which are just read from
the 'EXPECTED_MODELS' env var.
"""
with tu.temp_env_vars(
MODEL_COLLECTION_DIR=str(tmpdir),
EXPECTED_MODELS=json.dumps(["model-a", "model-b"]),
):
app = server.build_app()
app.testing = True
client = app.test_client()

resp = client.get("/gordo/v0/test-project/expected-models")
assert resp.json["expected-models"] == ["model-a", "model-b"]
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,23 @@ def test_log_level_key(test_file: str, log_level: str, path_to_config_files: str

# Assert all the values to the GORDO_LOG_LEVEL key contains the correct log-level
assert all([log_level in value for value in gordo_log_levels])


def test_expected_models_in_workflow(repo_dir):
"""
Server deployment depends on EXPECTED_MODELS env var being set,
which is a list of strings, indicating the expected model names to be served.
"""
workflow_str = _generate_test_workflow_str(
path_to_config_files=os.path.join(repo_dir, "examples"),
config_filename="config.yaml",
)
assert "name: EXPECTED_MODELS" in workflow_str

# Not the prettiest, but a whole lot prettier than digging down into the workflow yaml
# basically want to get to 'gordo-server-deployment' and ensure the EXPECTED_MODELS env var
# is set with a list (in string form) of model names which can be loaded.
expected_models_str = (
workflow_str.split("EXPECTED_MODELS")[1].split("value:")[1].split("\n")[0]
)
assert isinstance(yaml.safe_load(expected_models_str), list)

0 comments on commit 0e7549c

Please sign in to comment.