""" AUTHOR: Khushal P Soonderji DATE: Created: Wednesday, 18th Sept., 2024 Updated: Tuesday, 8th Oct., 2024 OBJECTIVE: To be able to fetch logs for rapid issue resolution. REFERENCES: N/A DOWNLOADS: N/A NOTES: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** # To make sibling directories accessible for imports: import sys sys.path.append(".") sys.path.append("..") # For using Quart: from quart import Blueprint, current_app # My utils: from utils_v2.string import json from utils_v2.api.codes import StatusCodes, HttpCodes from utils_v2.api.response import ResponseModel from utils_v2.api.async_quart import ( set_api_version, read_input, log_request_to_mongo, should_not_be_under_maintenance, only_whitelisted_ips, limit_rate, validate_input, handle_cancelled_request ) # For asynchronous activities: import asyncio # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** # Related to Quart: logs_bp = Blueprint("int_logs", __name__) # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** @logs_bp.record_once def init(blueprint_setup_state): # This gets called when the blueprint is registered. # Consider this to be a one-time setup for the whole blueprint: pass # --------------------------------------------------------------------------------------------------------------------- @logs_bp.route("/get/id/", methods = ["POST", "GET"]) @set_api_version(api_version = "2.1.0") @should_not_be_under_maintenance(attr_name = "is_under_maintenance") @only_whitelisted_ips(attr_name = "whitelisted_ips") @handle_cancelled_request() async def get_log( log_id, **kwargs ): """ To get the log from its log id. :param log_id: An identifier (string) for the log to fetch. """ fetched_log = await current_app.mongo.find_one( collection = "logs", filter = {"logId": log_id}, projection = {"_id": False} ) if not fetched_log: return ResponseModel( status_code = StatusCodes.FAILED, http_code = HttpCodes.NOT_FOUND, message = "no such log" ) else: return ResponseModel( status_code = StatusCodes.OK, message = f"log found", data = {"total": 1, "fetched": 1, "logs": fetched_log} ) # --------------------------------------------------------------------------------------------------------------------- @logs_bp.route("/get/exception/id/", methods = ["POST", "GET"]) @set_api_version(api_version = "2.1.0") @should_not_be_under_maintenance(attr_name = "is_under_maintenance") @only_whitelisted_ips(attr_name = "whitelisted_ips") async def get_exception_from_log( log_id, **kwargs ): """ To get the log's exception from its log id. :param log_id: An identifier (string) for the log to fetch. """ fetched_log = await current_app.mongo.find_one( collection = "logs", filter = {"logId": log_id}, projection = { "_id": False, "logId": True, "log": True, "operation": True, "ts": True, "exception": True } ) if not fetched_log: return ResponseModel( status_code = StatusCodes.FAILED, http_code = HttpCodes.NOT_FOUND, message = "no such log" ) else: return ResponseModel( status_code = StatusCodes.OK, message = f"log found", data = {"total": 1, "fetched": 1, "logs": fetched_log} ) # --------------------------------------------------------------------------------------------------------------------- @logs_bp.route("/get/chain/", methods = ["POST", "GET"]) @set_api_version(api_version = "2.1.0") @read_input(sanitize_headers = False, sanitize_data = False) @should_not_be_under_maintenance(attr_name = "is_under_maintenance") @only_whitelisted_ips(attr_name = "whitelisted_ips") async def get_log_chain( log_chain, inbound_headers: dict = None, inbound_data: dict = None, inbound_files: dict = None, **kwargs ): """ To get the series of logs from its chain identifier. :param log_chain: An identifier (string) for the log chain to fetch. :param inbound_headers: auto-extracted by the decorators. :param inbound_data: auto-extracted by the decorators. :param inbound_files: auto-extracted by the decorators. :param kwargs: Any number of extra inputs supplied by the decorators. """ total_count = await current_app.mongo.count( collection = "logs", filter = {"logChain": log_chain} ) fetched_logs = await current_app.mongo.find_many( collection = "logs", filter = {"logChain": log_chain}, projection = {"_id": False}, sort = inbound_data.get("sort", {"_id": -1}), limit = inbound_data.get("limit", 25), skip = inbound_data.get("skip", 0) ) fetched_count = len(fetched_logs) if not fetched_logs: return ResponseModel( status_code = StatusCodes.FAILED, http_code = HttpCodes.NOT_FOUND, message = "no such log chain" ) else: return ResponseModel( status_code = StatusCodes.OK, message = f"{fetched_count} logs fetched", data = {"total": total_count, "fetched": fetched_count, "logs": fetched_logs} ) # --------------------------------------------------------------------------------------------------------------------- @logs_bp.route("/get/exception/chain/", methods = ["POST", "GET"]) @set_api_version(api_version = "2.1.0") @read_input(sanitize_headers = False, sanitize_data = False) @should_not_be_under_maintenance(attr_name = "is_under_maintenance") @only_whitelisted_ips(attr_name = "whitelisted_ips") async def get_exceptions_from_log_chain( log_chain, inbound_headers: dict = None, inbound_data: dict = None, inbound_files: dict = None, **kwargs ): """ To get the series of log exceptions from its chain identifier. :param log_chain: An identifier (string) for the log chain to fetch. :param inbound_headers: auto-extracted by the decorators. :param inbound_data: auto-extracted by the decorators. :param inbound_files: auto-extracted by the decorators. :param kwargs: Any number of extra inputs supplied by the decorators. """ total_count = await current_app.mongo.count( collection = "logs", filter = {"logChain": log_chain} ) fetched_logs = await current_app.mongo.find_many( collection = "logs", filter = {"logChain": log_chain}, projection = { "_id": False, "log": True, "operation": True, "ts": True, "exception": True }, sort = inbound_data.get("sort", {"_id": -1}), limit = inbound_data.get("limit", 25), skip = inbound_data.get("skip", 0) ) fetched_count = len(fetched_logs) if not fetched_logs: return ResponseModel( status_code = StatusCodes.FAILED, http_code = HttpCodes.NOT_FOUND, message = "no such log chain" ) else: return ResponseModel( status_code = StatusCodes.OK, message = f"{fetched_count} logs fetched", data = {"total": total_count, "fetched": fetched_count, "logs": fetched_logs} ) # --------------------------------------------------------------------------------------------------------------------- @logs_bp.route("/get/filter", methods = ["POST", "GET"]) @set_api_version(api_version = "2.1.0") @read_input(sanitize_headers = False, sanitize_data = False) @should_not_be_under_maintenance(attr_name = "is_under_maintenance") @only_whitelisted_ips(attr_name = "whitelisted_ips") async def get_logs_by_filter( inbound_headers: dict = None, inbound_data: dict = None, inbound_files: dict = None, **kwargs ): """ To fetch logs by custom filters: :param inbound_headers: auto-extracted by the decorators. :param inbound_data: auto-extracted by the decorators. :param inbound_files: auto-extracted by the decorators. :param kwargs: Any number of extra inputs supplied by the decorators. """ total_count = await current_app.mongo.count( collection = "logs", filter = inbound_data["filter"] ) fetched_logs = await current_app.mongo.find_many( collection = "logs", filter = current_app.mongo.dict_to_dot_notation(inbound_data["filter"]), projection = inbound_data.get("projection", {"_id": False}), sort = inbound_data.get("sort", {"_id": -1}), limit = inbound_data.get("limit", 25), skip = inbound_data.get("skip", 0) ) fetched_count = len(fetched_logs) if not fetched_logs: return ResponseModel( status_code = StatusCodes.FAILED, http_code = HttpCodes.NOT_FOUND, message = "no matching logs" ) else: return ResponseModel( status_code = StatusCodes.OK, message = f"{len(fetched_logs)} / {total_count} logs fetched", data = {"total": total_count, "fetched": fetched_count, "logs": fetched_logs} ) # ***************************************************************************************************************** # ***** **** # *** MAIN PROGRAM *** # ***** **** # ***************************************************************************************************************** if __name__ == "__main__": pass