File size: 4,681 Bytes
90cbf22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { HistoricalTimeManager } from '@/hooks/useHistoricalTime';
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
import uPlot, { AlignedData, Options } from 'uplot';

const MAX_DATA_POINTS = 10000;

export function DebugTimeManager(props: {
  timeManager: HistoricalTimeManager;
  width: number;
  height: number;
}) {
  const [plotElement, setPlotElement] = useState<HTMLDivElement | null>(null);
  const [plot, setPlot] = useState<uPlot>();

  useLayoutEffect(() => {
    if (!plotElement) {
      return;
    }
    const opts: Options = {
      width: props.width,
      height: props.height,
      series: [
        {},
        {
          stroke: 'white',
          spanGaps: true,
          pxAlign: 0,
          points: { show: false },
          label: 'Buffer health',
        },
      ],
      scales: {
        y: { distr: 1 },
      },
      axes: [
        {
          side: 0,
          show: false,
        },
        {
          ticks: { size: 0 },
          side: 1,
          stroke: 'white',
        },
      ],
      legend: {
        show: false,
      },
    };
    const data: AlignedData = [[], []];
    const plot = new uPlot(opts, data, plotElement);
    setPlot(plot);
  }, [plotElement, props.width, props.height]);

  const timeManager = props.timeManager;
  const [intervals, setIntervals] = useState([...timeManager.intervals]);
  useEffect(() => {
    let reqId: ReturnType<typeof requestAnimationFrame> = 0;
    const data = {
      t: [] as number[],
      bufferHealth: [] as number[],
    };
    const update = () => {
      if (plot) {
        if (data.t.length > MAX_DATA_POINTS) {
          data.t = data.t.slice(-MAX_DATA_POINTS);
          data.bufferHealth = data.bufferHealth.slice(-MAX_DATA_POINTS);
        }
        const now = Date.now() / 1000;
        data.t.push(now);
        data.bufferHealth.push(timeManager.bufferHealth());
        setIntervals([...timeManager.intervals]);
        plot.setData([data.t, data.bufferHealth], true);
        plot.setScale('x', { min: now - 10, max: now });
      }
      reqId = requestAnimationFrame(update);
    };
    update();
    return () => cancelAnimationFrame(reqId);
  }, [plot, timeManager]);

  let intervalNode: React.ReactNode | null = null;
  if (intervals.length > 0) {
    const base = intervals[0].startTs;
    const baseAge = Date.now() - base;

    intervalNode = (
      <div style={{ fontSize: '12px' }}>
        {intervals.length} {intervals.length > 1 ? 'intervals' : 'interval'}:
        <div style={{ fontSize: '9px', height: '48px' }}>
          <p>Base: {toSeconds(baseAge)}s ago</p>
          {intervals.map((interval) => {
            const containsServerTs =
              timeManager.prevServerTs &&
              interval.startTs < timeManager.prevServerTs &&
              timeManager.prevServerTs <= interval.endTs;
            let serverTs = null;
            if (containsServerTs) {
              serverTs = ` (server: ${toSeconds((timeManager.prevServerTs ?? base) - base)})`;
            }
            return (
              <div
                key={interval.startTs}
                style={{ paddingRight: '3px', fontWeight: containsServerTs ? 'bold' : '' }}
              >
                {toSeconds(interval.startTs - base)} - {toSeconds(interval.endTs - base)}
                {serverTs}
              </div>
            );
          })}
        </div>
      </div>
    );
  }
  let statusNode: React.ReactNode | null = null;
  if (timeManager.latestEngineStatus) {
    const status = timeManager.latestEngineStatus;
    let statusMsg = status.running ? 'Running' : 'Stopped';
    statusNode = (
      <div style={{ fontSize: '12px', paddingTop: '8px' }}>
        <p>Generation number: {status.generationNumber}</p>
        <p>Input number: {status.processedInputNumber}</p>
        <p>Status: {statusMsg}</p>
        <p>Client skew: {toSeconds(timeManager.clockSkew())}s</p>
      </div>
    );
  }
  timeManager.latestEngineStatus?.generationNumber;

  return (
    <div
      style={{
        background: 'rgb(53, 59, 89)',
        position: 'fixed',
        top: '20px',
        left: '20px',
        padding: '10px',
        border: '1px solid rgb(23, 20, 33)',
        color: 'white',
        zIndex: 1,
      }}
    >
      <div style={{ height: '20px', width: '100%', textAlign: 'center' }}>Engine stats</div>
      {statusNode}
      <div ref={setPlotElement} />
      {intervalNode}
    </div>
  );
}

// D3's Tableau10
export const COLORS = (
  '4e79a7f28e2ce1575976b7b259a14fedc949af7aa1ff9da79c755fbab0ab'.match(/.{6}/g) as string[]
).map((x) => `#${x}`);

const toSeconds = (n: number) => (n / 1000).toFixed(2);