Skip to content

Commit

Permalink
workflows in one place working
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkTNO committed Jun 21, 2024
1 parent 762fd3c commit e858011
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 49 deletions.
16 changes: 14 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --constraint=requirements.txt --extra=dev --output-file=dev-requirements.txt pyproject.toml
Expand Down Expand Up @@ -63,6 +63,8 @@ colorama==0.4.6
# omotes-rest (pyproject.toml)
coverage[toml]==7.4.4
# via pytest-cov
exceptiongroup==1.2.1
# via pytest
flake8==6.0.0
# via
# flake8-docstrings
Expand Down Expand Up @@ -254,15 +256,25 @@ structlog==23.1.0
# via
# -c requirements.txt
# omotes-rest (pyproject.toml)
toml==0.10.2
# via setuptools-git-versioning
tomli==2.0.1
# via black
# via
# black
# build
# coverage
# flake8-pyproject
# mypy
# pyproject-hooks
# pytest
types-flask-cors==4.0.0.20240405
# via omotes-rest (pyproject.toml)
types-protobuf==4.24.0.20240311
# via omotes-rest (pyproject.toml)
typing-extensions==4.7.1
# via
# -c requirements.txt
# marshmallow-dataclass
# mypy
# sqlalchemy
# typing-inspect
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:

omotes-rest:
# build: .
image: ghcr.io/project-omotes/omotes_rest:0.0.8
image: ghcr.io/project-omotes/omotes_rest:0.0.9
networks:
- omotes
- mapeditor-net
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --output-file=requirements.txt pyproject.toml
Expand Down Expand Up @@ -116,6 +116,7 @@ structlog==23.1.0
# via omotes-rest (pyproject.toml)
typing-extensions==4.7.1
# via
# marshmallow-dataclass
# sqlalchemy
# typing-inspect
typing-inspect==0.9.0
Expand Down
2 changes: 2 additions & 0 deletions src/omotes_rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def create_app(object_name: str) -> Flask:

# Register blueprints.
from omotes_rest.apis.job import api as job_api
from omotes_rest.apis.workflow import api as workflow_api

api.register_blueprint(job_api)
api.register_blueprint(workflow_api)

CORS(app, resources={r"/*": {"origins": "*"}})

Expand Down
12 changes: 12 additions & 0 deletions src/omotes_rest/apis/api_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
from marshmallow_dataclass import add_schema, dataclass


@add_schema
@dataclass
class WorkflowResponse:
"""Response with available workflows."""

Schema: ClassVar[Type[Schema]] = Schema

workflow_type_name: str
workflow_type_description_name: str
workflow_parameters: dict[str, Any] | None


class JobRestStatus(Enum):
"""Possible job status."""

Expand Down
26 changes: 26 additions & 0 deletions src/omotes_rest/apis/workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging

from flask_smorest import Blueprint
from flask.views import MethodView
from flask import jsonify, Response

from omotes_rest.typed_app import current_app


logger = logging.getLogger("omotes_rest")

api = Blueprint(
"Workflow",
"Workflow",
url_prefix="/workflow",
description="Omotes workflows: retrieve the available workflow and their properties",
)


@api.route("/")
class JobAPI(MethodView):
"""Requests."""

def get(self) -> Response:
"""Return a summary of all jobs."""
return jsonify(current_app.rest_if.get_available_workflows_dict())
2 changes: 1 addition & 1 deletion src/omotes_rest/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os


class POSTGRESConfig:
class PostgresConfig:
"""Retrieve POSTGRES configuration from environment variables."""

host: str
Expand Down
3 changes: 1 addition & 2 deletions src/omotes_rest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from omotes_rest import create_app
from omotes_rest.rest_interface import RestInterface
from omotes_rest.settings import EnvSettings
from omotes_rest.workflows import WORKFLOW_TYPE_MANAGER
from omotes_rest.typed_app import current_app


Expand Down Expand Up @@ -87,7 +86,7 @@ def post_fork(_: Arbiter, __: SyncWorker) -> None:
with app.app_context():
"""current_app is only within the app context"""

current_app.rest_if = RestInterface(WORKFLOW_TYPE_MANAGER)
current_app.rest_if = RestInterface()
"""Interface for this Omotes Rest service."""

current_app.rest_if.start()
Expand Down
8 changes: 4 additions & 4 deletions src/omotes_rest/postgres_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
from omotes_rest.apis.api_dataclasses import JobRestStatus, JobInput
from omotes_rest.db_models.job_rest import JobRest
from omotes_rest.config import POSTGRESConfig
from omotes_rest.config import PostgresConfig

logger = logging.getLogger("omotes_rest")

Expand Down Expand Up @@ -60,7 +60,7 @@ def session_scope(do_expunge: bool = False) -> Generator[SQLSession, None, None]
Session.remove()


def initialize_db(application_name: str, config: POSTGRESConfig) -> Engine:
def initialize_db(application_name: str, config: PostgresConfig) -> Engine:
"""Initialize the database connection by creating the engine.
Also configure the default session maker.
Expand Down Expand Up @@ -112,12 +112,12 @@ class PostgresInterface:
in this interface must set up a Session (scope) separately.
"""

db_config: POSTGRESConfig
db_config: PostgresConfig
"""Configuration on how to connect to the database."""
engine: Engine
"""Engine for starting connections to the database."""

def __init__(self, postgres_config: POSTGRESConfig) -> None:
def __init__(self, postgres_config: PostgresConfig) -> None:
"""Create the PostgreSQL interface."""
self.db_config = postgres_config

Expand Down
37 changes: 28 additions & 9 deletions src/omotes_rest/rest_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
JobResult,
JobProgressUpdate,
JobStatusUpdate,
AvailableWorkflows,
)
from omotes_sdk.workflow_type import WorkflowTypeManager

import logging
from omotes_rest.postgres_interface import PostgresInterface
from omotes_rest.config import POSTGRESConfig
from omotes_rest.config import PostgresConfig
from omotes_rest.apis.api_dataclasses import JobInput, JobStatusResponse
from omotes_rest.db_models.job_rest import JobRestStatus, JobRest
from omotes_rest.workflows import FRONTEND_NAME_TO_OMOTES_WORKFLOW_NAME
Expand All @@ -32,14 +33,15 @@ class RestInterface:
workflow_type_manager: WorkflowTypeManager
"""Interface to Omotes."""

def __init__(self, workflow_type_manager: WorkflowTypeManager):
"""Create the omotes rest interface.
:param workflow_type_manager: All available OMOTES workflow types.
"""
self.omotes_if = OmotesInterface(EnvRabbitMQConfig(), workflow_type_manager)
self.postgres_if = PostgresInterface(POSTGRESConfig())
self.workflow_type_manager = workflow_type_manager
def __init__(
self,
) -> None:
"""Create the omotes rest interface."""
self.omotes_if = OmotesInterface(

Check failure on line 40 in src/omotes_rest/rest_interface.py

View workflow job for this annotation

GitHub Actions / Typecheck (3.11)

error: Unexpected keyword argument "callback_on_available_workflows_update" for "OmotesInterface" [call-arg]
EnvRabbitMQConfig(),
callback_on_available_workflows_update=self.handle_on_available_workflows_update,
)
self.postgres_if = PostgresInterface(PostgresConfig())

def start(self) -> None:
"""Start the omotes rest interface."""
Expand All @@ -50,6 +52,16 @@ def stop(self) -> None:
"""Stop the omotes rest interface."""
self.omotes_if.stop()

def handle_on_available_workflows_update(
self, available_workflows_pb: AvailableWorkflows
) -> None:
"""When the available workflows are updated.
:param available_workflows_pb: AvailableWorkflows protobuf message.
"""
self.workflow_type_manager = WorkflowTypeManager.from_pb_message(available_workflows_pb)

Check failure on line 62 in src/omotes_rest/rest_interface.py

View workflow job for this annotation

GitHub Actions / Typecheck (3.11)

error: "type[WorkflowTypeManager]" has no attribute "from_pb_message" [attr-defined]
logger.info("Updated the available workflows to: \n%s", self.workflow_type_manager)

def handle_on_job_finished(self, job: Job, result: JobResult) -> None:
"""When a job is finished.
Expand Down Expand Up @@ -108,6 +120,13 @@ def handle_on_job_progress_update(self, job: Job, progress_update: JobProgressUp
progress_message=progress_update.message,
)

def get_available_workflows_dict(self) -> dict:
"""Get the available workflows represented as dictionary.
:return: dictionary response.
"""
return self.workflow_type_manager.to_dict()

Check failure on line 128 in src/omotes_rest/rest_interface.py

View workflow job for this annotation

GitHub Actions / Typecheck (3.11)

error: Returning Any from function declared to return "dict[Any, Any]" [no-any-return]

Check failure on line 128 in src/omotes_rest/rest_interface.py

View workflow job for this annotation

GitHub Actions / Typecheck (3.11)

error: "WorkflowTypeManager" has no attribute "to_dict" [attr-defined]

def submit_job(self, job_input: JobInput) -> JobStatusResponse:
"""When a job has a progress update.
Expand Down
27 changes: 0 additions & 27 deletions src/omotes_rest/workflows.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
from omotes_sdk.workflow_type import WorkflowTypeManager, WorkflowType

# TODO to be retrieved via de omotes_sdk in the future
WORKFLOW_TYPE_MANAGER = WorkflowTypeManager(
possible_workflows=[
WorkflowType(
workflow_type_name="grow_optimizer_default",
workflow_type_description_name="Grow Optimizer",
),
WorkflowType(
workflow_type_name="grow_simulator", workflow_type_description_name="Grow Simulator"
),
WorkflowType(
workflow_type_name="grow_optimizer_no_heat_losses",
workflow_type_description_name="Grow Optimizer without heat losses",
),
WorkflowType(
workflow_type_name="grow_optimizer_with_pressure",
workflow_type_description_name="Grow Optimizer with pressure drops",
),
WorkflowType(
workflow_type_name="simulator",
workflow_type_description_name="High fidelity simulator",
),
]
)

FRONTEND_NAME_TO_OMOTES_WORKFLOW_NAME = {
"Draft Design - Quickscan Validation": "grow_optimizer_no_heat_losses",
"Draft Design - Optimization": "grow_optimizer_default",
Expand Down
4 changes: 2 additions & 2 deletions unit_test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import unittest
from omotes_rest.config import POSTGRESConfig
from omotes_rest.config import PostgresConfig


class MyTest(unittest.TestCase):
def test__construct_postgres_config__no_exception(self) -> None:
# Arrange

# Act
result = POSTGRESConfig()
result = PostgresConfig()

# Assert
self.assertIsNotNone(result)

0 comments on commit e858011

Please sign in to comment.