diff --git a/application/dao/log.py b/application/dao/log.py index 26e271b44f22aa3faee29e05ee6a35f08330a188..4ff20299e6609afe327ae8603ca76a184bbed4d0 100644 --- a/application/dao/log.py +++ b/application/dao/log.py @@ -16,6 +16,7 @@ from typing import Generator from loguru import logger from sqlalchemy import text +from application.libs import ServerError from application.libs.helper import MySQLHelper from application.models import Log from application.utils import ElasticsearchUtil, ParseUtil @@ -44,14 +45,14 @@ class LogDao: dict: The data returned from Elasticsearch. Raises: - SystemError: If none of the required parameters are provided. + ServerError: If none of the required parameters are provided. """ if sid is not None: return ElasticsearchUtil.scroll_search(sid) elif index is not None and dsl is not None: return ElasticsearchUtil.search(index, dsl) else: - raise SystemError('Could not get data from Elasticsearch') + raise ServerError('Could not get data from Elasticsearch') @classmethod def get_mdata(cls, data: dict) -> list: @@ -70,7 +71,7 @@ class LogDao: mdata = data.get('hits').get('hits') if not mdata: logger.error('the mdata is an empty list ...') - raise SystemError('the mdata is an empty list ...') + raise ServerError('the mdata is an empty list ...') return mdata @classmethod @@ -184,7 +185,7 @@ class LogDao: except Exception as e: # Log the error and raise a SystemError. logger.error(f'The error: {e}') - raise SystemError() + raise ServerError('Operate data error') else: # Log the success of the process. logger.debug('The process of inserting data succeed!') diff --git a/application/libs/__init__.py b/application/libs/__init__.py index e7ce33ef405801e81e63284cb6d67226c52ecbde..fb2a56017076fbdd99dc1d451e4f1b3e2a521d3b 100644 --- a/application/libs/__init__.py +++ b/application/libs/__init__.py @@ -11,7 +11,8 @@ """ from .config import ConsulConfig, LocalConfig -from .error import ConfigKeyError +from .error import ConfigKeyError, ServerError from .flask_consul import FlaskConsulService from .flask_elasticsearch import FlaskElasticsearch from .flask_loguru import FlaskLoguru +from .response import JsonResponse diff --git a/application/libs/error/__init__.py b/application/libs/error/__init__.py index c3ac498c9d5ad79e11f287cb39f9f207656105c6..dba1b8882e4297fd1608295fce0242b8ee65ca59 100644 --- a/application/libs/error/__init__.py +++ b/application/libs/error/__init__.py @@ -10,4 +10,4 @@ # @Description : error 自定义模块 """ -from .exceptions import ConfigKeyError +from .exceptions import ConfigKeyError, ServerError diff --git a/application/libs/error/exceptions.py b/application/libs/error/exceptions.py index 7caf33f02df9548a270921dd36b4b4cd98acf995..bf1b4bbe4d4991570bb2d46679ac9c16fd4e5653 100644 --- a/application/libs/error/exceptions.py +++ b/application/libs/error/exceptions.py @@ -18,3 +18,8 @@ class ConfigKeyError(Exception): def __init__(self, key): self.key = key super().__init__(f'Key {self.key} not found in configuration') + + +class ServerError(Exception): + def __init__(self, *args, **kwargs): + pass diff --git a/application/libs/flask_consul/consul.py b/application/libs/flask_consul/consul.py index e68482d48affbb0aed7f177229247aa0a8c80584..a265c8bf46ce5cc97be86e0498c39ca824ec75f4 100644 --- a/application/libs/flask_consul/consul.py +++ b/application/libs/flask_consul/consul.py @@ -14,7 +14,9 @@ import atexit import consul from flask import Flask +from loguru import logger +from application.libs import ServerError from application.libs.helper import ConfigHelper from application.libs.helper import EnvVarHelper @@ -59,8 +61,8 @@ class FlaskConsulService: if self.service_id is None: self.service_id = f'{service_name}-{service_address}-{service_port}' try: - check = consul.Check().tcp(host=service_address, port=service_port, interval=check_interval, timeout=timeout, - deregister=deregister_after) + check = consul.Check().tcp(host=service_address, port=service_port, interval=check_interval, + timeout=timeout, deregister=deregister_after) if self.client is None: self.client = consul.Consul(**env_vars) self.client.agent.service.register( @@ -71,7 +73,8 @@ class FlaskConsulService: check=check ) except Exception as e: - print(e) + logger.error(f'Failed to register service, error: {e}') + ServerError('Failed to register service') def deregister_service(self): self.client.agent.service.deregister(self.service_id) diff --git a/application/libs/response/__init__.py b/application/libs/response/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d3e612129c7bdc41e514830bcf5f76c6d555d16d --- /dev/null +++ b/application/libs/response/__init__.py @@ -0,0 +1,13 @@ +#!/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/21 14:09 +# @File : __init__.py +# @Description : +""" + +from .response import JsonResponse diff --git a/application/libs/response/response.py b/application/libs/response/response.py new file mode 100644 index 0000000000000000000000000000000000000000..6fdd6d513551e4d312798311210306cd88cb34d2 --- /dev/null +++ b/application/libs/response/response.py @@ -0,0 +1,34 @@ +#!/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/21 14:10 +# @File : response.py +# @Description : +""" + + +from flask import Flask, jsonify + +app = Flask(__name__) + + +class JsonResponse: + @staticmethod + def success(data, message='Success'): + return jsonify({ + 'status': 'success', + 'message': message, + 'data': data, + }), 200 + + @staticmethod + def error(message='Error', status_code=400): + return jsonify({ + 'status': 'error', + 'message': message, + }), status_code + diff --git a/application/services/log.py b/application/services/log.py index 5148e35f9e8a7d55e7a89f80c3dd28186aba6762..166e15f9f4e36ba66076d20a622523d41fdaf8ea 100644 --- a/application/services/log.py +++ b/application/services/log.py @@ -32,7 +32,7 @@ class LogService: The result of the log parsing as an integer. Raises: - SystemError: If there is an issue with the process. + ServerError: If there is an issue with the process. """ # Retrieve the start and end date from the given item. diff --git a/application/views/log/log.py b/application/views/log/log.py index 750575e258a5de1cc7c95a1b98c7e4def87ac46a..58aab659310735a1fd2a041e64e96330db523b93 100644 --- a/application/views/log/log.py +++ b/application/views/log/log.py @@ -17,6 +17,8 @@ from pydantic import ValidationError from application.schemas import ParseLogRequestItem from application.services import LogService +from application.libs import JsonResponse +from application.libs import ServerError # Define the log API blueprint @@ -37,15 +39,21 @@ def parse(): # If no data provided, return error message if not json_data: logger.error('No input data provided') - return jsonify({"message": "No input data provided"}), 400 + return JsonResponse.error(message='No input data provided', status_code=400) try: # Validate input data item = ParseLogRequestItem(**json_data) except ValidationError as e: # If validation fails, return error message - return jsonify({"message": "Invalid input data", "errors": e.errors()}), 400 + logger.error(f'Invalid input data, error: {e}') + return JsonResponse.error(message='Invalid input data', status_code=400) # If validation passes, parse logs and return total number of logs parsed - total = LogService.parse(item) - return jsonify(search_total=total), 200 + try: + total = LogService.parse(item) + except ServerError as e: + logger.error('Internal server error') + return JsonResponse.error(message='Internal server error', status_code=500) + else: + return JsonResponse.success(message='Successfully parsed', data=total)