fix async

This commit is contained in:
2025-05-27 23:50:25 +02:00
parent 670972bd45
commit c40378b654
17 changed files with 181 additions and 210 deletions

View File

@@ -1,5 +1,4 @@
#!.venv/bin/python
from utils.database.connection import connetti_db
from utils.database.nodes_query import get_nodes_type
import utils.timestamp.date_check as date_check
import logging
@@ -9,31 +8,15 @@ from itertools import islice
logger = logging.getLogger(__name__)
def get_data(cfg: object, id: int) -> tuple:
"""
Retrieves data for a specific tool from the database.
async def get_data(cfg: object, id: int, pool) -> tuple:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(f'select unit_name, tool_name, tool_data from {cfg.dbrectable} where id = {id}')
unit_name, tool_name, tool_data = await cur.fetchone()
This function connects to the database using the provided configuration,
executes a query to retrieve the unit name, tool name ID, and tool data
associated with the given ID from the raw data table, and returns the results.
return unit_name, tool_name, tool_data
Args:
cfg: A configuration object containing database connection parameters
and table names (cfg.dbname, cfg.dbrectable).
id: The ID of the tool record to retrieve.
Returns:
A tuple containing the unit name, tool name ID, and tool data.
"""
with connetti_db(cfg) as conn:
cur = conn.cursor()
cur.execute(f'select unit_name, tool_name, tool_data from {cfg.dbname}.{cfg.dbrectable} where id = {id}')
unit_name, tool_name, tool_data = cur.fetchone()
cur.close()
conn.close()
return unit_name, tool_name, tool_data
def make_pipe_sep_matrix(cfg: object, id: int) -> list:
async def make_pipe_sep_matrix(cfg: object, id: int, pool) -> list:
"""
Processes raw tool data and transforms it into a matrix format for database insertion.
@@ -55,7 +38,7 @@ def make_pipe_sep_matrix(cfg: object, id: int) -> list:
EventTime, BatLevel, Temperature, followed by up to 16 additional
measurement values (Val0 to ValF), padded with None if necessary.
"""
UnitName, ToolNameID, ToolData = get_data(cfg, id)
UnitName, ToolNameID, ToolData = await get_data(cfg, id, pool)
righe = ToolData.splitlines()
matrice_valori = []
for riga in [riga for riga in righe if ';|;' in riga]:
@@ -68,7 +51,7 @@ def make_pipe_sep_matrix(cfg: object, id: int) -> list:
return matrice_valori
def make_ain_din_matrix(cfg: object, id: int) -> list:
async def make_ain_din_matrix(cfg: object, id: int, pool) -> list:
"""
Processes raw location (LOC) tool data and transforms it into a matrix format for database insertion.
@@ -90,7 +73,7 @@ def make_ain_din_matrix(cfg: object, id: int) -> list:
A list of lists (matrix) representing the processed LOC data. Each inner
list contains data fields similar to `make_matrix`, adjusted for LOC data.
"""
UnitName, ToolNameID, ToolData = get_data(cfg, id)
UnitName, ToolNameID, ToolData = await get_data(cfg, id, pool)
node_channels, node_types, node_ains, node_dins = get_nodes_type(cfg, ToolNameID, UnitName)
righe = ToolData.splitlines()
matrice_valori = []
@@ -113,8 +96,8 @@ def make_ain_din_matrix(cfg: object, id: int) -> list:
return matrice_valori
def make_channels_matrix(cfg: object, id: int) -> list:
UnitName, ToolNameID, ToolData = get_data(cfg, id)
async def make_channels_matrix(cfg: object, id: int, pool) -> list:
UnitName, ToolNameID, ToolData = await get_data(cfg, id, pool)
node_channels, node_types, node_ains, node_dins = get_nodes_type(cfg, ToolNameID, UnitName)
righe = ToolData.splitlines()
matrice_valori = []

View File

@@ -6,7 +6,7 @@ import logging
logger = logging.getLogger(__name__)
async def main_loader(cfg: object, id: int, action: str) -> None:
async def main_loader(cfg: object, id: int, pool, action: str) -> None:
type_matrix_mapping = {
"pipe_separator": make_pipe_sep_matrix,
"analogic_digital": make_ain_din_matrix,
@@ -15,11 +15,11 @@ async def main_loader(cfg: object, id: int, action: str) -> None:
if action in type_matrix_mapping:
function_to_call = type_matrix_mapping[action]
# Create a matrix of values from the data
matrice_valori = function_to_call(cfg, id)
matrice_valori = await function_to_call(cfg, id, pool)
logger.info("matrice valori creata")
# Load the data into the database
if load_data(cfg, matrice_valori):
update_status(cfg, id, DATA_LOADED)
if await load_data(cfg, matrice_valori, pool):
await update_status(cfg, id, DATA_LOADED, pool)
else:
logger.warning(f"Action '{action}' non riconosciuta.")