"""Print {ticket_id: dedup_key} for Gandalf's tickets that are still open in Tinker (gandalf#66). Run in Gandalf's working directory (it reads config.json), then hand the output to Tinker: sudo -u www-data python3 scripts/dedup_key_map.py > gandalf-keys.json php scripts/backfill_dedup_keys.php --map=gandalf-keys.json --dry-run # on Tinker Read only: it changes nothing in Gandalf or Tinker. Run the backfill when the dedup_key change deploys, so a recurrence matches its open ticket instead of opening a new one. """ import json import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import db # noqa: E402 import ticket_text # noqa: E402 from monitor import TicketClient # noqa: E402 def main() -> int: with open('config.json') as f: cfg = json.load(f) open_ids = TicketClient(cfg.get('ticket_api', {})).open_tickets() if open_ids is None: print('Could not read the open ticket list from Tinker', file=sys.stderr) return 1 with db.get_conn() as conn: with conn.cursor() as cur: cur.execute('SELECT id, event_type, target_name, target_detail, first_seen, ticket_id ' 'FROM network_events WHERE ticket_id IS NOT NULL') events = cur.fetchall() print(json.dumps(ticket_text.dedup_key_map(events, open_ids), indent=1, sort_keys=True)) return 0 if __name__ == '__main__': sys.exit(main())