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.

9.9 KB
Raw Blame
import { Component, For, Show, createResource, createSignal } from 'solid-js';
import HistoryIcon from 'lucide-solid/icons/history';
import { UpdatesAPI, type UpdateHistoryEntry } from '@/api/updates';
import { updateStore } from '@/stores/updates';
import { Button } from '@/components/shared/Button';
import { Dialog } from '@/components/shared/Dialog';
import { LoadingSpinner } from '@/components/shared/LoadingSpinner';
import { getShippedDocUrl } from '@/utils/docsLinks';

const HISTORY_LIMIT = 20;

const formatVersionLabel = (version: string): string => {
  const trimmed = version.trim();
  if (!trimmed) return 'unknown';
  return trimmed.startsWith('v') ? trimmed : `v${trimmed}`;
};

const formatTimestamp = (timestamp: string): string => {
  const date = new Date(timestamp);
  return Number.isNaN(date.getTime()) ? timestamp : date.toLocaleString();
};

const actionLabel = (entry: UpdateHistoryEntry): string => {
  switch (entry.action) {
    case 'rollback':
      return 'Rollback';
    default:
      return entry.initiated_by === 'auto' ? 'Automatic update' : 'Update';
  }
};

const statusPresentation = (status: string): { label: string; className: string } => {
  switch (status) {
    case 'success':
      return { label: 'Succeeded', className: 'text-emerald-600 dark:text-emerald-400' };
    case 'failed':
      return { label: 'Failed', className: 'text-red-600 dark:text-red-400' };
    case 'in_progress':
      return { label: 'In progress', className: 'text-blue-600 dark:text-blue-400' };
    case 'rolled_back':
      return { label: 'Rolled back', className: 'text-amber-600 dark:text-amber-400' };
    case 'cancelled':
      return { label: 'Cancelled', className: 'text-muted' };
    default:
      return { label: status, className: 'text-muted' };
  }
};

// A rollback uses the backup taken before this update was applied, so it
// is only offered where that backup is still retained on disk (the backend
// clears backup_path when retention prunes it) and the update actually landed.
const canRollBack = (entry: UpdateHistoryEntry): boolean =>
  entry.action === 'update' && entry.status === 'success' && Boolean(entry.backup_path);

export const UpdateHistorySection: Component = () => {
  const [history, { refetch }] = createResource(() => UpdatesAPI.listUpdateHistory(HISTORY_LIMIT));
  const [confirmEntry, setConfirmEntry] = createSignal<UpdateHistoryEntry | null>(null);
  const [isStartingRollback, setIsStartingRollback] = createSignal(false);

  const startRollback = async () => {
    const entry = confirmEntry();
    if (!entry || isStartingRollback()) return;

    setIsStartingRollback(true);
    try {
      const accepted = await updateStore.rollbackUpdate({
        eventId: entry.event_id,
        fromVersion: updateStore.versionInfo()?.version || entry.version_to,
        toVersion: entry.version_from,
      });
      if (accepted) {
        // The global update progress watcher picks the rollback up from the
        // status stream and owns the restart/reload flow from here.
        setConfirmEntry(null);
      } else {
        // The store already surfaced the error toast; refresh in case the
        // entry state changed underneath us (e.g. backup pruned).
        void refetch();
      }
    } finally {
      setIsStartingRollback(false);
    }
  };

  return (
    <div class="space-y-4">
      <div>
        <h4 class="flex items-center gap-2 text-sm font-medium text-base-content">
          <HistoryIcon class="w-4 h-4" aria-hidden="true" />
          Update History
        </h4>
        <p class="mt-1 text-xs text-muted">
          Updates applied through Pulse, with version rollback using a retained backup.
        </p>
      </div>

      <Show
        when={!history.loading}
        fallback={
          <div class="flex items-center gap-2 text-sm text-muted">
            <LoadingSpinner size="sm" label="Loading update history" />
            Loading update history...
          </div>
        }
      >
        <Show
          when={!history.error}
          fallback={<p class="text-sm text-muted">Update history is unavailable right now.</p>}
        >
          <Show
            when={(history() ?? []).length > 0}
            fallback={
              <p class="text-sm text-muted">No updates have been applied through Pulse yet.</p>
            }
          >
            <div class="table-scroll-shell overflow-x-auto rounded-md border border-border">
              <table class="w-full min-w-0 table-fixed text-left text-sm">
                <thead class="border-b border-border bg-surface-alt text-xs uppercase text-muted">
                  <tr>
                    <th class="w-[26%] px-3 py-2 font-semibold">When</th>
                    <th class="hidden w-[20%] px-3 py-2 font-semibold sm:table-cell">Action</th>
                    <th class="w-[24%] px-3 py-2 font-semibold">Version</th>
                    <th class="w-[15%] px-3 py-2 font-semibold">Result</th>
                    <th class="w-[15%] px-3 py-2 text-right font-semibold"></th>
                  </tr>
                </thead>
                <tbody class="divide-y divide-border">
                  <For each={history()}>
                    {(entry) => (
                      <tr>
                        <td class="truncate px-3 py-2 text-muted" title={entry.timestamp}>
                          {formatTimestamp(entry.timestamp)}
                        </td>
                        <td class="hidden truncate px-3 py-2 text-base-content sm:table-cell">
                          {actionLabel(entry)}
                        </td>
                        <td
                          class="truncate px-3 py-2 text-muted"
                          title={`${formatVersionLabel(entry.version_from)} to ${formatVersionLabel(entry.version_to)}`}
                        >
                          {formatVersionLabel(entry.version_from)} &rarr;{' '}
                          {formatVersionLabel(entry.version_to)}
                        </td>
                        <td
                          class={`truncate px-3 py-2 ${statusPresentation(entry.status).className}`}
                          title={entry.error?.message || ''}
                        >
                          {statusPresentation(entry.status).label}
                        </td>
                        <td class="px-3 py-2 text-right">
                          <Show when={canRollBack(entry)}>
                            <Button
                              variant="dangerOutline"
                              size="xs"
                              type="button"
                              class="min-h-11 sm:min-h-0"
                              onClick={() => setConfirmEntry(entry)}
                            >
                              Roll back
                            </Button>
                          </Show>
                        </td>
                      </tr>
                    )}
                  </For>
                </tbody>
              </table>
            </div>
          </Show>
        </Show>
      </Show>

      <Dialog
        isOpen={confirmEntry() !== null}
        onClose={() => {
          if (!isStartingRollback()) setConfirmEntry(null);
        }}
        panelClass="max-w-lg"
        closeOnBackdrop={!isStartingRollback()}
        ariaLabel="Confirm rollback"
      >
        <Show when={confirmEntry()}>
          {(entry) => (
            <div class="w-full">
              <div class="px-6 py-4 border-b border-border">
                <h2 class="text-lg font-semibold text-base-content">
                  Roll back to Pulse {formatVersionLabel(entry().version_from)}?
                </h2>
              </div>
              <div class="px-6 py-4 space-y-3 text-sm text-base-content">
                <p class="text-muted">
                  Running now: Pulse {formatVersionLabel(updateStore.versionInfo()?.version || '')}.
                </p>
                <p>
                  This requests an installation rollback using the backup taken before the update to{' '}
                  {formatVersionLabel(entry().version_to)} on {formatTimestamp(entry().timestamp)}.
                  Pulse will restart if the rollback succeeds.
                </p>
                <p>
                  Restore scope is not reported for this backup. A scope-aware server keeps active
                  runtime data in place and restores only the binary and independent installation
                  files. Older recovery can also replace install-local data and configuration,
                  reverting later changes.
                </p>
                <p>
                  Do not assume this will undo settings or alerts. Full-state recovery requires
                  Pulse to be stopped and a consistent backup of every active store.{' '}
                  <a
                    class="text-primary underline"
                    href={`${getShippedDocUrl('AUTO_UPDATE.md')}#manual-rollback`}
                  >
                    Read the recovery instructions
                  </a>
                  .
                </p>
              </div>
              <div class="px-6 py-4 bg-surface-alt border-t border-border flex items-center justify-end gap-3">
                <Button
                  variant="ghost"
                  size="md"
                  type="button"
                  disabled={isStartingRollback()}
                  onClick={() => setConfirmEntry(null)}
                >
                  Cancel
                </Button>
                <Button
                  variant="danger"
                  size="md"
                  type="button"
                  disabled={isStartingRollback()}
                  onClick={() => void startRollback()}
                >
                  {isStartingRollback()
                    ? 'Starting rollback...'
                    : `Roll back to ${formatVersionLabel(entry().version_from)}`}
                </Button>
              </div>
            </div>
          )}
        </Show>
      </Dialog>
    </div>
  );
};