From cff1c211c5343ee9eb5b56d073923b53f72d1523 Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Tue, 28 Nov 2023 12:23:16 +0000 Subject: [PATCH 1/8] Use minutes for timestamp --- datahub/opal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datahub/opal.py b/datahub/opal.py index 4464c85..878b7db 100644 --- a/datahub/opal.py +++ b/datahub/opal.py @@ -166,6 +166,6 @@ def get_opal_row(data: dict[str, int | float] | list[int | float]) -> pd.DataFra row = pd.DataFrame( [data_array], index=[data_index], columns=list(opal_headers.keys()) ) - row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="S") + row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="m") return row From 28e30b94127d5bb9456c61d872a74acaf33c4f5d Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 17:46:54 +0000 Subject: [PATCH 2/8] GET method for start signal --- datahub/data.py | 3 +++ datahub/main.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/datahub/data.py b/datahub/data.py index ea0b2b2..8e008e4 100644 --- a/datahub/data.py +++ b/datahub/data.py @@ -7,3 +7,6 @@ opal_df: pd.DataFrame = create_opal_frame() dsr_data: list[dict[str, NDArray | str]] = [] # type: ignore[type-arg] wesim_data: dict[str, dict] = {} # type: ignore[type-arg] + +model_running: bool = False +model_resetting: bool = False diff --git a/datahub/main.py b/datahub/main.py index 49078d3..2bff1fb 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -245,3 +245,19 @@ def get_wesim_data() -> dict[str, dict[str, dict]]: # type: ignore[type-arg] dt.wesim_data = get_wesim() return {"data": dt.wesim_data} + + +@app.get("/start") +def get_start_signal() -> bool: + """GET method function for getting start model signal. + + It returns a boolean: True for if the model should start running. + + \f + + Returns: + A bool flag for if the model should start + """ # noqa: D301 + log.info("Start signal requested") + + return dt.model_running and not dt.model_resetting From 023e96ddf0a3adc671aa143d893ab7db67865d5d Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 18:07:24 +0000 Subject: [PATCH 3/8] POST request to start/stop the model from vis system --- datahub/main.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/datahub/main.py b/datahub/main.py index 2bff1fb..f20dae3 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -247,6 +247,30 @@ def get_wesim_data() -> dict[str, dict[str, dict]]: # type: ignore[type-arg] return {"data": dt.wesim_data} +@app.post("/run_model") +def set_model_signals(start: bool) -> str: + """POST method function for setting start model signal. + + It the query parameter: + - `start`: A boolean to indicate the model should start running or stop running. + + \f + + Args: + start: A bool flag for if the model should start running or stop running. + True to start the model, False to stop the model. + + Returns: + A confirmation message + """ # noqa: D301 + message = "Start signal received" if start else "Stop signal received" + log.info(message) + dt.model_running = start + dt.model_resetting = not start + + return message + + @app.get("/start") def get_start_signal() -> bool: """GET method function for getting start model signal. From 50a369159df36ce606eb1232867bddf9e0bc861e Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 18:15:20 +0000 Subject: [PATCH 4/8] GET request to stop model --- datahub/main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/datahub/main.py b/datahub/main.py index f20dae3..25a859d 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -285,3 +285,19 @@ def get_start_signal() -> bool: log.info("Start signal requested") return dt.model_running and not dt.model_resetting + + +@app.get("/stop") +def get_stop_signal() -> bool: + """GET method function for getting stop model signal. + + It returns a boolean: True for if the model should stop running. + + \f + + Returns: + A bool flag for if the model should stop + """ # noqa: D301 + log.info("Start signal requested") + + return not dt.model_running From 44d42b7497558e467b555d9062bfb71e88998749 Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 18:22:26 +0000 Subject: [PATCH 5/8] Create reset_data function --- datahub/data.py | 9 +++++++++ tests/test_dsr_api.py | 2 +- tests/test_opal_api.py | 4 +--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/datahub/data.py b/datahub/data.py index 8e008e4..232a18c 100644 --- a/datahub/data.py +++ b/datahub/data.py @@ -10,3 +10,12 @@ model_running: bool = False model_resetting: bool = False + + +def reset_data() -> None: + """Reset the OPAL and DSR data to their initial (empty) values.""" + global opal_df + global dsr_data + + opal_df = create_opal_frame() + dsr_data = [] diff --git a/tests/test_dsr_api.py b/tests/test_dsr_api.py index 057a9f8..768f1d4 100644 --- a/tests/test_dsr_api.py +++ b/tests/test_dsr_api.py @@ -12,7 +12,7 @@ @pytest.fixture(autouse=True) def reset_dsr_data(): """Pytest Fixture for resetting DSR data global variable.""" - dt.dsr_data = [] + dt.reset_data() def test_post_dsr_api(dsr_data_path): diff --git a/tests/test_opal_api.py b/tests/test_opal_api.py index 3367e56..e15a26c 100644 --- a/tests/test_opal_api.py +++ b/tests/test_opal_api.py @@ -9,9 +9,7 @@ @pytest.fixture(autouse=True) def reset_opal_data(): """Pytest Fixture for resetting Opal data global variable.""" - from datahub.opal import create_opal_frame - - dt.opal_df = create_opal_frame() + dt.reset_data() def test_post_opal_api(client, opal_data): From 1ab38f966be8b42bb1d40e8f80dbda9cbc20f53a Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 18:30:16 +0000 Subject: [PATCH 6/8] POST function for indicating model is ready --- datahub/main.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/datahub/main.py b/datahub/main.py index 25a859d..326393b 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -247,11 +247,11 @@ def get_wesim_data() -> dict[str, dict[str, dict]]: # type: ignore[type-arg] return {"data": dt.wesim_data} -@app.post("/run_model") +@app.post("/set_model_signals") def set_model_signals(start: bool) -> str: - """POST method function for setting start model signal. + """POST method function for setting start and stop model signals. - It the query parameter: + It has the query parameter: - `start`: A boolean to indicate the model should start running or stop running. \f @@ -271,6 +271,28 @@ def set_model_signals(start: bool) -> str: return message +@app.post("/model_ready") +def signal_model_ready(ready: bool) -> str: + """POST method function for indicating when the model has reset and is ready to run. + + It has the query parameter: + - `ready`: A boolean to indicate the model has completed setup and is ready. + + \f + + Args: + ready: A bool flag for if the model has completed setup and is ready. + + Returns: + A confirmation message + """ # noqa: D301 + message = "Ready signal received" if ready else "Not-Ready signal received" + log.info(message) + dt.model_resetting = not ready + + return message + + @app.get("/start") def get_start_signal() -> bool: """GET method function for getting start model signal. From 38f6edc64a26bd91baecdfaa5eb876ffe05de159 Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Wed, 29 Nov 2023 18:33:10 +0000 Subject: [PATCH 7/8] Do not set the model_resetting flag with the start post --- datahub/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/datahub/main.py b/datahub/main.py index 326393b..2364fa6 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -266,7 +266,6 @@ def set_model_signals(start: bool) -> str: message = "Start signal received" if start else "Stop signal received" log.info(message) dt.model_running = start - dt.model_resetting = not start return message From dc6e6657b79ac2c4f53e897dd61165e9856d795e Mon Sep 17 00:00:00 2001 From: Adrian D'Alessandro Date: Thu, 30 Nov 2023 09:33:41 +0000 Subject: [PATCH 8/8] Use minutes resolution in test opal --- tests/test_opal.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_opal.py b/tests/test_opal.py index f0deb9c..e483cf9 100644 --- a/tests/test_opal.py +++ b/tests/test_opal.py @@ -44,10 +44,10 @@ def test_append_opal_data(opal_data): # Checks that data appended to Dataframe matches data input. data_1["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_1["time"], unit="S" + data_1["time"], unit="m" ) data_2["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_2["time"], unit="S" + data_2["time"], unit="m" ) assert (df.loc[1] == list(data_1.values())[1:]).all() @@ -58,7 +58,7 @@ def test_append_opal_data(opal_data): assert len(df.index) == 2 data_3["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_3["time"], unit="S" + data_3["time"], unit="m" ) assert (df.loc[2] == list(data_3.values())[1:]).all() @@ -100,6 +100,6 @@ def test_append_opal_data_array(opal_data_array): assert df.shape == (1, len(opal_headers)) - data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="S") + data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="m") assert (df.loc[1] == data_1[1:]).all()