Reads IDs from CSV or TXT, writes per-query SUCCESS, NOT_FOUND or FAILED rows to CSV or JSON Lines and mirrors results into SQLite.
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
||
"""Look up point alarm registrations by registrationId or recipientId from a CSV/TXT list."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import logging
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from common import ConfigurationError, IklimClient, configure_logging
|
||
from common.errors import ApiError
|
||
from point_alarm import (
|
||
RESULT_COLUMNS,
|
||
PointAlarmService,
|
||
PointStore,
|
||
lookup_rows,
|
||
read_id_list,
|
||
timestamped_path,
|
||
write_records,
|
||
)
|
||
|
||
|
||
BASE_DIR = Path(__file__).resolve().parent
|
||
|
||
|
||
def parse_args() -> argparse.Namespace:
|
||
parser = argparse.ArgumentParser(
|
||
description=(
|
||
"Nokta alarm kayıtlarını get-by-id veya get-by-recipient-id ile sorgular. Girdi CSV "
|
||
"(başlıkta registrationId/recipientId sütunu, ';' ayraçlı) veya TXT (satır başına bir ID; "
|
||
"boş ve '#' ile başlayan satırlar atlanır) olabilir. Sonuçlar SQLite'a da yazılır."
|
||
)
|
||
)
|
||
parser.add_argument("input_file", type=Path, help="ID listesi (.csv veya .txt)")
|
||
parser.add_argument(
|
||
"--by",
|
||
choices=["id", "recipient"],
|
||
default="id",
|
||
help="Sorgu türü: id=registrationId, recipient=recipientId (varsayılan: id)",
|
||
)
|
||
parser.add_argument(
|
||
"--output",
|
||
type=Path,
|
||
required=True,
|
||
help=(
|
||
"Çıktı dosyası: .csv (noktalı virgüllü) veya .txt (JSON Lines); dosya adına zaman soneki "
|
||
"eklenir. Satır durumları: SUCCESS, NOT_FOUND (HTTP 404), FAILED (httpStatus+errorBody). "
|
||
"Bir recipientId birden çok kayıt dönerse her kayıt ayrı satırdır"
|
||
),
|
||
)
|
||
parser.add_argument(
|
||
"--db", type=Path, default=BASE_DIR / "iklim-sandbox.db", help="SQLite dosyası"
|
||
)
|
||
parser.add_argument(
|
||
"--debug-log",
|
||
type=Path,
|
||
default=BASE_DIR / "logs" / "iklim-api-debug.log",
|
||
help="Maskelenmiş HTTP debug log dosyası",
|
||
)
|
||
return parser.parse_args()
|
||
|
||
|
||
def main() -> int:
|
||
args = parse_args()
|
||
configure_logging(args.debug_log)
|
||
store = None
|
||
try:
|
||
column = "registrationId" if args.by == "id" else "recipientId"
|
||
queries = read_id_list(args.input_file, column)
|
||
client = IklimClient.from_env(BASE_DIR / ".env")
|
||
service = PointAlarmService(client)
|
||
store = PointStore(args.db)
|
||
rows, webhooks = lookup_rows(service, store, queries, args.by, None)
|
||
path = timestamped_path(args.output)
|
||
write_records(path, rows, webhooks, columns=RESULT_COLUMNS)
|
||
failed = sum(1 for row in rows if row["status"] == "FAILED")
|
||
print(f"{len(queries)} sorgu, {len(rows)} satır yazıldı: {path.resolve()}")
|
||
return 1 if failed else 0
|
||
except (ConfigurationError, ApiError, OSError) as exc:
|
||
logging.error("İşlem durduruldu: %s", exc)
|
||
return 1
|
||
finally:
|
||
if store:
|
||
store.close()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|