Skip to content

Commit

Permalink
[monitorlib] Retrieve interactions from mock_uss (interuss#270)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
punamverma authored Oct 24, 2023
1 parent 64b0c4a commit 5a685db
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion monitoring/mock_uss/interaction_logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions monitoring/mock_uss/interaction_logging/routes_interactions_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions monitoring/uss_qualifier/reports/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions monitoring/uss_qualifier/resources/interuss/mock_uss/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Tuple, Optional, List

from loguru import logger
from implicitdict import ImplicitDict

from monitoring.monitorlib import fetch
from monitoring.monitorlib.clients.mock_uss.locality import (
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 (
Expand All @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 0 additions & 1 deletion monitoring/uss_qualifier/scenarios/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a685db

Please sign in to comment.