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.

2.8 KB
Raw Blame
import type { MetadataBadgeTone } from '@/components/shared/MetadataBadge';

export interface ApprovalRiskPresentation {
  badgeTone: MetadataBadgeTone;
  label: string;
}

const APPROVAL_RISK_SORT_ORDER: Record<string, number> = {
  critical: 0,
  high: 1,
  medium: 2,
  low: 3,
  unknown: 4,
};

function normalizeApprovalRiskLevel(level?: string): string {
  const normalized = level?.trim().toLowerCase();
  if (!normalized) return 'unknown';
  return normalized;
}

export function getApprovalRiskPresentation(level?: string): ApprovalRiskPresentation {
  const normalized = normalizeApprovalRiskLevel(level);

  switch (normalized) {
    case 'critical':
    case 'high':
      return {
        badgeTone: 'danger',
        label: normalized,
      };
    case 'medium':
      return {
        badgeTone: 'warning',
        label: normalized,
      };
    case 'low':
      return {
        badgeTone: 'success',
        label: normalized,
      };
    default:
      return {
        badgeTone: 'muted',
        label: normalized,
      };
  }
}

export function getApprovalRiskSortOrder(level?: string): number {
  const normalized = normalizeApprovalRiskLevel(level);
  return APPROVAL_RISK_SORT_ORDER[normalized] ?? APPROVAL_RISK_SORT_ORDER.unknown;
}

function parseApprovalTimestamp(value?: string | null): number | null {
  const parsed = Date.parse(value ?? '');
  return Number.isFinite(parsed) ? parsed : null;
}

function getApprovalTimestampSortValue(value?: string | null): number {
  return parseApprovalTimestamp(value) ?? Number.POSITIVE_INFINITY;
}

function compareApprovalTimestamps(a?: string | null, b?: string | null): number {
  const aValue = getApprovalTimestampSortValue(a);
  const bValue = getApprovalTimestampSortValue(b);

  if (aValue === bValue) return 0;
  return aValue < bValue ? -1 : 1;
}

export function sortPendingApprovalsByUrgency<
  T extends { expiresAt?: string | null; requestedAt?: string | null; riskLevel?: string },
>(approvals: T[]): T[] {
  return [...approvals].sort((a, b) => {
    const expiryDiff = compareApprovalTimestamps(a.expiresAt, b.expiresAt);
    if (expiryDiff !== 0) return expiryDiff;

    const riskDiff = getApprovalRiskSortOrder(a.riskLevel) - getApprovalRiskSortOrder(b.riskLevel);
    if (riskDiff !== 0) return riskDiff;

    return compareApprovalTimestamps(a.requestedAt, b.requestedAt);
  });
}

export function getApprovalExpiryStatusLabel(expiresAt?: string | null, now = Date.now()): string {
  const expiryTime = parseApprovalTimestamp(expiresAt);
  if (expiryTime === null) return 'expiry unavailable';

  const diff = expiryTime - now;
  if (diff <= 0) return 'expired';

  const mins = Math.floor(diff / 60000);
  const secs = Math.floor((diff % 60000) / 1000);
  if (mins > 0) return `expires ${mins}m ${secs}s`;
  return `expires ${secs}s`;
}