This commit is contained in:
2025-04-26 16:39:39 +02:00
parent ddd45d7276
commit dc6ccc1744
9 changed files with 802 additions and 106 deletions

View File

@@ -6,18 +6,15 @@ import os
import re
from datetime import datetime
import json
import psycopg2
from psycopg2.extras import execute_values
import mysql.connector as mysql
import logging
import psycopg2.sql
from utils.time import timestamp_fmt as ts
from utils.config import set_config as setting
def conn_db(cfg):
return psycopg2.connect(dbname=cfg.dbname, user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport )
return mysql.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport )
def extract_value(patterns, source, default='Not Defined'):
ip = {}
@@ -41,21 +38,11 @@ def write_db(records, cfg):
]
query = f"""
INSERT INTO {cfg.dbschema}.{cfg.dbdataraw} (
INSERT IGNORE INTO {cfg.dbname}.{cfg.dbdataraw} (
unit_name, unit_type, tool_name, tool_type, unit_ip, unit_subnet, unit_gateway,
event_timestamp, battery_level, temperature, nodes_jsonb
)
VALUES %s
ON CONFLICT ON CONSTRAINT dataraw_unique
DO UPDATE SET
unit_type = EXCLUDED.unit_type,
tool_type = EXCLUDED.tool_type,
unit_ip = EXCLUDED.unit_ip,
unit_subnet = EXCLUDED.unit_subnet,
unit_gateway = EXCLUDED.unit_gateway,
battery_level = EXCLUDED.battery_level,
temperature = EXCLUDED.temperature,
nodes_jsonb = EXCLUDED.nodes_jsonb;
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
try:
@@ -63,12 +50,12 @@ def write_db(records, cfg):
conn.autocommit = True
with conn.cursor() as cur:
try:
execute_values(cur, query, insert_values)
cur.executemany(query, insert_values)
cur.close()
conn.commit()
except psycopg2.Error as e:
except Exception as e:
logging.error(f'Records not inserted: {e}')
logging.info(f'Exit')
logging.info('Exit')
exit()
except Exception as e:
logging.error(f'Records not inserted: {e}')
@@ -78,9 +65,9 @@ def elab_csv(cfg):
try:
with conn_db(cfg) as conn:
cur = conn.cursor()
cur.execute(f'select id, unit_name, unit_type, tool_name, tool_type, tool_data from {cfg.dbschema}.{cfg.dbrectable} where locked = 0 and status = 0')
cur.execute(f'select id, unit_name, unit_type, tool_name, tool_type, tool_data from {cfg.dbname}.{cfg.dbrectable} where locked = 0 and status = 0 limit 1')
id, unit_name, unit_type, tool_name, tool_type, tool_data = cur.fetchone()
cur.execute(f'update {cfg.dbschema}.{cfg.dbrectable} set locked = 1 where id = {id}')
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 1 where id = {id}')
data_list = str(tool_data).strip("('{\"").strip("\"}\',)").split('","')
# Estrarre le informazioni degli ip dalla header
infos = extract_value(cfg.csv_infos, str(data_list[:9]))