add comment
This commit is contained in:
@@ -4,14 +4,28 @@ import asyncio
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
timestamp_cols = ['inserted_at', 'loaded_at', 'elaborated_at']
|
||||
timestamp_cols = ["inserted_at", "loaded_at", "elaborated_at"]
|
||||
|
||||
|
||||
async def load_data(cfg: object, matrice_valori: list, pool) -> bool :
|
||||
async def load_data(cfg: object, matrice_valori: list, pool) -> bool:
|
||||
"""Carica una lista di record di dati grezzi nel database.
|
||||
|
||||
Esegue un'operazione di inserimento massivo (executemany) per caricare i dati.
|
||||
Utilizza la clausola 'ON DUPLICATE KEY UPDATE' per aggiornare i record esistenti.
|
||||
Implementa una logica di re-tentativo in caso di deadlock.
|
||||
|
||||
Args:
|
||||
cfg (object): L'oggetto di configurazione contenente i nomi delle tabelle e i parametri di re-tentativo.
|
||||
matrice_valori (list): Una lista di tuple, dove ogni tupla rappresenta una riga da inserire.
|
||||
pool: Il pool di connessioni al database.
|
||||
|
||||
Returns:
|
||||
bool: True se il caricamento ha avuto successo, False altrimenti.
|
||||
"""
|
||||
if not matrice_valori:
|
||||
logger.info("Nulla da caricare.")
|
||||
return True
|
||||
sql_insert_RAWDATA = f'''
|
||||
sql_insert_RAWDATA = f"""
|
||||
INSERT INTO {cfg.dbrawdata} (
|
||||
`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,
|
||||
`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,
|
||||
@@ -47,7 +61,7 @@ async def load_data(cfg: object, matrice_valori: list, pool) -> bool :
|
||||
`TemperatureModule` = IF({cfg.dbrawdata}.`TemperatureModule` != new_data.TemperatureModule, new_data.TemperatureModule, {cfg.dbrawdata}.`TemperatureModule`),
|
||||
`RssiModule` = IF({cfg.dbrawdata}.`RssiModule` != new_data.RssiModule, new_data.RssiModule, {cfg.dbrawdata}.`RssiModule`),
|
||||
`Created_at` = NOW()
|
||||
'''
|
||||
"""
|
||||
|
||||
rc = False
|
||||
async with pool.acquire() as conn:
|
||||
@@ -65,10 +79,12 @@ async def load_data(cfg: object, matrice_valori: list, pool) -> bool :
|
||||
logging.error(f"Error: {e}.")
|
||||
|
||||
if e.args[0] == 1213: # Deadlock detected
|
||||
logging.warning(f"Deadlock detected, attempt {attempt + 1}/{cfg.max_retries}")
|
||||
logging.warning(
|
||||
f"Deadlock detected, attempt {attempt + 1}/{cfg.max_retries}"
|
||||
)
|
||||
|
||||
if attempt < cfg.max_retries - 1:
|
||||
delay = (2 * attempt)
|
||||
delay = 2 * attempt
|
||||
await asyncio.sleep(delay)
|
||||
continue
|
||||
else:
|
||||
@@ -76,29 +92,64 @@ async def load_data(cfg: object, matrice_valori: list, pool) -> bool :
|
||||
raise
|
||||
return rc
|
||||
|
||||
|
||||
async def update_status(cfg: object, id: int, status: int, pool) -> None:
|
||||
"""Aggiorna lo stato di un record nella tabella dei record CSV.
|
||||
|
||||
Args:
|
||||
cfg (object): L'oggetto di configurazione contenente il nome della tabella.
|
||||
id (int): L'ID del record da aggiornare.
|
||||
status (int): Il nuovo stato da impostare.
|
||||
pool: Il pool di connessioni al database.
|
||||
"""
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.execute(f'update {cfg.dbrectable} set status = {status}, {timestamp_cols[status]} = now() where id = {id}')
|
||||
await cur.execute(
|
||||
f"update {cfg.dbrectable} set status = {status}, {timestamp_cols[status]} = now() where id = {id}"
|
||||
)
|
||||
await conn.commit()
|
||||
logging.info("Status updated.")
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
logging.error(f"Error: {e}")
|
||||
|
||||
|
||||
async def unlock(cfg: object, id: int, pool) -> None:
|
||||
"""Sblocca un record nella tabella dei record CSV.
|
||||
|
||||
Imposta il campo 'locked' a 0 per un dato ID.
|
||||
|
||||
Args:
|
||||
cfg (object): L'oggetto di configurazione contenente il nome della tabella.
|
||||
id (int): L'ID del record da sbloccare.
|
||||
pool: Il pool di connessioni al database.
|
||||
"""
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.execute(f'update {cfg.dbrectable} set locked = 0 where id = {id}')
|
||||
await cur.execute(
|
||||
f"update {cfg.dbrectable} set locked = 0 where id = {id}"
|
||||
)
|
||||
await conn.commit()
|
||||
logging.info(f"id {id} unlocked.")
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
logging.error(f"Error: {e}")
|
||||
|
||||
|
||||
async def get_matlab_cmd(cfg: object, unit: str, tool: str, pool) -> tuple:
|
||||
"""Recupera le informazioni per l'esecuzione di un comando Matlab dal database.
|
||||
|
||||
Args:
|
||||
cfg (object): L'oggetto di configurazione.
|
||||
unit (str): Il nome dell'unità.
|
||||
tool (str): Il nome dello strumento.
|
||||
pool: Il pool di connessioni al database.
|
||||
|
||||
Returns:
|
||||
tuple: Una tupla contenente le informazioni del comando Matlab, o None in caso di errore.
|
||||
"""
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
@@ -110,4 +161,4 @@ async def get_matlab_cmd(cfg: object, unit: str, tool: str, pool) -> tuple:
|
||||
where t.name = "{tool}" and u.name = "{unit}"''')
|
||||
return cur.fetchone()
|
||||
except Exception as e:
|
||||
logging.error(f'Error: {e}')
|
||||
logging.error(f"Error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user