|
| 1 | +# Copyright (C) 2022-2023 Indoc Systems |
| 2 | +# |
| 3 | +# Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE, Version 3.0 (the "License") available at https://www.gnu.org/licenses/agpl-3.0.en.html. |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | + |
| 6 | +import base64 |
| 7 | +import io |
| 8 | +import logging |
| 9 | +import math |
| 10 | +from datetime import datetime |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +from aiokafka import ConsumerRecord |
| 14 | +from fastavro import schema |
| 15 | +from fastavro import schemaless_reader |
| 16 | +from fastavro import validate |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class BaseConsumer: |
| 22 | + def __init__(self) -> None: |
| 23 | + pass |
| 24 | + |
| 25 | + def decode_label_from_ltree(self, encoded_string: str) -> str: |
| 26 | + missing_padding = math.ceil(len(encoded_string) / 8) * 8 - len(encoded_string) |
| 27 | + if missing_padding: |
| 28 | + encoded_string += '=' * missing_padding |
| 29 | + utf8_string = base64.b32decode(encoded_string.encode('utf-8')).decode('utf-8') |
| 30 | + return utf8_string |
| 31 | + |
| 32 | + def convert_timestamp_millis_to_second(self, timestamp: int) -> int: |
| 33 | + return timestamp // 1000 |
| 34 | + |
| 35 | + def convert_datetime_to_timestamp(self, date: datetime) -> int: |
| 36 | + return int(date.timestamp()) |
| 37 | + |
| 38 | + def decode_message(self, message: bytes, topic: str) -> dict[str, Any]: |
| 39 | + logger.info(f'Starting to decode message from topic "{topic}".') |
| 40 | + try: |
| 41 | + imported_schema = schema.load_schema(self.KAFKA_SCHEMAS_PATH / f'{topic}.avsc') |
| 42 | + message_reader = io.BytesIO(message) |
| 43 | + message_decoded = schemaless_reader(message_reader, imported_schema) |
| 44 | + is_valid = validate(message_decoded, imported_schema, raise_errors=False) |
| 45 | + logger.info(f'Decoded a message from a topic "{topic}": {message_decoded}') |
| 46 | + if not is_valid: |
| 47 | + logger.warning(f'Unable validate decoded message from topic "{topic}".') |
| 48 | + return {} |
| 49 | + |
| 50 | + except Exception: |
| 51 | + logger.exception(f'Unable to decode message from topic "{topic}".') |
| 52 | + return {} |
| 53 | + |
| 54 | + logger.info(f'Decoded a message from a topic "{topic}": {message_decoded}') |
| 55 | + return message_decoded |
| 56 | + |
| 57 | + async def process_event(self, event: ConsumerRecord) -> None: |
| 58 | + topic = event.topic |
| 59 | + message = self.decode_message(message=event.value, topic=topic) |
| 60 | + if not message: |
| 61 | + await self.producer.send_and_wait('metadata.dlq', event.value) |
| 62 | + else: |
| 63 | + await self.process_topic_message(topic, message) |
| 64 | + |
| 65 | + async def process_topic_message(self, topic: str, message: dict[str, Any]) -> None: |
| 66 | + pass |
| 67 | + |
| 68 | + async def run(self) -> None: |
| 69 | + raise Exception('The class is missing the entry funtion `run()`!') |
0 commit comments