-
Notifications
You must be signed in to change notification settings - Fork 4k
/
MetricsPlotPanel.js
624 lines (570 loc) · 22.5 KB
/
MetricsPlotPanel.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import React from 'react';
import { connect } from 'react-redux';
import Utils from '../../common/utils/Utils';
import RequestStateWrapper from '../../common/components/RequestStateWrapper';
import { getMetricHistoryApi, getRunApi } from '../actions';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { MetricsPlotView } from './MetricsPlotView';
import { getRunTags, getRunInfo } from '../reducers/Reducers';
import {
MetricsPlotControls,
X_AXIS_WALL,
X_AXIS_RELATIVE,
X_AXIS_STEP,
} from './MetricsPlotControls';
import qs from 'qs';
import { withRouter } from 'react-router-dom';
import Routes from '../routes';
import { RunLinksPopover } from './RunLinksPopover';
import { getUUID } from '../../common/utils/ActionUtils';
export const CHART_TYPE_LINE = 'line';
export const CHART_TYPE_BAR = 'bar';
export class MetricsPlotPanel extends React.Component {
static propTypes = {
experimentId: PropTypes.string.isRequired,
runUuids: PropTypes.arrayOf(PropTypes.string).isRequired,
metricKey: PropTypes.string.isRequired,
// A map of { runUuid : { metricKey: value } }
latestMetricsByRunUuid: PropTypes.object.isRequired,
// An array of distinct metric keys across all runUuids
distinctMetricKeys: PropTypes.arrayOf(PropTypes.string).isRequired,
// An array of { metricKey, history, runUuid, runDisplayName }
metricsWithRunInfoAndHistory: PropTypes.arrayOf(PropTypes.object).isRequired,
getMetricHistoryApi: PropTypes.func.isRequired,
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
runDisplayNames: PropTypes.arrayOf(PropTypes.string).isRequired,
runs: PropTypes.arrayOf(PropTypes.object),
getRunApi: PropTypes.func,
};
// The fields below are exposed as instance attributes rather than component state so that they
// can be updated without triggering a rerender.
//
// ID of Javascript future (created via setTimeout()) used to trigger legend-click events after a
// delay, to allow time for double-click events to occur
legendClickTimeout = null;
// Time (millis after Unix epoch) since last legend click - if two clicks occur in short
// succession, we trigger a double-click event & cancel the pending single-click.
prevLegendClickTime = Math.inf;
// Last curve ID clicked in the legend, used to determine if we're double-clicking on a specific
// legend curve
lastClickedLegendCurveId = null;
// Max time interval (in milliseconds) between two successive clicks on the metric plot legend
// that constitutes a double-click
MAX_DOUBLE_CLICK_INTERVAL_MS = 300;
// Delay (in ms) between when a user clicks on the metric plot legend & when event-handler logic
// (to toggle display of the selected curve on or off) actually fires. Set to a larger value than
// MAX_DOUBLE_CLICK_INTERVAL_MS to allow time for the double-click handler to fire before firing
// a single-click event.
SINGLE_CLICK_EVENT_DELAY_MS = this.MAX_DOUBLE_CLICK_INTERVAL_MS + 10;
DURATION_BETWEEN_HISTORY_UPDATES_MS = 8000;
constructor(props) {
super(props);
this.state = {
historyRequestIds: [],
popoverVisible: false,
popoverX: 0,
popoverY: 0,
popoverRunItems: [],
timerId: null,
};
this.displayPopover = false;
this.loadMetricHistory(this.props.runUuids, this.getUrlState().selectedMetricKeys);
}
componentDidMount() {
const { runs } = this.props;
if (runs && this.checkOnRunUnfinished()) {
const timerId = setInterval(() => {
const requestIds = [];
if (this.checkOnRunUnfinished()) {
const getRequestIds = this.loadMetricHistory(
this.props.runUuids,
this.getUrlState().selectedMetricKeys,
);
requestIds.push(getRequestIds);
this.props.runs.forEach((run) => {
const runUuid = run.getRunUuid();
const getRunRequestId = getUUID();
requestIds.push(getRunRequestId);
this.props.getRunApi(runUuid, getRunRequestId);
});
this.setState((prevState) => ({
historyRequestIds: [...prevState.historyRequestIds, ...requestIds],
}));
} else {
clearInterval(this.state.timerId);
}
}, this.DURATION_BETWEEN_HISTORY_UPDATES_MS);
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({ timerId });
}
}
getUrlState() {
return Utils.getMetricPlotStateFromUrl(this.props.location.search);
}
static predictChartType(metrics) {
// Show bar chart when every metric has exactly 1 metric history
if (
metrics &&
metrics.length &&
_.every(metrics, (metric) => metric.history && metric.history.length === 1)
) {
return CHART_TYPE_BAR;
}
return CHART_TYPE_LINE;
}
static isComparing(search) {
const params = qs.parse(search);
const runs = params && params['?runs'];
return runs ? JSON.parse(runs).length > 1 : false;
}
// Update page URL from component state. Intended to be called after React applies component
// state updates, e.g. in a setState callback
updateUrlState = (updatedState) => {
const { runUuids, metricKey, location, history } = this.props;
const experimentId = qs.parse(location.search)['experiment'];
const newState = {
...this.getUrlState(),
...updatedState,
};
const {
selectedXAxis,
selectedMetricKeys,
showPoint,
yAxisLogScale,
lineSmoothness,
layout,
deselectedCurves,
lastLinearYAxisRange,
} = newState;
history.replace(
Routes.getMetricPageRoute(
runUuids,
metricKey,
experimentId,
selectedMetricKeys,
layout,
selectedXAxis,
yAxisLogScale,
lineSmoothness,
showPoint,
deselectedCurves,
lastLinearYAxisRange,
),
);
};
loadMetricHistory = (runUuids, metricKeys) => {
const requestIds = [];
const { latestMetricsByRunUuid } = this.props;
runUuids.forEach((runUuid) => {
metricKeys.forEach((metricKey) => {
if (latestMetricsByRunUuid[runUuid][metricKey]) {
const id = getUUID();
this.props.getMetricHistoryApi(runUuid, metricKey, id);
requestIds.push(id);
}
});
});
return requestIds;
};
getMetrics = () => {
/* eslint-disable no-param-reassign */
const state = this.getUrlState();
const selectedMetricsSet = new Set(state.selectedMetricKeys);
const { selectedXAxis } = state;
const { metricsWithRunInfoAndHistory } = this.props;
// Take only selected metrics
const metrics = metricsWithRunInfoAndHistory.filter((m) => selectedMetricsSet.has(m.metricKey));
// Sort metric history based on selected x-axis
metrics.forEach((metric) => {
const isStep =
selectedXAxis === X_AXIS_STEP && metric.history[0] && _.isNumber(metric.history[0].step);
// Metric history can be large. Doing an in-place here to save memory
metric.history.sort(isStep ? Utils.compareByStepAndTimestamp : Utils.compareByTimestamp);
});
return metrics;
};
/**
* Handle changes in the scale type of the y-axis
* @param yAxisLogScale: Boolean - if true, y-axis should be converted to log scale, and if false,
* y-axis scale should be converted to a linear scale.
*/
handleYAxisLogScaleChange = (yAxisLogScale) => {
const state = this.getUrlState();
const newLayout = _.cloneDeep(state.layout);
const newAxisType = yAxisLogScale ? 'log' : 'linear';
// Handle special case of a linear y-axis scale with negative values converted to log scale &
// now being restored to linear scale, by restoring the old linear-axis range from
// state.linearYAxisRange. In particular, we assume that if state.linearYAxisRange
// is non-empty, it contains a linear y axis range with negative values.
if (!yAxisLogScale && state.lastLinearYAxisRange && state.lastLinearYAxisRange.length > 0) {
newLayout.yaxis = {
type: 'linear',
range: state.lastLinearYAxisRange,
};
this.updateUrlState({ layout: newLayout, lastLinearYAxisRange: [] });
return;
}
// Otherwise, if plot previously had no y axis range configured, simply set the axis type to
// log or linear scale appropriately
if (!state.layout.yaxis || !state.layout.yaxis.range) {
newLayout.yaxis = { type: newAxisType, autorange: true };
this.updateUrlState({ layout: newLayout, lastLinearYAxisRange: [] });
return;
}
// lastLinearYAxisRange contains the last range used for a linear-scale y-axis. We set
// this state attribute if and only if we're converting from a linear-scale y-axis with
// negative bounds to a log scale axis, so that we can restore the negative bounds if we
// subsequently convert back to a linear scale axis. Otherwise, we reset this attribute to an
// empty array
let lastLinearYAxisRange = [];
// At this point, we know the plot previously had a y axis specified with range bounds
// Convert the range to/from log scale as appropriate
const oldLayout = state.layout;
const oldYRange = oldLayout.yaxis.range;
if (yAxisLogScale) {
if (oldYRange[0] <= 0) {
lastLinearYAxisRange = oldYRange;
// When converting to log scale, handle negative values (which have no log-scale
// representation as taking the log of a negative number is not possible) as follows:
// If bottom of old Y range is negative, then tell plotly to infer the log y-axis scale
// (set 'autorange' to true), and preserve the old range in the lastLinearYAxisRange
// state attribute so that we can restore it if the user converts back to a linear-scale
// y axis. We defer to Plotly's autorange here under the assumption that it will produce
// a reasonable y-axis log scale for plots containing negative values.
newLayout.yaxis = {
type: 'log',
autorange: true,
};
} else {
newLayout.yaxis = {
type: 'log',
range: [Math.log(oldYRange[0]) / Math.log(10), Math.log(oldYRange[1]) / Math.log(10)],
};
}
} else {
// Otherwise, convert from log to linear scale normally
newLayout.yaxis = {
type: 'linear',
range: [Math.pow(10, oldYRange[0]), Math.pow(10, oldYRange[1])],
};
}
this.updateUrlState({ layout: newLayout, lastLinearYAxisRange });
};
/**
* Handle changes in the type of the metric plot's X axis (e.g. changes from wall-clock
* scale to relative-time scale to step-based scale).
* @param e: Selection event such that e.target.value is a string containing the new X axis type
*/
handleXAxisChange = (e) => {
// Set axis value type, & reset axis scaling via autorange
const state = this.getUrlState();
const axisEnumToPlotlyType = {
[X_AXIS_WALL]: 'date',
[X_AXIS_RELATIVE]: 'linear',
[X_AXIS_STEP]: 'linear',
};
const axisType = axisEnumToPlotlyType[e.target.value] || 'linear';
const newLayout = {
...state.layout,
xaxis: {
autorange: true,
type: axisType,
},
};
this.updateUrlState({ selectedXAxis: e.target.value, layout: newLayout });
};
getAxisType() {
const state = this.getUrlState();
return state.layout && state.layout.yaxis && state.layout.yaxis.type === 'log'
? 'log'
: 'linear';
}
/**
* Handle changes to metric plot layout (x & y axis ranges), e.g. specifically if the user
* zooms in or out on the plot.
*
* @param newLayout: Object containing the new Plot layout. See
* https://plot.ly/javascript/plotlyjs-events/#update-data for details on the object's fields
* and schema.
*/
handleLayoutChange = (newLayout) => {
this.displayPopover = false;
const state = this.getUrlState();
// Unfortunately, we need to parse out the x & y axis range changes from the onLayout event...
// see https://plot.ly/javascript/plotlyjs-events/#update-data
const {
'xaxis.range[0]': newXRange0,
'xaxis.range[1]': newXRange1,
'yaxis.range[0]': newYRange0,
'yaxis.range[1]': newYRange1,
'xaxis.autorange': xAxisAutorange,
'yaxis.autorange': yAxisAutorange,
'yaxis.showspikes': yAxisShowSpikes,
'xaxis.showspikes': xAxisShowSpikes,
...restFields
} = newLayout;
let mergedLayout = {
...state.layout,
...restFields,
};
let lastLinearYAxisRange = [...state.lastLinearYAxisRange];
// Set fields for x axis
const newXAxis = mergedLayout.xaxis || {};
if (newXRange0 !== undefined && newXRange1 !== undefined) {
newXAxis.range = [newXRange0, newXRange1];
newXAxis.autorange = false;
}
if (xAxisShowSpikes) {
newXAxis.showspikes = true;
}
if (xAxisAutorange) {
newXAxis.autorange = true;
}
// Set fields for y axis
const newYAxis = mergedLayout.yaxis || {};
if (newYRange0 !== undefined && newYRange1 !== undefined) {
newYAxis.range = [newYRange0, newYRange1];
newYAxis.autorange = false;
}
if (yAxisShowSpikes) {
newYAxis.showspikes = true;
}
if (yAxisAutorange) {
lastLinearYAxisRange = [];
const axisType =
state.layout && state.layout.yaxis && state.layout.yaxis.type === 'log' ? 'log' : 'linear';
newYAxis.autorange = true;
newYAxis.type = axisType;
}
// Merge new X & Y axis info into layout
mergedLayout = {
...mergedLayout,
xaxis: newXAxis,
yaxis: newYAxis,
};
this.updateUrlState({ layout: mergedLayout, lastLinearYAxisRange });
};
// Return unique key identifying the curve or bar chart corresponding to the specified
// Plotly plot data element
static getCurveKey(plotDataElem) {
// In bar charts, each legend item consists of a single run ID (all bars for that run are
// associated with & toggled by that legend item)
if (plotDataElem.type === 'bar') {
return plotDataElem.runId;
} else {
// In line charts, each (run, metricKey) tuple has its own legend item, so construct
// a unique legend item identifier by concatenating the run id & metric key
return Utils.getCurveKey(plotDataElem.runId, plotDataElem.metricName);
}
}
/**
* Handle clicking on a single curve within the plot legend in order to toggle its display
* on/off.
*/
handleLegendClick = ({ curveNumber, data }) => {
// If two clicks in short succession, trigger double-click event
const state = this.getUrlState();
const currentTime = Date.now();
if (
currentTime - this.prevLegendClickTime < this.MAX_DOUBLE_CLICK_INTERVAL_MS &&
curveNumber === this.lastClickedLegendCurveId
) {
this.handleLegendDoubleClick({ curveNumber, data });
this.prevLegendClickTime = Math.inf;
} else {
// Otherwise, record time of current click & trigger click event
// Wait full double-click window to trigger setting state, and only if there was no
// double-click do we run the single-click logic (we wait a little extra to be safe)
const curveKey = MetricsPlotPanel.getCurveKey(data[curveNumber]);
this.legendClickTimeout = window.setTimeout(() => {
const existingDeselectedCurves = new Set(state.deselectedCurves);
if (existingDeselectedCurves.has(curveKey)) {
existingDeselectedCurves.delete(curveKey);
} else {
existingDeselectedCurves.add(curveKey);
}
this.updateUrlState({ deselectedCurves: Array.from(existingDeselectedCurves) });
}, this.SINGLE_CLICK_EVENT_DELAY_MS);
this.prevLegendClickTime = currentTime;
}
this.lastClickedLegendCurveId = curveNumber;
// Return false to disable plotly event handler
return false;
};
/**
* Handle double-clicking on a single curve within the plot legend in order to toggle display
* of the selected curve on (and disable display of all other curves).
*/
handleLegendDoubleClick = ({ curveNumber, data }) => {
window.clearTimeout(this.legendClickTimeout);
// Exclude everything besides the current curve key
const curveKey = MetricsPlotPanel.getCurveKey(data[curveNumber]);
const allCurveKeys = data.map((elem) => MetricsPlotPanel.getCurveKey(elem));
const newDeselectedCurves = allCurveKeys.filter((curvePair) => curvePair !== curveKey);
this.updateUrlState({ deselectedCurves: newDeselectedCurves });
return false;
};
handleMetricsSelectChange = (metricValues, metricLabels, { triggerValue }) => {
const requestIds = this.loadMetricHistory(this.props.runUuids, [triggerValue]);
this.setState(
(prevState) => ({
historyRequestIds: [...prevState.historyRequestIds, ...requestIds],
}),
() => {
this.updateUrlState({
selectedMetricKeys: metricValues,
});
},
);
};
handleShowPointChange = (showPoint) => this.updateUrlState({ showPoint });
handleLineSmoothChange = (lineSmoothness) => this.updateUrlState({ lineSmoothness });
handleKeyDownOnPopover = ({ key }) => {
if (key === 'Escape') {
this.setState({ popoverVisible: false });
}
};
updatePopover = (data) => {
this.displayPopover = !this.displayPopover;
// Ignore double click.
setTimeout(() => {
if (this.displayPopover) {
this.displayPopover = false;
const { popoverVisible, popoverX, popoverY } = this.state;
const {
points,
event: { clientX, clientY },
} = data;
const samePointClicked = popoverX === clientX && popoverY === clientY;
const runItems = points
.sort((a, b) => b.y - a.y)
.map((point) => ({
runId: point.data.runId,
name: point.data.name,
color: point.fullData.marker.color,
y: point.y,
}));
this.setState({
popoverVisible: !popoverVisible || !samePointClicked,
popoverX: clientX,
popoverY: clientY,
popoverRunItems: runItems,
});
}
}, 300);
};
checkOnRunUnfinished() {
const { runs } = this.props;
let boolean = false;
runs &&
runs.forEach((run) => {
if (run.getStatus() === 'RUNNING') boolean = true;
});
return boolean;
}
render() {
const { experimentId, runUuids, runDisplayNames, distinctMetricKeys, location } = this.props;
const { popoverVisible, popoverX, popoverY, popoverRunItems } = this.state;
const state = this.getUrlState();
const { showPoint, selectedXAxis, selectedMetricKeys, lineSmoothness } = state;
const yAxisLogScale = this.getAxisType() === 'log';
const { historyRequestIds } = this.state;
const metrics = this.getMetrics();
const chartType = MetricsPlotPanel.predictChartType(metrics);
return (
<div className='metrics-plot-container'>
<MetricsPlotControls
distinctMetricKeys={distinctMetricKeys}
selectedXAxis={selectedXAxis}
selectedMetricKeys={selectedMetricKeys}
handleXAxisChange={this.handleXAxisChange}
handleMetricsSelectChange={this.handleMetricsSelectChange}
handleShowPointChange={this.handleShowPointChange}
handleYAxisLogScaleChange={this.handleYAxisLogScaleChange}
handleLineSmoothChange={this.handleLineSmoothChange}
chartType={chartType}
initialLineSmoothness={lineSmoothness}
yAxisLogScale={yAxisLogScale}
showPoint={showPoint}
/>
<RequestStateWrapper
requestIds={historyRequestIds}
// In this case where there are no history request IDs (e.g. on the
// initial page load / before we try to load additional metrics),
// optimistically render the children
shouldOptimisticallyRender={historyRequestIds.length === 0}
>
<RunLinksPopover
experimentId={experimentId}
visible={popoverVisible}
x={popoverX}
y={popoverY}
runItems={popoverRunItems}
handleKeyDown={this.handleKeyDownOnPopover}
handleClose={() => this.setState({ popoverVisible: false })}
handleVisibleChange={(visible) => this.setState({ popoverVisible: visible })}
/>
<MetricsPlotView
runUuids={runUuids}
runDisplayNames={runDisplayNames}
xAxis={selectedXAxis}
metrics={this.getMetrics()}
metricKeys={selectedMetricKeys}
showPoint={showPoint}
chartType={chartType}
isComparing={MetricsPlotPanel.isComparing(location.search)}
lineSmoothness={lineSmoothness}
extraLayout={state.layout}
deselectedCurves={state.deselectedCurves}
onLayoutChange={this.handleLayoutChange}
onClick={this.updatePopover}
onLegendClick={this.handleLegendClick}
onLegendDoubleClick={this.handleLegendDoubleClick}
/>
</RequestStateWrapper>
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
const { runUuids } = ownProps;
const { latestMetricsByRunUuid, metricsByRunUuid } = state.entities;
// All metric keys from all runUuids, non-distinct
const metricKeys = _.flatMap(runUuids, (runUuid) => {
const latestMetrics = latestMetricsByRunUuid[runUuid];
return latestMetrics ? Object.keys(latestMetrics) : [];
});
const distinctMetricKeys = [...new Set(metricKeys)].sort();
const runDisplayNames = [];
const runs = runUuids.map((runUuid) => getRunInfo(runUuid, state));
// Flat array of all metrics, with history and information of the run it belongs to
// This is used for underlying MetricsPlotView & predicting chartType for MetricsPlotControls
const metricsWithRunInfoAndHistory = _.flatMap(runUuids, (runUuid) => {
const runDisplayName = Utils.getRunDisplayName(getRunTags(runUuid, state), runUuid);
runDisplayNames.push(runDisplayName);
const metricsHistory = metricsByRunUuid[runUuid];
return metricsHistory
? Object.keys(metricsHistory).map((metricKey) => {
const history = metricsHistory[metricKey].map((entry) => ({
key: entry.key,
value: entry.value,
step: Number.parseInt(entry.step, 10) || 0, // default step to 0
timestamp: Number.parseFloat(entry.timestamp),
}));
return { metricKey, history, runUuid, runDisplayName };
})
: [];
});
return {
runDisplayNames,
latestMetricsByRunUuid,
distinctMetricKeys,
metricsWithRunInfoAndHistory,
runs,
state,
};
};
const mapDispatchToProps = { getMetricHistoryApi, getRunApi };
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MetricsPlotPanel));