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.

19.9 KB
Raw Blame
import { batch, createEffect, createMemo, createSignal, onCleanup, onMount } from 'solid-js';
import { ChartsAPI, type HistoryTimeRange } from '@/api/charts';
import { getAPIReadAccessErrorMessage } from '@/utils/apiAccessError';
import { eventBus } from '@/stores/events';
import { isRangeLocked, loadRuntimeCapabilities, maxHistoryDays } from '@/stores/license';
import { calculateOptimalPoints } from '@/utils/downsample';
import { setupCanvasDPR } from '@/utils/canvasRenderQueue';
import {
  HISTORY_CHART_RANGES,
  HISTORY_CHART_MIN_LEFT_INSET,
  createHistoryChartGeometry,
  findHistoryChartClosestPoint,
  formatHistoryChartTimeLabel,
  getHistoryChartDataMax,
  getHistoryChartDataMin,
  getHistoryChartDefaultColor,
  getHistoryChartLeftInset,
  getHistoryChartRefreshIntervalMs,
  getHistoryChartRightInset,
  getHistoryChartScale,
  getHistoryChartTimeTickCount,
  hasHistoryChartSpread,
  withoutRepeatedHistoryTimeTicks,
  getHistoryChartYAxisLabels,
  type HistoryChartProps,
  type HistoryChartHoverPoint,
} from './historyChartModel';
import type { HistoryChartHoverGroupState } from './HistoryChartHoverGroup';

interface HistoryChartRefs {
  getCanvas: () => HTMLCanvasElement | undefined;
  getContainer: () => HTMLDivElement | undefined;
}

export function useHistoryChartState(
  props: HistoryChartProps,
  refs: HistoryChartRefs,
  hoverGroup?: HistoryChartHoverGroupState,
) {
  const [range, setRange] = createSignal<HistoryTimeRange>(props.range || '24h');
  const [data, setData] = createSignal(props.data ?? []);
  const [keyboardInspecting, setKeyboardInspecting] = createSignal(false);
  const [loading, setLoading] = createSignal(false);
  const [refreshFailed, setRefreshFailed] = createSignal(false);
  const [error, setError] = createSignal<string | null>(null);
  const [source, setSource] = createSignal<'store' | 'memory' | 'live' | 'mock_synthetic' | null>(
    null,
  );
  const [maxPoints, setMaxPoints] = createSignal<number | null>(null);
  const [localHoveredTimestamp, setLocalHoveredTimestamp] = createSignal<number | null>(null);
  const [hoveredPoint, setHoveredPoint] = createSignal<HistoryChartHoverPoint | null>(null);
  const [chartWidth, setChartWidth] = createSignal(300);
  const [orgVersion, setOrgVersion] = createSignal(0);
  const chartHeight = createMemo(() => props.height || 200);
  let chartLeftInset = HISTORY_CHART_MIN_LEFT_INSET;
  let chartRightInset = 0;
  let touchFocus = false;
  let touchTimestamp: number | null = null;
  let pendingTouch: { pointerId: number; x: number; y: number; moved: boolean } | undefined;
  const hoveredTimestamp = hoverGroup?.hoveredTimestamp ?? localHoveredTimestamp;
  const setHoveredTimestamp = hoverGroup?.setHoveredTimestamp ?? setLocalHoveredTimestamp;

  // Resource IDs can be identical in different organisations. A context switch
  // replaces the request owner even when none of the chart props have changed.
  const unsubscribeOrgSwitch = eventBus.on('org_switched', () => {
    setOrgVersion((version) => version + 1);
  });
  onCleanup(unsubscribeOrgSwitch);

  const refreshIntervalMs = createMemo(() => getHistoryChartRefreshIntervalMs(range()));

  onMount(() => {
    loadRuntimeCapabilities();
  });

  createEffect(() => {
    if (props.range) {
      setRange(props.range);
    }
  });

  const updateRange = (nextRange: HistoryTimeRange) => {
    setRange(nextRange);
    props.onRangeChange?.(nextRange);
  };

  const isLocked = createMemo(() => isRangeLocked(range()));
  const lockDays = createMemo(() => {
    switch (range()) {
      case '14d':
        return '14';
      case '30d':
        return '30';
      case '90d':
        return '90';
      default:
        return '14';
    }
  });
  const lockTierLabel = createMemo(() => {
    const max = maxHistoryDays();
    const targetDays =
      range() === '14d' ? 14 : range() === '30d' ? 30 : range() === '90d' ? 90 : 14;
    if (max <= 7 && targetDays <= 14) return 'Relay';
    return 'Pro';
  });

  const dataMin = createMemo(() => getHistoryChartDataMin(data()));
  const dataMax = createMemo(() => getHistoryChartDataMax(data()));

  let previousSelection: string | undefined;
  let previousSeries: string | undefined;
  // The series whose stored reply is on screen, so a pause or resize keeps it.
  let loadedSeries: string | undefined;

  // One effect owns a selection, its request and its polling timer. Cleanup
  // invalidates completions even when a transport ignores cancellation.
  createEffect(() => {
    const suppliedData = props.data;
    const resourceId = props.resourceId;
    const resourceType = props.resourceType;
    const metric = props.metric;
    const chartRange = range();
    const pointsCap = maxPoints();
    const locked = isLocked();
    const interval = refreshIntervalMs();
    const orgSelection = orgVersion();
    const enabled = props.enabled !== false;
    let active = true;
    let pending = false;
    let controller: AbortController | undefined;
    let timer: number | undefined;

    onCleanup(() => {
      active = false;
      controller?.abort();
      if (timer !== undefined) window.clearInterval(timer);
    });

    // The series is what the chart shows. The point cap follows the chart's
    // width, so a resize asks for the same series at a new resolution: it
    // keeps the readings on screen while the new request is in flight.
    const series = JSON.stringify([
      orgSelection,
      resourceType,
      resourceId,
      metric,
      chartRange,
      locked,
      suppliedData !== undefined,
    ]);
    const selection = JSON.stringify([series, pointsCap]);
    // Pausing, resuming or resizing the same series keeps its readings on
    // screen; any other change starts from nothing.
    if (suppliedData !== undefined || series !== previousSeries) {
      setData(suppliedData ?? []);
      setSource(suppliedData !== undefined ? 'live' : null);
      setError(null);
      setRefreshFailed(false);
      loadedSeries = undefined;
    }
    previousSeries = series;
    setLoading(false);
    if (selection !== previousSelection) {
      pendingTouch = undefined;
      touchTimestamp = null;
      setHoveredPoint(null);
      setHoveredTimestamp(null);
      setKeyboardInspecting(false);
    }
    previousSelection = selection;
    if (suppliedData !== undefined || locked || !resourceId || !resourceType) return;
    // A paused chart (inside a hidden drawer tab) reads nothing until resumed.
    if (!enabled) return;
    let hasLoaded = loadedSeries === series;

    const loadData = async () => {
      if (!active || pending) return;
      pending = true;
      controller = new AbortController();
      if (!hasLoaded) setLoading(true);
      try {
        const result = await ChartsAPI.getMetricsHistory({
          resourceType,
          resourceId,
          metric,
          range: chartRange,
          maxPoints: pointsCap ?? undefined,
          signal: controller.signal,
        });
        if (!active) return;
        setData('points' in result ? (result.points ?? []) : []);
        setSource(result.source ?? 'store');
        setError(null);
        setRefreshFailed(false);
        hasLoaded = true;
        loadedSeries = series;
      } catch (err) {
        if (!active) return;
        console.error('Failed to fetch metrics history:', err);
        const accessError = getAPIReadAccessErrorMessage(err);
        if (accessError) {
          hasLoaded = false;
          loadedSeries = undefined;
          pendingTouch = undefined;
          touchTimestamp = null;
          batch(() => {
            setData([]);
            setSource(null);
            setHoveredPoint(null);
            setHoveredTimestamp(null);
            setKeyboardInspecting(false);
            setRefreshFailed(false);
            setError(accessError);
          });
        } else if (hasLoaded) setRefreshFailed(true);
        else setError('Failed to load history data');
      } finally {
        if (active) {
          pending = false;
          setLoading(false);
        }
      }
    };

    void loadData();
    if (interval > 0) timer = window.setInterval(() => void loadData(), interval);
  });

  const drawChart = () => {
    const canvas = refs.getCanvas();
    if (!canvas) return;
    const ctx = canvas.getContext('2d');
    if (!ctx) return;

    const points = data();
    const width = canvas.parentElement?.clientWidth || 300;
    const height = chartHeight();

    setupCanvasDPR(canvas, ctx, width, height);
    ctx.clearRect(0, 0, width, height);

    const isDark = document.documentElement.classList.contains('dark');
    const gridColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.05)';
    const textColor = isDark ? '#9ca3af' : '#6b7280';
    const axisTextColor = isDark ? '#9ca3af' : '#6b7280';
    const mainColor = getHistoryChartDefaultColor(props.metric, props.color);
    const scale = getHistoryChartScale(points, props.unit);
    const yAxisTicks = getHistoryChartYAxisLabels(scale, props.unit);
    const timeTicks = (count: number) =>
      Array.from({ length: count }, (_, index) => {
        const timestamp =
          points[0].timestamp +
          ((points[points.length - 1].timestamp - points[0].timestamp) * index) / (count - 1);
        return { timestamp, label: formatHistoryChartTimeLabel(timestamp, range()) };
      });

    ctx.font = '10px sans-serif';
    chartLeftInset = getHistoryChartLeftInset(
      yAxisTicks.map((tick) => ctx.measureText(tick.label).width),
    );
    let timeAxisTicks =
      points.length === 1
        ? [
            {
              timestamp: points[0].timestamp,
              label: formatHistoryChartTimeLabel(points[0].timestamp, range()),
            },
          ]
        : points.length > 0
          ? timeTicks(4)
          : [];
    chartRightInset = getHistoryChartRightInset(
      timeAxisTicks.length > 0
        ? ctx.measureText(timeAxisTicks[timeAxisTicks.length - 1].label).width
        : 0,
    );
    // A narrow card fits fewer time labels: as many as fit without touching,
    // down to the first and last time.
    if (timeAxisTicks.length > 2) {
      const count = getHistoryChartTimeTickCount(
        width - chartLeftInset - chartRightInset,
        Math.max(...timeAxisTicks.map((tick) => ctx.measureText(tick.label).width)),
        timeAxisTicks.length,
      );
      if (count < timeAxisTicks.length) timeAxisTicks = timeTicks(count);
    }
    timeAxisTicks = withoutRepeatedHistoryTimeTicks(timeAxisTicks);

    ctx.strokeStyle = gridColor;
    ctx.lineWidth = 1;
    for (const tick of yAxisTicks) {
      const y = height - 20 - tick.pct * (height - 40);
      ctx.beginPath();
      ctx.moveTo(chartLeftInset, y);
      ctx.lineTo(width - chartRightInset, y);
      ctx.stroke();

      ctx.fillStyle = textColor;
      ctx.textAlign = 'right';
      ctx.textBaseline = 'middle';
      ctx.fillText(tick.label, chartLeftInset - 5, y);
    }

    if (points.length === 0) {
      setHoveredPoint(null);
      return;
    }

    const geometry = createHistoryChartGeometry({
      width,
      height,
      startTime: points[0].timestamp,
      endTime: points[points.length - 1].timestamp,
      minValue: scale.minValue,
      maxValue: scale.maxValue,
      leftInset: chartLeftInset,
      rightInset: chartRightInset,
    });

    if (points.length === 1) {
      // A stored observation is visible before inspection, including measured
      // zero. A line or filled area would invent a duration for this sample.
      ctx.beginPath();
      ctx.arc(
        geometry.getX(points[0].timestamp),
        geometry.getY(points[0].value),
        4,
        0,
        Math.PI * 2,
      );
      ctx.fillStyle = mainColor;
      ctx.fill();
    } else {
      // Each stored point is a bucket average with its lowest and highest
      // reading. A band spans those extremes behind the line, so a short
      // spike stays visible after downsampling; flat buckets draw no band.
      // The drawer charts draw the same band.
      if (hasHistoryChartSpread(points)) {
        ctx.beginPath();
        points.forEach((point, index) => {
          const x = geometry.getX(point.timestamp);
          const y = geometry.getY(Math.max(point.max ?? point.value, point.value));
          if (index === 0) ctx.moveTo(x, y);
          else ctx.lineTo(x, y);
        });
        for (let index = points.length - 1; index >= 0; index--) {
          const point = points[index];
          ctx.lineTo(
            geometry.getX(point.timestamp),
            geometry.getY(Math.min(point.min ?? point.value, point.value)),
          );
        }
        ctx.closePath();
        ctx.fillStyle = `${mainColor}33`;
        ctx.fill();
        ctx.strokeStyle = `${mainColor}73`;
        ctx.lineWidth = 1;
        ctx.stroke();
      }

      ctx.beginPath();
      ctx.strokeStyle = mainColor;
      ctx.lineWidth = 2;
      points.forEach((point, index) => {
        if (index === 0) ctx.moveTo(geometry.getX(point.timestamp), geometry.getY(point.value));
        else ctx.lineTo(geometry.getX(point.timestamp), geometry.getY(point.value));
      });
      ctx.stroke();
    }

    ctx.fillStyle = axisTextColor;
    ctx.font = '10px sans-serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';

    for (const tick of timeAxisTicks) {
      ctx.fillText(tick.label, geometry.getX(tick.timestamp), height - 2);
    }

    const hoverTimestamp = hoveredTimestamp();
    if (
      hoverTimestamp === null ||
      hoverTimestamp < points[0].timestamp ||
      hoverTimestamp > points[points.length - 1].timestamp
    ) {
      setHoveredPoint(null);
      return;
    }

    const cursor = geometry.getX(hoverTimestamp);

    ctx.save();
    ctx.strokeStyle = isDark ? 'rgba(255, 255, 255, 0.4)' : 'rgba(0, 0, 0, 0.3)';
    ctx.lineWidth = 1;
    ctx.setLineDash([4, 4]);
    ctx.beginPath();
    ctx.moveTo(cursor, 0);
    ctx.lineTo(cursor, height - 20);
    ctx.stroke();
    ctx.restore();

    const closest = findHistoryChartClosestPoint(points, hoverTimestamp);
    const pointX = geometry.getX(closest.timestamp);
    const pointY = geometry.getY(closest.value);

    ctx.beginPath();
    ctx.arc(pointX, pointY, 5, 0, Math.PI * 2);
    ctx.fillStyle = isDark ? '#1f2937' : '#ffffff';
    ctx.fill();

    ctx.beginPath();
    ctx.arc(pointX, pointY, 4, 0, Math.PI * 2);
    ctx.fillStyle = mainColor;
    ctx.fill();

    ctx.beginPath();
    ctx.arc(pointX, pointY, 2, 0, Math.PI * 2);
    ctx.fillStyle = isDark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(255, 255, 255, 0.8)';
    ctx.fill();

    setHoveredPoint({
      value: closest.value,
      timestamp: closest.timestamp,
      x: pointX,
      y: pointY,
    });
  };

  createEffect(() => {
    hoveredTimestamp();
    drawChart();
  });

  createEffect(() => {
    const container = refs.getContainer();
    if (!container) return;

    const updateMaxPoints = () => {
      const width = container.clientWidth || 0;
      if (width <= 0) return;
      setChartWidth(width);
      const next = calculateOptimalPoints(width, 'history');
      if (next !== maxPoints()) {
        setMaxPoints(next);
      }
    };

    const resizeObserver = new ResizeObserver(() => {
      updateMaxPoints();
      drawChart();
    });
    resizeObserver.observe(container);
    updateMaxPoints();
    onCleanup(() => resizeObserver.disconnect());
  });

  const handleFocus = () => {
    // Touch's compatibility mouse events can focus the canvas after pointerup.
    // That focus must not replace the tapped reading with the latest sample.
    if (touchFocus) {
      setHoveredTimestamp(touchTimestamp);
      return;
    }
    setKeyboardInspecting(true);
    const points = data();
    setHoveredTimestamp(points.length ? points[points.length - 1].timestamp : null);
  };

  const handleBlur = () => {
    touchFocus = false;
    touchTimestamp = null;
    pendingTouch = undefined;
    setKeyboardInspecting(false);
    setHoveredTimestamp(null);
  };

  const handleKeyDown = (event: KeyboardEvent) => {
    if (event.altKey || event.ctrlKey || event.metaKey) return;
    if (event.key === 'Escape') {
      touchFocus = false;
      touchTimestamp = null;
      pendingTouch = undefined;
      setHoveredTimestamp(null);
      return;
    }
    const points = data();
    if (!points.length || !['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
    event.preventDefault();
    touchFocus = false;
    touchTimestamp = null;
    pendingTouch = undefined;
    setKeyboardInspecting(true);
    const timestamp = hoveredTimestamp();
    const index =
      timestamp === null
        ? points.length - 1
        : points.indexOf(findHistoryChartClosestPoint(points, timestamp));
    const next =
      event.key === 'Home'
        ? 0
        : event.key === 'End'
          ? points.length - 1
          : Math.max(0, Math.min(points.length - 1, index + (event.key === 'ArrowLeft' ? -1 : 1)));
    setHoveredTimestamp(points[next].timestamp);
  };

  const inspectAtClientX = (clientX: number, snapToSample = false) => {
    const canvas = refs.getCanvas();
    const points = data();
    if (!canvas || points.length === 0) return;

    setKeyboardInspecting(false);
    const rect = canvas.getBoundingClientRect();
    const x = clientX - rect.left;
    const width = rect.width;
    if (x < chartLeftInset || x > width - chartRightInset) {
      setHoveredTimestamp(null);
      return;
    }

    const geometry = createHistoryChartGeometry({
      width,
      height: chartHeight(),
      startTime: points[0].timestamp,
      endTime: points[points.length - 1].timestamp,
      minValue: 0,
      maxValue: 1,
      leftInset: chartLeftInset,
      rightInset: chartRightInset,
    });
    const timestamp = geometry.getTimestamp(x);
    const inspectedTimestamp = snapToSample
      ? findHistoryChartClosestPoint(points, timestamp).timestamp
      : timestamp;
    setHoveredTimestamp(inspectedTimestamp);
    return inspectedTimestamp;
  };

  const handleMouseMove = (event: MouseEvent) => {
    if (!touchFocus) inspectAtClientX(event.clientX);
  };

  const handlePointerDown = (event: PointerEvent) => {
    if (event.pointerType === 'mouse') touchFocus = false;
    if (event.pointerType !== 'touch') return;
    touchFocus = true;
    touchTimestamp = null;
    setKeyboardInspecting(false);
    setHoveredTimestamp(null);
    pendingTouch = event.isPrimary
      ? { pointerId: event.pointerId, x: event.clientX, y: event.clientY, moved: false }
      : undefined;
  };

  const handlePointerMove = (event: PointerEvent) => {
    if (event.pointerType === 'mouse') touchFocus = false;
    if (event.pointerType !== 'touch' || pendingTouch?.pointerId !== event.pointerId) return;
    if (Math.hypot(event.clientX - pendingTouch.x, event.clientY - pendingTouch.y) > 10) {
      pendingTouch.moved = true;
    }
  };

  const handlePointerUp = (event: PointerEvent) => {
    if (event.pointerType !== 'touch') return;
    const touch = pendingTouch;
    pendingTouch = undefined;
    if (!touch || !event.isPrimary || touch.pointerId !== event.pointerId || touch.moved) return;
    if (Math.hypot(event.clientX - touch.x, event.clientY - touch.y) > 10) return;
    const rect = refs.getCanvas()?.getBoundingClientRect();
    if (!rect || event.clientY < rect.top || event.clientY > rect.bottom) return;
    touchTimestamp = inspectAtClientX(event.clientX, true) ?? null;
  };

  const handlePointerCancel = (event: PointerEvent) => {
    if (event.pointerType !== 'touch') return;
    pendingTouch = undefined;
    touchTimestamp = null;
    setHoveredTimestamp(null);
  };

  const handleMouseLeave = () => {
    if (keyboardInspecting() || touchFocus) return;
    setHoveredTimestamp(null);
  };

  return {
    data,
    dataMax,
    dataMin,
    error,
    refreshFailed,
    handleFocus,
    handleBlur,
    handleKeyDown,
    keyboardInspecting,
    handleMouseLeave,
    handleMouseMove,
    handlePointerDown,
    handlePointerMove,
    handlePointerUp,
    handlePointerCancel,
    chartHeight,
    chartWidth,
    hoveredPoint,
    isLocked,
    loading,
    lockDays,
    lockTierLabel,
    range,
    ranges: HISTORY_CHART_RANGES,
    source,
    updateRange,
  };
}

export type HistoryChartState = ReturnType<typeof useHistoryChartState>;