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.4 KB
Raw Blame
import { Component, For, Show } from 'solid-js';
import { Disk } from '@/types/api';
import { formatBytes, type NormalizedDisk } from '@/utils/format';
import { getMetricColorRgba, getMetricTextColorClass } from '@/utils/metricThresholds';
import { InfoCardFrame, InfoCardKeyValueRow } from '@/components/shared/InfoCardFrame';
import { StackedDiskBar } from '@/components/Workloads/StackedDiskBar';

interface DisksCardProps {
  disks?: Disk[];
  /**
   * Why the usage figures are retained rather than current, such as a host
   * agent past its reporting lease. The card keeps every figure, since a
   * disk that filled before the machine went quiet is evidence, but reads
   * them as last known, without threshold colour or usage bars.
   */
  lastKnownReason?: string;
}

export const DisksCard: Component<DisksCardProps> = (props) => {
  if (!props.disks || props.disks.length === 0) return null;

  const lastKnownTitle = () => {
    const reason = props.lastKnownReason?.trim();
    return reason ? `Last known reading, not current: ${reason}` : undefined;
  };
  const aggregateDisk = (): NormalizedDisk | null => {
    const total = props.disks?.reduce((sum, disk) => sum + (disk.total || 0), 0) ?? 0;
    const used = props.disks?.reduce((sum, disk) => sum + (disk.used || 0), 0) ?? 0;
    if (total <= 0 && used <= 0) return null;
    return {
      total,
      used,
      free: Math.max(total - used, 0),
      usage: total > 0 ? used / total : 0,
      mountpoint: 'Total',
    };
  };
  const aggregateUsagePercent = () => {
    const aggregate = aggregateDisk();
    return aggregate && aggregate.total > 0 ? (aggregate.used / aggregate.total) * 100 : 0;
  };

  return (
    <InfoCardFrame>
      <div class="text-[11px] font-medium uppercase tracking-wide text-base-content mb-2">
        Disks
      </div>
      <Show when={aggregateDisk()}>
        {(aggregate) => (
          <div class="mb-3 space-y-1.5 border-b border-border pb-3" data-testid="disks-card-total">
            <Show
              when={!lastKnownTitle()}
              fallback={
                <InfoCardKeyValueRow
                  class="text-[10px]"
                  label="Total Usage"
                  value={`Last known ${aggregateUsagePercent().toFixed(0)}% · ${formatBytes(
                    aggregate().used,
                  )} / ${formatBytes(aggregate().total)}`}
                  valueClass="text-muted"
                  valueTitle={lastKnownTitle()}
                />
              }
            >
              <InfoCardKeyValueRow
                class="text-[10px]"
                label="Total Usage"
                value={`${formatBytes(aggregate().used)} / ${formatBytes(aggregate().total)}`}
                valueClass={getMetricTextColorClass(aggregateUsagePercent(), 'disk')}
              />
              {/* NormalizedDisk.usage is a ratio; aggregate Disk.usage is a percentage. */}
              <StackedDiskBar
                disks={props.disks}
                aggregateDisk={{ ...aggregate(), usage: aggregateUsagePercent() }}
                mode="aggregate"
              />
            </Show>
          </div>
        )}
      </Show>
      {/* Keep mounts in the parent scroll flow: overlay scrollbars can hide a nested list. */}
      <div class="space-y-2" data-testid="disks-card-mounts">
        <For each={props.disks}>
          {(disk) => {
            const total = disk.total ?? 0;
            const used = disk.used ?? 0;
            const usagePercent = total > 0 ? (used / total) * 100 : 0;
            const barColor = getMetricColorRgba(usagePercent, 'disk');
            const textColor = getMetricTextColorClass(usagePercent, 'disk');
            return (
              <div class="text-[10px]">
                {/* A long mountpoint takes its own line on a phone rather
                    than squeezing the usage figures out of the card. */}
                <InfoCardKeyValueRow
                  class="mb-0.5 flex-wrap gap-y-0.5 text-[10px]"
                  label={disk.mountpoint}
                  labelClass="max-w-full truncate"
                  labelTitle={disk.mountpoint}
                  valueTitle={lastKnownTitle()}
                  value={
                    <span class="flex items-center gap-1.5">
                      <span class={`font-medium ${lastKnownTitle() ? 'text-muted' : textColor}`}>
                        {lastKnownTitle() ? 'Last known ' : ''}
                        {usagePercent.toFixed(0)}%
                      </span>
                      <span class="text-muted">·</span>
                      <span class="text-muted">
                        {formatBytes(used)} / {formatBytes(total)}
                      </span>
                    </span>
                  }
                />
                <Show when={!lastKnownTitle()}>
                  <div
                    class="h-1.5 w-full rounded-full bg-surface-hover overflow-hidden"
                    data-testid="disks-card-mount-bar"
                  >
                    <div
                      class="h-full rounded-full transition-all duration-500"
                      style={{
                        width: `${Math.min(100, Math.max(0, usagePercent))}%`,
                        'background-color': barColor,
                      }}
                    />
                  </div>
                </Show>
              </div>
            );
          }}
        </For>
      </div>
    </InfoCardFrame>
  );
};