Skip to content

Commit

Permalink
[monitorlib] Refactor fetch+mutate functions (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmis authored Jul 12, 2023
1 parent 9083360 commit eb1f657
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 108 deletions.
2 changes: 1 addition & 1 deletion monitoring/mock_uss/riddp/routes_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def riddp_display_data() -> Tuple[str, int]:
t = arrow.utcnow().datetime
isa_list: FetchedISAs = fetch.isas(view, t, t, rid_version, utm_client)
if not isa_list.success:
msg = f"Error fetching ISAs from DSS: {isa_list.error}"
msg = f"Error fetching ISAs from DSS: {isa_list.errors}"
logger.error(msg)
response = ErrorResponse(message=msg)
response["fetched_isas"] = isa_list
Expand Down
6 changes: 4 additions & 2 deletions monitoring/mock_uss/ridsp/routes_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from monitoring.mock_uss.ridsp import utm_client
from . import database
from .database import db

from monitoring.monitorlib import geo

require_config_value(KEY_BASE_URL)
require_config_value(KEY_RID_VERSION)
Expand Down Expand Up @@ -64,7 +64,9 @@ def ridsp_create_test(test_id: str) -> Tuple[str, int]:
f"Unable to determine base URL for RID version {rid_version}"
)
mutated_isa = mutate.put_isa(
area=rect,
area_vertices=geo.get_latlngrect_vertices(rect),
alt_lo=0,
alt_hi=3048,
start_time=t0,
end_time=t1,
uss_base_url=uss_base_url,
Expand Down
6 changes: 4 additions & 2 deletions monitoring/mock_uss/tracer/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TASK_POLL_CONSTRAINTS,
)
from monitoring.mock_uss.tracer.config import KEY_RID_VERSION
from monitoring.monitorlib import ids, versioning
from monitoring.monitorlib import ids, versioning, geo
from monitoring.monitorlib import fetch
import monitoring.monitorlib.fetch.rid
import monitoring.monitorlib.fetch.scd
Expand Down Expand Up @@ -141,7 +141,9 @@ def _subscribe_rid(resources: ResourceSet, uss_base_url: str) -> None:
_clear_existing_rid_subscription(resources, "old")

create_result = mutate.rid.upsert_subscription(
area=resources.area,
area_vertices=geo.get_latlngrect_vertices(resources.area),
alt_lo=0,
alt_hi=3048,
start_time=resources.start_time,
end_time=resources.end_time,
uss_base_url=uss_base_url,
Expand Down
66 changes: 29 additions & 37 deletions monitoring/monitorlib/fetch/rid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import yaml
from yaml.representer import Representer

from monitoring.monitorlib import fetch, rid_v1
from monitoring.monitorlib import fetch, rid_v1, rid_v2, geo
from monitoring.monitorlib.fetch import Query
from monitoring.monitorlib.infrastructure import UTMClientSession
from monitoring.monitorlib.rid import RIDVersion
Expand Down Expand Up @@ -346,6 +346,14 @@ def query(self) -> Query:
def status_code(self):
return self.query.status_code

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
raise NotImplementedError("RIDQuery.errors must be overriden")


class FetchedISAs(RIDQuery):
"""Version-independent representation of a list of F3411 identification service areas."""
Expand All @@ -369,33 +377,37 @@ def _v22a_response(
)

@property
def error(self) -> Optional[str]:
def errors(self) -> List[str]:
# Overall errors
if self.status_code != 200:
return f"Failed to search ISAs in DSS ({self.status_code})"
return [f"Failed to search ISAs in DSS ({self.status_code})"]

if self.query.response.json is None:
return "DSS response to search ISAs did not contain valid JSON"
return ["DSS response to search ISAs did not contain valid JSON"]

if self.rid_version == RIDVersion.f3411_19:
try:
if not self._v19_response:
return "Unknown error with F3411-19 SearchIdentificationServiceAreasResponse"
return [
"Unknown error with F3411-19 SearchIdentificationServiceAreasResponse"
]
except ValueError as e:
return f"Error parsing F3411-19 DSS SearchIdentificationServiceAreasResponse: {str(e)}"
return [
f"Error parsing F3411-19 DSS SearchIdentificationServiceAreasResponse: {str(e)}"
]

if self.rid_version == RIDVersion.f3411_22a:
try:
if not self._v22a_response:
return "Unknown error with F3411-22a SearchIdentificationServiceAreasResponse"
return [
"Unknown error with F3411-22a SearchIdentificationServiceAreasResponse"
]
except ValueError as e:
return f"Error parsing F3411-22a DSS SearchIdentificationServiceAreasResponse: {str(e)}"

return None
return [
f"Error parsing F3411-22a DSS SearchIdentificationServiceAreasResponse: {str(e)}"
]

@property
def success(self) -> bool:
return self.error is None
return []

@property
def isas(self) -> Dict[str, ISA]:
Expand Down Expand Up @@ -424,7 +436,7 @@ def flights_urls(self) -> Dict[str, str]:
def has_different_content_than(self, other: Any) -> bool:
if not isinstance(other, FetchedISAs):
return True
if self.error != other.error:
if self.errors != other.errors:
return True
if self.rid_version != other.rid_version:
return True
Expand All @@ -451,7 +463,7 @@ def isas(
t1 = rid_version.format_time(end_time)
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.SearchIdentificationServiceAreas]
area = rid_v1.geo_polygon_string(rid_v1.vertices_from_latlng_rect(box))
area = rid_v1.geo_polygon_string_from_s2(geo.get_latlngrect_vertices(box))
url = f"{dss_base_url}{op.path}?area={area}&earliest_time={t0}&latest_time={t1}"
return FetchedISAs(
v19_query=fetch.query_and_describe(
Expand All @@ -460,7 +472,7 @@ def isas(
)
elif rid_version == RIDVersion.f3411_22a:
op = v22a.api.OPERATIONS[v22a.api.OperationID.SearchIdentificationServiceAreas]
area = rid_v1.geo_polygon_string(rid_v1.vertices_from_latlng_rect(box))
area = rid_v2.geo_polygon_string_from_s2(geo.get_latlngrect_vertices(box))
url = f"{dss_base_url}{op.path}?area={area}&earliest_time={t0}&latest_time={t1}"
return FetchedISAs(
v22a_query=fetch.query_and_describe(
Expand Down Expand Up @@ -494,10 +506,6 @@ def _v22a_response(
v22a.api.GetFlightsResponse,
)

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
if self.status_code != 200:
Expand Down Expand Up @@ -611,10 +619,6 @@ def _v22a_response(
v22a.api.GetFlightDetailsResponse,
)

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
if self.status_code != 200:
Expand Down Expand Up @@ -700,14 +704,10 @@ class FetchedFlights(ImplicitDict):
uss_flight_queries: Dict[str, FetchedUSSFlights]
uss_flight_details_queries: Dict[str, FetchedUSSFlightDetails]

@property
def success(self):
return not self.errors

@property
def errors(self) -> List[str]:
if not self.dss_isa_query.success:
return ["Failed to obtain ISAs: " + self.dss_isa_query.error]
return self.dss_isa_query.errors
result = []
for flights in self.uss_flight_queries.values():
result.extend(flights.errors)
Expand Down Expand Up @@ -785,14 +785,6 @@ def _v22a_response(
v22a.api.GetSubscriptionResponse,
)

@property
def id(self) -> str:
return self.raw.id

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
if self.status_code == 404:
Expand Down
2 changes: 1 addition & 1 deletion monitoring/monitorlib/fetch/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def isas(fetched: rid.FetchedISAs) -> Dict:
isa_key = "{} ({})".format(isa.id, isa.owner)
summary[isa.flights_url][isa_key] = isa_summary
else:
summary["error"] = fetched.error
summary["error"] = fetched.errors
return summary


Expand Down
10 changes: 10 additions & 0 deletions monitoring/monitorlib/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ def bounding_rect(latlngs: List[Tuple[float, float]]) -> s2sphere.LatLngRect:
def get_latlngrect_diagonal_km(rect: s2sphere.LatLngRect) -> float:
"""Compute the distance in km between two opposite corners of the rect"""
return rect.lo().get_distance(rect.hi()).degrees * EARTH_CIRCUMFERENCE_KM / 360


def get_latlngrect_vertices(rect: s2sphere.LatLngRect) -> List[s2sphere.LatLng]:
"""Returns the rect as a list of vertices"""
return [
s2sphere.LatLng.from_angles(lat=rect.lat_lo(), lng=rect.lng_lo()),
s2sphere.LatLng.from_angles(lat=rect.lat_lo(), lng=rect.lng_hi()),
s2sphere.LatLng.from_angles(lat=rect.lat_hi(), lng=rect.lng_hi()),
s2sphere.LatLng.from_angles(lat=rect.lat_hi(), lng=rect.lng_lo()),
]
86 changes: 38 additions & 48 deletions monitoring/monitorlib/mutate/rid.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ def _v22a_response(self) -> v22a.api.PutSubscriptionResponse:
v22a.api.PutSubscriptionResponse,
)

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
if self.status_code != 200:
Expand Down Expand Up @@ -82,7 +78,9 @@ def subscription(self) -> Optional[Subscription]:


def upsert_subscription(
area: s2sphere.LatLngRect,
area_vertices: List[s2sphere.LatLng],
alt_lo: float,
alt_hi: float,
start_time: datetime.datetime,
end_time: datetime.datetime,
uss_base_url: str,
Expand All @@ -94,15 +92,13 @@ def upsert_subscription(
mutation = "create" if subscription_version is None else "update"
if rid_version == RIDVersion.f3411_19:
body = {
"extents": {
"spatial_volume": {
"footprint": {"vertices": rid_v1.vertices_from_latlng_rect(area)},
"altitude_lo": 0,
"altitude_hi": 3048,
},
"time_start": start_time.strftime(rid_v1.DATE_FORMAT),
"time_end": end_time.strftime(rid_v1.DATE_FORMAT),
},
"extents": rid_v1.make_volume_4d(
area_vertices,
alt_lo,
alt_hi,
start_time,
end_time,
),
"callbacks": {
"identification_service_area_url": uss_base_url
+ v19.api.OPERATIONS[
Expand All @@ -124,15 +120,13 @@ def upsert_subscription(
)
elif rid_version == RIDVersion.f3411_22a:
body = {
"extents": {
"volume": {
"outline_polygon": rid_v2.make_polygon_outline(area),
"altitude_lower": rid_v2.make_altitude(0),
"altitude_upper": rid_v2.make_altitude(3048),
},
"time_start": rid_v2.make_time(start_time),
"time_end": rid_v2.make_time(end_time),
},
"extents": rid_v2.make_volume_4d(
area_vertices,
alt_lo,
alt_hi,
start_time,
end_time,
),
"uss_base_url": uss_base_url,
}
if subscription_version is None:
Expand Down Expand Up @@ -191,9 +185,11 @@ class ISAChangeNotification(RIDQuery):
"""Version-independent representation of response to a USS notification following an ISA change in the DSS."""

@property
def success(self) -> bool:
def errors(self) -> List[str]:
# Tolerate not-strictly-correct 200 response
return self.status_code == 204 or self.status_code == 200
if self.status_code != 204 and self.status_code != 200:
return ["Failed to notify ({})".format(self.status_code)]
return []


class SubscriberToNotify(ImplicitDict):
Expand Down Expand Up @@ -299,10 +295,6 @@ def _v22a_response(
v22a.api.PutIdentificationServiceAreaResponse,
)

@property
def success(self) -> bool:
return not self.errors

@property
def errors(self) -> List[str]:
if self.status_code != 200:
Expand Down Expand Up @@ -375,7 +367,9 @@ class ISAChange(ImplicitDict):


def put_isa(
area: s2sphere.LatLngRect,
area_vertices: List[s2sphere.LatLng],
alt_lo: float,
alt_hi: float,
start_time: datetime.datetime,
end_time: datetime.datetime,
uss_base_url: str,
Expand All @@ -387,15 +381,13 @@ def put_isa(
mutation = "create" if isa_version is None else "update"
if rid_version == RIDVersion.f3411_19:
body = {
"extents": {
"spatial_volume": {
"footprint": {"vertices": rid_v1.vertices_from_latlng_rect(area)},
"altitude_lo": 0,
"altitude_hi": 3048,
},
"time_start": start_time.strftime(rid_v1.DATE_FORMAT),
"time_end": end_time.strftime(rid_v1.DATE_FORMAT),
},
"extents": rid_v1.make_volume_4d(
area_vertices,
alt_lo,
alt_hi,
start_time,
end_time,
),
"flights_url": uss_base_url
+ v19.api.OPERATIONS[v19.api.OperationID.SearchFlights].path,
}
Expand All @@ -413,15 +405,13 @@ def put_isa(
)
elif rid_version == RIDVersion.f3411_22a:
body = {
"extents": {
"volume": {
"outline_polygon": rid_v2.make_polygon_outline(area),
"altitude_lower": rid_v2.make_altitude(0),
"altitude_upper": rid_v2.make_altitude(3048),
},
"time_start": rid_v2.make_time(start_time),
"time_end": rid_v2.make_time(end_time),
},
"extents": rid_v2.make_volume_4d(
area_vertices,
alt_lo,
alt_hi,
start_time,
end_time,
),
"uss_base_url": uss_base_url,
}
if isa_version is None:
Expand Down
Loading

0 comments on commit eb1f657

Please sign in to comment.