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.6 KB
Raw Blame
import type { Accessor } from 'solid-js';
import type { Resource, ResourceType } from '@/types/resource';
import { hasDockerRuntimeHostEvidence, isPulseAgentPlatformResource } from '@/utils/agentResources';
import {
  ADMITTED_PLATFORM_IDS,
  SUPPORTED_PLATFORM_IDS,
  getSourcePlatformManifestEntry,
  type SourcePlatformManifestEntry,
} from '@/utils/platformSupportManifest';
import { normalizeSourcePlatformKey, resolveResourcePlatformType } from '@/utils/sourcePlatforms';

export type PrimaryPlatformNavId =
  'proxmox' | 'docker' | 'kubernetes' | 'truenas' | 'vmware' | 'standalone';

export type PlatformNavigationVisibility = Record<PrimaryPlatformNavId, boolean>;

export type PlatformNavigationShortcut = {
  key: string;
  route: string;
};

export const PRIMARY_PLATFORM_NAV_IDS: readonly PrimaryPlatformNavId[] = [
  'proxmox',
  'docker',
  'kubernetes',
  'truenas',
  'vmware',
  'standalone',
] as const;

export const PRIMARY_PLATFORM_NAV_SCOPE_IDS: Record<PrimaryPlatformNavId, readonly string[]> = {
  proxmox: ['proxmox-pve', 'proxmox-pbs', 'proxmox-pmg'],
  docker: ['docker'],
  kubernetes: ['kubernetes'],
  truenas: ['truenas'],
  vmware: ['vmware-vsphere'],
  standalone: ['agent'],
};

const NAVIGABLE_PLATFORM_ID_SET = new Set<string>([
  ...SUPPORTED_PLATFORM_IDS,
  ...ADMITTED_PLATFORM_IDS,
]);

const KUBERNETES_RESOURCE_TYPES = new Set<ResourceType>([
  'k8s-cluster',
  'k8s-node',
  'pod',
  'k8s-deployment',
  'k8s-service',
]);
const DOCKER_RESOURCE_TYPES = new Set<ResourceType>(['docker-host', 'docker-service']);

const asRecord = (value: unknown): Record<string, unknown> | null =>
  typeof value === 'object' && value !== null ? (value as Record<string, unknown>) : null;

const addManifestPlatformId = (ids: Set<string>, value: string | null | undefined): void => {
  const normalized = normalizeSourcePlatformKey(value);
  if (!normalized || normalized === 'generic') return;
  if (!getSourcePlatformManifestEntry(normalized)) return;
  ids.add(normalized);
};

const addPlatformDataSources = (ids: Set<string>, platformData: Record<string, unknown> | null) => {
  const sources = platformData?.sources;
  if (!Array.isArray(sources)) return;
  sources.forEach((source) => {
    if (typeof source === 'string') addManifestPlatformId(ids, source);
  });
};

const addPlatformScopeEvidence = (ids: Set<string>, values: unknown): void => {
  if (!Array.isArray(values)) return;
  values.forEach((value) => {
    if (typeof value === 'string') addManifestPlatformId(ids, value);
  });
};

const collectExplicitPlatformScopeEvidence = (
  resource: Resource,
  platformData: Record<string, unknown> | null,
): string[] => {
  const ids = new Set<string>();
  addPlatformScopeEvidence(ids, resource.platformScopes);
  addPlatformScopeEvidence(ids, platformData?.platformScopes);
  return [...ids];
};

const addDirectDockerRuntimeEvidence = (ids: Set<string>, resource: Resource): void => {
  const resolvedPlatformType = resolveResourcePlatformType(resource);
  if (normalizeSourcePlatformKey(resource.platformType) === 'docker') {
    ids.add('docker');
    return;
  }
  if (normalizeSourcePlatformKey(resolvedPlatformType) === 'docker') {
    ids.add('docker');
    return;
  }
  if (DOCKER_RESOURCE_TYPES.has(resource.type)) {
    ids.add('docker');
    return;
  }
  if (ids.has('agent') && hasDockerRuntimeHostEvidence(resource)) {
    ids.add('docker');
  }
};

export function collectResourcePlatformEvidence(resource: Resource): string[] {
  const platformData = asRecord(resource.platformData);
  const ids = new Set<string>(collectExplicitPlatformScopeEvidence(resource, platformData));
  if (ids.size > 0) {
    addDirectDockerRuntimeEvidence(ids, resource);
    return [...ids];
  }

  addManifestPlatformId(ids, resource.platformType);
  addManifestPlatformId(ids, resolveResourcePlatformType(resource));
  resource.sources?.forEach((source) => addManifestPlatformId(ids, source));

  addPlatformDataSources(ids, platformData);

  if (resource.type === 'pbs' || resource.pbs || asRecord(platformData?.pbs)) {
    ids.add('proxmox-pbs');
  }
  if (resource.type === 'pmg' || resource.pmg || asRecord(platformData?.pmg)) {
    ids.add('proxmox-pmg');
  }
  if (resource.proxmox || asRecord(platformData?.proxmox)) {
    ids.add('proxmox-pve');
  }
  if (resource.type === 'ceph' || resource.ceph || asRecord(platformData?.ceph)) {
    ids.add('proxmox-pve');
  }
  if (resource.vmware || asRecord(platformData?.vmware)) {
    ids.add('vmware-vsphere');
  }
  if (resource.kubernetes || asRecord(platformData?.kubernetes)) {
    ids.add('kubernetes');
  }
  if (hasDockerRuntimeHostEvidence(resource) || DOCKER_RESOURCE_TYPES.has(resource.type)) {
    ids.add('docker');
  }
  if (KUBERNETES_RESOURCE_TYPES.has(resource.type)) {
    ids.add('kubernetes');
  }

  return [...ids];
}

export function buildNavigableResourcePlatformScopeSet(
  resources: readonly Resource[],
): Set<string> {
  const present = new Set<string>();
  for (const resource of resources) {
    for (const platformId of collectResourcePlatformEvidence(resource)) {
      const manifestEntry = getSourcePlatformManifestEntry(
        platformId,
      ) as SourcePlatformManifestEntry | null;
      if (!manifestEntry || !NAVIGABLE_PLATFORM_ID_SET.has(manifestEntry.id)) continue;
      present.add(manifestEntry.id);
    }
  }
  return present;
}

export function buildPrimaryPlatformNavigationVisibility(
  resources: readonly Resource[],
): PlatformNavigationVisibility {
  const presentNavigableScopes = buildNavigableResourcePlatformScopeSet(resources);
  const hasAvailabilityEndpoints = resources.some(
    (resource) =>
      resource.type === 'network-endpoint' ||
      resource.platformType === 'availability' ||
      resource.sources?.includes('availability'),
  );
  return {
    proxmox: PRIMARY_PLATFORM_NAV_SCOPE_IDS.proxmox.some((id) => presentNavigableScopes.has(id)),
    docker: PRIMARY_PLATFORM_NAV_SCOPE_IDS.docker.some((id) => presentNavigableScopes.has(id)),
    kubernetes: PRIMARY_PLATFORM_NAV_SCOPE_IDS.kubernetes.some((id) =>
      presentNavigableScopes.has(id),
    ),
    truenas: PRIMARY_PLATFORM_NAV_SCOPE_IDS.truenas.some((id) => presentNavigableScopes.has(id)),
    vmware: PRIMARY_PLATFORM_NAV_SCOPE_IDS.vmware.some((id) => presentNavigableScopes.has(id)),
    standalone: resources.some(isPulseAgentPlatformResource) || hasAvailabilityEndpoints,
  };
}

export function primaryPlatformNavigationIsVisible(
  visibility: PlatformNavigationVisibility,
  navId: PrimaryPlatformNavId,
): boolean {
  return visibility[navId] === true;
}

export function selectFirstVisiblePrimaryPlatformNavigationId(
  visibility: PlatformNavigationVisibility,
): PrimaryPlatformNavId | null {
  return (
    PRIMARY_PLATFORM_NAV_IDS.find((navId) =>
      primaryPlatformNavigationIsVisible(visibility, navId),
    ) ?? null
  );
}

export function filterPlatformNavigationShortcuts(
  shortcuts: Record<string, PlatformNavigationShortcut>,
  visibility: PlatformNavigationVisibility,
): Record<string, string> {
  const routes: Record<string, string> = {};
  for (const [navId, shortcut] of Object.entries(shortcuts)) {
    if (!primaryPlatformNavigationIsVisible(visibility, navId as PrimaryPlatformNavId)) {
      continue;
    }
    routes[shortcut.key] = shortcut.route;
  }
  return routes;
}

export function createEmptyPlatformNavigationVisibility(): PlatformNavigationVisibility {
  return {
    proxmox: false,
    docker: false,
    kubernetes: false,
    truenas: false,
    vmware: false,
    standalone: false,
  };
}

export function platformNavigationVisibilityFromResources(
  resources: Accessor<readonly Resource[]>,
): PlatformNavigationVisibility {
  return buildPrimaryPlatformNavigationVisibility(resources());
}