Skip to content

Commit

Permalink
Merge branch 'main' into interactions_and_invalid_op_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
punamverma committed Dec 1, 2023
2 parents a44b49e + 0f4b4d4 commit 907cbfe
Show file tree
Hide file tree
Showing 26 changed files with 1,298 additions and 101 deletions.
20 changes: 16 additions & 4 deletions monitoring/monitorlib/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ def describe_flask_request(request: flask.Request) -> RequestDescription:
"received_at": StringBasedDateTime(datetime.datetime.utcnow()),
"headers": headers,
}
try:
kwargs["json"] = request.json
except ValueError:
kwargs["body"] = request.data.decode("utf-8")
data = request.data.decode("utf-8")
if request.is_json:
try:
kwargs["json"] = json.loads(data)
except ValueError:
kwargs["body"] = data
else:
kwargs["body"] = data
return RequestDescription(**kwargs)


Expand Down Expand Up @@ -263,6 +267,14 @@ def status_code(self) -> int:
def json_result(self) -> Optional[Dict]:
return self.response.json

@property
def error_message(self) -> Optional[str]:
return (
self.json_result["message"]
if self.json_result is not None and "message" in self.json_result
else None
)


class QueryError(RuntimeError):
"""Error encountered when interacting with a server in the UTM ecosystem."""
Expand Down
36 changes: 36 additions & 0 deletions monitoring/monitorlib/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from implicitdict import ImplicitDict
import numpy as np
import s2sphere
from s2sphere import LatLng
from scipy.interpolate import RectBivariateSpline as Spline
import shapely.geometry
from uas_standards.astm.f3548.v21 import api as f3548v21
Expand Down Expand Up @@ -501,3 +502,38 @@ def egm96_geoid_offset(p: s2sphere.LatLng) -> float:
# listed -90 to 90. Since latitude data are symmetric, we can simply
# convert "-90 to 90" to "90 to -90" by inverting the requested latitude.
return _egm96.ev(-lat, lng)


def generate_slight_overlap_area(in_points: List[LatLng]) -> List[LatLng]:
"""
Takes a list of LatLng points and returns a list of LatLng points that represents
a polygon only slightly overlapping with the input, and that is roughly half the diameter of the input.
The returned polygon is built from the first point of the input, from which a square
is drawn in the direction opposite of the center of the input polygon.
"""
overlap_corner = in_points[0] # the spot that will have a tiny overlap

# Compute the center of mass of the input polygon
center = LatLng.from_degrees(
sum([point.lat().degrees for point in in_points]) / len(in_points),
sum([point.lng().degrees for point in in_points]) / len(in_points),
)

delta_lat = center.lat().degrees - overlap_corner.lat().degrees
delta_lng = center.lng().degrees - overlap_corner.lng().degrees

same_lat_point = LatLng.from_degrees(
overlap_corner.lat().degrees, overlap_corner.lng().degrees - delta_lng
)
same_lng_point = LatLng.from_degrees(
overlap_corner.lat().degrees - delta_lat, overlap_corner.lng().degrees
)

opposite_corner = LatLng.from_degrees(
overlap_corner.lat().degrees - delta_lat,
overlap_corner.lng().degrees - delta_lng,
)

return [overlap_corner, same_lat_point, opposite_corner, same_lng_point]
36 changes: 36 additions & 0 deletions monitoring/monitorlib/geo_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Tuple

from s2sphere import LatLng

from monitoring.monitorlib.geo import generate_slight_overlap_area


def _points(in_points: List[Tuple[float, float]]) -> List[LatLng]:
return [LatLng.from_degrees(*p) for p in in_points]


def test_generate_slight_overlap_area():
# Square around 0,0 of edge length 2 -> first corner at 1,1 -> expect a square with overlapping corner at 1,1
assert generate_slight_overlap_area(
_points([(1, 1), (1, -1), (-1, -1), (-1, 1)])
) == _points([(1, 1), (1, 2), (2, 2), (2, 1)])

# Square with diagonal from 0,0 to 1,1 -> first corner at 1,1 -> expect a square with overlapping corner at 1,1
assert generate_slight_overlap_area(
_points([(1, 1), (0, 1), (0, 0), (1, 0)])
) == _points([(1, 1), (1, 1.5), (1.5, 1.5), (1.5, 1)])

# Square with diagonal from 0,0 to -1,-1 -> first corner at -1,-1 -> expect a square with overlapping corner at -1,-1
assert generate_slight_overlap_area(
_points([(-1, -1), (0, -1), (0, 0), (-1, 0)])
) == _points([(-1, -1), (-1, -1.5), (-1.5, -1.5), (-1.5, -1)])

# Square with diagonal from 0,0 to -1,1 -> first corner at -1,1 -> expect a square with overlapping corner at -1,0
assert generate_slight_overlap_area(
_points([(-1, 1), (-1, 0), (0, 0), (0, 1)])
) == _points([(-1, 1), (-1, 1.5), (-1.5, 1.5), (-1.5, 1)])

# Square with diagonal from 0,0 to 1,-1 -> first corner at 1,-1 -> expect a square with overlapping corner at 1,-1
assert generate_slight_overlap_area(
_points([(1, -1), (1, 0), (0, 0), (0, -1)])
) == _points([(1, -1), (1, -1.5), (1.5, -1.5), (1.5, -1)])
30 changes: 25 additions & 5 deletions monitoring/monitorlib/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
from typing import Dict, List, Optional
import urllib.parse
from aiohttp import ClientSession
from aiohttp import ClientSession, ClientResponse

import jwt
import requests
Expand Down Expand Up @@ -190,32 +190,52 @@ def adjust_request_kwargs(self, url, method, kwargs):
return kwargs

async def put(self, url, **kwargs):
"""Returns (status, headers, json)"""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "PUT", kwargs)
async with self._client.put(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def get(self, url, **kwargs):
"""Returns (status, headers, json)"""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "GET", kwargs)
async with self._client.get(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def post(self, url, **kwargs):
"""Returns (status, headers, json)"""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "POST", kwargs)
async with self._client.post(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def delete(self, url, **kwargs):
"""Returns (status, headers, json)"""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "DELETE", kwargs)
async with self._client.delete(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)


def default_scopes(scopes: List[str]):
Expand Down
2 changes: 1 addition & 1 deletion monitoring/prober/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def wrapper_default_scope(*args, **kwargs):
resource_type_code_descriptions: Dict[ResourceType, str] = {}


# Next code: 373
# Next code: 374
def register_resource_type(code: int, description: str) -> ResourceType:
"""Register that the specified code refers to the described resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_create_isa_concurrent(ids, session_ridv1_async):
)
)
for isa_id, resp in results:
assert resp[0] == 200, resp[1]
data = resp[1]
assert resp[0] == 200, resp[2]
data = resp[2]
assert data["service_area"]["id"] == isa_id
assert data["service_area"]["flights_url"] == "https://example.com/dss"
assert_datetimes_are_equal(
Expand All @@ -133,9 +133,9 @@ def test_get_isa_by_ids_concurrent(ids, session_ridv1_async):
)
)
for isa_id, resp in results:
assert resp[0] == 200, resp[1]
assert resp[0] == 200, resp[2]

data = resp[1]
data = resp[2]
assert data["service_area"]["id"] == isa_id
assert data["service_area"]["flights_url"] == FLIGHTS_URL

Expand All @@ -162,8 +162,8 @@ def test_delete_isa_concurrent(ids, session_ridv1_async):
)

for isa_id, resp in results:
assert resp[0] == 200, resp[1]
version = resp[1]["service_area"]["version"]
assert resp[0] == 200, resp[2]
version = resp[2]["service_area"]["version"]
version_map[isa_id] = version

# Delete ISAs concurrently
Expand All @@ -178,4 +178,4 @@ def test_delete_isa_concurrent(ids, session_ridv1_async):
)

for isa_id, resp in results:
assert resp[0], resp[1]
assert resp[0], resp[2]
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def test_create_ops_concurrent(ids, scd_api, scd_session_async):
op_id = req_map[0]
op_resp_map[op_id] = {}
op_resp_map[op_id]["status_code"] = resp[0][0]
op_resp_map[op_id]["content"] = resp[0][1]
op_resp_map[op_id]["content"] = resp[0][2]
for op_id, resp in op_resp_map.items():
if resp["status_code"] != 201:
try:
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_get_ops_by_ids_concurrent(ids, scd_api, scd_session_async):
for op_id, resp in zip(map(ids, OP_TYPES), results):
op_resp_map[op_id] = {}
op_resp_map[op_id]["status_code"] = resp[0]
op_resp_map[op_id]["content"] = resp[1]
op_resp_map[op_id]["content"] = resp[2]

for op_id, resp in op_resp_map.items():
assert resp["status_code"] == 200, resp["content"]
Expand Down Expand Up @@ -381,7 +381,7 @@ def test_get_ops_by_search_concurrent(ids, scd_api, scd_session_async):
for idx, resp in zip(range(len(OP_TYPES)), results):
op_resp_map[idx] = {}
op_resp_map[idx]["status_code"] = resp[0]
op_resp_map[idx]["content"] = resp[1]
op_resp_map[idx]["content"] = resp[2]

for idx, resp in op_resp_map.items():
assert resp["status_code"] == 200, resp["content"]
Expand Down Expand Up @@ -431,7 +431,7 @@ def test_mutate_ops_concurrent(ids, scd_api, scd_session, scd_session_async):
op_id = req_map[0]
op_resp_map[op_id] = {}
op_resp_map[op_id]["status_code"] = resp[0][0]
op_resp_map[op_id]["content"] = resp[0][1]
op_resp_map[op_id]["content"] = resp[0][2]

ovn_map.clear()

Expand Down Expand Up @@ -486,7 +486,7 @@ def test_delete_op_concurrent(ids, scd_api, scd_session_async):
for op_id, resp in zip(map(ids, OP_TYPES), results):
op_resp_map[op_id] = {}
op_resp_map[op_id]["status_code"] = resp[0]
op_resp_map[op_id]["content"] = resp[1]
op_resp_map[op_id]["content"] = resp[2]

assert len(op_resp_map) == len(OP_TYPES)

Expand Down
Loading

0 comments on commit 907cbfe

Please sign in to comment.