Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

3.9 KB
Raw Blame
import type { AlertDeliveryDiagnosis } from '@/types/api';

export interface AlertDeliveryStatusLine {
  label: string;
  // 'attention' marks held notifications the user may not expect; 'muted'
  // marks healthy or user-chosen states.
  tone: 'muted' | 'attention';
}

// Short chip label for an event-log hold reason (event.reason on
// notification_suppressed / notification_deferred events). Falls back to the
// raw reason so a future backend reason is shown rather than hidden.
export const describeAlertEventReason = (reason?: string): string => {
  switch ((reason || '').split(':')[0]) {
    case 'acknowledged':
      return 'Acknowledged';
    case 'flapping':
      return 'Flapping';
    case 'notifications_inactive':
      return 'Delivery not turned on';
    case 'notifications_disabled':
      return 'Notifications off';
    case 'monitor_only':
      return 'Monitor-only';
    case 'quiet_hours':
      return 'Quiet hours';
    case 'rate_limited':
      return 'Hourly limit';
    case 'suppression_window':
      return 'Paused';
    case 'cooldown':
      return 'Cooldown';
    default:
      return reason || 'Held';
  }
};

const formatShortTime = (value?: string): string | null => {
  if (!value) return null;
  const parsed = new Date(value);
  if (Number.isNaN(parsed.getTime())) return null;
  return parsed.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
};

// describeAlertDeliveryStatus turns a delivery diagnosis into the one-line
// notification status shown on the alert card. Returns null when the card
// should show no line (acknowledged alerts already carry a badge, and an
// unknown reason is better silent than wrong).
export const describeAlertDeliveryStatus = (
  diagnosis: AlertDeliveryDiagnosis | undefined,
  acknowledged: boolean,
): AlertDeliveryStatusLine | null => {
  if (!diagnosis || acknowledged) return null;

  const reason = (diagnosis.reason || '').split(':')[0];

  // lastNotified is recorded before delivery callbacks; it is dispatch evidence,
  // not confirmation that a destination accepted or a person received anything.
  if (diagnosis.status === 'would_send') {
    const notifiedAt = formatShortTime(diagnosis.lastNotified);
    if (notifiedAt) return { label: `Dispatch requested ${notifiedAt}`, tone: 'muted' };
    return { label: 'Notification pending', tone: 'muted' };
  }

  if (diagnosis.status === 'deferred') {
    const replayAt = formatShortTime(diagnosis.quietHoursReplayAt);
    return {
      label: replayAt ? `Quiet hours — notifies ${replayAt}` : 'Quiet hours — notifies later',
      tone: 'muted',
    };
  }

  switch (reason) {
    case 'acknowledged':
      return null;
    case 'cooldown': {
      const notifiedAt = formatShortTime(diagnosis.lastNotified);
      const nextAt = formatShortTime(diagnosis.nextEligibleAt);
      if (notifiedAt && nextAt) {
        return {
          label: `Dispatch requested ${notifiedAt} — next eligible ${nextAt}`,
          tone: 'muted',
        };
      }
      if (notifiedAt) return { label: `Dispatch requested ${notifiedAt}`, tone: 'muted' };
      return { label: 'Waiting for cooldown', tone: 'muted' };
    }
    case 'rate_limited':
      return { label: 'Hourly notification limit reached', tone: 'attention' };
    case 'flapping':
      return { label: 'Flapping — notifications paused', tone: 'attention' };
    case 'suppression_window': {
      const until = formatShortTime(diagnosis.suppressedUntil);
      return {
        label: until ? `Notifications paused until ${until}` : 'Notifications paused',
        tone: 'attention',
      };
    }
    case 'notifications_disabled':
      return { label: 'Notifications are turned off', tone: 'attention' };
    case 'notifications_inactive':
      return { label: 'Notification delivery not turned on', tone: 'attention' };
    case 'monitor_only':
      return { label: 'Monitor-only — no notifications', tone: 'muted' };
    default:
      return null;
  }
};