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.

10.8 KB
Raw Blame
import type { Resource, ResourceType } from '@/types/resource';
import {
  getAgentDiscoveryResourceId,
  isAgentDiscoveryResourceType,
  isAppContainerDiscoveryResourceType,
} from '@/utils/discoveryTarget';
import { normalizeSourcePlatformKey } from '@/utils/sourcePlatforms';
import { asTrimmedString } from '@/utils/stringUtils';
import type { StatusIndicator } from '@/utils/status';

export const AGENT_WAITING_FOR_FIRST_REPORT_LABEL = 'Waiting for first report';

export const getAgentFirstReportStatusIndicator = (
  resource: Pick<Resource, 'status' | 'sourceStatus'>,
): StatusIndicator | undefined =>
  resource.sourceStatus?.agent?.status === 'pending' && resource.status === 'unknown'
    ? { variant: 'info', label: AGENT_WAITING_FOR_FIRST_REPORT_LABEL }
    : undefined;

const AGENT_FACET_INFRASTRUCTURE_TYPES = new Set<ResourceType>(['agent', 'pbs', 'pmg']);
const AGENT_PROFILE_ASSIGNABLE_TYPES = new Set<ResourceType>([
  'docker-host',
  'agent',
  'pbs',
  'pmg',
  'k8s-cluster',
]);
const AGENT_PROVIDER_OWNER_PLATFORM_IDS = new Set<string>([
  'proxmox-pve',
  'proxmox-pbs',
  'proxmox-pmg',
  'kubernetes',
  'truenas',
  'vmware-vsphere',
]);

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

const hasMeaningfulFacetValue = (value: unknown): boolean => {
  if (typeof value === 'string') return value.trim().length > 0;
  if (typeof value === 'number') return Number.isFinite(value) && value !== 0;
  if (typeof value === 'boolean') return value;
  if (Array.isArray(value)) return value.some(hasMeaningfulFacetValue);

  const record = asRecord(value);
  if (!record) return false;
  return Object.values(record).some(hasMeaningfulFacetValue);
};

export const hasDockerFacetEvidence = (value: unknown): boolean => {
  const docker = asRecord(value);
  return Boolean(docker && hasMeaningfulFacetValue(docker));
};

const DOCKER_RUNTIME_IDENTITY_FIELDS = ['runtime', 'runtimeVersion', 'dockerVersion'] as const;
const DOCKER_RUNTIME_INVENTORY_COUNT_FIELDS = [
  'containerCount',
  'imageCount',
  'volumeCount',
  'networkCount',
  'nodeCount',
  'secretCount',
  'configCount',
] as const;

export const hasDockerRuntimeHostEvidence = (resource: Resource): boolean => {
  if (resource.type === 'docker-host') return true;
  if (resource.type !== 'agent') return false;
  const platformData = getPlatformDataRecord(resource);
  const docker = asRecord(resource.docker) ?? asRecord(platformData?.docker);
  if (!docker) return false;
  if (DOCKER_RUNTIME_IDENTITY_FIELDS.some((field) => Boolean(asTrimmedString(docker[field])))) {
    return true;
  }
  return DOCKER_RUNTIME_INVENTORY_COUNT_FIELDS.some((field) => {
    const value = docker[field];
    return typeof value === 'number' && Number.isFinite(value) && value > 0;
  });
};

type KubernetesContextLike = {
  clusterId?: string | null;
  name?: string | null;
  kubernetes?: {
    clusterName?: string | null;
    context?: string | null;
    clusterId?: string | null;
  } | null;
  platformData?: {
    kubernetes?: {
      clusterName?: string | null;
      context?: string | null;
      clusterId?: string | null;
    } | null;
  } | null;
};

type ResourceClusterNameLike = KubernetesContextLike & {
  identity?: {
    clusterName?: string | null;
  } | null;
  proxmox?: {
    clusterName?: string | null;
  } | null;
  platformData?: {
    kubernetes?: {
      clusterName?: string | null;
      context?: string | null;
      clusterId?: string | null;
    } | null;
    proxmox?: {
      clusterName?: string | null;
    } | null;
  } | null;
};

export const getPlatformDataRecord = (resource: Resource): Record<string, unknown> | undefined =>
  resource.platformData ? (resource.platformData as Record<string, unknown>) : undefined;

const hasSourceListPlatform = (values: unknown, source: string): boolean => {
  if (!Array.isArray(values)) return false;
  return values.some(
    (value) => typeof value === 'string' && normalizeSourcePlatformKey(value) === source,
  );
};

const getSourceStatusRecord = (resource: Resource): Record<string, unknown> | undefined =>
  asRecord(getPlatformDataRecord(resource)?.sourceStatus);

const hasSourceStatusPlatform = (resource: Resource, source: string): boolean => {
  const sourceStatus = getSourceStatusRecord(resource);
  if (!sourceStatus) return false;
  return Object.keys(sourceStatus).some((value) => normalizeSourcePlatformKey(value) === source);
};

const hasExplicitSourceEvidence = (resource: Resource): boolean => {
  const platformData = getPlatformDataRecord(resource);
  return Boolean(
    resource.sources?.length ||
    (Array.isArray(platformData?.sources) && platformData.sources.length > 0) ||
    Object.keys(getSourceStatusRecord(resource) || {}).length > 0,
  );
};

const hasPlatformSource = (resource: Resource, source: string): boolean =>
  hasSourceListPlatform(resource.sources, source) ||
  hasSourceListPlatform(getPlatformDataRecord(resource)?.sources, source);

const hasProviderOwnerPlatformEvidence = (resource: Resource): boolean => {
  const platformData = getPlatformDataRecord(resource);
  const candidates: unknown[] = [
    resource.platformType,
    ...(Array.isArray(resource.platformScopes) ? resource.platformScopes : []),
    ...(Array.isArray(platformData?.platformScopes) ? platformData.platformScopes : []),
    ...(Array.isArray(resource.sources) ? resource.sources : []),
    ...(Array.isArray(platformData?.sources) ? platformData.sources : []),
    ...Object.keys(getSourceStatusRecord(resource) || {}),
  ];

  return candidates.some((value) => {
    if (typeof value !== 'string') return false;
    const normalized = normalizeSourcePlatformKey(value);
    return normalized ? AGENT_PROVIDER_OWNER_PLATFORM_IDS.has(normalized) : false;
  });
};

export const hasPulseAgentSourceEvidence = (resource: Resource): boolean => {
  if (hasPlatformSource(resource, 'agent') || hasSourceStatusPlatform(resource, 'agent')) {
    return true;
  }

  if (hasExplicitSourceEvidence(resource)) {
    return false;
  }

  return resource.sourceType === 'agent' || resource.sourceType === 'hybrid';
};

export const isPulseAgentPlatformResource = (resource: Resource): boolean =>
  resource.type === 'agent' &&
  !hasProviderOwnerPlatformEvidence(resource) &&
  (hasPulseAgentSourceEvidence(resource) ||
    (!hasExplicitSourceEvidence(resource) &&
      normalizeSourcePlatformKey(resource.platformType) === 'agent'));

export const getPlatformAgentRecord = (resource: Resource): Record<string, unknown> | undefined =>
  asRecord(getPlatformDataRecord(resource)?.agent);

export const isTrueNASSystemResource = (resource: Resource): boolean =>
  resource.type === 'agent' &&
  (resource.platformType === 'truenas' || hasPlatformSource(resource, 'truenas'));

export const getExplicitAgentIdFromResource = (resource: Resource): string | undefined => {
  const platformData = getPlatformDataRecord(resource);
  const platformAgent = getPlatformAgentRecord(resource);
  const kubernetes = asRecord(platformData?.kubernetes);

  return (
    asTrimmedString(resource.agent?.agentId) ||
    asTrimmedString(platformAgent?.agentId) ||
    asTrimmedString(platformData?.agentId) ||
    asTrimmedString(platformData?.linkedAgentId) ||
    asTrimmedString(resource.kubernetes?.agentId) ||
    asTrimmedString(kubernetes?.agentId)
  );
};

export const getActionableAgentIdFromResource = (resource: Resource): string | undefined =>
  getExplicitAgentIdFromResource(resource) ||
  getAgentDiscoveryResourceId(resource.discoveryTarget ?? null) ||
  asTrimmedString(resource.discoveryTarget?.agentId);

export const getActionableDockerRuntimeIdFromResource = (
  resource: Resource,
): string | undefined => {
  const platformData = getPlatformDataRecord(resource);
  const docker = asRecord(platformData?.docker);
  const identity = asRecord(resource.identity);

  if (
    isAppContainerDiscoveryResourceType(resource.discoveryTarget?.resourceType) &&
    resource.discoveryTarget?.resourceId
  ) {
    return resource.discoveryTarget.resourceId;
  }

  return (
    asTrimmedString(docker?.hostSourceId) ||
    asTrimmedString(platformData?.hostSourceId) ||
    (resource.type === 'docker-host'
      ? asTrimmedString(identity?.machineId) || asTrimmedString(platformData?.machineId)
      : undefined) ||
    (resource.metricsTarget?.resourceType === 'docker-host'
      ? asTrimmedString(resource.metricsTarget.resourceId)
      : undefined) ||
    (resource.type === 'docker-host'
      ? asTrimmedString(resource.discoveryTarget?.agentId)
      : undefined)
  );
};

export const hasDockerWorkloadsScope = (resource: Resource): boolean => {
  const platformData = getPlatformDataRecord(resource);
  return (
    resource.type === 'docker-host' ||
    hasDockerFacetEvidence(resource.docker) ||
    hasDockerFacetEvidence(platformData?.docker)
  );
};

export const getActionableKubernetesClusterIdFromResource = (
  resource: Resource,
): string | undefined => {
  if (resource.discoveryTarget?.resourceType === 'pod' && resource.discoveryTarget.resourceId) {
    return resource.discoveryTarget.resourceId;
  }

  return (
    getPreferredResourceKubernetesContext(resource) ||
    (resource.type === 'k8s-cluster' ? resource.id : undefined)
  );
};

export const getPreferredResourceKubernetesContext = (
  resource: KubernetesContextLike,
): string | undefined => {
  return (
    asTrimmedString(resource.kubernetes?.clusterName) ||
    asTrimmedString(resource.kubernetes?.context) ||
    asTrimmedString(resource.kubernetes?.clusterId) ||
    asTrimmedString(resource.platformData?.kubernetes?.clusterName) ||
    asTrimmedString(resource.platformData?.kubernetes?.context) ||
    asTrimmedString(resource.platformData?.kubernetes?.clusterId) ||
    asTrimmedString(resource.clusterId)
  );
};

export const getExplicitResourceClusterName = (
  resource: ResourceClusterNameLike,
): string | undefined =>
  getPreferredResourceKubernetesContext(resource) ||
  asTrimmedString(resource.identity?.clusterName) ||
  asTrimmedString(resource.proxmox?.clusterName) ||
  asTrimmedString(resource.platformData?.proxmox?.clusterName);

export const getPreferredResourceClusterName = (
  resource: ResourceClusterNameLike,
): string | undefined => getExplicitResourceClusterName(resource) || asTrimmedString(resource.name);

export const hasAgentFacet = (resource: Resource): boolean => {
  const discoveryTarget = resource.discoveryTarget;
  return Boolean(
    resource.agent ||
    getPlatformAgentRecord(resource) ||
    getExplicitAgentIdFromResource(resource) ||
    (isAgentDiscoveryResourceType(discoveryTarget?.resourceType) && discoveryTarget?.agentId),
  );
};

export const isAgentFacetInfrastructureResource = (resource: Resource): boolean =>
  AGENT_FACET_INFRASTRUCTURE_TYPES.has(resource.type) && hasAgentFacet(resource);

export const isAgentProfileAssignableResource = (resource: Resource): boolean =>
  AGENT_PROFILE_ASSIGNABLE_TYPES.has(resource.type);