All files / memory-monitor/src/components/Visualizations MemoryGauge.tsx

100% Statements 23/23
76.47% Branches 13/17
100% Functions 5/5
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                                                                  39x 39x 39x   39x                       78x 78x                                     27x 27x     27x 27x 27x 27x 27x 27x     27x 27x 27x     27x 20x       27x   20x           27x                                                                                                                                       1x  
import React, { useMemo } from "react";
import clsx from "clsx";
import { SEVERITY_COLORS, formatPercentage } from "../../constants";
import type { Severity } from "../../types";
import styles from "./MemoryGauge.module.scss";
 
export interface MemoryGaugeProps {
  /** Usage percentage (0-100) */
  usagePercentage: number | null;
  /** Current severity level */
  severity: Severity;
  /** Formatted heap used string */
  heapUsed?: string;
  /** Formatted heap limit string */
  heapLimit?: string;
  /** Warning threshold */
  warningThreshold?: number;
  /** Critical threshold */
  criticalThreshold?: number;
  /** Custom class name */
  className?: string;
}
 
/**
 * Custom SVG arc path generator
 */
function describeArc(
  cx: number,
  cy: number,
  radius: number,
  startAngle: number,
  endAngle: number
): string {
  const start = polarToCartesian(cx, cy, radius, endAngle);
  const end = polarToCartesian(cx, cy, radius, startAngle);
  const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
 
  return [
    "M", start.x, start.y,
    "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y,
  ].join(" ");
}
 
function polarToCartesian(
  cx: number,
  cy: number,
  radius: number,
  angleInDegrees: number
) {
  const angleInRadians = ((angleInDegrees - 180) * Math.PI) / 180;
  return {
    x: cx + radius * Math.cos(angleInRadians),
    y: cy + radius * Math.sin(angleInRadians),
  };
}
 
/**
 * Radial gauge chart showing memory usage percentage
 * Uses custom SVG for precise control over appearance
 */
export function MemoryGauge({
  usagePercentage,
  severity,
  heapUsed,
  heapLimit,
  warningThreshold = 70,
  criticalThreshold = 90,
  className,
}: MemoryGaugeProps) {
  const percentage = usagePercentage ?? 0;
  const color = SEVERITY_COLORS[severity].accent;
 
  // SVG dimensions
  const width = 280;
  const height = 160;
  const cx = width / 2;
  const cy = height - 20;
  const radius = 100;
  const strokeWidth = 24;
 
  // Arc angles (180° arc from left to right)
  const startAngle = 0;
  const endAngle = 180;
  const progressAngle = (percentage / 100) * 180;
 
  // Generate arc paths
  const backgroundArc = useMemo(
    () => describeArc(cx, cy, radius, startAngle, endAngle),
    [cx, cy, radius]
  );
 
  const progressArc = useMemo(
    () =>
      percentage > 0
        ? describeArc(cx, cy, radius, startAngle, Math.min(progressAngle, 180))
        : "",
    [cx, cy, radius, percentage, progressAngle]
  );
 
  return (
    <div className={clsx(styles.container, className)}>
      {/* SVG Gauge */}
      <div className={styles.svgWrapper}>
        <svg
          width={width}
          height={height}
          viewBox={`0 0 ${width} ${height}`}
          className={styles.svg}
        >
          {/* Background arc */}
          <path
            d={backgroundArc}
            fill="none"
            stroke="currentColor"
            strokeWidth={strokeWidth}
            strokeLinecap="round"
            className={styles.backgroundArc}
          />
 
          {/* Progress arc */}
          {percentage > 0 && (
            <path
              d={progressArc}
              fill="none"
              stroke={color}
              strokeWidth={strokeWidth}
              strokeLinecap="round"
              style={{
                transition: "stroke-dashoffset 0.5s ease-out",
              }}
            />
          )}
        </svg>
 
        {/* Center content - positioned over the SVG */}
        <div className={styles.centerContent}>
          <div
            className={clsx(
              styles.percentageValue,
              severity === "critical" && styles.critical,
              severity === "warning" && styles.warning
            )}
          >
            {formatPercentage(usagePercentage)}
          </div>
          {heapUsed && heapLimit && (
            <div className={styles.heapInfo}>
              {heapUsed} / {heapLimit}
            </div>
          )}
        </div>
      </div>
 
      {/* Threshold markers */}
      <div
        className={styles.thresholdMarkers}
        style={{ width: width - 20 }}
      >
        <span>0%</span>
        <span className={styles.warningMarker}>{warningThreshold}%</span>
        <span className={styles.criticalMarker}>{criticalThreshold}%</span>
        <span>100%</span>
      </div>
    </div>
  );
}
 
MemoryGauge.displayName = "MemoryGauge";