(20241226) Started working on the server management system.

This commit is contained in:
2024-12-25 19:48:25 +05:30
parent 7607a427a6
commit 5051f73725
5 changed files with 446 additions and 630 deletions
+36 -267
View File
@@ -6,12 +6,11 @@
DATE:
Created: Wednesday, 18th Sept., 2024
Updated: Wednesday, 25th Dec., 2024
Created: Wednesday, 25th Dec., 2024
OBJECTIVE:
To be able to fetch logs for rapid issue resolution.
To register and enlist servers for various projects.
REFERENCES:
@@ -60,7 +59,8 @@ from utils_v2.api.async_quart import (
# Models:
from utils_v2.api.log import APILogModel
from models.logs.api import LogChainRequestData, LogsByFilterRequestData
from models.servers.api import RegisterServerRequestData
from models.servers.core import CoreServerInfoModel
# For asynchronous activities:
import asyncio
@@ -74,7 +74,7 @@ import asyncio
# Related to Quart:
logs_bp = Blueprint("int_logs", __name__)
servers_bp = Blueprint("int_servers", __name__)
# *****************************************************************************************************************
@@ -94,7 +94,7 @@ logs_bp = Blueprint("int_logs", __name__)
# *****************************************************************************************************************
@logs_bp.record_once
@servers_bp.record_once
def init(blueprint_setup_state):
# This gets called when the blueprint is registered.
@@ -105,288 +105,57 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@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"])
@servers_bp.route("", 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))
@validate_input(data_validator = lambda x: RegisterServerRequestData(**x))
@handle_cancelled_request()
async def get_logs_by_filter(
inbound_headers: dict = None,
inbound_data: dict | LogsByFilterRequestData = None,
inbound_data: dict | RegisterServerRequestData = None,
inbound_files: dict = None,
**kwargs
):
"""
To fetch logs by custom filters:
To register a server's details.
: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
batch = await current_app.server_controller.get_batch(mongo_data_conn = current_app.mongo)
logs = await current_app.server_controller.check_server_batch(
mongo_data_conn = current_app.mongo,
servers = batch
)
for log in logs: print("ONLINE:", log.online)
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
)
# for b in batch:
# print("SERVER:", json.to_string(b.model_dump(), default=str))
# server_log = await current_app.server_controller.check_one_server(
# mongo_data_conn=current_app.mongo,
# server = b
# )
# print("ONLINE:", server_log.online)
return "ok"
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
)
# # register the server to the database:
# registered_server = await current_app.server_controller.register(
# mongo_data_conn = current_app.mongo,
# server = CoreServerInfoModel(**inbound_data.model_dump())
# )
#
# # Done here:
# success = True if registered_server else False
# return ResponseModel(
# status_code = StatusCodes.OK if success else StatusCodes.FAILED,
# http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
# message = "Server registered successfully" if success else "Failed to register server."
# )
# *****************************************************************************************************************