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.

15.4 KB
Raw Blame
import { Show, createMemo, type Component, type JSX } from 'solid-js';
import { StatusDot } from '@/components/shared/StatusDot';
import { StackedDiskBar } from '@/components/Workloads/StackedDiskBar';
import { TableCell, TableRow } from '@/components/shared/Table';
import { filterChipStatusDot } from '@/components/shared/FilterBar';
import { asTrimmedString } from '@/utils/stringUtils';
import {
  PlatformWindowedRows,
  PlatformSortableTableHead,
  PlatformResponsiveTableLabel,
  PlatformTableEmptyState,
  PlatformTableMetricFallback,
  PlatformTableToolbar,
  createPlatformTableFilterState,
  createPlatformTableSortState,
  getPlatformTableFiniteMetric,
  getPlatformTableCellClassForKind,
  summarizePlatformTableValues,
  type PlatformTableFilterOption,
  type PlatformTableSortValue,
  PlatformTableShell,
  withPlatformStatusCounts,
} from '@/features/platformPage/sharedPlatformPage';
import { PlatformIssueReason } from '@/features/platformPage/PlatformIssueReason';
import {
  PlatformResourceDetailToggleButton,
  PlatformResourceDetailTableRow,
  createPlatformResourceDetailState,
  createPlatformResourceLabelResolver,
  getPlatformResourceDetailRowClass,
} from '@/features/platformPage/PlatformResourceDetailTableRow';
import type { Resource } from '@/types/resource';
import {
  filterVmwareDatastores,
  getVmwareDatastoreIssue,
  vmwareIssueSortRank,
  getVmwareRowIndicator,
  type VmwareDatastoreStatusFilter,
} from './vmwarePageModel';

const VSPHERE_DATASTORE_STATUS_OPTIONS: PlatformTableFilterOption<VmwareDatastoreStatusFilter>[] = [
  { value: 'all', label: 'All' },
  {
    value: 'accessible',
    label: 'Accessible',
    tone: 'success',
    leading: filterChipStatusDot('bg-emerald-500'),
  },
  {
    value: 'attention',
    label: 'Attention',
    tone: 'warning',
    leading: filterChipStatusDot('bg-amber-500'),
  },
  {
    value: 'inaccessible',
    label: 'Inaccessible',
    tone: 'danger',
    leading: filterChipStatusDot('bg-red-500'),
  },
  {
    value: 'maintenance',
    label: 'Maintenance',
    tone: 'warning',
    leading: filterChipStatusDot('bg-amber-500'),
  },
  { value: 'unknown', label: 'Unknown' },
];

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

const datastoreType = (resource: Resource): string =>
  asTrimmedString(resource.vmware?.datastoreType) ||
  asTrimmedString(resource.storage?.type)?.toUpperCase() ||
  '—';

const hostSummary = (resource: Resource): { label: string; title: string } =>
  summarizePlatformTableValues(resource.storage?.nodes);

const consumerSummary = (resource: Resource): { label: string; title: string } => {
  const consumers = resource.storage?.topConsumers?.map((consumer) => consumer.name) ?? [];
  return summarizePlatformTableValues(consumers);
};

const consumerCount = (resource: Resource): number => resource.storage?.consumerCount ?? 0;

const capacityDisk = (resource: Resource) => {
  const total = getPlatformTableFiniteMetric(resource.disk?.total) ?? 0;
  const used = getPlatformTableFiniteMetric(resource.disk?.used) ?? 0;
  const free = getPlatformTableFiniteMetric(resource.disk?.free) ?? Math.max(0, total - used);
  const current = getPlatformTableFiniteMetric(resource.disk?.current);
  const usage = current !== undefined ? current : total > 0 ? (used / total) * 100 : 0;
  return { total, used, free, usage };
};

const hasCapacityMetric = (resource: Resource): boolean => {
  if (getPlatformTableFiniteMetric(resource.disk?.current) !== undefined) return true;
  return (
    typeof resource.disk?.used === 'number' &&
    typeof resource.disk?.total === 'number' &&
    resource.disk.total > 0
  );
};

// Columns a user can sort by. Hosts and Consumers summarize several names at
// once, so they carry no single scalar to order on. Capacity orders on the
// used percentage the bar shows.
const VSPHERE_DATASTORE_SORT_KEYS = [
  'datastore',
  'type',
  'capacity',
  'vms',
  'datacenter',
  'health',
] as const;

type VsphereDatastoreSortKey = (typeof VSPHERE_DATASTORE_SORT_KEYS)[number];

const getVsphereDatastoreSortValue = (
  resource: Resource,
  key: VsphereDatastoreSortKey,
): PlatformTableSortValue => {
  switch (key) {
    case 'datastore':
      return datastoreName(resource);
    case 'type': {
      const type = datastoreType(resource);
      return type === '—' ? null : type;
    }
    case 'capacity':
      return hasCapacityMetric(resource) ? capacityDisk(resource).usage : null;
    case 'vms':
      return consumerCount(resource);
    case 'datacenter':
      return asTrimmedString(resource.vmware?.datacenterName) || null;
    case 'health':
      return vmwareIssueSortRank(getVmwareDatastoreIssue(resource));
    default:
      key satisfies never;
      return null;
  }
};

export const VsphereDatastoresTable: Component<{
  datastores: Resource[];
  scope: Resource[];
  emptyIcon: JSX.Element;
  emptyTitle: string;
  emptyDescription: string;
  showToolbar?: boolean;
}> = (props) => {
  const tableState = createPlatformTableFilterState({
    resources: () => props.datastores,
    initialStatus: 'all' as VmwareDatastoreStatusFilter,
    filter: filterVmwareDatastores,
  });
  const drawer = createPlatformResourceDetailState({ idPrefix: 'vsphere-datastore-drawer' });
  const resolveResourceLabel = createPlatformResourceLabelResolver(() => props.scope);
  const sort = createPlatformTableSortState({
    storageKey: 'vsphereDatastores',
    sortKeys: VSPHERE_DATASTORE_SORT_KEYS,
    descendingFirst: ['capacity', 'vms'],
  });
  const sortedRows = createMemo(() =>
    sort.sortRows(tableState.filtered(), getVsphereDatastoreSortValue),
  );

  return (
    <Show
      when={props.datastores.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 vSphere datastores"
            searchSuggestions={tableState.searchSuggestions}
            status={tableState.status()}
            onStatusChange={tableState.setStatus}
            statusOptions={withPlatformStatusCounts(
              VSPHERE_DATASTORE_STATUS_OPTIONS,
              tableState.countForStatus,
            )}
            visible={tableState.visible()}
            total={tableState.total()}
            rowNoun="datastores"
          />
        </Show>

        <Show
          when={tableState.filtered().length > 0}
          fallback={
            <PlatformTableEmptyState
              icon={props.emptyIcon}
              title="No datastores match current filters"
              description="Adjust the search or status filter to see more vSphere datastores."
            />
          }
        >
          <PlatformTableShell
            title="Datastores"
            tableClass="min-w-full table-fixed text-xs md:min-w-[1080px]"
            header={
              <>
                <PlatformSortableTableHead
                  kind="name"
                  sort={sort}
                  sortKey="datastore"
                  class="platform-table-mobile-w-30 md:w-[16%]"
                  bandWidth={28}
                >
                  Datastore
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="type"
                  class="platform-table-mobile-w-15 md:w-[6%]"
                  bandWidth={11}
                >
                  Type
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="metric-bar"
                  sort={sort}
                  sortKey="capacity"
                  class="platform-table-mobile-w-25 md:w-[16%]"
                  bandWidth={22}
                >
                  <PlatformResponsiveTableLabel compact="Cap" full="Capacity" />
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  class="hidden md:table-cell md:w-[16%]"
                >
                  Hosts
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="numeric-value"
                  sort={sort}
                  sortKey="vms"
                  class="platform-table-mobile-w-15 md:w-[5%]"
                  bandWidth={9}
                >
                  VMs
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  class="hidden lg:table-cell md:w-[15%]"
                >
                  Consumers
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="datacenter"
                  class="hidden md:table-cell md:w-[8%]"
                >
                  Datacenter
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="health"
                  class="platform-table-phone-hidden md:w-[18%]"
                  bandWidth={30}
                >
                  Health
                </PlatformSortableTableHead>
              </>
            }
            body={
              <>
                <PlatformWindowedRows items={sortedRows} estimatedRowHeight={32}>
                  {(datastore) => {
                    const hosts = createMemo(() => hostSummary(datastore));
                    const consumers = createMemo(() => consumerSummary(datastore));
                    const issue = createMemo(() => getVmwareDatastoreIssue(datastore));
                    const indicator = () => getVmwareRowIndicator(datastore, issue());
                    const name = () => datastoreName(datastore);
                    const datacenter = () =>
                      asTrimmedString(datastore.vmware?.datacenterName) || '—';
                    const datastoreSubtitle = () =>
                      asTrimmedString(datastore.vmware?.datastoreUrl) ||
                      asTrimmedString(datastore.vmware?.folderName) ||
                      asTrimmedString(datastore.vmware?.vcenterHost) ||
                      '';
                    const datastoreTitle = () =>
                      [name(), datastoreSubtitle()].filter(Boolean).join(' · ') || name();
                    const disk = createMemo(() => capacityDisk(datastore));
                    const showCapacity = () => hasCapacityMetric(datastore);
                    const detailRowId = () => drawer.detailRowId(datastore);
                    const isExpanded = () => drawer.isExpanded(datastore);
                    return (
                      <>
                        <TableRow
                          class={`${getPlatformResourceDetailRowClass(isExpanded())} text-[11px] sm:text-xs`}
                          data-vsphere-datastore-row={datastore.id}
                          onClick={() => drawer.toggle(datastore)}
                        >
                          <TableCell class={getPlatformTableCellClassForKind('name')}>
                            <div class="flex min-w-0 items-center gap-2">
                              <PlatformResourceDetailToggleButton
                                expanded={isExpanded()}
                                resourceLabel={name()}
                                controlsId={detailRowId()}
                                onToggle={() => drawer.toggle(datastore)}
                              />
                              <StatusDot
                                size="sm"
                                variant={indicator().variant}
                                title={indicator().label}
                              />
                              <span
                                class="truncate font-medium text-base-content"
                                title={datastoreTitle()}
                              >
                                {name()}
                              </span>
                            </div>
                          </TableCell>
                          <TableCell class={getPlatformTableCellClassForKind('text')}>
                            <span class="font-mono text-[11px] text-base-content">
                              {datastoreType(datastore)}
                            </span>
                          </TableCell>
                          <TableCell class={getPlatformTableCellClassForKind('metric-bar')}>
                            <Show when={showCapacity()} fallback={<PlatformTableMetricFallback />}>
                              <StackedDiskBar aggregateDisk={disk()} />
                            </Show>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content md:table-cell`}
                            title={hosts().title}
                          >
                            <span class="block truncate">{hosts().label}</span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('numeric-value')} text-base-content tabular-nums`}
                          >
                            {consumerCount(datastore)}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content lg:table-cell`}
                            title={consumers().title}
                          >
                            <span class="block truncate">{consumers().label}</span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content md:table-cell`}
                            title={datacenter()}
                          >
                            <span class="block truncate">{datacenter()}</span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} platform-table-phone-hidden`}
                          >
                            <PlatformIssueReason
                              issue={issue()}
                              data-vsphere-health={issue()?.tone}
                            />
                          </TableCell>
                        </TableRow>
                        <PlatformResourceDetailTableRow
                          resource={datastore}
                          statusIndicator={indicator()}
                          open={isExpanded()}
                          detailRowId={detailRowId()}
                          colSpan={8}
                          resolveResourceLabel={resolveResourceLabel}
                          onClose={() => drawer.close(datastore)}
                        />
                      </>
                    );
                  }}
                </PlatformWindowedRows>
              </>
            }
          />
        </Show>
      </div>
    </Show>
  );
};

export default VsphereDatastoresTable;