From 5a685db2bef1f32e36edaff9c819a541e49abc1f Mon Sep 17 00:00:00 2001 From: Punam Verma Date: Tue, 24 Oct 2023 10:25:41 -0700 Subject: [PATCH] [monitorlib] Retrieve interactions from mock_uss (#270) * Recording interuss interactions from mock_uss * Fixed format * Fixing format * Removing import of non-exisitng module * Removing a file that should go in with scenario PR * Removing interaction recording in test steps as per PR review --- .../mock_uss/interaction_logging/logger.py | 2 +- .../routes_interactions_log.py | 24 +++++------ monitoring/uss_qualifier/reports/report.py | 2 + .../resources/interuss/mock_uss/client.py | 42 +++++++++++++++++++ .../scenarios/astm/utm/interuss/__init__.py | 1 + .../uss_qualifier/scenarios/scenario.py | 1 - 6 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 monitoring/uss_qualifier/scenarios/astm/utm/interuss/__init__.py diff --git a/monitoring/mock_uss/interaction_logging/logger.py b/monitoring/mock_uss/interaction_logging/logger.py index 106b4a2399..78f090541f 100644 --- a/monitoring/mock_uss/interaction_logging/logger.py +++ b/monitoring/mock_uss/interaction_logging/logger.py @@ -66,7 +66,7 @@ def interaction_log_after_request(response): datetime.datetime.utcnow() - flask.current_app.custom_profiler["start"] ).total_seconds() # TODO: Make this configurable instead of hardcoding exactly these query types - if flask.request.url_rule is not None and "/uss/v1/" in flask.request.url_rule.rule: + if "/uss/v1/" in flask.request.url: query = describe_flask_query(flask.request, response, elapsed_s) log_interaction(QueryDirection.Incoming, query) return response diff --git a/monitoring/mock_uss/interaction_logging/routes_interactions_log.py b/monitoring/mock_uss/interaction_logging/routes_interactions_log.py index 9a0e6fc6ec..739858fa50 100644 --- a/monitoring/mock_uss/interaction_logging/routes_interactions_log.py +++ b/monitoring/mock_uss/interaction_logging/routes_interactions_log.py @@ -39,18 +39,18 @@ def interaction_logs() -> Tuple[str, int]: try: obj = json.load(f) interaction = ImplicitDict.parse(obj, Interaction) - if ( - ("received_at" in interaction.query.request) - and interaction.query.request.received_at.datetime - >= from_time.datetime - ): - interactions.append(interaction) - elif ( - "initiated_at" in interaction.query.request - and interaction.query.request.initiated_at.datetime - >= from_time.datetime - ): - interactions.append(interaction) + if "received_at" in interaction.query.request: + if ( + interaction.query.request.received_at.datetime + >= from_time.datetime + ): + interactions.append(interaction) + elif "initiated_at" in interaction.query.request: + if ( + interaction.query.request.initiated_at.datetime + >= from_time.datetime + ): + interactions.append(interaction) else: raise ValueError( f"There is no received_at or initiated_at field in the request in {fname}" diff --git a/monitoring/uss_qualifier/reports/report.py b/monitoring/uss_qualifier/reports/report.py index 562f3f6d6f..b9e6ad8ed6 100644 --- a/monitoring/uss_qualifier/reports/report.py +++ b/monitoring/uss_qualifier/reports/report.py @@ -22,6 +22,8 @@ from monitoring.uss_qualifier.scenarios.definitions import TestScenarioTypeName from monitoring.uss_qualifier.suites.definitions import TestSuiteActionDeclaration +from monitoring.mock_uss.interaction_logging.interactions import Interaction + class FailedCheck(ImplicitDict): name: str diff --git a/monitoring/uss_qualifier/resources/interuss/mock_uss/client.py b/monitoring/uss_qualifier/resources/interuss/mock_uss/client.py index 2c6fd7f534..53c7a104e7 100644 --- a/monitoring/uss_qualifier/resources/interuss/mock_uss/client.py +++ b/monitoring/uss_qualifier/resources/interuss/mock_uss/client.py @@ -1,5 +1,6 @@ from typing import Tuple, Optional, List +from loguru import logger from implicitdict import ImplicitDict from monitoring.monitorlib import fetch @@ -7,6 +8,7 @@ GetLocalityResponse, PutLocalityRequest, ) +from monitoring.monitorlib.fetch import QueryError, Query from monitoring.monitorlib.infrastructure import AuthAdapter, UTMClientSession from monitoring.monitorlib.locality import LocalityCode from monitoring.monitorlib.scd_automated_testing.scd_injection_api import ( @@ -15,6 +17,12 @@ from monitoring.uss_qualifier.reports.report import ParticipantID from monitoring.uss_qualifier.resources.communications import AuthAdapterResource from monitoring.uss_qualifier.resources.resource import Resource +from monitoring.mock_uss.interaction_logging.interactions import ( + Interaction, + ListLogsResponse, +) +from typing import Tuple, List +from implicitdict import StringBasedDateTime MOCK_USS_CONFIG_SCOPE = "interuss.mock_uss.configure" @@ -29,6 +37,7 @@ def __init__( base_url: str, auth_adapter: AuthAdapter, ): + self.base_url = base_url self.session = UTMClientSession(base_url, auth_adapter) self.participant_id = participant_id @@ -68,6 +77,39 @@ def set_locality(self, locality_code: LocalityCode) -> fetch.Query: # TODO: Add other methods to interact with the mock USS in other ways (like starting/stopping message signing data collection) + def get_interactions(self, from_time: StringBasedDateTime) -> List[Interaction]: + """ + Requesting interuss interactions from mock_uss from a given time till now + Args: + from_time: the time from which the interactions are requested + Returns: + List of Interactions + """ + url = "{}/mock_uss/interuss_logging/logs?from_time={}".format( + self.base_url, from_time + ) + logger.debug(f"Getting interactions from {from_time} : {url}") + query = fetch.query_and_describe( + self.session, "GET", url, scope=SCOPE_SCD_QUALIFIER_INJECT + ) + if query.status_code != 200: + raise QueryError( + f"Request to mock uss {url} returned a {query.status_code} ", [query] + ) + try: + response = ImplicitDict.parse(query.response.get("json"), ListLogsResponse) + except KeyError: + raise QueryError( + msg=f"RecordedInteractionsResponse from mock_uss response did not contain JSON body", + queries=[query], + ) + except ValueError as e: + raise QueryError( + msg=f"RecordedInteractionsResponse from mock_uss response contained invalid JSON: {str(e)}", + queries=[query], + ) + return response.interactions + class MockUSSSpecification(ImplicitDict): mock_uss_base_url: str diff --git a/monitoring/uss_qualifier/scenarios/astm/utm/interuss/__init__.py b/monitoring/uss_qualifier/scenarios/astm/utm/interuss/__init__.py new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/monitoring/uss_qualifier/scenarios/astm/utm/interuss/__init__.py @@ -0,0 +1 @@ + diff --git a/monitoring/uss_qualifier/scenarios/scenario.py b/monitoring/uss_qualifier/scenarios/scenario.py index da1bbda238..30db6314d6 100644 --- a/monitoring/uss_qualifier/scenarios/scenario.py +++ b/monitoring/uss_qualifier/scenarios/scenario.py @@ -4,7 +4,6 @@ from enum import Enum import inspect from typing import Callable, Dict, List, Optional, TypeVar, Union, Set, Type - import arrow from implicitdict import StringBasedDateTime from loguru import logger