#!/usr/bin/env python3 import argparse import sys from datetime import datetime, timezone from common import ( ClinicLoginRequiredError, DEFAULT_BASE_URL, DEFAULT_CHROME_PROFILE, clinic_login_state, login_hint, write_json, ) def to_iso8601(timestamp: int | None) -> str | None: if timestamp is None: return None return datetime.fromtimestamp(timestamp, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") def main() -> int: parser = argparse.ArgumentParser( description="Check whether a reusable local Clinic browser session exists in Chrome" ) parser.add_argument( "--base-url", default=DEFAULT_BASE_URL, help="Clinic base URL (default: env CLINIC_BASE_URL or https://clinic.pingcap.com.cn)", ) parser.add_argument( "--profile", default=DEFAULT_CHROME_PROFILE, help="Chrome profile name. Use 'auto' to scan profiles for clinic.session_id " "(default: env CLINIC_CHROME_PROFILE or auto).", ) args = parser.parse_args() try: state = clinic_login_state(base_url=args.base_url, profile_name=args.profile) except ClinicLoginRequiredError as exc: print(exc, file=sys.stderr) return 1 write_json( { "ok": True, "base_url": state["base_url"], "profile_name": state["profile_name"], "expires_at": state["expires_at"], "expires_at_rfc3339": to_iso8601(state["expires_at"]), "privacy_note": ( "The preflight confirms the Chrome session is reusable without printing the raw session cookie, " "CSRF token, account email, or local cookie DB path." ), "next_step": ( "Browser login is ready. You can now run discover_queries.py, basic_healthy_check.py, " "or metrics_only_perf_diagnosis.py with the same --base-url / --profile values." ), "login_reminder": login_hint( base_url=state["base_url"], profile_name=state["profile_name"], ), } ) return 0 if __name__ == "__main__": raise SystemExit(main())