|
| 1 | +"""Checkly-backed service status state and polling utilities.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import contextlib |
| 5 | +from enum import StrEnum |
| 6 | + |
| 7 | +import httpx |
| 8 | + |
| 9 | +import reflex as rx |
| 10 | +from reflex_site_shared.constants import ( |
| 11 | + CHECKLY_ACCOUNT_ID, |
| 12 | + CHECKLY_API_BASE_URL, |
| 13 | + CHECKLY_API_KEY, |
| 14 | + CHECKLY_CHECK_GROUP_ID, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class ServiceStatus(StrEnum): |
| 19 | + """Supported service health states exposed in the UI.""" |
| 20 | + |
| 21 | + SUCCESS = "Success" |
| 22 | + WARNING = "Warning" |
| 23 | + CRITICAL = "Critical" |
| 24 | + |
| 25 | + |
| 26 | +CURRENT_STATUS = ServiceStatus.SUCCESS.value |
| 27 | + |
| 28 | + |
| 29 | +# Check status of each check in parallel |
| 30 | +async def check_status(check_id: str) -> dict: |
| 31 | + """Fetch status flags for a single Checkly check. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + A dictionary with failure and degraded flags. |
| 35 | + """ |
| 36 | + status_url = f"{CHECKLY_API_BASE_URL}/check-statuses/{check_id}" |
| 37 | + async with httpx.AsyncClient() as client: |
| 38 | + status_response = await client.get( |
| 39 | + status_url, |
| 40 | + headers={ |
| 41 | + "Authorization": f"Bearer {CHECKLY_API_KEY}", |
| 42 | + "X-Checkly-Account": CHECKLY_ACCOUNT_ID, |
| 43 | + }, |
| 44 | + ) |
| 45 | + if status_response.status_code == 200: |
| 46 | + status_data = status_response.json() |
| 47 | + return { |
| 48 | + "has_failures": status_data.get("hasFailures", False), |
| 49 | + "is_degraded": status_data.get("isDegraded", False), |
| 50 | + } |
| 51 | + |
| 52 | + return {"has_failures": False, "is_degraded": False} |
| 53 | + |
| 54 | + |
| 55 | +async def monitor_checkly_status() -> None: |
| 56 | + """Continuously monitor Checkly check group status. |
| 57 | +
|
| 58 | + Updates the global STATUS variable every 60 seconds. |
| 59 | + - Critical: if any check has failures |
| 60 | + - Warning: if no failures but some checks are degraded |
| 61 | + - Success: all checks are healthy |
| 62 | +
|
| 63 | + """ |
| 64 | + if not all((CHECKLY_API_KEY, CHECKLY_ACCOUNT_ID, CHECKLY_CHECK_GROUP_ID)): |
| 65 | + return |
| 66 | + |
| 67 | + headers = { |
| 68 | + "Authorization": f"Bearer {CHECKLY_API_KEY}", |
| 69 | + "X-Checkly-Account": CHECKLY_ACCOUNT_ID, |
| 70 | + } |
| 71 | + |
| 72 | + try: |
| 73 | + while True: |
| 74 | + with contextlib.suppress(Exception): |
| 75 | + global CURRENT_STATUS |
| 76 | + |
| 77 | + # Get checks in this group |
| 78 | + checks_url = f"{CHECKLY_API_BASE_URL}/check-groups/{CHECKLY_CHECK_GROUP_ID}/checks" |
| 79 | + async with httpx.AsyncClient(timeout=httpx.Timeout(30)) as client: |
| 80 | + checks_response = await client.get(checks_url, headers=headers) |
| 81 | + if checks_response.status_code == 200: |
| 82 | + checks = checks_response.json() |
| 83 | + |
| 84 | + check_ids = [check.get("id") for check in checks if check.get("id")] |
| 85 | + results = await asyncio.gather(*[ |
| 86 | + check_status(check_id) for check_id in check_ids |
| 87 | + ]) |
| 88 | + |
| 89 | + # Determine overall status |
| 90 | + has_any_failures = any(r["has_failures"] for r in results) |
| 91 | + has_any_degraded = any(r["is_degraded"] for r in results) |
| 92 | + |
| 93 | + if has_any_failures: |
| 94 | + CURRENT_STATUS = ServiceStatus.CRITICAL.value |
| 95 | + elif has_any_degraded: |
| 96 | + CURRENT_STATUS = ServiceStatus.WARNING.value |
| 97 | + else: |
| 98 | + CURRENT_STATUS = ServiceStatus.SUCCESS.value |
| 99 | + |
| 100 | + await asyncio.sleep(60) |
| 101 | + except asyncio.CancelledError: |
| 102 | + pass |
| 103 | + |
| 104 | + |
| 105 | +class StatusState(rx.State): |
| 106 | + """Reflex state that exposes the current service status.""" |
| 107 | + |
| 108 | + @rx.var(interval=60) |
| 109 | + def status(self) -> str: |
| 110 | + """Return the current status value for the status pill.""" |
| 111 | + return CURRENT_STATUS |
0 commit comments