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

100% Statements 21/21
92% Branches 23/25
100% Functions 5/5
100% Lines 18/18

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                                                  27x 20x   19x 20x 20x 20x   20x                                                   27x 20x 19x       27x 20x 19x     27x 1x             26x                                                                                                                         78x                                               1x  
import React, { useMemo } from "react";
import clsx from "clsx";
import {
  CHART_COLORS,
  SEVERITY_COLORS,
  formatBytes,
  formatPercentage,
} from "../../constants";
import type { Severity } from "../../types";
import styles from "./HeapBreakdown.module.scss";
 
export interface HeapBreakdownProps {
  /** Heap used in bytes */
  heapUsed: number | null;
  /** Heap total in bytes */
  heapTotal: number | null;
  /** Heap limit in bytes */
  heapLimit: number | null;
  /** Current severity level */
  severity?: Severity;
  /** Warning threshold percentage */
  warningThreshold?: number;
  /** Critical threshold percentage */
  criticalThreshold?: number;
  /** Custom class name */
  className?: string;
}
 
interface BreakdownSegment {
  label: string;
  value: number;
  percentage: number;
  color: string;
  type: 'used' | 'available' | 'reserved';
}
 
/**
 * Visual breakdown of heap memory distribution
 */
export function HeapBreakdown({
  heapUsed,
  heapTotal,
  heapLimit,
  severity = "normal",
  warningThreshold = 70,
  criticalThreshold = 90,
  className,
}: HeapBreakdownProps) {
  // Calculate segments
  const segments = useMemo((): BreakdownSegment[] => {
    if (heapLimit === null || heapLimit === 0) return [];
 
    const used = heapUsed ?? 0;
    const total = heapTotal ?? 0;
    const available = Math.max(0, total - used);
    const reserved = Math.max(0, heapLimit - total);
 
    return [
      {
        label: "Used",
        value: used,
        percentage: (used / heapLimit) * 100,
        color: SEVERITY_COLORS[severity].accent,
        type: 'used',
      },
      {
        label: "Available",
        value: available,
        percentage: (available / heapLimit) * 100,
        color: CHART_COLORS.secondary,
        type: 'available',
      },
      {
        label: "Reserved",
        value: reserved,
        percentage: (reserved / heapLimit) * 100,
        color: CHART_COLORS.grayLight,
        type: 'reserved',
      },
    ];
  }, [heapUsed, heapTotal, heapLimit, severity]);
 
  // Calculate utilization percentage (used / total)
  const utilizationPercent = useMemo(() => {
    if (!heapUsed || !heapTotal || heapTotal === 0) return 0;
    return (heapUsed / heapTotal) * 100;
  }, [heapUsed, heapTotal]);
 
  // Calculate limit usage percentage (used / limit)
  const limitUsagePercent = useMemo(() => {
    if (!heapUsed || !heapLimit || heapLimit === 0) return 0;
    return (heapUsed / heapLimit) * 100;
  }, [heapUsed, heapLimit]);
 
  if (segments.length === 0) {
    return (
      <div className={clsx(styles.emptyState, className)}>
        No memory data available
      </div>
    );
  }
 
  return (
    <div className={clsx(styles.container, className)}>
      {/* Heap Utilization Bar */}
      <div>
        <div className={styles.sectionHeader}>
          <span className={styles.sectionTitle}>Heap Utilization</span>
          <span className={clsx(styles.sectionValue, styles[severity])}>
            {formatPercentage(utilizationPercent)}
          </span>
        </div>
        <div className={styles.progressBarContainer}>
          <div
            className={styles.progressBar}
            style={{
              width: `${Math.min(utilizationPercent, 100)}%`,
              backgroundColor: SEVERITY_COLORS[severity].accent,
            }}
          />
        </div>
        <div className={styles.progressLabels}>
          <span>{formatBytes(heapUsed)} used</span>
          <span>{formatBytes(heapTotal)} allocated</span>
        </div>
      </div>
 
      {/* Limit Usage Bar */}
      <div>
        <div className={styles.sectionHeader}>
          <span className={styles.sectionTitle}>Limit Usage</span>
          <span className={clsx(styles.sectionValue, styles[severity])}>
            {formatPercentage(limitUsagePercent)}
          </span>
        </div>
        <div className={clsx(styles.progressBarContainer, styles.progressBarContainerWithMarkers)}>
          {/* Threshold markers */}
          <div
            className={clsx(styles.thresholdMarker, styles.warning)}
            style={{ left: `${warningThreshold}%` }}
          />
          <div
            className={clsx(styles.thresholdMarker, styles.critical)}
            style={{ left: `${criticalThreshold}%` }}
          />
          {/* Progress bar */}
          <div
            className={styles.progressBar}
            style={{
              width: `${Math.max(Math.min(limitUsagePercent, 100), 1)}%`,
              backgroundColor: SEVERITY_COLORS[severity].accent,
            }}
          />
        </div>
        <div className={styles.progressLabels}>
          <span>{formatBytes(heapUsed)} used</span>
          <span>{formatBytes(heapLimit)} limit</span>
        </div>
      </div>
 
      {/* Memory Values Grid */}
      <div className={styles.segmentsGrid}>
        {segments.map((segment) => (
          <div key={segment.label} className={styles.segmentCard}>
            <div
              className={styles.segmentIndicator}
              style={{ backgroundColor: segment.color }}
            />
            <div className={styles.segmentLabel}>
              {segment.label}
            </div>
            <div
              className={clsx(styles.segmentValue, styles[segment.type])}
              style={segment.type === 'used' ? { color: segment.color } : undefined}
            >
              {formatBytes(segment.value)}
            </div>
            <div className={styles.segmentPercentage}>
              {formatPercentage(segment.percentage)}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}
 
HeapBreakdown.displayName = "HeapBreakdown";