|
2 | 2 | import os |
3 | 3 | import json |
4 | 4 | import logging |
| 5 | +from typing import Any, Callable, Dict |
| 6 | +from datetime import datetime |
5 | 7 |
|
6 | 8 | logger = logging.getLogger() |
7 | 9 | logger.setLevel(logging.INFO) |
|
15 | 17 | logger.error("The 'FIREHOSE_STREAM_NAME' environment variable is not set.") |
16 | 18 | raise |
17 | 19 |
|
| 20 | +TimestampMapper = Dict[str, Callable[[Dict[str, Any]], str]] |
| 21 | + |
| 22 | +ARCHIVE_TIMESTAMP_MAPPER: TimestampMapper = { |
| 23 | + "infra-core-api-room-requests-status": lambda x: x["createdAt#status"].split("#")[0] |
| 24 | +} |
| 25 | + |
18 | 26 |
|
19 | 27 | def deserialize_dynamodb_item(item): |
20 | 28 | """ |
@@ -53,12 +61,22 @@ def lambda_handler(event, context): |
53 | 61 |
|
54 | 62 | deserialized_data = deserialize_dynamodb_item(old_image) |
55 | 63 |
|
56 | | - # 4. **Construct the Payload**: Create the specified {'table': ..., 'data': ...} |
57 | | - # payload that will be sent to Firehose. |
58 | | - payload = {"table": table_name, "data": deserialized_data} |
| 64 | + # 4. Construct the Payload |
| 65 | + payload = { |
| 66 | + "table": table_name, |
| 67 | + "data": deserialized_data, |
| 68 | + "timestamp": datetime.now().isoformat(), |
| 69 | + } |
| 70 | + if table_name in ARCHIVE_TIMESTAMP_MAPPER: |
| 71 | + try: |
| 72 | + payload["timestamp"] = ARCHIVE_TIMESTAMP_MAPPER[table_name]( |
| 73 | + deserialized_data |
| 74 | + ) |
| 75 | + except Exception as e: |
| 76 | + logger.error( |
| 77 | + f"Failed to extract timestamp for record from {table_name}: {str(e)}. Using now as timestamp." |
| 78 | + ) |
59 | 79 |
|
60 | | - # 5. **Format for Firehose**: The PutRecordBatch API expects each record |
61 | | - # to have a 'Data' key with a byte-encoded string value. |
62 | 80 | firehose_records_to_send.append( |
63 | 81 | {"Data": json.dumps(payload).encode("utf-8")} |
64 | 82 | ) |
|
0 commit comments