log.py 1.93 KB
Newer Older
崔为之's avatar
崔为之 committed
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/11/19 16:02
# @File        : log.py
# @Description :
"""

崔为之's avatar
崔为之 committed
13

崔为之's avatar
崔为之 committed
14 15 16
from flask import current_app
from loguru import logger

崔为之's avatar
崔为之 committed
17 18
from application.dao import LogDao
from application.schemas import ParseLogRequestItem
崔为之's avatar
崔为之 committed
19 20 21 22 23


class LogService:

    @classmethod
崔为之's avatar
崔为之 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    def parse(cls, item: ParseLogRequestItem) -> int:
        """
        Parse logs from Elasticsearch and process them according to the configured SQL commands.

        Args:
            item: The item containing the start and end dates for which logs are to be parsed.

        Returns:
            The result of the log parsing as an integer.

        Raises:
            SystemError: If there is an issue with the process.
        """

        # Retrieve the start and end date from the given item.
崔为之's avatar
崔为之 committed
39 40
        start_date = item.start
        end_date = item.end
崔为之's avatar
崔为之 committed
41 42 43 44 45 46 47 48

        # Log the time interval for debugging purposes.
        logger.debug(f'The interval of time is between {start_date} and {end_date}...')

        # Retrieve the Elasticsearch index from the current application's configuration.
        index = current_app.config.Elasticsearch.Index

        # Retrieve the MySQL configuration from the current application's configuration.
崔为之's avatar
崔为之 committed
49
        mysql_config = current_app.config.ExtraDB
崔为之's avatar
崔为之 committed
50 51

        # Retrieve the SQL to be executed from the MySQL configuration.
崔为之's avatar
崔为之 committed
52
        sql = mysql_config.Sql
崔为之's avatar
崔为之 committed
53 54

        # Construct a configuration dictionary for the MySQL database.
崔为之's avatar
崔为之 committed
55 56 57 58 59 60 61
        cfg = {
            'host': mysql_config.Host,
            'user': mysql_config.User,
            'password': mysql_config.Password,
            'db': mysql_config.DB,
            'port': mysql_config.Port,
        }
崔为之's avatar
崔为之 committed
62 63 64

        # Parse the logs using the LogDao class and return the result.
        return LogDao.parse(start_date, end_date, index, sql, cfg)