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.

21.0 KB
Raw Blame
import { Show, createMemo, type Component, type JSX } from 'solid-js';
import {
  InlineDetailPanel,
  compactDetailRows,
  compactDetailSections,
  makeDetailRow,
  type DetailSection,
  type DetailValueTone,
} from '@/components/shared/DetailSectionTable';
import { InlineDetailTableRow } from '@/components/shared/InlineDetailTableRow';
import { StatusDot } from '@/components/shared/StatusDot';
import { TableCell, TableRow } from '@/components/shared/Table';
import { filterChipStatusDot } from '@/components/shared/FilterBar';
import {
  PlatformWindowedRows,
  PlatformSortableTableHead,
  PlatformTableRelativeTimeValue,
  PlatformTableEmptyState,
  PlatformTableToolbar,
  createPlatformTableFilterState,
  createPlatformTableSortState,
  formatPlatformTableTitleCaseValue,
  getPlatformTableCellClassForKind,
  getPlatformTableDateTimeSortValue,
  type PlatformTableFilterOption,
  type PlatformTableSortValue,
  PlatformTableShell,
  withPlatformStatusCounts,
} from '@/features/platformPage/sharedPlatformPage';
import {
  getPlatformInlineStateClass,
  getPlatformStatusTextToneClass,
  isPlatformAttentionVariant,
} from '@/features/platformPage/platformStatusTone';
import {
  PlatformResourceDetailToggleButton,
  createPlatformResourceDetailState,
  getPlatformResourceDetailRowClass,
} from '@/features/platformPage/PlatformResourceDetailTableRow';
import { RecordOwnerLink } from '@/features/platformPage/RecordOwnerLink';
import type { ResourceType } from '@/types/resource';
import type { StatusIndicatorVariant } from '@/utils/status';
import {
  filterVmwareActivity,
  type VmwareActivityKind,
  type VmwareActivityRow,
  type VmwareActivityStateBucket,
  type VmwareActivityStatusFilter,
} from './vmwarePageModel';

const VSPHERE_ACTIVITY_STATUS_OPTIONS: PlatformTableFilterOption<VmwareActivityStatusFilter>[] = [
  { value: 'all', label: 'All' },
  {
    value: 'tasks',
    label: 'Tasks',
    tone: 'success',
    leading: filterChipStatusDot('bg-emerald-500'),
  },
  {
    value: 'events',
    label: 'Events',
    tone: 'info',
    leading: filterChipStatusDot('bg-blue-500'),
  },
  {
    value: 'failed',
    label: 'Failed',
    tone: 'danger',
    leading: filterChipStatusDot('bg-red-500'),
  },
];

const formatResourceType = (type: ResourceType): string => {
  switch (type) {
    case 'agent':
      return 'Host';
    case 'vm':
      return 'VM';
    case 'storage':
      return 'Datastore';
    default:
      return type;
  }
};

const formatEntityType = (value: string): string => {
  const normalized = value.trim().toLowerCase();
  if (normalized === 'host') return 'Host';
  if (normalized === 'vm') return 'VM';
  if (normalized === 'datastore') return 'Datastore';
  return normalized ? normalized.charAt(0).toUpperCase() + normalized.slice(1) : '-';
};

const formatActivityKind = (kind: VmwareActivityKind): string => {
  switch (kind) {
    case 'task':
      return 'Task';
    case 'event':
      return 'Event';
    case 'activity':
      return 'Activity';
  }
};

const activityStateVariant = (bucket: VmwareActivityStateBucket): StatusIndicatorVariant => {
  switch (bucket) {
    case 'success':
      return 'success';
    case 'running':
      return 'warning';
    case 'failed':
      return 'danger';
    case 'unknown':
      return 'muted';
  }
};

const formatIdentifierLabel = (value: string): string =>
  value
    .trim()
    .split(/[\s_-]+/)
    .filter(Boolean)
    .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
    .join(' ');

const formatActivityState = (row: VmwareActivityRow): string =>
  row.state.trim() ? formatIdentifierLabel(row.state) : '-';

const detailDateTime = (value?: string): string => {
  if (!value) return '-';
  const parsed = new Date(value);
  if (Number.isNaN(parsed.getTime())) return value;
  if (parsed.getUTCFullYear() < 2000) return '-';
  return parsed.toLocaleString(undefined, {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
};

type ActivityDetailTone = DetailValueTone;
type ActivityDetailSection = DetailSection;

const detailRow = makeDetailRow;

const activityTone = (bucket: VmwareActivityStateBucket): ActivityDetailTone => {
  if (bucket === 'success') return 'success';
  if (bucket === 'running') return 'warning';
  if (bucket === 'failed') return 'danger';
  return 'muted';
};

// An event's title is vCenter's own message when it sent one, so the message
// and description rows would only repeat it.
const distinctFromTitle = (activity: VmwareActivityRow, value: string): string =>
  value.trim() && value.trim() !== activity.title.trim() ? value : '';

// Recent activity reads as an age ("3h ago"); the exact time is on hover and
// in the drawer. vCenter reports unset times as dates before 2000.
const activityTimestamp = (activity: VmwareActivityRow): string | undefined => {
  const value = activity.occurredAt || activity.observedAt;
  if (!value) return undefined;
  const parsed = new Date(value);
  return Number.isNaN(parsed.getTime()) || parsed.getUTCFullYear() < 2000 ? undefined : value;
};

const buildActivityDetailSections = (activity: VmwareActivityRow): ActivityDetailSection[] => {
  const meta = activity.resource.vmware;
  return compactDetailSections([
    {
      label: 'Activity',
      rows: compactDetailRows([
        detailRow('Kind', formatActivityKind(activity.activityKind)),
        detailRow('State', formatActivityState(activity), {
          tone: activityTone(activity.stateBucket),
        }),
        detailRow('Activity', activity.title),
        detailRow('Event type', activity.eventType),
        detailRow('Message', distinctFromTitle(activity, activity.message)),
        detailRow('Description', distinctFromTitle(activity, activity.description)),
      ]),
    },
    {
      label: 'Affected resource',
      rows: compactDetailRows([
        detailRow('Resource', activity.resourceName),
        detailRow('Type', formatResourceType(activity.resourceType)),
        detailRow('Entity', formatEntityType(activity.entityType)),
        detailRow('Managed object', activity.managedObjectId),
        detailRow('vCenter', meta?.connectionName || meta?.vcenterHost),
        detailRow('Datacenter', meta?.datacenterName),
        detailRow('Cluster', meta?.clusterName || meta?.computeResourceName),
        detailRow('Resource ID', activity.resourceId),
      ]),
    },
    {
      label: 'Source',
      rows: compactDetailRows([
        detailRow('Native ID', activity.nativeId),
        detailRow('Actor', activity.actor),
        detailRow('Source', formatIdentifierLabel(activity.source)),
        detailRow('Occurred', detailDateTime(activity.occurredAt)),
        detailRow('Observed', detailDateTime(activity.observedAt)),
      ]),
    },
  ]);
};

const ActivityDetail: Component<{ activity: VmwareActivityRow; onClose: () => void }> = (props) => (
  <InlineDetailPanel
    testId="vsphere-activity-detail"
    detailFor={props.activity.id}
    title={props.activity.resourceName || props.activity.title}
    status={{
      variant: activityStateVariant(props.activity.stateBucket),
      label: formatPlatformTableTitleCaseValue(props.activity.stateBucket),
    }}
    summary={`${formatActivityKind(props.activity.activityKind)} · ${props.activity.title}`}
    actions={
      <RecordOwnerLink
        subject={{
          id: props.activity.resourceId,
          name: props.activity.resourceName,
          type: props.activity.resourceType,
          resource: props.activity.resource,
        }}
      />
    }
    sections={buildActivityDetailSections(props.activity)}
    detailAttributes={{ 'data-vsphere-activity-detail-for': props.activity.id }}
    onClose={props.onClose}
  />
);

// Columns a user can sort by: every activity column carries a scalar. When
// orders chronologically, newest first on the first click.
const VSPHERE_ACTIVITY_SORT_KEYS = [
  'resource',
  'type',
  'activity',
  'state',
  'actor',
  'vcenter',
  'when',
] as const;

type VsphereActivitySortKey = (typeof VSPHERE_ACTIVITY_SORT_KEYS)[number];

const getVsphereActivitySortValue = (
  activity: VmwareActivityRow,
  key: VsphereActivitySortKey,
): PlatformTableSortValue => {
  switch (key) {
    case 'resource':
      return activity.resourceName || null;
    case 'type':
      return formatActivityKind(activity.activityKind);
    case 'activity':
      return activity.title || null;
    case 'state': {
      const state = formatActivityState(activity);
      return state === '-' ? null : state;
    }
    case 'actor':
      return activity.actor || null;
    case 'vcenter': {
      const meta = activity.resource.vmware;
      return meta?.connectionName || meta?.vcenterHost || null;
    }
    case 'when':
      return getPlatformTableDateTimeSortValue(activity.occurredAt || activity.observedAt);
    default:
      key satisfies never;
      return null;
  }
};

export const VsphereActivityTable: Component<{
  activity: VmwareActivityRow[];
  emptyIcon: JSX.Element;
  emptyTitle: string;
  emptyDescription: string;
  showToolbar?: boolean;
  // One vCenter connection: its name would repeat on every row, so the
  // column hides (the row drawer still names it).
  singleVcenter?: boolean;
}> = (props) => {
  const tableState = createPlatformTableFilterState({
    resources: () => props.activity,
    initialStatus: 'all' as VmwareActivityStatusFilter,
    filter: filterVmwareActivity,
  });
  const drawer = createPlatformResourceDetailState({ idPrefix: 'vsphere-activity-drawer' });
  const sort = createPlatformTableSortState({
    storageKey: 'vsphereActivity',
    sortKeys: VSPHERE_ACTIVITY_SORT_KEYS,
    descendingFirst: ['when'],
  });
  const sortedRows = createMemo(() =>
    sort.sortRows(tableState.filtered(), getVsphereActivitySortValue),
  );

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

        <Show
          when={tableState.filtered().length > 0}
          fallback={
            <PlatformTableEmptyState
              icon={props.emptyIcon}
              title="No activity matches current filters"
              description="Adjust the search or activity filter to see more vCenter tasks and events."
            />
          }
        >
          <PlatformTableShell
            title="Recent Activity"
            tableClass="min-w-full table-fixed text-xs md:min-w-[1120px]"
            header={
              <>
                <PlatformSortableTableHead
                  kind="name"
                  sort={sort}
                  sortKey="resource"
                  class="platform-table-mobile-w-30 md:w-[20%]"
                >
                  Resource
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="badge"
                  sort={sort}
                  sortKey="type"
                  class="platform-table-mobile-w-15 md:w-[8%]"
                >
                  Type
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="activity"
                  class="platform-table-mobile-w-25 md:w-[34%]"
                >
                  Activity
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="badge"
                  sort={sort}
                  sortKey="state"
                  class="platform-table-phone-hidden md:w-[10%]"
                >
                  State
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="actor"
                  class="hidden md:table-cell md:w-[10%]"
                >
                  Actor
                </PlatformSortableTableHead>
                <Show when={!props.singleVcenter}>
                  <PlatformSortableTableHead
                    kind="text"
                    sort={sort}
                    sortKey="vcenter"
                    class="hidden lg:table-cell md:w-[10%]"
                  >
                    vCenter
                  </PlatformSortableTableHead>
                </Show>
                <PlatformSortableTableHead
                  kind="numeric-value"
                  sort={sort}
                  sortKey="when"
                  class="platform-table-mobile-w-15 md:w-[8%]"
                >
                  When
                </PlatformSortableTableHead>
              </>
            }
            body={
              <>
                <PlatformWindowedRows items={sortedRows} estimatedRowHeight={32}>
                  {(activity) => {
                    const meta = () => activity.resource.vmware;
                    const detailRowId = () => drawer.detailRowId(activity);
                    const isExpanded = () => drawer.isExpanded(activity);
                    return (
                      <>
                        <TableRow
                          class={`${getPlatformResourceDetailRowClass(isExpanded())} text-[11px] sm:text-xs`}
                          data-vsphere-activity-row={activity.id}
                          onClick={() => drawer.toggle(activity)}
                        >
                          <TableCell class={getPlatformTableCellClassForKind('name')}>
                            <div class="flex min-w-0 items-center gap-2">
                              <PlatformResourceDetailToggleButton
                                expanded={isExpanded()}
                                resourceLabel={activity.resourceName}
                                controlsId={detailRowId()}
                                onToggle={() => drawer.toggle(activity)}
                              />
                              <StatusDot
                                size="sm"
                                variant={activityStateVariant(activity.stateBucket)}
                                title={formatActivityState(activity)}
                              />
                              <div class="flex min-w-0 flex-1 items-center gap-1.5">
                                <div
                                  class="min-w-0 truncate font-medium text-base-content"
                                  title={[
                                    activity.resourceName,
                                    formatResourceType(activity.resourceType),
                                    activity.resource.parentName
                                      ? `on ${activity.resource.parentName}`
                                      : '',
                                  ]
                                    .filter(Boolean)
                                    .join(' · ')}
                                >
                                  {activity.resourceName}
                                </div>
                                {/* A phone hides State; a state that needs attention joins the
                                    name on its line. */}
                                <span
                                  class={`platform-table-phone-only-inline text-[11px] ${getPlatformInlineStateClass(isPlatformAttentionVariant(activityStateVariant(activity.stateBucket)))} ${getPlatformStatusTextToneClass(activityStateVariant(activity.stateBucket))}`}
                                  data-vsphere-activity-state-phone={activityStateVariant(
                                    activity.stateBucket,
                                  )}
                                >
                                  <span class="min-w-0 truncate">
                                    {formatActivityState(activity)}
                                  </span>
                                </span>
                              </div>
                            </div>
                          </TableCell>
                          <TableCell class={getPlatformTableCellClassForKind('badge')}>
                            <span class="text-[11px] font-semibold text-base-content">
                              {formatActivityKind(activity.activityKind)}
                            </span>
                          </TableCell>
                          <TableCell class={getPlatformTableCellClassForKind('text')}>
                            <span
                              class="block truncate text-base-content"
                              title={[
                                activity.title,
                                distinctFromTitle(activity, activity.message) ||
                                  distinctFromTitle(activity, activity.description) ||
                                  activity.nativeId,
                              ]
                                .filter(Boolean)
                                .join(' · ')}
                            >
                              {activity.title}
                            </span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('badge')} platform-table-phone-hidden`}
                          >
                            <span
                              class={`text-[11px] font-semibold ${getPlatformStatusTextToneClass(activityStateVariant(activity.stateBucket)) || 'text-base-content'}`}
                              data-vsphere-activity-state={activityStateVariant(
                                activity.stateBucket,
                              )}
                            >
                              {formatActivityState(activity)}
                            </span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden text-base-content md:table-cell`}
                          >
                            <span class="block truncate" title={activity.actor}>
                              {activity.actor || '-'}
                            </span>
                          </TableCell>
                          <Show when={!props.singleVcenter}>
                            <TableCell
                              class={`${getPlatformTableCellClassForKind('text')} hidden lg:table-cell`}
                            >
                              <span
                                class="block truncate text-base-content"
                                title={[
                                  meta()?.connectionName || meta()?.vcenterHost,
                                  meta()?.datacenterName || meta()?.clusterName,
                                ]
                                  .filter(Boolean)
                                  .join(' · ')}
                              >
                                {meta()?.connectionName || meta()?.vcenterHost || '-'}
                              </span>
                            </TableCell>
                          </Show>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('numeric-value')} text-base-content`}
                          >
                            <span title={detailDateTime(activityTimestamp(activity))}>
                              <PlatformTableRelativeTimeValue
                                value={activityTimestamp(activity)}
                                emptyText="-"
                              />
                            </span>
                          </TableCell>
                        </TableRow>
                        <Show when={isExpanded()}>
                          <InlineDetailTableRow
                            cellId={detailRowId()}
                            colspan={props.singleVcenter ? 6 : 7}
                            data-inline-detail-for={activity.id}
                            data-vsphere-activity-detail-row={activity.id}
                          >
                            <ActivityDetail
                              activity={activity}
                              onClose={() => drawer.close(activity)}
                            />
                          </InlineDetailTableRow>
                        </Show>
                      </>
                    );
                  }}
                </PlatformWindowedRows>
              </>
            }
          />
        </Show>
      </div>
    </Show>
  );
};

export default VsphereActivityTable;