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.

14.6 KB
Raw Blame
import { Show, createMemo, type Component, type JSX } from 'solid-js';
import { StatusDot } from '@/components/shared/StatusDot';
import { TableCell, TableRow } from '@/components/shared/Table';
import { getSimpleStatusIndicator } from '@/utils/status';
import { asTrimmedString } from '@/utils/stringUtils';
import {
  PlatformWindowedRows,
  PLATFORM_HEALTH_FILTER_OPTIONS,
  PlatformResponsiveTableLabel,
  PlatformSortableTableHead,
  PlatformTableEmptyState,
  PlatformTableToolbar,
  createPlatformTableFilterState,
  createPlatformTableSortState,
  formatPlatformTableCountRatioValue,
  formatPlatformTableTextValue,
  getPlatformTableCellClassForKind,
  summarizePlatformTableValues,
  PlatformTableShell,
  type PlatformTableSortValue,
  withPlatformStatusCounts,
} from '@/features/platformPage/sharedPlatformPage';
import {
  PlatformResourceDetailToggleButton,
  PlatformResourceDetailTableRow,
  createPlatformResourceDetailState,
  createPlatformResourceLabelResolver,
  getPlatformResourceDetailRowClass,
} from '@/features/platformPage/PlatformResourceDetailTableRow';
import type { Resource } from '@/types/resource';
import {
  filterKubernetesResources,
  kubernetesScopeLabel,
  type KubernetesResourceStatusFilter,
  isSingleKubernetesCluster,
} from './kubernetesPageModel';
import { KubernetesScopeText } from './KubernetesScopeText';

const resourceName = (resource: Resource): string =>
  asTrimmedString(resource.displayName) || asTrimmedString(resource.name) || resource.id;

const networkKind = (resource: Resource): string => {
  if (resource.type === 'k8s-ingress') return 'Ingress';
  if (resource.type === 'k8s-endpoint-slice') return 'EndpointSlice';
  return resource.kubernetes?.resourceKind || resource.type;
};

const portLabel = (resource: Resource): { label: string; title: string } => {
  if (resource.kubernetes?.endpointPorts?.length) {
    return summarizePlatformTableValues(
      resource.kubernetes.endpointPorts.map((port) => {
        if (!port.port) return undefined;
        const protocol = port.protocol ? `/${port.protocol.toLowerCase()}` : '';
        const appProtocol = port.appProtocol ? ` ${port.appProtocol}` : '';
        return `${port.port}${protocol}${appProtocol}`;
      }),
    );
  }
  return { label: '—', title: '' };
};

const typeOrClass = (resource: Resource): string =>
  formatPlatformTableTextValue(resource.kubernetes?.className || resource.kubernetes?.addressType);

const addressOrHosts = (resource: Resource): { label: string; title: string } => {
  if (resource.type === 'k8s-ingress') {
    return summarizePlatformTableValues([
      ...(resource.kubernetes?.hosts ?? []),
      ...(resource.kubernetes?.addresses ?? []),
    ]);
  }
  const ready = resource.kubernetes?.readyEndpointCount;
  const total = resource.kubernetes?.endpointCount;
  if (typeof ready === 'number' || typeof total === 'number') {
    const label = formatPlatformTableCountRatioValue(ready, total, { suffix: 'ready' });
    return {
      label,
      title: label,
    };
  }
  return summarizePlatformTableValues(resource.kubernetes?.addresses);
};

const targetSummary = (resource: Resource): { label: string; title: string } => {
  if (resource.type === 'k8s-ingress') {
    const rules = resource.kubernetes?.ingressRuleCount;
    const hosts = summarizePlatformTableValues(resource.kubernetes?.hosts);
    const hostCount = resource.kubernetes?.hosts?.length ?? 0;
    const label =
      typeof rules === 'number'
        ? `${rules} rule${rules === 1 ? '' : 's'}`
        : hostCount > 0
          ? `${hostCount} host${hostCount === 1 ? '' : 's'}`
          : hosts.label;
    return { label, title: hosts.title || label };
  }
  const service = formatPlatformTableTextValue(resource.kubernetes?.serviceName);
  const ready = resource.kubernetes?.readyEndpointCount;
  const total = resource.kubernetes?.endpointCount;
  const endpointLabel =
    typeof ready === 'number' || typeof total === 'number'
      ? formatPlatformTableCountRatioValue(ready, total, { suffix: 'ready' })
      : '';
  return {
    label: [service, endpointLabel].filter((value) => value && value !== '—').join(' · ') || '—',
    title: [service, endpointLabel].filter((value) => value && value !== '—').join(' · '),
  };
};

// Address / hosts, Ports, and Targets are composite summaries with no single
// scalar to order on, so they stay non-sortable.
const KUBERNETES_NETWORKING_SORT_KEYS = ['resource', 'kind', 'scope', 'typeClass'] as const;

type KubernetesNetworkingSortKey = (typeof KUBERNETES_NETWORKING_SORT_KEYS)[number];

const getKubernetesNetworkingSortValue = (
  resource: Resource,
  key: KubernetesNetworkingSortKey,
): PlatformTableSortValue => {
  switch (key) {
    case 'resource':
      return resourceName(resource);
    case 'kind':
      return networkKind(resource);
    case 'scope':
      return kubernetesScopeLabel(resource);
    case 'typeClass': {
      const typeClass = typeOrClass(resource);
      return typeClass === '—' ? null : typeClass;
    }
    default:
      key satisfies never;
      return null;
  }
};

export const KubernetesNetworkingTable: Component<{
  resources: Resource[];
  emptyIcon: JSX.Element;
  emptyTitle: string;
  emptyDescription: string;
  title?: string;
  showToolbar?: boolean;
  externalSearch?: () => string;
  externalStatus?: () => KubernetesResourceStatusFilter;
}> = (props) => {
  const singleCluster = createMemo(() => isSingleKubernetesCluster(props.resources));
  const tableState = createPlatformTableFilterState({
    resources: () => props.resources,
    initialStatus: 'all' as KubernetesResourceStatusFilter,
    filter: filterKubernetesResources,
    externalSearch: props.externalSearch,
    externalStatus: props.externalStatus,
  });
  const drawer = createPlatformResourceDetailState({ idPrefix: 'kubernetes-networking-drawer' });
  const resolveResourceLabel = createPlatformResourceLabelResolver(() => props.resources);
  const sort = createPlatformTableSortState({
    storageKey: 'kubernetesNetworking',
    sortKeys: KUBERNETES_NETWORKING_SORT_KEYS,
  });
  const sortedRows = createMemo(() =>
    sort.sortRows(tableState.filtered(), getKubernetesNetworkingSortValue),
  );

  return (
    <Show
      when={props.resources.length > 0}
      fallback={
        <PlatformTableEmptyState
          icon={props.emptyIcon}
          title={props.emptyTitle}
          description={props.emptyDescription}
        />
      }
    >
      <div class="space-y-3">
        <Show when={props.showToolbar !== false}>
          <PlatformTableToolbar
            search={tableState.search}
            onSearchChange={tableState.setSearch}
            searchPlaceholder="Search networking inventory"
            searchSuggestions={tableState.searchSuggestions}
            status={tableState.status()}
            onStatusChange={tableState.setStatus}
            statusOptions={withPlatformStatusCounts(
              PLATFORM_HEALTH_FILTER_OPTIONS,
              tableState.countForStatus,
            )}
            visible={tableState.visible()}
            total={tableState.total()}
            rowNoun="network resources"
          />
        </Show>

        <Show
          when={tableState.filtered().length > 0}
          fallback={
            <PlatformTableEmptyState
              icon={props.emptyIcon}
              title="No network resources match current filters"
              description="Adjust the search or status filter to see more Kubernetes network resources."
            />
          }
        >
          <PlatformTableShell
            title={props.title ?? 'Ingresses and EndpointSlices'}
            tableClass="min-w-full table-fixed text-xs md:min-w-[1180px]"
            header={
              <>
                <PlatformSortableTableHead
                  kind="name"
                  sort={sort}
                  sortKey="resource"
                  class="kubernetes-network-name-column platform-table-mobile-w-30 md:w-[19%]"
                >
                  Resource
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="kind"
                  class="kubernetes-network-kind-column platform-table-mobile-w-15 md:w-[12%]"
                >
                  Kind
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="scope"
                  class="platform-table-mobile-w-20 md:w-[14%]"
                >
                  Scope
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="typeClass"
                  class="kubernetes-network-class-column platform-table-mobile-w-15 md:w-[12%]"
                >
                  <PlatformResponsiveTableLabel compact="Cls" full="Type / class" />
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  class="hidden md:table-cell md:w-[18%]"
                >
                  Address / hosts
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  class="kubernetes-network-ports-column platform-table-mobile-w-20 md:w-[12%]"
                >
                  Ports
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  class="hidden md:table-cell md:w-[13%]"
                >
                  Targets
                </PlatformSortableTableHead>
              </>
            }
            body={
              <>
                <PlatformWindowedRows items={sortedRows} estimatedRowHeight={32}>
                  {(resource) => {
                    const indicator = () => getSimpleStatusIndicator(resource.status);
                    const name = () => resourceName(resource);
                    const address = () => addressOrHosts(resource);
                    const ports = () => portLabel(resource);
                    const targets = () => targetSummary(resource);
                    const detailRowId = () => drawer.detailRowId(resource);
                    const isExpanded = () => drawer.isExpanded(resource);

                    return (
                      <>
                        <TableRow
                          class={`${getPlatformResourceDetailRowClass(isExpanded())} text-[11px] sm:text-xs`}
                          data-kubernetes-networking-row={resource.id}
                          onClick={() => drawer.toggle(resource)}
                        >
                          <TableCell class={getPlatformTableCellClassForKind('name')}>
                            <div class="flex min-w-0 items-center gap-2">
                              <PlatformResourceDetailToggleButton
                                expanded={isExpanded()}
                                resourceLabel={name()}
                                controlsId={detailRowId()}
                                onToggle={() => drawer.toggle(resource)}
                              />
                              <StatusDot
                                size="sm"
                                variant={indicator().variant}
                                title={resource.status || 'unknown'}
                                ariaHidden
                              />
                              <span
                                class="kubernetes-network-name-value truncate font-semibold text-base-content"
                                title={name()}
                              >
                                {name()}
                              </span>
                            </div>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} text-base-content`}
                          >
                            {networkKind(resource)}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} text-base-content`}
                          >
                            <KubernetesScopeText
                              resource={resource}
                              singleCluster={singleCluster()}
                              class="block truncate"
                            />
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} text-base-content`}
                          >
                            {typeOrClass(resource)}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content md:table-cell`}
                          >
                            <span class="block truncate" title={address().title}>
                              {address().label}
                            </span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} text-base-content`}
                          >
                            <span class="block truncate" title={ports().title}>
                              {ports().label}
                            </span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content md:table-cell`}
                          >
                            <span class="block truncate" title={targets().title}>
                              {targets().label}
                            </span>
                          </TableCell>
                        </TableRow>
                        <PlatformResourceDetailTableRow
                          resource={resource}
                          statusIndicator={indicator()}
                          open={isExpanded()}
                          detailRowId={detailRowId()}
                          colSpan={7}
                          resolveResourceLabel={resolveResourceLabel}
                          onClose={() => drawer.close(resource)}
                        />
                      </>
                    );
                  }}
                </PlatformWindowedRows>
              </>
            }
          />
        </Show>
      </div>
    </Show>
  );
};

export default KubernetesNetworkingTable;