From 5335dee2cffd0acbb24c2668d75e0e2d28a97e2c Mon Sep 17 00:00:00 2001 From: xss Date: Wed, 2 Aug 2023 14:25:16 +1000 Subject: [PATCH] Adds upload_time and enables filter flags for incorrect time closes #117 --- lambda/ham_put_api/__init__.py | 40 +++++++++++++++++++++++++++++----- lambda/ham_put_api/__main__.py | 4 +++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lambda/ham_put_api/__init__.py b/lambda/ham_put_api/__init__.py index a28bbb7..5f041e6 100644 --- a/lambda/ham_put_api/__init__.py +++ b/lambda/ham_put_api/__init__.py @@ -63,6 +63,7 @@ def upload(event, context): payloads = json.loads(event["body"]) to_sns = [] errors = [] + warnings = [] @@ -72,8 +73,36 @@ def upload(event, context): payload["user-agent"] = event["headers"]["user-agent"] payload["position"] = f'{payload["lat"]},{payload["lon"]}' + payload["upload_time"] = datetime.datetime.now().isoformat() + valid, error_message = telemetry_filter(payload) + try: + _delta_time = ( + datetime.datetime.now() - datetime.datetime.fromisoformat(payload["datetime"].replace("Z","")) + ).total_seconds() + except: + errors.append({ + "error_message": "Unable to parse datetime", + "payload": payload + }) + + future_time_threshold_seconds = 60 + time_threshold_hours = 24 + if _delta_time < -future_time_threshold_seconds: + payload["telemetry_hidden"] = True + warnings.append({ + "warning_message": f"Payload reported time too far in the future. Either sonde time or system time is invalid. (Threshold: {future_time_threshold_seconds} seconds)", + "payload": payload + }) + if "historical" not in payload or payload['historical'] == False: + if abs(_delta_time) > (3600 * time_threshold_hours): + payload["telemetry_hidden"] = True + warnings.append({ + "warning_message": f"Payload reported time too far from current UTC time. Either payload time or system time is invalid. (Threshold: {time_threshold_hours} hours)", + "payload": payload + }) + if not valid: errors.append({ "error_message": error_message, @@ -98,19 +127,20 @@ def upload(event, context): post(to_sns) - return errors + return errors, warnings def lambda_handler(event, context): try: - errors = upload(event, context) + errors, warnings = upload(event, context) except zlib.error: return {"statusCode": 400, "body": "Could not decompress"} except json.decoder.JSONDecodeError: return {"statusCode": 400, "body": "Not valid json"} error_message = { - "message": "some or all payloads could not be processed", - "errors": errors + "message": "some or all payloads could not be processed or have warnings", + "errors": errors, + "warnings": warnings } - if errors: + if errors or warnings: output = { "statusCode": 202, "body": json.dumps(error_message), diff --git a/lambda/ham_put_api/__main__.py b/lambda/ham_put_api/__main__.py index fa00c4f..4acf8f9 100644 --- a/lambda/ham_put_api/__main__.py +++ b/lambda/ham_put_api/__main__.py @@ -12,7 +12,7 @@ body = [ "payload_callsign": "4FSKTEST", "uploader_antenna": "1/4 wave monopole", "time_received": "2021-12-30T03:55:05.510688Z", - "datetime": "2021-12-30T03:55:05.510688Z", + "datetime": "2022-08-02T04:10:05.510688Z", "manufacturer": "Graw", "type": "DFM", "subtype": "DFM09", @@ -32,6 +32,8 @@ body = [ "position": "47.8319,10.89474", "uploader_alt": 545, "uploader_position": [None,None,None], + "historical": True, + "dev": True }] compressed = BytesIO()