Skip to content
Snippets Groups Projects
Commit f117aed4 authored by Charlotte Hausman's avatar Charlotte Hausman
Browse files

clean up aat_wrest code

parent 825fd065
No related branches found
No related tags found
1 merge request!752clean up aat_wrest code
Pipeline #4172 passed
......@@ -79,55 +79,3 @@ class WrestObservationMetadata:
finally:
self.conn.close()
return metadata
#
# currently unused. Was intended for analyst list display of waiting capability requests/executions
#
def wrest_observation_time_info(self) -> json:
"""
Given a product locator, reports the project code, observation length,
and time in queue for an incomplete observation.
:return:
"""
sql = """
SELECT project_code, mjd_to_timestamp(starttime), mjd_to_timestamp(endtime)
FROM execution_blocks
WHERE science_product_locator = %(spl)s
"""
make_json = {}
try:
cursor = self.conn.cursor()
cursor.execute(sql, {"spl": self.spl})
data = cursor.fetchall()
if data:
current_time = pendulum.now().in_timezone(TIME_ZONE)
start_time = data[0][1]
end_time_raw = data[0][2]
pend_endtime = pendulum.instance(end_time_raw)
print(pend_endtime)
print(current_time)
since = current_time.diff(pend_endtime).as_interval()
print(since)
make_json = json.dumps(
{
"projectCode": data[0][0],
"timeObserved": start_time,
"timeInQueue": format_interval(since),
},
default=str,
)
else:
self.logger.error(
f"ERROR: Query returned no results!"
f" The database appears to be missing information for SPL {self.spl}!"
)
finally:
self.conn.close()
return make_json
......@@ -42,7 +42,8 @@ class MDDBConnector:
def __init__(self):
self.connection = self._connect_to_mddb()
def _connect_to_mddb(self) -> Connection:
@staticmethod
def _connect_to_mddb() -> Connection:
"""
Establish a DB connection
......@@ -65,7 +66,7 @@ class MDDBConnector:
)
return conn
except Exception as exc:
print(f"Unable to connect to database: {exc}")
logger.error(f"Unable to connect to database: {exc}")
raise exc
def cursor(self):
......
#
# Copyright (C) 2021 Associated Universities, Inc. Washington DC, USA.
#
# This file is part of NRAO Workspaces.
#
# Workspaces is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Workspaces is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Workspaces. If not, see <https://www.gnu.org/licenses/>.
CONNECT_MOCK = "aat_wrest.utilities.MDDBConnector._connect_to_mddb"
WREST_INFO_MOCK = "aat_wrest.observation_wrester.ObservationWrester.wrest_observation_info"
......@@ -22,35 +22,11 @@ Tests for aat_wrest.observation_wrester
import argparse
# pylint: disable=E0401, E0402
import json
from enum import Enum
from unittest.mock import MagicMock, patch
import pytest
from aat_wrest.observation_wrester import WrestObservationMetadata
class Keys(Enum):
"""The things we're reporting"""
PROJECT = "projectCode"
OBS_TIME = "timeObserved"
QUEUE_TIME = "timeInQueue"
PROJECT = Keys.PROJECT.value
OBS_TIME = Keys.OBS_TIME.value
QUEUE_TIME = Keys.QUEUE_TIME.value
_17A_109_SPL = "uid://evla/execblock/5c71ade0-d035-4fd5-a36f-0389e34db0e5"
_17A_109_ARGS = argparse.Namespace(spl=_17A_109_SPL)
_17A_109_EXPECTED = {
PROJECT: "17A-109",
OBS_TIME: "0 days, 0 hours, 4 minutes, 34 seconds",
QUEUE_TIME: "1125 days, 11 hours, 43 minutes, 58 seconds",
}
args = argparse.Namespace(observation="17B-197.sb34663512.eb34806505.58108.78427738426")
result_obs = (
......@@ -100,38 +76,3 @@ class TestWrestObservationInfo:
'"obs_end_time": "123598898", '
'"is_srdp": "True" }'
)
@pytest.mark.skip("Dates are broken. Method superseded by wrest_observation_info")
@patch("json.dumps", MagicMock(return_value=_17A_109_EXPECTED))
def test_gets_expected_observation_info(self):
"""
Does ObservationWrester wrest the expected data from the MDDB?
:return:
"""
wrester = mock_wrester(_17A_109_ARGS)
assert wrester.spl == _17A_109_SPL
actual = wrester.wrest_observation_time_info()
assert actual == _17A_109_EXPECTED
@pytest.mark.skip("... Dates are broken... Method superseded by wrest_observation_info")
def test_handles_evla_exec_block(self):
"""
Confirm we get what we expect when observing time is minimal.
:return:
"""
spl = "uid://evla/execblock/91c685b6-4527-44b1-9f91-3904e1125817"
args = argparse.Namespace(spl=spl)
expected = {
PROJECT: "19A-440",
OBS_TIME: "0 days, 0 hours, 0 minutes, 4 seconds",
QUEUE_TIME: "627 days, 22 hours, 43 minutes, 6 seconds",
}
with patch("json.dumps", MagicMock(return_value=expected)):
wrester = mock_wrester(args)
actual = wrester.wrest_observation_time_info()
assert actual[PROJECT] == expected[PROJECT]
obs_parts = actual[OBS_TIME].split(", ")
seconds_part = obs_parts[3].split(" ")
assert int(seconds_part[0]) == 4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment