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.

12.9 KB
Raw Blame
import { For, Show, createMemo, type Component } from 'solid-js';
import type { ResourceCorrelation } from '@/types/aiIntelligence';
import type { ResourceRelationship } from '@/types/resource';
import { formatRelativeTime } from '@/utils/format';
import { useRelativeTimeNow } from '@/utils/relativeTimeClock';
import {
  formatResourceCorrelationEndpoint,
  formatResourceCorrelationHeadline,
  formatResourceCorrelationPattern,
  formatResourceCorrelationSummary,
  formatResourceCorrelationSummaryText,
  formatResourceRelationshipSummary,
  formatResourceRelationshipType,
  sortResourceCorrelations,
  sortResourceRelationships,
} from '@/utils/resourceCorrelationPresentation';

interface ResourceCorrelationSummaryProps {
  correlations?: readonly ResourceCorrelation[] | null;
  relationships?: readonly ResourceRelationship[] | null;
  dependencies?: readonly string[] | null;
  dependents?: readonly string[] | null;
  title?: string;
  summaryText?: string;
  dataTestId?: string;
  buildResourceHref?: (resourceId: string) => string | null | undefined;
  resolveResourceLabel?: (resourceId: string) => string | null | undefined;
  showLastSeen?: boolean;
  class?: string;
  maxCorrelations?: number;
}

export const ResourceCorrelationSummary: Component<ResourceCorrelationSummaryProps> = (props) => {
  const now = useRelativeTimeNow();
  const className = () => props.class?.trim() ?? '';
  const correlations = createMemo(() => sortResourceCorrelations(props.correlations ?? []));
  const relationships = createMemo(() => sortResourceRelationships(props.relationships ?? []));
  const dependencies = () => props.dependencies ?? [];
  const dependents = () => props.dependents ?? [];
  const buildResourceHref = props.buildResourceHref ?? (() => null);
  const resolveResourceLabel = props.resolveResourceLabel;
  const maxCorrelations = () => props.maxCorrelations ?? 3;
  const hasContent = () =>
    relationships().length > 0 ||
    dependencies().length > 0 ||
    dependents().length > 0 ||
    correlations().length > 0;
  const summaryText = () =>
    formatResourceCorrelationSummaryText({
      relationshipsCount: relationships().length,
      dependenciesCount: dependencies().length,
      dependentsCount: dependents().length,
      correlationsCount: correlations().length,
      summaryText: props.summaryText,
    });

  return (
    <Show when={hasContent()}>
      <div
        data-testid={props.dataTestId}
        class={`rounded-md border border-border-subtle bg-surface p-4 ${className()}`.trim()}
      >
        <div class="flex flex-wrap items-start justify-between gap-2">
          <div>
            <h3 class="text-sm font-semibold text-base-content">
              {props.title ?? 'Correlation context'}
            </h3>
            <Show when={summaryText()}>
              {(summary) => <p class="mt-1 text-xs text-muted">{summary()}</p>}
            </Show>
          </div>
        </div>

        <Show when={relationships().length > 0}>
          <div class="mt-3" data-testid="resource-canonical-relationships">
            <div class="text-[9px] uppercase tracking-wide text-muted">Canonical relationships</div>
            <div class="mt-1 space-y-1.5">
              <For each={relationships().slice(0, maxCorrelations())}>
                {(relationship) => {
                  const sourceLabel =
                    resolveResourceLabel?.(relationship.sourceId)?.trim() || relationship.sourceId;
                  const targetLabel =
                    resolveResourceLabel?.(relationship.targetId)?.trim() || relationship.targetId;
                  const sourceHref = buildResourceHref(relationship.sourceId);
                  const targetHref = buildResourceHref(relationship.targetId);
                  const typeLabel = formatResourceRelationshipType(relationship);
                  const summary = formatResourceRelationshipSummary(relationship);
                  const lastSeenLabel = () =>
                    props.showLastSeen && relationship.lastSeenAt
                      ? formatRelativeTime(relationship.lastSeenAt, {
                          compact: true,
                          emptyText: 'just now',
                          now: now(),
                        })
                      : '';

                  return (
                    <div class="rounded-sm bg-surface px-2 py-1">
                      <div class="flex flex-wrap items-center gap-1 text-[10px] text-base-content">
                        {sourceHref ? (
                          <a
                            class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                            href={sourceHref}
                            aria-label={`Open source resource ${sourceLabel} in Infrastructure`}
                          >
                            {sourceLabel}
                          </a>
                        ) : (
                          <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                            {sourceLabel}
                          </span>
                        )}
                        <span class="text-muted">→</span>
                        {targetHref ? (
                          <a
                            class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                            href={targetHref}
                            aria-label={`Open target resource ${targetLabel} in Infrastructure`}
                          >
                            {targetLabel}
                          </a>
                        ) : (
                          <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                            {targetLabel}
                          </span>
                        )}
                        <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[9px] text-muted">
                          {typeLabel}
                        </span>
                      </div>
                      <Show when={summary || lastSeenLabel()}>
                        <div class="mt-0.5 text-[10px] text-muted">
                          {summary}
                          <Show when={lastSeenLabel()}>
                            <>
                              {summary ? ' · ' : ''}last seen {lastSeenLabel()}
                            </>
                          </Show>
                        </div>
                      </Show>
                    </div>
                  );
                }}
              </For>
            </div>
          </div>
        </Show>

        <Show when={dependencies().length > 0}>
          <div class="mt-3">
            <div class="text-[9px] uppercase tracking-wide text-muted">Depends on</div>
            <div class="mt-1 flex flex-wrap gap-1">
              <For each={dependencies().slice(0, maxCorrelations())}>
                {(dependency) => {
                  const label = resolveResourceLabel?.(dependency)?.trim() || dependency;
                  const href = buildResourceHref(dependency);
                  return href ? (
                    <a
                      class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                      href={href}
                      aria-label={`Open dependency resource ${label} in Infrastructure`}
                    >
                      {label}
                    </a>
                  ) : (
                    <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                      {label}
                    </span>
                  );
                }}
              </For>
            </div>
          </div>
        </Show>

        <Show when={dependents().length > 0}>
          <div class="mt-3">
            <div class="text-[9px] uppercase tracking-wide text-muted">Used by</div>
            <div class="mt-1 flex flex-wrap gap-1">
              <For each={dependents().slice(0, maxCorrelations())}>
                {(dependent) => {
                  const label = resolveResourceLabel?.(dependent)?.trim() || dependent;
                  const href = buildResourceHref(dependent);
                  return href ? (
                    <a
                      class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                      href={href}
                      aria-label={`Open dependent resource ${label} in Infrastructure`}
                    >
                      {label}
                    </a>
                  ) : (
                    <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                      {label}
                    </span>
                  );
                }}
              </For>
            </div>
          </div>
        </Show>

        <Show when={correlations().length > 0}>
          <div class="mt-3">
            <div class="text-[9px] uppercase tracking-wide text-muted">Correlations</div>
            <div class="mt-1 space-y-1.5">
              <For each={correlations().slice(0, maxCorrelations())}>
                {(correlation) => {
                  const sourceHref = buildResourceHref(correlation.source_id);
                  const targetHref = buildResourceHref(correlation.target_id);
                  const headline = formatResourceCorrelationHeadline(correlation);
                  const summary = formatResourceCorrelationSummary(correlation);
                  const sourceLabel = formatResourceCorrelationEndpoint(correlation, 'source');
                  const targetLabel = formatResourceCorrelationEndpoint(correlation, 'target');
                  const patternLabel = formatResourceCorrelationPattern(correlation);
                  const lastSeenLabel = () =>
                    props.showLastSeen && correlation.last_seen
                      ? formatRelativeTime(correlation.last_seen, {
                          compact: true,
                          emptyText: 'just now',
                          now: now(),
                        })
                      : '';
                  return (
                    <div class="rounded-sm bg-surface px-2 py-1" title={headline}>
                      <div class="flex flex-wrap items-center gap-1 text-[10px] text-base-content">
                        {sourceHref ? (
                          <a
                            class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                            href={sourceHref}
                            aria-label={`Open source resource ${sourceLabel} in Infrastructure`}
                          >
                            {sourceLabel}
                          </a>
                        ) : (
                          <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                            {sourceLabel}
                          </span>
                        )}
                        <span class="text-muted">→</span>
                        {targetHref ? (
                          <a
                            class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px] text-blue-700 hover:underline dark:text-blue-300"
                            href={targetHref}
                            aria-label={`Open target resource ${targetLabel} in Infrastructure`}
                          >
                            {targetLabel}
                          </a>
                        ) : (
                          <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[10px]">
                            {targetLabel}
                          </span>
                        )}
                        <span class="inline-flex items-center rounded-sm bg-surface-alt px-1.5 py-0.5 text-[9px] text-muted">
                          {patternLabel}
                        </span>
                      </div>
                      <div class="mt-0.5 text-[10px] text-muted">
                        {summary}
                        <Show when={lastSeenLabel()}>
                          <> · last seen {lastSeenLabel()}</>
                        </Show>
                      </div>
                      <Show when={correlation.description}>
                        {(description) => (
                          <div class="mt-0.5 text-[10px] text-muted">{description()}</div>
                        )}
                      </Show>
                    </div>
                  );
                }}
              </For>
            </div>
          </div>
        </Show>
      </div>
    </Show>
  );
};

export default ResourceCorrelationSummary;