diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b042241 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.python-version +DT0028_20241106044856.csv +DT0029_20241106044856.csv +DT0030_20241106104859.csv +input_file.txt +README.md +uv.lock +csv_ase.egg-info/dependency_links.txt +csv_ase.egg-info/PKG-INFO +csv_ase.egg-info/requires.txt +csv_ase.egg-info/SOURCES.txt +csv_ase.egg-info/top_level.txt +dist/csv_ase-0.1.0-py3-none-any.whl +dist/csv_ase-0.1.0.tar.gz diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..35fe170 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "CodeGPT.apiKey": "Ollama" +} \ No newline at end of file diff --git a/transform_file.py b/transform_file.py index b093b2a..b7fc1a7 100644 --- a/transform_file.py +++ b/transform_file.py @@ -20,6 +20,7 @@ if len(lines) >= 7: else: raise ValueError("Il file non contiene abbastanza righe per estrarre i dati richiesti.") +records = [] # Elabora le righe dei dati a partire dalla riga 8 in poi for line in lines[7:]: # Rimuovi spazi bianchi o caratteri di nuova riga @@ -41,7 +42,6 @@ for line in lines[7:]: nodes = parts[2:] node_list = [] - records = [] for i, node_data in enumerate(nodes, start=1): # Dividi ogni nodo in valori separati da ";" node_values = node_data.split(';') @@ -71,22 +71,26 @@ for line in lines[7:]: "temperature": float(measurements.split(';')[1]), "nodes_jsonb": json.dumps(node_list) # Converti la lista di dizionari in una stringa JSON } + records.append(record) -# Esegui l'upsert con un comando SQL with engine.connect() as conn: - with conn.execution_options(isolation_level='AUTOCOMMIT'): - result = conn.execute(text(""" - INSERT INTO dataraw (tipo_centralina, unit, ip_centralina, path, ip_gateway, event_timestamp, battery_level, temperature, nodes_jsonb) - VALUES (:tipo_centralina, :unit, :ip_centralina, :path, :ip_gateway, :event_timestamp, :battery_level, :temperature, :nodes_jsonb) - ON CONFLICT ON CONSTRAINT dataraw_unique - DO UPDATE SET - path = EXCLUDED.path, - ip_gateway = EXCLUDED.ip_gateway, - battery_level = EXCLUDED.battery_level, - temperature = EXCLUDED.temperature, - nodes_jsonb = EXCLUDED.nodes_jsonb; - """), records) - print(result) + conn.execute(text(""" + INSERT INTO dataraw (tipo_centralina, unit, ip_centralina, path, ip_gateway, event_timestamp, battery_level, temperature, nodes_jsonb) + VALUES + """ + ",".join([ + f"(:{i}_tipo_centralina, :{i}_unit, :{i}_ip_centralina, :{i}_path, :{i}_ip_gateway, :{i}_event_timestamp, :{i}_battery_level, :{i}_temperature, :{i}_nodes_jsonb)" + for i in range(len(records)) + ]) + """ + ON CONFLICT ON CONSTRAINT dataraw_unique + DO UPDATE SET + path = EXCLUDED.path, + ip_gateway = EXCLUDED.ip_gateway, + battery_level = EXCLUDED.battery_level, + temperature = EXCLUDED.temperature, + nodes_jsonb = EXCLUDED.nodes_jsonb; + """), {f"{i}_{key}": value for i, record in enumerate(records) for key, value in record.items()}) + + conn.commit() print("Tutte le righe del file sono state caricate con successo nella tabella PostgreSQL!")