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.

5.8 KB
Raw Blame
import type { Component } from 'solid-js';
import CheckCircle from 'lucide-solid/icons/check-circle';
import XCircle from 'lucide-solid/icons/x-circle';
import { getAllFilterOptionLabel } from '@/components/shared/filterOptionPresentation';

export type AuditVerificationStatus = 'verified' | 'failed' | 'unavailable' | 'error';

export interface AuditBadgePresentation {
  label: string;
  className: string;
}

export interface AuditEventStatusPresentation {
  icon: Component<{ class?: string }>;
  className: string;
}

export interface AuditLogEmptyStatePresentation {
  title: string;
  description: string;
}

export interface AuditLogFeatureGateCopyOptions {
  paidRuntimeRequired?: boolean;
}

type AuditLogFetchErrorLike = {
  code?: string;
  message?: string;
  status?: number;
};

export const AUDIT_TOOLBAR_BUTTON_CLASS =
  'flex min-h-11 sm:min-h-10 items-center gap-2 px-3 py-2 text-sm font-medium bg-surface border border-border rounded-md hover:bg-surface-hover disabled:opacity-50';
export const AUDIT_REFRESH_BUTTON_CLASS = `${AUDIT_TOOLBAR_BUTTON_CLASS} text-base-content`;
export const AUDIT_VERIFY_ALL_BUTTON_CLASS =
  'flex min-h-11 sm:min-h-10 items-center gap-2 px-3 py-2 text-sm font-medium text-blue-700 dark:text-blue-200 bg-blue-50 dark:bg-blue-900/25 border border-blue-200 dark:border-blue-700 rounded-md hover:bg-blue-100 dark:hover:bg-blue-800 disabled:opacity-50';
export const AUDIT_VERIFY_ROW_BUTTON_CLASS =
  'inline-flex min-h-11 sm:min-h-10 items-center rounded-md border border-blue-200 dark:border-blue-700 px-3 py-2 text-sm font-medium text-blue-600 dark:text-blue-400 hover:bg-blue-50 dark:hover:bg-blue-900/25 disabled:opacity-50';
export const AUDIT_EVENT_FILTER_ALL_LABEL = getAllFilterOptionLabel('events');
export const AUDIT_EVENT_CONFIG_CHANGE_LABEL = 'Config change';
export const AUDIT_SUCCESS_FILTER_SUCCESS_ONLY_LABEL = 'Success only';
export const AUDIT_SUCCESS_FILTER_FAILED_ONLY_LABEL = 'Failed only';
export const AUDIT_VERIFICATION_FILTER_ALL_LABEL = getAllFilterOptionLabel('verification');
export const AUDIT_VERIFICATION_FILTER_NEEDS_LABEL = 'Needs verification';

export function getAuditEventTypeBadgeClass(event?: string | null): string {
  switch ((event ?? '').trim()) {
    case 'login':
      return 'bg-blue-100 text-blue-800 dark:bg-blue-900/25 dark:text-blue-200';
    case 'config_change':
      return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/25 dark:text-yellow-200';
    case 'startup':
      return 'bg-green-100 text-green-800 dark:bg-green-900/25 dark:text-green-200';
    case 'logout':
    case 'oidc_token_refresh':
    default:
      return 'bg-surface-alt text-base-content';
  }
}

export function getAuditEventStatusPresentation(success: boolean): AuditEventStatusPresentation {
  return success
    ? {
        icon: CheckCircle,
        className: 'w-4 h-4 text-emerald-400',
      }
    : {
        icon: XCircle,
        className: 'w-4 h-4 text-rose-400',
      };
}

export function getAuditVerificationBadgePresentation(
  state?: { status: AuditVerificationStatus } | null,
): AuditBadgePresentation {
  if (!state) {
    return { label: 'Not checked', className: 'bg-surface-alt text-base-content' };
  }

  switch (state.status) {
    case 'verified':
      return {
        label: 'Verified',
        className: 'bg-green-100 text-green-800 dark:bg-green-900/25 dark:text-green-200',
      };
    case 'failed':
      return {
        label: 'Failed',
        className: 'bg-red-100 text-red-800 dark:bg-red-900/25 dark:text-red-200',
      };
    case 'error':
      return {
        label: 'Error',
        className: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
      };
    default:
      return { label: 'Unavailable', className: 'bg-surface-alt text-base-content' };
  }
}

export function getAuditLogLoadingState() {
  return {
    text: 'Loading audit events…',
  } as const;
}

export function getAuditLogFetchErrorMessage(error: unknown): string {
  const errorLike = error as AuditLogFetchErrorLike;
  if (errorLike?.code === 'audit_store_busy') {
    return 'Audit log storage is busy. Wait a moment, then refresh the audit log.';
  }
  if (errorLike?.code === 'audit_store_unavailable') {
    return 'Audit log storage is unavailable. Check the server logs for audit database initialization or disk errors, then refresh the audit log.';
  }
  if (errorLike?.code === 'query_failed') {
    return 'Audit events could not be loaded. Check the server logs for the audit query failure, then refresh the audit log.';
  }
  if (
    errorLike?.status === 500 ||
    /failed to fetch audit events:\s*internal server error/i.test(errorLike?.message ?? '')
  ) {
    return 'Audit events could not be loaded because the server returned an internal error. Check the server logs, then refresh the audit log.';
  }
  return error instanceof Error ? error.message : 'Unknown error';
}

export function getAuditLogFeatureGateCopy(options: AuditLogFeatureGateCopyOptions = {}) {
  if (options.paidRuntimeRequired) {
    return {
      title: 'Pulse Pro runtime required',
      body: 'Your Pro license is active, but this install is running the community runtime. Install the private Pulse Pro runtime to use Audit Log and Audit Webhooks. Public GitHub releases and the public Docker image do not include those Pro runtime hooks.',
    } as const;
  }

  return {
    title: 'Audit Logging',
    body: 'Persistent, searchable audit logs with cryptographic signature verification.',
  } as const;
}

export function getAuditLogEmptyState(activeFilterCount: number): AuditLogEmptyStatePresentation {
  return activeFilterCount > 0
    ? {
        title: 'No audit events found',
        description: 'No events match your current filters. Try adjusting or clearing them.',
      }
    : {
        title: 'No audit events found',
        description: 'Audit logging is active, but no events have been recorded yet.',
      };
}