import gzip import urllib.request import urllib.error import os import sys import time #SOURCE = "/root/home_assistant_export.lp.gz" #URL = "http://127.0.0.1:8181/write?db=homeassistant&precision=ns" MAX_BATCH_BYTES = 4 * 1024 * 1024 MAX_BATCH_LINES = 20000 token = os.environ.get("HA_TOKEN") if not token: print("FEHLER: HA_TOKEN ist nicht gesetzt.") sys.exit(1) headers = { "Authorization": f"Bearer {token}", "Content-Type": "text/plain", "Accept": "application/json", } total_lines = 0 batch_no = 0 batch = [] batch_bytes = 0 start = time.time() def send_batch(lines): global batch_no if not lines: return data = "".join(lines).encode("utf-8") req = urllib.request.Request( URL, data=data, headers=headers, method="POST" ) try: with urllib.request.urlopen(req, timeout=120) as r: if r.status != 204: raise RuntimeError(f"HTTP {r.status}") except urllib.error.HTTPError as e: body = e.read().decode("utf-8", errors="replace") print(f"\nFEHLER in Batch {batch_no + 1}: HTTP {e.code}") print(body) sys.exit(1) except Exception as e: print(f"\nFEHLER in Batch {batch_no + 1}: {e}") sys.exit(1) batch_no += 1 with gzip.open(SOURCE, "rt", encoding="utf-8") as f: for line in f: batch.append(line) batch_bytes += len(line.encode("utf-8")) total_lines += 1 if batch_bytes >= MAX_BATCH_BYTES or len(batch) >= MAX_BATCH_LINES: send_batch(batch) batch = [] batch_bytes = 0 elapsed = time.time() - start rate = total_lines / elapsed if elapsed else 0 print( f"Batch {batch_no}: " f"{total_lines:,} Zeilen importiert " f"({rate:,.0f} Zeilen/s)", flush=True ) if batch: send_batch(batch) elapsed = time.time() - start print() print("IMPORT ABGESCHLOSSEN") print(f"Zeilen: {total_lines:,}") print(f"Batches: {batch_no:,}") print(f"Dauer: {elapsed/60:.1f} Minuten")