All files / client/src/hooks use-telemetry-store.ts

88.46% Statements 46/52
50% Branches 2/4
90.9% Functions 10/11
89.13% Lines 41/46

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                                            5x   5x   5x           5x 5x 5x   5x 60x     5x 44x 44x 44x 489x     489x 489x   44x     5x 44x 44x 44x   44x           44x     5x 15x 15x 15x 15x         15x     65x   5x 9x 9x     5x                                         1x         1x 1x 1x 1x       5x 20x   20x                
import { useSyncExternalStore } from "react";
 
export interface TelemetryMetrics {
  timestamp: number;
  intendedPinChangesPerSecond: number;
  actualPinChangesPerSecond: number;
  droppedPinChangesPerSecond: number;
  batchesPerSecond: number;
  avgStatesPerBatch: number;
  serialOutputPerSecond: number;
  serialBytesPerSecond: number;
  serialBytesTotal: number;  // Cumulative bytes sent since simulation start
  serialIntendedBytesPerSecond?: number;  // Bytes that sketch tried to send (optional for backward compat)
  serialDroppedBytesPerSecond?: number;   // Bytes dropped due to baudrate limit (optional for backward compat)
}
 
interface TelemetrySnapshot {
  history: TelemetryMetrics[];
  last: TelemetryMetrics | null;
  lastHeartbeatAt: number | null;
}
 
const TELEMETRY_BUFFER_SIZE = 60;
 
const subscribers = new Set<() => void>();
 
let snapshot: TelemetrySnapshot = {
  history: [],
  last: null,
  lastHeartbeatAt: null,
};
 
let ringBuffer: TelemetryMetrics[] = new Array(TELEMETRY_BUFFER_SIZE);
let ringIndex = 0;
let ringCount = 0;
 
const notify = () => {
  subscribers.forEach((fn) => fn());
};
 
const getHistory = (): TelemetryMetrics[] => {
  Iif (ringCount === 0) return [];
  const ordered: TelemetryMetrics[] = [];
  for (let i = 0; i < ringCount; i += 1) {
    const idx =
      (ringIndex - ringCount + i + TELEMETRY_BUFFER_SIZE) %
      TELEMETRY_BUFFER_SIZE;
    const entry = ringBuffer[idx];
    Eif (entry) ordered.push(entry);
  }
  return ordered;
};
 
const pushTelemetry = (metrics: TelemetryMetrics) => {
  ringBuffer[ringIndex] = metrics;
  ringIndex = (ringIndex + 1) % TELEMETRY_BUFFER_SIZE;
  ringCount = Math.min(TELEMETRY_BUFFER_SIZE, ringCount + 1);
 
  snapshot = {
    history: getHistory(),
    last: metrics,
    lastHeartbeatAt: metrics.timestamp,
  };
 
  notify();
};
 
const resetTelemetry = () => {
  ringBuffer = new Array(TELEMETRY_BUFFER_SIZE);
  ringIndex = 0;
  ringCount = 0;
  snapshot = {
    history: [],
    last: null,
    lastHeartbeatAt: null,
  };
  notify();
};
 
const getSnapshot = (): TelemetrySnapshot => snapshot;
 
const subscribe = (callback: () => void) => {
  subscribers.add(callback);
  return () => subscribers.delete(callback);
};
 
export const telemetryStore = {
  subscribe,
  getSnapshot,
  pushTelemetry,
  resetTelemetry,
  resetToInitial: () => {
    // Clear telemetry history  
    snapshot = {
      history: [],
      last: null,
      lastHeartbeatAt: null,
    };
    ringBuffer = new Array(TELEMETRY_BUFFER_SIZE);
    ringIndex = 0;
    ringCount = 0;
    notify();
  },
  /**
   * Hard reset for complete telemetry wipe
   */
  resetToEmpty: () => {
    snapshot = {
      history: [],
      last: null,
      lastHeartbeatAt: null,
    };
    ringBuffer = new Array(TELEMETRY_BUFFER_SIZE);
    ringIndex = 0;
    ringCount = 0;
    notify();
  },
};
 
export const useTelemetryStore = () => {
  const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
 
  return {
    history: state.history,
    last: state.last,
    lastHeartbeatAt: state.lastHeartbeatAt,
    pushTelemetry,
    resetTelemetry,
  };
};