CXCV/Python/Procedures/SQLite/LogsSQLiteProcedure.py

94 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Optional, Self, Any
from hashlib import sha256
from Interfaces.Application.CXCVInterface import CXCVInterface
from Abstracts.ProcedureAbstract import ProcedureAbstract
from Interfaces.Procedures.LogsProcedureInterface import LogsProcedureInterface
from Drivers.SQLiteDriver import SQLiteDriver
from Utils.Utils import Utils
class LogsSQLiteProcedure(ProcedureAbstract, LogsProcedureInterface):
def __init__(self:Self,
cxcv:CXCVInterface,
key:str,
via:str,
inputs:Optional[dict[str, Any|None]|tuple[Any|None, ...]|list[Any|None]] = None
) -> None:
super().__init__(cxcv, key, via, inputs)
self.__database:SQLiteDriver = self.cxcv.databases.get("logs")
def __set_item(self:Self, table:str, column:str, data:str, hashed:bool = False) -> int:
parameters:dict[str, Any|None] = {
"hash": sha256(data.encode("utf-8")).hexdigest() if hashed else "",
"data" : data
}
return self.__database.get_id((
"select id from " + table + " where date_out is null and " + (
"hash = {hash}" if hashed else
column + " = {data}") + ";"
), parameters) or self.__database.execute((
"insert into " + table + "(hash, " + column + ") values({hash}, {data});" if hashed else
"insert into " + table + "(" + column + ") values({data});"), parameters).ids[0]
def __get_action_data(self:Self, application:str, file:str, method:str) -> dict[str, Any|None]:
parameters:dict[str, Any|None] = {
"application": self.__set_item("Applications", "name", application),
"language" : self.__set_item("Languages", "name", "Python"),
"file": self.__set_item("Files", "path", file),
"method": method
}
return self.__database.get_id((
"select id from Methods where date_out is null and application = {application} and language = {language} and file = {file} and name = {method};"
), parameters) or self.__database.execute((
"insert into Methods(application, language, file, name) values({application}, {language}, {file}, {method});"
), parameters).ids[0]
def set_log(self:Self,
application:str,
file:str,
method:str,
line:int,
message:str,
error:int = 0,
parameters:dict[str, Any|None] = {}
) -> None:
self.__database.execute((
"insert into Logs(method, message, parameters, line, error) values({method}, {message}, {parameters}, {line}, {error});"
), {
"method" : self.__get_action_data(application, file, method),
"line" : line,
"message": self.__set_item("Messages", "message", message, True),
"parameters": self.__set_item("Parameters", "data", Utils.json_encode(parameters), True),
"error" : error
})
def set_exception(self:Self,
application:str,
file:str,
method:str,
line:int,
message:str,
exception:str,
trace:str,
parameters:dict[str, Any|None] = {},
status:Optional[str] = None
) -> None:
self.__database.execute((
"insert into Exceptions(method, message, exception, parameters, trace, line, status) values({method}, {message}, {exception}, {parameters}, {trace}, {line}, {status});"
), {
"method" : self.__get_action_data(application, file, method),
"line" : line,
"message": self.__set_item("Messages", "message", message, True),
"exception" : self.__set_item("Messages", "message", exception, True),
"parameters": self.__set_item("Parameters", "data", Utils.json_encode(parameters), True),
"trace": self.__set_item("Traces", "trace", Utils.json_encode(trace), True),
"status" : status
})