|
| 1 | +import click |
| 2 | +from dotenv import load_dotenv |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import logging |
| 6 | +from logging.handlers import RotatingFileHandler |
| 7 | +import json |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) |
| 13 | + |
| 14 | +import segment.analytics as analytics # noqa: E402 (ignore autopep8) |
| 15 | + |
| 16 | + |
| 17 | +@click.command() |
| 18 | +@click.option('--writeKey', type=str, help='Segment write key') |
| 19 | +@click.option('--payload', type=str, help='A JSON string that specifies the event payload.') |
| 20 | +def run(writekey, payload): |
| 21 | + analytics.write_key = writekey |
| 22 | + analytics.debug = os.getenv('DEBUG_MODE') |
| 23 | + analytics.send = os.getenv('SEND_EVENTS') |
| 24 | + logger = log_config() |
| 25 | + |
| 26 | + try: |
| 27 | + # Decode the JSON payload |
| 28 | + decodedJson = json.loads(payload) |
| 29 | + data = json.loads(decodedJson) |
| 30 | + |
| 31 | + specType = data.get('type') if data.get('type') is not None else None |
| 32 | + messageId = data.get('messageId') if data.get('messageId') is not None else None |
| 33 | + userId = data.get('userId') if data.get('userId') is not None else '' |
| 34 | + eventName = data.get('event') if data.get('event') is not None else None |
| 35 | + traits = data.get('traits') if data.get('traits') is not None else None |
| 36 | + properties = data.get('properties') if data.get('properties') is not None else None |
| 37 | + context = data.get('context') if data.get('context') is not None else None |
| 38 | + integrations = data.get('integrations') if data.get('integrations') is not None else None |
| 39 | + groupId = data.get('groupId') if data.get('groupId') is not None else None |
| 40 | + pageOrScreenName = data.get('name') if data.get('name') is not None else None |
| 41 | + pageOrScreenCategory = data.get('category') if data.get('category') is not None else None |
| 42 | + timestamp = data.get('timestamp') if data.get('timestamp') is not None else None |
| 43 | + anonymousId = data.get('anonymousId') if data.get('anonymousId') is not None else '' |
| 44 | + previousId = data.get('previousId') if data.get('previousId') is not None else None |
| 45 | + if specType == 'identify': |
| 46 | + analytics.identify(userId, traits, context, timestamp, anonymousId, integrations, messageId) |
| 47 | + elif specType == 'track': |
| 48 | + analytics.track(userId, eventName, properties, context, timestamp, anonymousId, integrations, messageId) |
| 49 | + elif specType == 'page': |
| 50 | + analytics.page(userId, pageOrScreenCategory, pageOrScreenName, properties, |
| 51 | + context, timestamp, anonymousId, integrations, messageId) |
| 52 | + elif specType == 'screen': |
| 53 | + analytics.screen(userId, pageOrScreenCategory, pageOrScreenName, properties, |
| 54 | + context, timestamp, anonymousId, integrations, messageId) |
| 55 | + elif specType == 'alias': |
| 56 | + analytics.alias(previousId, userId, context, timestamp, integrations, messageId) |
| 57 | + elif specType == 'group': |
| 58 | + analytics.group(userId, groupId, traits, context, timestamp, anonymousId, integrations, messageId) |
| 59 | + else: |
| 60 | + raise Exception |
| 61 | + except Exception as e: |
| 62 | + logger.exception(e) |
| 63 | + finally: |
| 64 | + analytics.flush() |
| 65 | + |
| 66 | + |
| 67 | +def log_config(): |
| 68 | + # Create a logger object |
| 69 | + logger = logging.getLogger(os.getenv('APP_NAME')) |
| 70 | + logger.setLevel(logging.DEBUG) |
| 71 | + |
| 72 | + # Create a file handler to log messages to a file |
| 73 | + log_directory = f"{os.getenv('LOG_DIRECTORY')}/{datetime.now().strftime('%Y%m%d')}" |
| 74 | + os.makedirs(log_directory, exist_ok=True) |
| 75 | + log_filename = f"{os.getenv('LOG_FILENAME_SUFFIX')}_{datetime.now().strftime('%H')}00.log" |
| 76 | + |
| 77 | + # Create a rotating file handler |
| 78 | + handler = RotatingFileHandler(os.path.join(log_directory, log_filename), |
| 79 | + mode='a', maxBytes=1024*1024, backupCount=100) |
| 80 | + handler.setLevel(logging.DEBUG) |
| 81 | + |
| 82 | + # Define the log message format |
| 83 | + formatter = logging.Formatter(os.getenv('LOG_FORMAT')) |
| 84 | + handler.setFormatter(formatter) |
| 85 | + |
| 86 | + # Attach the handler to the logger |
| 87 | + logger.addHandler(handler) |
| 88 | + |
| 89 | + return logger |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + run() |
0 commit comments