fix GD
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!.venv/bin/python
|
||||
from utils.database.nodes_query import get_nodes_type
|
||||
from utils.timestamp.date_check import normalizza_data, normalizza_orario
|
||||
from utils.database.loader_action import find_nearest_timestamp
|
||||
import logging
|
||||
import re
|
||||
|
||||
@@ -224,8 +225,11 @@ async def make_gd_matrix(cfg: object, id: int, pool: object) -> list:
|
||||
unit_metrics, data = rilevazioni.split(';|;')
|
||||
batlevel, temperature = unit_metrics.split(';')
|
||||
#logger.debug(f"GD id {id}: {EventDate}, {EventTime}, {batlevel}, {temperature}, {data}")
|
||||
|
||||
dt_timestamp, dt_batlevel, dt_temperature = await find_nearest_timestamp(cfg, {"timestamp": f"{normalizza_data(EventDate)} {normalizza_orario(EventTime)}", "unit": UnitName, "tool": ToolNameID.replace("GD", "DT"), "node_num": 1}, pool)
|
||||
EventDate, EventTime = dt_timestamp.strftime('%Y-%m-%d %H:%M:%S').split(' ')
|
||||
valori = data.split(';')
|
||||
matrice_valori.append([UnitName, ToolNameID.replace("GD", "DT"), 2, normalizza_data(EventDate), normalizza_orario(EventTime), batlevel, temperature] + valori + ([None] * (19 - len(valori))))
|
||||
matrice_valori.append([UnitName, ToolNameID.replace("GD", "DT"), 2, EventDate, EventTime, float(dt_batlevel), float(dt_temperature)] + valori + ([None] * (16 - len(valori))) + [batlevel, temperature, None])
|
||||
else:
|
||||
logger.warning(f"GD id {id}: dati non trattati - {rilevazioni}")
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
import asyncio
|
||||
|
||||
from utils.database import FLAG_TO_TIMESTAMP, BATCH_SIZE
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -189,6 +190,43 @@ async def get_matlab_cmd(cfg: object, unit: str, tool: str, pool: object) -> tup
|
||||
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()
|
||||
return await cur.fetchone()
|
||||
except Exception as e:
|
||||
logger.error(f"Error: {e}")
|
||||
|
||||
async def find_nearest_timestamp(cfg: object, unit_tool_data: dict, pool: object) -> tuple:
|
||||
"""
|
||||
Finds the nearest timestamp in the raw data table based on a reference timestamp
|
||||
and unit/tool/node information.
|
||||
|
||||
Args:
|
||||
cfg (object): Configuration object containing database table name (`cfg.dbrawdata`).
|
||||
unit_tool_data (dict): A dictionary containing:
|
||||
- "timestamp" (str): The reference timestamp string in "%Y-%m-%d %H:%M:%S" format.
|
||||
- "unit" (str): The UnitName to filter by.
|
||||
- "tool" (str): The ToolNameID to filter by.
|
||||
- "node_num" (int): The NodeNum to filter by.
|
||||
pool (object): The database connection pool.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the event timestamp, BatLevel, and Temperature of the
|
||||
nearest record, or None if an error occurs or no record is found.
|
||||
"""
|
||||
|
||||
ref_timestamp = datetime.strptime(unit_tool_data["timestamp"], "%Y-%m-%d %H:%M:%S")
|
||||
start_timestamp = ref_timestamp - timedelta(seconds=45)
|
||||
end_timestamp = ref_timestamp + timedelta(seconds=45)
|
||||
logger.info(f"Find nearest timestamp: {ref_timestamp}")
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.execute(f'''SELECT TIMESTAMP(`EventDate`, `EventTime`) AS event_timestamp, BatLevel, Temperature
|
||||
FROM {cfg.dbrawdata}
|
||||
WHERE UnitName = "{unit_tool_data["unit"]}" AND ToolNameID = "{unit_tool_data["tool"]}" AND NodeNum = {unit_tool_data["node_num"]}
|
||||
AND TIMESTAMP(`EventDate`, `EventTime`) BETWEEN "{start_timestamp}" AND "{end_timestamp}"
|
||||
ORDER BY ABS(TIMESTAMPDIFF(SECOND, TIMESTAMP(`EventDate`, `EventTime`), "{ref_timestamp}"))
|
||||
LIMIT 1
|
||||
''')
|
||||
return await cur.fetchone()
|
||||
except Exception as e:
|
||||
logger.error(f"Error: {e}")
|
||||
Reference in New Issue
Block a user