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.

7.9 KB
Raw Blame
import type { PlatformType, SourceType } from '@/types/resource';
import {
  KNOWN_SOURCE_PLATFORM_KEYS as GENERATED_KNOWN_SOURCE_PLATFORM_KEYS,
  PRESENTATION_ONLY_PLATFORM_IDS,
  SOURCE_PLATFORM_PRESENTATION as GENERATED_SOURCE_PLATFORM_PRESENTATION,
  type GeneratedKnownSourcePlatform,
  getSourcePlatformManifestEntry,
  SOURCE_PLATFORM_ALIAS_MAP,
} from '@/utils/platformSupportManifest';
import { titleCaseDelimitedLabel } from '@/utils/textPresentation';

export type PresentationOnlySourcePlatform = (typeof PRESENTATION_ONLY_PLATFORM_IDS)[number];
export type ProviderSourcePlatform = 'availability';
export type KnownSourcePlatform = GeneratedKnownSourcePlatform | ProviderSourcePlatform;

export interface SourcePlatformPresentation {
  label: string;
  tone: string;
}

export interface SourcePlatformFlags {
  hasAgent: boolean;
  hasProxmox: boolean;
  hasDocker: boolean;
  hasKubernetes: boolean;
  hasPbs: boolean;
  hasPmg: boolean;
  hasTrueNAS: boolean;
  hasVMware: boolean;
}

export const SOURCE_PLATFORM_PRESENTATION: Record<KnownSourcePlatform, SourcePlatformPresentation> =
  {
    ...GENERATED_SOURCE_PLATFORM_PRESENTATION,
    availability: {
      label: 'Availability',
      tone: 'bg-sky-100 text-sky-700 dark:bg-sky-900/25 dark:text-sky-300',
    },
  } as Record<KnownSourcePlatform, SourcePlatformPresentation>;

export const KNOWN_SOURCE_PLATFORM_KEYS = [
  ...GENERATED_KNOWN_SOURCE_PLATFORM_KEYS,
  'availability',
] as readonly KnownSourcePlatform[];

const PLATFORM_ALIASES = SOURCE_PLATFORM_ALIAS_MAP as Record<
  string,
  Exclude<KnownSourcePlatform, 'generic'>
>;

const SOURCE_PROVIDER_ALIASES: Record<string, ProviderSourcePlatform> = {
  'availability-probe': 'availability',
  endpoint: 'availability',
  'network-endpoint': 'availability',
};

export const normalizeSourcePlatformKey = (
  value: string | null | undefined,
): KnownSourcePlatform | null => {
  const normalized = (value || '').trim().toLowerCase();
  if (!normalized) return null;
  if (Object.prototype.hasOwnProperty.call(SOURCE_PLATFORM_PRESENTATION, normalized)) {
    return normalized as KnownSourcePlatform;
  }
  if (Object.prototype.hasOwnProperty.call(PLATFORM_ALIASES, normalized)) {
    return PLATFORM_ALIASES[normalized];
  }
  if (Object.prototype.hasOwnProperty.call(SOURCE_PROVIDER_ALIASES, normalized)) {
    return SOURCE_PROVIDER_ALIASES[normalized];
  }
  return null;
};

export const getSourcePlatformPresentation = (
  value: string | null | undefined,
): SourcePlatformPresentation | null => {
  const manifestPlatform = getSourcePlatformManifestEntry(value);
  if (manifestPlatform) {
    return SOURCE_PLATFORM_PRESENTATION[
      manifestPlatform.id as Exclude<KnownSourcePlatform, 'generic'>
    ];
  }

  const normalized = normalizeSourcePlatformKey(value);
  return normalized ? SOURCE_PLATFORM_PRESENTATION[normalized] : null;
};

export const getSourcePlatformLabel = (value: string | null | undefined): string =>
  getSourcePlatformPresentation(value)?.label ||
  titleCaseDelimitedLabel((value || '').toString()) ||
  'Unknown';

export const normalizeSourcePlatformQueryValue = (value: string | null | undefined): string => {
  const normalized = (value || '').trim().toLowerCase();
  if (!normalized) return '';
  if (normalized === 'all') return 'all';
  return normalizeSourcePlatformKey(normalized) || normalized;
};

// `proxmox-all` is a filter-only aggregate covering every Proxmox-family
// scope (proxmox-pve, proxmox-pbs, proxmox-pmg). It never appears as a
// resource scope itself, so any matcher comparing filter keys against scope
// keys must resolve it here rather than by equality.
export const sourcePlatformScopeMatchesFilter = (
  scopeKey: string | null | undefined,
  filterKey: string | null | undefined,
): boolean => {
  const scope = normalizeSourcePlatformQueryValue(scopeKey);
  const filter = normalizeSourcePlatformQueryValue(filterKey);
  if (!filter || filter === 'all') return true;
  if (filter === 'proxmox-all') return scope.startsWith('proxmox-');
  return scope === filter;
};

export const normalizeSourcePlatformScopes = (
  values?: readonly (string | null | undefined)[] | null,
  fallbackPlatform?: string | null | undefined,
): string[] => {
  const scopes: string[] = [];
  const seen = new Set<string>();
  const add = (value: string | null | undefined): void => {
    const normalized = normalizeSourcePlatformQueryValue(value);
    if (!normalized || normalized === 'all' || seen.has(normalized)) return;
    seen.add(normalized);
    scopes.push(normalized);
  };

  values?.forEach(add);
  if (scopes.length === 0) {
    add(fallbackPlatform);
  }
  return scopes;
};

export const readSourcePlatformFlags = (sources?: string[]): SourcePlatformFlags => {
  const flags: SourcePlatformFlags = {
    hasAgent: false,
    hasProxmox: false,
    hasDocker: false,
    hasKubernetes: false,
    hasPbs: false,
    hasPmg: false,
    hasTrueNAS: false,
    hasVMware: false,
  };

  if (!sources || sources.length === 0) {
    return flags;
  }

  for (const source of sources) {
    switch (normalizeSourcePlatformKey(source) || source.toLowerCase()) {
      case 'agent':
        flags.hasAgent = true;
        break;
      case 'proxmox-pve':
        flags.hasProxmox = true;
        break;
      case 'docker':
        flags.hasDocker = true;
        break;
      case 'kubernetes':
        flags.hasKubernetes = true;
        break;
      case 'proxmox-pbs':
        flags.hasPbs = true;
        break;
      case 'proxmox-pmg':
        flags.hasPmg = true;
        break;
      case 'truenas':
        flags.hasTrueNAS = true;
        break;
      case 'vmware-vsphere':
        flags.hasVMware = true;
        break;
      default:
        break;
    }
  }

  return flags;
};

const hasGenericSource = (sources?: string[]): boolean =>
  Boolean(
    sources?.some((source) => {
      const normalized = normalizeSourcePlatformKey(source) || source.toLowerCase();
      return normalized === 'generic';
    }),
  );

const hasAvailabilitySource = (sources?: string[]): boolean =>
  Boolean(sources?.some((source) => normalizeSourcePlatformKey(source) === 'availability'));

export const resolvePlatformTypeFromSources = (sources?: string[]): PlatformType | undefined => {
  const flags = readSourcePlatformFlags(sources);
  if (flags.hasProxmox) return 'proxmox-pve';
  if (flags.hasPbs) return 'proxmox-pbs';
  if (flags.hasPmg) return 'proxmox-pmg';
  if (flags.hasVMware) return 'vmware-vsphere';
  if (flags.hasTrueNAS) return 'truenas';
  if (flags.hasKubernetes) return 'kubernetes';
  if (flags.hasDocker) return 'docker';
  if (flags.hasAgent) return 'agent';
  if (hasAvailabilitySource(sources)) return 'availability';
  if (hasGenericSource(sources)) return 'generic';
  return undefined;
};

// Canonical resolver: prefer the backend-emitted `platformType` when present,
// otherwise derive it from the resource's `sources` array. Platform pages and
// other consumers that filter unified resources by family should use this so
// they work uniformly against mock fixtures and live data — the legacy backend
// path leaves `platformType` empty on certain resource types and the
// derivation by source remains the canonical fallback.
export const resolveResourcePlatformType = (resource: {
  platformType?: string | null;
  sources?: string[];
}): PlatformType | undefined => {
  const fromField = normalizeSourcePlatformQueryValue(resource.platformType || '');
  if (fromField) return fromField as PlatformType;
  return resolvePlatformTypeFromSources(resource.sources);
};

export const resolveSourceTypeFromSources = (sources?: string[]): SourceType => {
  const flags = readSourcePlatformFlags(sources);
  const hasOther =
    flags.hasProxmox ||
    flags.hasDocker ||
    flags.hasKubernetes ||
    flags.hasPbs ||
    flags.hasPmg ||
    flags.hasTrueNAS ||
    flags.hasVMware ||
    hasGenericSource(sources) ||
    hasAvailabilitySource(sources);
  if (flags.hasAgent && hasOther) return 'hybrid';
  if (flags.hasAgent) return 'agent';
  return 'api';
};