diff --git a/src/monitor/app.py b/src/monitor/app.py index a9dfe37..ad044dd 100644 --- a/src/monitor/app.py +++ b/src/monitor/app.py @@ -74,14 +74,19 @@ def _front_get(url_or_path, params=None): raise RuntimeError(f"Front GET {url_or_path} failed ({e.code}): {body}") +MAX_PAGES = 10 + + def _front_paginate(path, params=None): results = [] data = _front_get(path, params) results.extend(data.get("_results", [])) + pages = 1 - while data.get("_pagination", {}).get("next"): + while data.get("_pagination", {}).get("next") and pages < MAX_PAGES: data = _front_get(data["_pagination"]["next"]) results.extend(data.get("_results", [])) + pages += 1 return results @@ -260,12 +265,24 @@ def handler(event, context): now = datetime.now(timezone.utc) + monitor_names = os.environ.get("MONITOR_INBOXES", "").strip() + if monitor_names: + monitor_set = {n.strip().lower() for n in monitor_names.split(",")} + else: + monitor_set = None + inboxes = _front_paginate("/inboxes") shared = [i for i in inboxes if not i.get("is_private", False)] - logger.info("Found %d shared inboxes", len(shared)) + + if monitor_set: + shared = [i for i in shared if i.get("name", "").lower() in monitor_set] + + logger.info("Monitoring %d inboxes: %s", len(shared), + ", ".join(i.get("name", "?") for i in shared)) tier1_count = 0 tier2_count = 0 + seen = set() for inbox in shared: inbox_id = inbox["id"] @@ -280,9 +297,12 @@ def handler(event, context): logger.error("Failed to fetch conversations for %s: %s", inbox_name, e) continue - logger.info("Inbox '%s': %d open conversations", inbox_name, len(conversations)) + logger.info("Inbox '%s': %d recent open conversations", inbox_name, len(conversations)) for conv in conversations: + if conv.get("id") in seen: + continue + seen.add(conv.get("id")) try: last_msg = conv.get("last_message") if not last_msg or last_msg.get("type") != "inbound": diff --git a/template.yaml b/template.yaml index 7cd3aa0..1450c77 100644 --- a/template.yaml +++ b/template.yaml @@ -24,11 +24,15 @@ Parameters: Type: Number Default: 1440 Description: Business minutes before Tier 2 alert (1 business day) + MonitorInboxes: + Type: String + Default: "Triage,California,West Coast,Central,East Coast,Vendors" + Description: Comma-separated inbox names to monitor (empty = all shared) Globals: Function: Runtime: python3.12 - Timeout: 120 + Timeout: 300 MemorySize: 128 Architectures: - arm64 @@ -71,6 +75,7 @@ Resources: TABLE_NAME: !Ref AlertsTable ACK_SLA_MINUTES: !Ref AckSlaMinutes ACTION_SLA_MINUTES: !Ref ActionSlaMinutes + MONITOR_INBOXES: !Ref MonitorInboxes Policies: - DynamoDBCrudPolicy: TableName: !Ref AlertsTable