evol 5
This commit is contained in:
@@ -3,7 +3,7 @@ import mysql.connector
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def connetti_db(cfg):
|
||||
def connetti_db(cfg: object) -> object:
|
||||
"""
|
||||
Establishes a connection to a MySQL database.
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#!.venv/bin/python
|
||||
from utils.database.connection import connetti_db
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def load_data(cfg, matrice_valori):
|
||||
sql_insert_RAWDATA = f'''
|
||||
INSERT IGNORE INTO {cfg.dbname}.{cfg.dbrawdata} (
|
||||
`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,
|
||||
`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,
|
||||
`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`,
|
||||
`BatLevelModule`,`TemperatureModule`, `RssiModule`
|
||||
)
|
||||
VALUES (
|
||||
%s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s
|
||||
)
|
||||
'''
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.executemany(sql_insert_RAWDATA, matrice_valori)
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
print(f'Error: {e}')
|
||||
finally:
|
||||
cur.close()
|
||||
conn.close()
|
||||
63
utils/database/loader_action.py
Normal file
63
utils/database/loader_action.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#!.venv/bin/python
|
||||
from utils.database.connection import connetti_db
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CSV_RECEIVED = 0
|
||||
DATA_LOADED = 1
|
||||
DATA_ELABORATED = 2
|
||||
|
||||
def load_data(cfg: object, matrice_valori: list) -> bool :
|
||||
sql_insert_RAWDATA = f'''
|
||||
INSERT IGNORE INTO {cfg.dbname}.{cfg.dbrawdata} (
|
||||
`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,
|
||||
`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,
|
||||
`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`,
|
||||
`BatLevelModule`,`TemperatureModule`, `RssiModule`
|
||||
)
|
||||
VALUES (
|
||||
%s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s
|
||||
)
|
||||
'''
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.executemany(sql_insert_RAWDATA, matrice_valori)
|
||||
conn.commit()
|
||||
logging.info("Data loaded.")
|
||||
rc = True
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
logging.error(f"Error: {e}.")
|
||||
rc = False
|
||||
finally:
|
||||
conn.close()
|
||||
return rc
|
||||
|
||||
def update_status(cfg: object, id: int, status: int) -> None:
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 0, status = {status} where id = {id}')
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
|
||||
def get_matlab_cmd(cfg: object, unit: str, tool: str) -> tuple:
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'''select m.matcall, t.ftp_send , t.unit_id, s.`desc` as statustools, t.api_send, u.inoltro_api, u.inoltro_api_url, u.inoltro_api_bearer_token, IFNULL(u.duedate, "") as duedate
|
||||
from matfuncs as m
|
||||
inner join tools as t on t.matfunc = m.id
|
||||
inner join units as u on u.id = t.unit_id
|
||||
inner join statustools as s on t.statustool_id = s.id
|
||||
where t.name = "{tool}" and u.name = "{unit}"''')
|
||||
return cur.fetchone()
|
||||
except Exception as e:
|
||||
logging.error(f'Error: {e}')
|
||||
@@ -3,7 +3,7 @@ import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_nodes_type(cfg, tool, unit):
|
||||
def get_nodes_type(cfg: object, tool: str, unit: str) -> tuple:
|
||||
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor(dictionary=True)
|
||||
@@ -16,7 +16,7 @@ def get_nodes_type(cfg, tool, unit):
|
||||
WHERE y.type NOT IN ('Anchor Link', 'None') AND t.name = '{tool}' AND u.name = '{unit}'
|
||||
ORDER BY n.num;
|
||||
"""
|
||||
logger.info(f"{unit} - {tool}: Executing query: {query}")
|
||||
#logger.info(f"{unit} - {tool}: Executing query: {query}")
|
||||
cur.execute(query)
|
||||
results = cur.fetchall()
|
||||
logger.info(f"{unit} - {tool}: {cur.rowcount} rows selected to get node type/Ain/Din/channels.")
|
||||
|
||||
Reference in New Issue
Block a user