Skip to content

Commit

Permalink
[monitorlib] Add scd.make_exchange_record (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmis authored Mar 18, 2024
1 parent a4d3ed3 commit c784033
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
18 changes: 16 additions & 2 deletions monitoring/monitorlib/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ def token(self) -> Dict:

@property
def timestamp(self) -> datetime.datetime:
if self.outgoing:
return self.initiated_at.datetime
else:
return self.received_at.datetime

@property
def outgoing(self) -> bool:
if "initiated_at" in self:
# This was an outgoing request
return self.initiated_at.datetime
return True
elif "received_at" in self:
# This was an incoming request
return self.received_at.datetime
return False
else:
raise KeyError(
"RequestDescription missing both initiated_at and received_at"
Expand All @@ -62,6 +69,13 @@ def timestamp(self) -> datetime.datetime:
def url_hostname(self) -> str:
return urlparse(self.url).hostname

@property
def content(self) -> Optional[str]:
if self.json is not None:
return json.dumps(self.json)
else:
return self.body


yaml.add_representer(RequestDescription, Representer.represent_dict)

Expand Down
45 changes: 42 additions & 3 deletions monitoring/monitorlib/scd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from typing import Optional
import base64
from typing import Optional, List, Dict

from implicitdict import ImplicitDict
from uas_standards.astm.f3548.v21.api import OperationalIntentDetails
from implicitdict import StringBasedDateTime
from uas_standards.astm.f3548.v21.api import (
OperationalIntentDetails,
ExchangeRecord,
ExchangeRecordRecorderRole,
Time,
)
from uas_standards.astm.f3548.v21.constants import Scope

from monitoring.monitorlib.fetch import Query

DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"

Expand All @@ -25,3 +32,35 @@ def priority_of(details: OperationalIntentDetails) -> int:
if "priority" in details and details.priority:
priority = details.priority
return priority


def make_exchange_record(query: Query, msg_problem: str) -> ExchangeRecord:
def str_headers(headers: Optional[Dict[str, str]]) -> List[str]:
if headers is None:
return []
return [f"{h_name}: {h_val}" for h_name, h_val in headers.items()]

er = ExchangeRecord(
url=query.request.url,
method=query.request.method,
headers=str_headers(query.request.headers)
+ str_headers(query.response.headers),
recorder_role=ExchangeRecordRecorderRole.Client
if query.request.outgoing
else ExchangeRecordRecorderRole.Server,
request_time=Time(value=StringBasedDateTime(query.request.timestamp)),
response_time=Time(value=StringBasedDateTime(query.response.reported)),
response_code=query.status_code,
problem=msg_problem,
)

if query.request.content is not None:
er.request_body = base64.b64encode(
query.request.content.encode("utf-8")
).decode("utf-8")
if query.response.content is not None:
er.response_body = base64.b64encode(
query.response.content.encode("utf-8")
).decode("utf-8")

return er

0 comments on commit c784033

Please sign in to comment.