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.

18.4 KB
Raw Blame
import { getActiveLocale, t } from '@/i18n';
import { getAllFilterOptionLabel } from '@/components/shared/filterOptionPresentation';

export const ALERTS_EMPTY_STATE = 'No active alerts';
export const ALERTS_THRESHOLD_HINT = 'Alerts will appear here when thresholds are exceeded';
export const ALERT_TIMELINE_LOADING_STATE = 'Loading timeline...';
export const ALERT_TIMELINE_FILTER_EMPTY_STATE = 'No timeline events match the selected filters.';
export const ALERT_TIMELINE_EMPTY_STATE = 'No timeline events yet.';
export const ALERT_TIMELINE_UNAVAILABLE_STATE = 'No incident timeline available.';
export const ALERT_TIMELINE_FAILURE_STATE = 'Failed to load timeline.';
export const ALERT_TIMELINE_RETRY_LABEL = 'Retry';
export const ALERT_HISTORY_SEARCH_PLACEHOLDER = 'Search alerts...';
export const ALERT_HISTORY_EMPTY_STATE = 'No alerts found';
export const ALERT_HISTORY_EMPTY_DESCRIPTION = 'Try adjusting your filters or check back later';
export const ALERT_BUCKET_EMPTY_LABEL = 'No alerts';
export const ALERT_HISTORY_LOADING_STATE = 'Loading alert history...';
export const ALERT_HISTORY_ALL_TIME_FILTER_LABEL = getAllFilterOptionLabel('time');
export const ALERT_OVERVIEW_ACKNOWLEDGED_LABEL = 'Acknowledged';
export const ALERT_OVERVIEW_LAST_24_HOURS_LABEL = 'Triggered (24h)';
export const ALERT_OVERVIEW_WORKLOAD_OVERRIDES_LABEL = 'Workload Overrides';
export const ALERTS_PAGE_DEFAULT_TITLE = 'Alerts';
export const ALERTS_PAGE_OVERVIEW_TITLE = 'Alerts Overview';
export const ALERTS_PAGE_THRESHOLDS_TITLE = 'Alert Thresholds';
export const ALERTS_PAGE_DESTINATIONS_TITLE = 'Notifications';
// The page sets how alerts are delivered (quiet hours, cooldown, grouping,
// recovery, escalation). Maintenance windows are a separate per-resource
// feature, so the title must not promise them.
export const ALERTS_PAGE_SCHEDULE_TITLE = 'Delivery Schedule';
export const ALERTS_PAGE_HISTORY_TITLE = 'Alert History';
export const ALERTS_PAGE_DEFAULT_DESCRIPTION =
  'Review active incidents, inspect alert history, and manage thresholds, notifications, and schedules.';
export const ALERTS_PAGE_OVERVIEW_DESCRIPTION =
  'Review active incidents and confirm current alert coverage across monitored resources.';
export const ALERTS_PAGE_THRESHOLDS_DESCRIPTION =
  'Tune thresholds and scoped overrides for infrastructure, workloads, and integrations.';
export const ALERTS_PAGE_DESTINATIONS_DESCRIPTION =
  'Route alert notifications to email, Apprise, webhook, and mobile push destinations.';
export const ALERTS_PAGE_SCHEDULE_DESCRIPTION =
  'Define quiet hours, grouping, cooldowns, recovery, and escalation behavior for alert delivery.';
export const ALERTS_PAGE_HISTORY_DESCRIPTION =
  'Search prior alerts, review incident timelines, and inspect alert frequency over time.';

export interface AlertOverviewCardPresentation {
  cardClassName: string;
  iconClassName: string;
  resourceClassName: string;
}

export function getAlertsPageHeaderMeta() {
  return {
    overview: {
      title: t('alerts.page.overview.title'),
      description: t('alerts.page.overview.description'),
    },
    thresholds: {
      title: t('alerts.page.thresholds.title'),
      description: t('alerts.page.thresholds.description'),
    },
    destinations: {
      title: t('alerts.page.destinations.title'),
      description: t('alerts.page.destinations.description'),
    },
    schedule: {
      title: t('alerts.page.schedule.title'),
      description: t('alerts.page.schedule.description'),
    },
    history: {
      title: t('alerts.page.history.title'),
      description: t('alerts.page.history.description'),
    },
    default: {
      title: t('alerts.page.default.title'),
      description: t('alerts.page.default.description'),
    },
  } as const;
}

export function getAlertListEmptyState(showAcknowledged: boolean): string {
  return showAcknowledged
    ? t('alerts.overview.filteredEmpty.all')
    : t('alerts.overview.filteredEmpty.unacknowledged');
}

export function getAlertOverviewEmptyState() {
  return {
    title: t('alerts.overview.empty.title'),
    description: t('alerts.overview.empty.description'),
  } as const;
}

export function getAlertOverviewPausedState() {
  return {
    title: t('alerts.overview.paused.title'),
    description: t('alerts.overview.paused.description'),
  } as const;
}

export function getAlertOverviewHydrationState(status: 'pending' | 'ready' | 'unavailable') {
  if (status === 'unavailable') {
    return {
      title: t('alerts.overview.hydration.unavailable.title'),
      description: t('alerts.overview.hydration.unavailable.description'),
      retryLabel: t('alerts.overview.hydration.retry'),
    } as const;
  }

  return {
    title: t('alerts.overview.hydration.pending.title'),
    description: t('alerts.overview.hydration.pending.description'),
    retryLabel: t('alerts.overview.hydration.retry'),
  } as const;
}

export function getAlertOverviewStatsLabels() {
  return {
    last24Hours: t('alerts.overview.stats.triggered24h'),
    acknowledged: t('alerts.overview.stats.acknowledged'),
    workloadOverrides: t('alerts.overview.stats.workloadOverrides'),
  } as const;
}

export function getAlertOverviewActiveSectionTitle(): string {
  return t('alerts.overview.section.activeAlerts');
}

export function getAlertOverviewAcknowledgedToggleLabel(showAcknowledged: boolean): string {
  return showAcknowledged
    ? t('alerts.overview.action.hideAcknowledged')
    : t('alerts.overview.action.showAcknowledged');
}

export function getAlertOverviewBulkAcknowledgeLabel(count: number, processing: boolean): string {
  return processing
    ? t('alerts.overview.action.acknowledging')
    : t('alerts.overview.action.acknowledgeAll', { count });
}

export function getAlertOverviewGroupDisclosureLabel({
  count,
  correlated,
  expanded,
}: {
  count: number;
  correlated: boolean;
  expanded: boolean;
}): string {
  if (expanded) return t('alerts.overview.group.hide');
  if (!correlated) return t('alerts.overview.group.related', { count });
  return t(
    count === 1 ? 'alerts.overview.group.linkedSignal' : 'alerts.overview.group.linkedSignals',
    { count },
  );
}

export function getAlertOverviewGroupAcknowledgeLabel(count: number): string {
  return t('alerts.overview.group.acknowledge', { count });
}

export function getAlertOverviewAcknowledgedBadgeLabel(): string {
  return t('alerts.overview.acknowledgedBadge');
}

export function getAlertOverviewNodeLabel(node: string): string {
  return t('alerts.overview.nodePrefix', { node });
}

export function getAlertOverviewStartedAtLabel(startedAt: string): string {
  return t('alerts.overview.startedAt', { startedAt });
}

export function getAlertOverviewPrimaryActionLabel({
  acknowledged,
  processing,
}: {
  acknowledged: boolean;
  processing: boolean;
}): string {
  if (processing) return t('alerts.overview.action.processing');
  return acknowledged
    ? t('alerts.overview.action.unacknowledge')
    : t('alerts.overview.action.acknowledge');
}

export function getAlertOverviewTimelineActionLabel(isExpanded: boolean): string {
  return isExpanded
    ? t('alerts.overview.action.hideTimeline')
    : t('alerts.overview.action.timeline');
}

export function getAlertOverviewMoreActionsLabel(isExpanded: boolean): string {
  return isExpanded
    ? t('alerts.overview.action.lessActions')
    : t('alerts.overview.action.moreActions');
}

// How long ago an alert started, in the active locale ("10 min ago",
// "vor 10 Min."). `now` comes from the overview's shared minute clock so the
// text keeps moving while the page stays open.
export function formatAlertOverviewStartedAgo(startTime: string, now: number): string {
  const started = new Date(startTime).getTime();
  if (Number.isNaN(started)) return '';
  const seconds = Math.max(0, Math.round((now - started) / 1000));
  const format = new Intl.RelativeTimeFormat(getActiveLocale(), {
    numeric: 'auto',
    style: 'short',
  });
  if (seconds < 60) return format.format(0, 'minute');
  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) return format.format(-minutes, 'minute');
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return format.format(-hours, 'hour');
  return format.format(-Math.floor(hours / 24), 'day');
}

export function getAlertOverviewSeverityCountLabel(
  level: 'critical' | 'warning',
  count: number,
): string {
  return level === 'critical'
    ? t('alerts.overview.summary.critical', { count })
    : t('alerts.overview.summary.warning', { count });
}

export function getAlertOverviewSnoozeLabel(): string {
  return t('alerts.overview.action.snooze');
}

export function getAlertOverviewResumeLabel(): string {
  return t('alerts.overview.action.resume');
}

export function getAlertOverviewCancelLabel(): string {
  return t('alerts.overview.action.cancel');
}

export function getAlertOverviewSnoozeTitle(): string {
  return t('alerts.overview.snooze.title');
}

export function getAlertOverviewSnoozeDescription(): string {
  return t('alerts.overview.snooze.description');
}

export function getAlertOverviewSnoozeOptionLabel(
  preset: 'oneHour' | 'twoHours' | 'eightHours' | 'tomorrowMorning' | 'sevenDays',
): string {
  return t(`alerts.overview.snooze.option.${preset}`);
}

export function getAlertOverviewSnoozedUntilLabel(until: string): string {
  return t('alerts.overview.snoozedUntil', { until });
}

export function getAlertOverviewSnoozedNotification(until: string): string {
  return t('alerts.overview.notification.snoozed', { until });
}

export function getAlertOverviewResumedNotification(): string {
  return t('alerts.overview.notification.resumed');
}

export function getAlertOverviewSnoozeFailureNotification(resuming: boolean): string {
  return t(
    resuming
      ? 'alerts.overview.notification.resumeFailed'
      : 'alerts.overview.notification.snoozeFailed',
  );
}

export function getAlertOverviewRestoredNotification(): string {
  return t('alerts.overview.notification.restored');
}

export function getAlertOverviewAcknowledgedNotification(): string {
  return t('alerts.overview.notification.acknowledged');
}

export function getAlertOverviewAcknowledgementFailureNotification(
  wasAcknowledged: boolean,
): string {
  return wasAcknowledged
    ? t('alerts.overview.notification.restoreFailed')
    : t('alerts.overview.notification.acknowledgeFailed');
}

export function getAlertOverviewBulkAcknowledgedNotification(count: number): string {
  return count === 1
    ? t('alerts.overview.notification.bulkSuccess.singular', { count })
    : t('alerts.overview.notification.bulkSuccess.plural', { count });
}

export function getAlertOverviewBulkAcknowledgeFailureNotification(count: number): string {
  return count === 1
    ? t('alerts.overview.notification.bulkFailure.singular', { count })
    : t('alerts.overview.notification.bulkFailure.plural', { count });
}

export function getAlertOverviewBulkAcknowledgeGenericFailureNotification(): string {
  return t('alerts.overview.notification.bulkFailureGeneric');
}

export function getAlertTimelineLoadingState() {
  return {
    text: t('alerts.timeline.loading'),
  } as const;
}

export function getAlertTimelineFilterEmptyState() {
  return {
    text: t('alerts.timeline.filterEmpty'),
  } as const;
}

export function getAlertTimelineFilterLabel(variant: 'panel' | 'compact'): string {
  return variant === 'panel'
    ? t('alerts.timeline.filterLabel.panel')
    : t('alerts.timeline.filterLabel.compact');
}

export function getAlertTimelineQuickFilterLabel(action: 'all' | 'none'): string {
  return action === 'all'
    ? t('alerts.timeline.quickFilter.all')
    : t('alerts.timeline.quickFilter.none');
}

export function getAlertTimelineEventTypeLabel(type: string): string {
  switch (type) {
    case 'alert_fired':
      return t('alerts.timeline.event.alertFired');
    case 'alert_acknowledged':
      return t('alerts.timeline.event.alertAcknowledged');
    case 'alert_unacknowledged':
      return t('alerts.timeline.event.alertUnacknowledged');
    case 'alert_snoozed':
      return t('alerts.timeline.event.alertSnoozed');
    case 'alert_unsnoozed':
      return t('alerts.timeline.event.alertUnsnoozed');
    case 'alert_resolved':
      return t('alerts.timeline.event.alertResolved');
    case 'ai_analysis':
      return t('alerts.timeline.event.aiAnalysis');
    case 'command':
      return t('alerts.timeline.event.command');
    case 'runbook':
      return t('alerts.timeline.event.runbook');
    case 'note':
      return t('alerts.timeline.event.note');
    default:
      return type;
  }
}

export function getAlertTimelineEmptyState() {
  return {
    text: t('alerts.timeline.empty'),
  } as const;
}

export function getAlertTimelineUnavailableState() {
  return {
    text: t('alerts.timeline.unavailable'),
  } as const;
}

export function getAlertTimelineFailureState() {
  return {
    text: t('alerts.timeline.failure'),
    actionLabel: t('alerts.timeline.retry'),
  } as const;
}

export function getAlertTimelineHeading(): string {
  return t('alerts.timeline.heading');
}

export function getAlertTimelineAcknowledgedLabel(): string {
  return t('alerts.timeline.acknowledged');
}

export function getAlertTimelineOpenedAtLabel(openedAt: string): string {
  return t('alerts.timeline.openedAt', { openedAt });
}

export function getAlertTimelineClosedAtLabel(closedAt: string): string {
  return t('alerts.timeline.closedAt', { closedAt });
}

export function getAlertTimelineNoteLabel(): string {
  return t('alerts.timeline.noteLabel');
}

export function getAlertTimelineNotePlaceholder(): string {
  return t('alerts.timeline.notePlaceholder');
}

export function getAlertTimelineSaveNoteLabel(noteSaving: boolean): string {
  return noteSaving ? t('alerts.timeline.savingNote') : t('alerts.timeline.saveNote');
}

export function getAlertHistorySearchPlaceholder() {
  return ALERT_HISTORY_SEARCH_PLACEHOLDER;
}

export function getAlertHistoryEmptyState() {
  return {
    title: ALERT_HISTORY_EMPTY_STATE,
    description: ALERT_HISTORY_EMPTY_DESCRIPTION,
  } as const;
}

export function getAlertFilteredEmptyState(subject = 'alerts', filterLabel = 'active') {
  const normalizedSubject = subject.trim() || 'alerts';
  const normalizedFilterLabel = filterLabel.trim() || 'active';
  return {
    title: `No ${normalizedSubject} match current filters`,
    description: `Adjust the search or ${normalizedFilterLabel} filter to see more ${normalizedSubject}.`,
  } as const;
}

export function getAlertHistoryLoadingState() {
  return {
    text: ALERT_HISTORY_LOADING_STATE,
  } as const;
}

export function getAlertHistoryLoadFailure() {
  return {
    title: 'Could not load alert history',
    description:
      'Any entries shown may be incomplete or out of date. Retry to refresh saved history.',
    retryLabel: 'Retry history',
    retryingLabel: 'Retrying…',
  } as const;
}

/**
 * The notice above History when a link narrowed it to one alert. It names the
 * alert once it is found and says plainly when history no longer holds it.
 */
export function getAlertHistoryFocusNotice(options: {
  alertTitle?: string;
  resourceName?: string;
  missing: boolean;
  liveAlertsUnavailable?: boolean;
}) {
  const clearLabel = 'Show all alerts';
  // Saved history holds resolved alerts only; without the live list Pulse
  // cannot tell whether the linked alert is still firing.
  if (options.liveAlertsUnavailable) {
    return {
      text: 'Pulse could not load the alerts firing now, so this alert may still be active.',
      clearLabel,
    } as const;
  }
  if (options.missing) {
    return { text: "That alert is no longer in Pulse's alert history.", clearLabel } as const;
  }
  const subject = [options.alertTitle, options.resourceName && `on ${options.resourceName}`]
    .filter(Boolean)
    .join(' ');
  return {
    text: subject
      ? `Showing ${subject}, opened from a link.`
      : 'Showing one alert, opened from a link.',
    clearLabel,
  } as const;
}

export function getAlertBucketCountLabel(count: number) {
  return count === 0 ? ALERT_BUCKET_EMPTY_LABEL : `${count} alert${count === 1 ? '' : 's'}`;
}

export function getAlertOverviewCardPresentation(
  level: 'critical' | 'warning' | string,
  acknowledged: boolean,
  processing: boolean,
): AlertOverviewCardPresentation {
  const isCritical = level === 'critical';
  const isInformational = level === 'info';
  const opacityClass = processing ? 'opacity-50' : acknowledged ? 'opacity-60' : '';
  // Severity rides on a left edge, the icon and the badge rather than a fully
  // tinted card, so a page of warnings stays scannable instead of one block
  // of colour.
  const stateClass = acknowledged
    ? 'border-border border-l-border bg-surface-alt'
    : isCritical
      ? 'border-border border-l-red-500 dark:border-l-red-500 bg-surface'
      : isInformational
        ? 'border-border border-l-blue-500 dark:border-l-blue-400 bg-surface'
        : 'border-border border-l-yellow-500 dark:border-l-yellow-400 bg-surface';
  const iconClassName = acknowledged
    ? 'mr-2.5 mt-0.5 shrink-0 transition-all text-green-600 dark:text-green-400'
    : isCritical
      ? 'mr-2.5 mt-0.5 shrink-0 transition-all text-red-600 dark:text-red-400'
      : isInformational
        ? 'mr-2.5 mt-0.5 shrink-0 transition-all text-blue-600 dark:text-blue-400'
        : 'mr-2.5 mt-0.5 shrink-0 transition-all text-yellow-600 dark:text-yellow-400';
  const resourceClassName = 'text-sm font-semibold truncate text-base-content';

  return {
    cardClassName: [
      'border border-l-4 rounded-md px-3 py-2.5 transition-all',
      opacityClass,
      stateClass,
    ]
      .filter(Boolean)
      .join(' '),
    iconClassName,
    resourceClassName,
  };
}

export function getAlertOverviewAcknowledgedBadgeClass(): string {
  return 'px-2 py-0.5 text-xs bg-yellow-200 dark:bg-yellow-800 text-yellow-800 dark:text-yellow-200 rounded-sm';
}

export function getAlertOverviewStartedAtClass(): string {
  // The footer flex row already carries the top margin; a margin on this child
  // shifts the Started run below its siblings under items-center (#2119).
  return 'text-xs text-muted';
}

export function getAlertOverviewPrimaryActionClass(acknowledged: boolean): string {
  const stateClass = acknowledged
    ? 'text-base-content border-border hover:bg-surface-hover'
    : 'text-yellow-700 dark:text-yellow-300 border-yellow-300 dark:border-yellow-700 hover:bg-yellow-50 dark:hover:bg-yellow-900/25';
  return `px-3 py-1.5 text-xs font-medium border rounded-md transition-all disabled:opacity-50 disabled:cursor-not-allowed ${stateClass}`;
}

export function getAlertOverviewSecondaryActionClass(): string {
  return 'px-3 py-1.5 text-xs font-medium border rounded-md transition-all bg-surface text-base-content border-border hover:bg-surface-hover';
}