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.1 KB
Raw Blame
import type { Resource } from '@/types/resource';
import {
  isContainerUpdateLocalImage,
  isContainerUpdatePinned,
  isContainerUpdateUncheckable,
} from '@/components/shared/containerUpdateBadgeModel';
import { asTrimmedString } from '@/utils/stringUtils';

export type DockerImageUpdateTone = 'danger' | 'warning' | 'success' | 'muted';

const trimmed = (value: unknown): string => asTrimmedString(value) ?? '';

export type DockerImageOperationalPresentation = {
  consumerCount: number;
  consumerSummary: string;
  updateLabel: string;
  updateDetail: string;
  updateTone: DockerImageUpdateTone;
};

const imageIdentityTokens = (image: Resource): Set<string> =>
  new Set(
    [
      image.id,
      image.name,
      image.displayName,
      image.docker?.image,
      image.docker?.imageId,
      ...(image.docker?.repoTags ?? []),
    ]
      .map(trimmed)
      .filter((value): value is string => value.length > 0),
  );

const containerUsesImage = (container: Resource, tokens: ReadonlySet<string>): boolean =>
  [container.docker?.image, container.docker?.imageId]
    .map(trimmed)
    .some((value) => value.length > 0 && tokens.has(value));

const resourceLabel = (resource: Resource): string =>
  trimmed(resource.name) || trimmed(resource.displayName) || resource.id;

const isPulseManagedImage = (image: Resource): boolean => {
  const references = [
    image.name,
    image.displayName,
    image.docker?.image,
    ...(image.docker?.repoTags ?? []),
  ].map((value) => trimmed(value).toLowerCase());
  return references.some((reference) =>
    ['license.pulserelay.pro/pulse-pro', 'registry.pulserelay.pro/pulse/pulse-pro'].some(
      (repository) =>
        reference === repository ||
        reference.startsWith(`${repository}:`) ||
        reference.startsWith(`${repository}@`),
    ),
  );
};

const summarizeConsumers = (consumers: readonly Resource[], reportedCount: number): string => {
  if (consumers.length === 0) {
    if (reportedCount <= 0) return 'Unused';
    return `${reportedCount} container${reportedCount === 1 ? '' : 's'}`;
  }
  const labels = consumers.map(resourceLabel);
  const visible = labels.slice(0, 2);
  const extra = labels.length - visible.length;
  return `${visible.join(', ')}${extra > 0 ? ` +${extra}` : ''}`;
};

export function getDockerImageOperationalPresentation(
  image: Resource,
  containers: readonly Resource[] = [],
): DockerImageOperationalPresentation {
  const tokens = imageIdentityTokens(image);
  const consumers = containers.filter((container) => containerUsesImage(container, tokens));
  const reportedCount = Math.max(0, image.docker?.imageContainers ?? 0);
  const consumerCount = Math.max(reportedCount, consumers.length);
  const updateStates = [image, ...consumers]
    .map((resource) => resource.docker?.updateStatus)
    .filter((state): state is NonNullable<typeof state> => state !== undefined);
  const failed = updateStates.find(
    (state) => trimmed(state.error).length > 0 && !isContainerUpdateUncheckable(state),
  );

  if (consumerCount > 0 && isPulseManagedImage(image)) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Managed by Pulse',
      updateDetail: 'Pulse checks this private image through its product update service.',
      updateTone: 'muted',
    };
  }
  if (failed) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Check failed',
      updateDetail: trimmed(failed.error),
      updateTone: 'danger',
    };
  }
  if (updateStates.some((state) => state.updateAvailable === true)) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Update available',
      updateDetail: 'At least one running container is behind the latest reported digest.',
      updateTone: 'warning',
    };
  }
  if (updateStates.some((state) => isContainerUpdatePinned(state))) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Pinned',
      updateDetail:
        'The image is pinned to a specific digest, so there is no tag to check against.',
      updateTone: 'muted',
    };
  }
  if (updateStates.some((state) => isContainerUpdateLocalImage(state))) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Local',
      updateDetail:
        'The image was built or loaded on this host, so no registry has a newer version to check.',
      updateTone: 'muted',
    };
  }
  if (updateStates.some((state) => state.updateAvailable === false)) {
    return {
      consumerCount,
      consumerSummary: summarizeConsumers(consumers, reportedCount),
      updateLabel: 'Current',
      updateDetail: 'No newer digest was reported by the last image check.',
      updateTone: 'success',
    };
  }
  return {
    consumerCount,
    consumerSummary: summarizeConsumers(consumers, reportedCount),
    updateLabel: 'Not checked',
    updateDetail: 'No update comparison has been reported for this image.',
    updateTone: 'muted',
  };
}