(20241225) Small changes in Async Mongo.
This commit is contained in:
@@ -0,0 +1,401 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Created: Wednesday, 18th Sept., 2024
|
||||
Updated: Wednesday, 25th Dec., 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
|
||||
)
|
||||
|
||||
# Models:
|
||||
from utils_v2.api.log import APILogModel
|
||||
from models.logs.api import LogChainRequestData, LogsByFilterRequestData
|
||||
|
||||
# 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/<log_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/<log_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/<log_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")
|
||||
@validate_input(data_validator = lambda x: LogChainRequestData(**x))
|
||||
@handle_cancelled_request()
|
||||
async def get_log_chain(
|
||||
log_chain,
|
||||
inbound_headers: dict = None,
|
||||
inbound_data: dict | LogChainRequestData = 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 = inbound_data.projection,
|
||||
sort = inbound_data.sort,
|
||||
limit = inbound_data.limit,
|
||||
skip = inbound_data.skip
|
||||
)
|
||||
|
||||
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} log(s) fetched",
|
||||
data = {"total": total_count, "fetched": fetched_count, "logs": fetched_logs}
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@logs_bp.route("/get/exception/chain/<log_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")
|
||||
@validate_input(data_validator = lambda x: LogChainRequestData(**x))
|
||||
@handle_cancelled_request()
|
||||
async def get_exceptions_from_log_chain(
|
||||
log_chain,
|
||||
inbound_headers: dict = None,
|
||||
inbound_data: dict | LogChainRequestData = 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.sort,
|
||||
limit = inbound_data.limit,
|
||||
skip = inbound_data.skip
|
||||
)
|
||||
|
||||
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} log(s) 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")
|
||||
@validate_input(data_validator = lambda x: LogsByFilterRequestData(**x))
|
||||
@handle_cancelled_request()
|
||||
async def get_logs_by_filter(
|
||||
inbound_headers: dict = None,
|
||||
inbound_data: dict | LogsByFilterRequestData = 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 = inbound_data.filter,
|
||||
projection = inbound_data.projection,
|
||||
sort = inbound_data.sort,
|
||||
limit = inbound_data.limit,
|
||||
skip = inbound_data.skip
|
||||
)
|
||||
|
||||
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} log(s) fetched.",
|
||||
data = {"total": total_count, "fetched": fetched_count, "logs": fetched_logs}
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@logs_bp.route("/set/api", methods = ["POST"])
|
||||
@set_api_version(api_version = "2.1.0")
|
||||
@read_input(sanitize_headers = True, sanitize_data = True)
|
||||
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||
@only_whitelisted_ips(attr_name = "whitelisted_ips")
|
||||
@validate_input(data_validator = lambda x: APILogModel(**x))
|
||||
async def set_api_log(
|
||||
inbound_headers: dict = None,
|
||||
inbound_data: dict | APILogModel = None,
|
||||
inbound_files: dict = None,
|
||||
**kwargs
|
||||
):
|
||||
|
||||
"""
|
||||
To set logs from internal whitelisted IPs.
|
||||
: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.
|
||||
"""
|
||||
|
||||
inserted_id = await current_app.mongo.insert_one(
|
||||
collection = "logs",
|
||||
document = inbound_data.model_dump()
|
||||
)
|
||||
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if inserted_id else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if inserted_id else HttpCodes.BAD_REQUEST
|
||||
)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
pass
|
||||
Reference in New Issue
Block a user