2021-01-07 16:15:34 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-20 18:41:51 -07:00
|
|
|
import type { ActionTraceEvent } from '@trace/trace';
|
2022-03-25 13:12:00 -08:00
|
|
|
import { msToString } from '@web/uiUtils';
|
2021-01-07 16:15:34 -08:00
|
|
|
import * as React from 'react';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Boundaries } from '../geometry';
|
2021-04-08 05:32:12 +08:00
|
|
|
import { FilmStrip } from './filmStrip';
|
2022-02-07 17:05:42 -08:00
|
|
|
import { useMeasure } from './helpers';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { MultiTraceModel } from './modelUtil';
|
2022-02-07 17:05:42 -08:00
|
|
|
import './timeline.css';
|
2021-01-15 18:30:55 -08:00
|
|
|
|
|
|
|
|
type TimelineBar = {
|
2021-05-13 20:41:32 -07:00
|
|
|
action?: ActionTraceEvent;
|
|
|
|
|
event?: ActionTraceEvent;
|
2021-01-15 18:30:55 -08:00
|
|
|
leftPosition: number;
|
|
|
|
|
rightPosition: number;
|
|
|
|
|
leftTime: number;
|
|
|
|
|
rightTime: number;
|
|
|
|
|
type: string;
|
|
|
|
|
label: string;
|
2023-01-10 18:33:20 +01:00
|
|
|
title: string;
|
2021-05-13 20:41:32 -07:00
|
|
|
className: string;
|
2021-01-15 18:30:55 -08:00
|
|
|
};
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
|
export const Timeline: React.FunctionComponent<{
|
2022-02-08 12:27:29 -08:00
|
|
|
context: MultiTraceModel,
|
2021-01-07 16:15:34 -08:00
|
|
|
boundaries: Boundaries,
|
2021-05-13 20:41:32 -07:00
|
|
|
selectedAction: ActionTraceEvent | undefined,
|
|
|
|
|
onSelected: (action: ActionTraceEvent) => void,
|
2023-02-16 07:59:21 -08:00
|
|
|
}> = ({ context, boundaries, selectedAction, onSelected }) => {
|
2021-01-07 16:15:34 -08:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
2021-06-03 21:52:29 -07:00
|
|
|
const barsRef = React.useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
2021-05-12 20:54:17 -07:00
|
|
|
const [previewPoint, setPreviewPoint] = React.useState<{ x: number, clientY: number } | undefined>();
|
2021-02-17 17:51:57 -08:00
|
|
|
const [hoveredBarIndex, setHoveredBarIndex] = React.useState<number | undefined>();
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
|
const offsets = React.useMemo(() => {
|
|
|
|
|
return calculateDividerOffsets(measure.width, boundaries);
|
|
|
|
|
}, [measure.width, boundaries]);
|
2021-01-15 18:30:55 -08:00
|
|
|
|
|
|
|
|
const bars = React.useMemo(() => {
|
|
|
|
|
const bars: TimelineBar[] = [];
|
2021-10-15 14:22:49 -08:00
|
|
|
for (const entry of context.actions) {
|
|
|
|
|
let detail = trimRight(entry.metadata.params.selector || '', 50);
|
|
|
|
|
if (entry.metadata.method === 'goto')
|
|
|
|
|
detail = trimRight(entry.metadata.params.url || '', 50);
|
|
|
|
|
bars.push({
|
|
|
|
|
action: entry,
|
|
|
|
|
leftTime: entry.metadata.startTime,
|
|
|
|
|
rightTime: entry.metadata.endTime,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, entry.metadata.startTime),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, entry.metadata.endTime),
|
|
|
|
|
label: entry.metadata.apiName + ' ' + detail,
|
2023-01-10 18:33:20 +01:00
|
|
|
title: entry.metadata.endTime ? msToString(entry.metadata.endTime - entry.metadata.startTime) : 'Timed Out',
|
2021-10-15 14:22:49 -08:00
|
|
|
type: entry.metadata.type + '.' + entry.metadata.method,
|
|
|
|
|
className: `${entry.metadata.type}_${entry.metadata.method}`.toLowerCase()
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-05-13 20:41:32 -07:00
|
|
|
|
2021-10-15 14:22:49 -08:00
|
|
|
for (const event of context.events) {
|
|
|
|
|
const startTime = event.metadata.startTime;
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: startTime,
|
|
|
|
|
rightTime: startTime,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, startTime),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, startTime),
|
|
|
|
|
label: event.metadata.method,
|
2023-01-10 18:33:20 +01:00
|
|
|
title: event.metadata.endTime ? msToString(event.metadata.endTime - event.metadata.startTime) : 'Timed Out',
|
2021-10-15 14:22:49 -08:00
|
|
|
type: event.metadata.type + '.' + event.metadata.method,
|
|
|
|
|
className: `${event.metadata.type}_${event.metadata.method}`.toLowerCase()
|
|
|
|
|
});
|
2021-01-15 18:30:55 -08:00
|
|
|
}
|
|
|
|
|
return bars;
|
|
|
|
|
}, [context, boundaries, measure.width]);
|
|
|
|
|
|
2021-02-17 17:51:57 -08:00
|
|
|
const hoveredBar = hoveredBarIndex !== undefined ? bars[hoveredBarIndex] : undefined;
|
2023-02-16 07:59:21 -08:00
|
|
|
let targetBar: TimelineBar | undefined = bars.find(bar => bar.action === selectedAction);
|
2021-02-17 17:51:57 -08:00
|
|
|
targetBar = hoveredBar || targetBar;
|
|
|
|
|
|
2021-06-03 21:52:29 -07:00
|
|
|
const findHoveredBarIndex = (x: number, y: number) => {
|
2021-01-14 20:16:02 -08:00
|
|
|
const time = positionToTime(measure.width, boundaries, x);
|
|
|
|
|
const time1 = positionToTime(measure.width, boundaries, x - 5);
|
|
|
|
|
const time2 = positionToTime(measure.width, boundaries, x + 5);
|
2021-02-17 17:51:57 -08:00
|
|
|
let index: number | undefined;
|
2021-06-03 21:52:29 -07:00
|
|
|
let yDistance: number | undefined;
|
|
|
|
|
let xDistance: number | undefined;
|
2021-02-17 17:51:57 -08:00
|
|
|
for (let i = 0; i < bars.length; i++) {
|
|
|
|
|
const bar = bars[i];
|
2021-06-03 21:52:29 -07:00
|
|
|
const yMiddle = kBarHeight / 2 + barTop(bar);
|
2021-02-17 17:51:57 -08:00
|
|
|
const left = Math.max(bar.leftTime, time1);
|
|
|
|
|
const right = Math.min(bar.rightTime, time2);
|
2021-06-03 21:52:29 -07:00
|
|
|
const xMiddle = (bar.leftTime + bar.rightTime) / 2;
|
|
|
|
|
const xd = Math.abs(time - xMiddle);
|
|
|
|
|
const yd = Math.abs(y - yMiddle);
|
|
|
|
|
if (left > right)
|
|
|
|
|
continue;
|
|
|
|
|
// Prefer closest yDistance (the same bar), among those prefer the closest xDistance.
|
|
|
|
|
if (index === undefined ||
|
|
|
|
|
(yd < yDistance!) ||
|
|
|
|
|
(Math.abs(yd - yDistance!) < 1e-2 && xd < xDistance!)) {
|
2021-02-17 17:51:57 -08:00
|
|
|
index = i;
|
2021-06-03 21:52:29 -07:00
|
|
|
xDistance = xd;
|
|
|
|
|
yDistance = yd;
|
2021-01-14 20:16:02 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-17 17:51:57 -08:00
|
|
|
return index;
|
2021-01-14 20:16:02 -08:00
|
|
|
};
|
|
|
|
|
|
2021-01-07 16:15:34 -08:00
|
|
|
const onMouseMove = (event: React.MouseEvent) => {
|
2021-06-03 21:52:29 -07:00
|
|
|
if (!ref.current || !barsRef.current)
|
2021-02-17 17:51:57 -08:00
|
|
|
return;
|
2021-06-03 21:52:29 -07:00
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
|
|
|
|
const y = event.clientY - barsRef.current.getBoundingClientRect().top;
|
|
|
|
|
const index = findHoveredBarIndex(x, y);
|
2021-05-12 20:54:17 -07:00
|
|
|
setPreviewPoint({ x, clientY: event.clientY });
|
2021-04-19 19:50:11 -07:00
|
|
|
setHoveredBarIndex(index);
|
2021-01-07 16:15:34 -08:00
|
|
|
};
|
2021-04-19 19:50:11 -07:00
|
|
|
|
2021-01-07 16:15:34 -08:00
|
|
|
const onMouseLeave = () => {
|
2021-05-12 20:54:17 -07:00
|
|
|
setPreviewPoint(undefined);
|
2021-04-19 19:50:11 -07:00
|
|
|
setHoveredBarIndex(undefined);
|
2021-01-07 16:15:34 -08:00
|
|
|
};
|
2021-04-19 19:50:11 -07:00
|
|
|
|
2021-02-17 17:51:57 -08:00
|
|
|
const onClick = (event: React.MouseEvent) => {
|
2021-05-12 20:54:17 -07:00
|
|
|
setPreviewPoint(undefined);
|
2021-06-03 21:52:29 -07:00
|
|
|
if (!ref.current || !barsRef.current)
|
2021-02-17 17:51:57 -08:00
|
|
|
return;
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
2021-06-03 21:52:29 -07:00
|
|
|
const y = event.clientY - barsRef.current.getBoundingClientRect().top;
|
|
|
|
|
const index = findHoveredBarIndex(x, y);
|
2021-02-17 17:51:57 -08:00
|
|
|
if (index === undefined)
|
|
|
|
|
return;
|
2021-05-13 20:41:32 -07:00
|
|
|
const entry = bars[index].action;
|
2021-02-17 17:51:57 -08:00
|
|
|
if (entry)
|
|
|
|
|
onSelected(entry);
|
2021-01-14 20:16:02 -08:00
|
|
|
};
|
2021-01-07 16:15:34 -08:00
|
|
|
|
2021-02-17 17:51:57 -08:00
|
|
|
return <div ref={ref} className='timeline-view' onMouseMove={onMouseMove} onMouseOver={onMouseMove} onMouseLeave={onMouseLeave} onClick={onClick}>
|
2021-01-07 16:15:34 -08:00
|
|
|
<div className='timeline-grid'>{
|
|
|
|
|
offsets.map((offset, index) => {
|
2021-01-15 18:30:55 -08:00
|
|
|
return <div key={index} className='timeline-divider' style={{ left: offset.position + 'px' }}>
|
|
|
|
|
<div className='timeline-time'>{msToString(offset.time - boundaries.minimum)}</div>
|
2021-01-07 16:15:34 -08:00
|
|
|
</div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-01-15 18:30:55 -08:00
|
|
|
<div className='timeline-lane timeline-labels'>{
|
|
|
|
|
bars.map((bar, index) => {
|
|
|
|
|
return <div key={index}
|
2021-05-13 20:41:32 -07:00
|
|
|
className={'timeline-label ' + bar.className + (targetBar === bar ? ' selected' : '')}
|
2021-01-14 20:16:02 -08:00
|
|
|
style={{
|
2021-06-30 17:56:48 -07:00
|
|
|
left: bar.leftPosition,
|
|
|
|
|
maxWidth: 100,
|
2021-01-14 20:16:02 -08:00
|
|
|
}}
|
2021-01-07 16:15:34 -08:00
|
|
|
>
|
2021-01-15 18:30:55 -08:00
|
|
|
{bar.label}
|
2021-01-07 16:15:34 -08:00
|
|
|
</div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-06-03 21:52:29 -07:00
|
|
|
<div className='timeline-lane timeline-bars' ref={barsRef}>{
|
2021-01-15 18:30:55 -08:00
|
|
|
bars.map((bar, index) => {
|
|
|
|
|
return <div key={index}
|
2021-05-13 20:41:32 -07:00
|
|
|
className={'timeline-bar ' + (bar.action ? 'action ' : '') + (bar.event ? 'event ' : '') + bar.className + (targetBar === bar ? ' selected' : '')}
|
2021-01-07 16:15:34 -08:00
|
|
|
style={{
|
2021-01-15 18:30:55 -08:00
|
|
|
left: bar.leftPosition + 'px',
|
|
|
|
|
width: Math.max(1, bar.rightPosition - bar.leftPosition) + 'px',
|
2021-06-03 21:52:29 -07:00
|
|
|
top: barTop(bar) + 'px',
|
2021-01-07 16:15:34 -08:00
|
|
|
}}
|
2023-01-10 18:33:20 +01:00
|
|
|
title={bar.title}
|
2021-01-07 16:15:34 -08:00
|
|
|
></div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-05-12 20:54:17 -07:00
|
|
|
<FilmStrip context={context} boundaries={boundaries} previewPoint={previewPoint} />
|
2021-01-15 18:30:55 -08:00
|
|
|
<div className='timeline-marker timeline-marker-hover' style={{
|
2021-05-12 20:54:17 -07:00
|
|
|
display: (previewPoint !== undefined) ? 'block' : 'none',
|
|
|
|
|
left: (previewPoint?.x || 0) + 'px',
|
2021-01-07 16:15:34 -08:00
|
|
|
}}></div>
|
|
|
|
|
</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-15 18:30:55 -08:00
|
|
|
function calculateDividerOffsets(clientWidth: number, boundaries: Boundaries): { position: number, time: number }[] {
|
2021-01-07 16:15:34 -08:00
|
|
|
const minimumGap = 64;
|
|
|
|
|
let dividerCount = clientWidth / minimumGap;
|
|
|
|
|
const boundarySpan = boundaries.maximum - boundaries.minimum;
|
|
|
|
|
const pixelsPerMillisecond = clientWidth / boundarySpan;
|
|
|
|
|
let sectionTime = boundarySpan / dividerCount;
|
|
|
|
|
|
|
|
|
|
const logSectionTime = Math.ceil(Math.log(sectionTime) / Math.LN10);
|
|
|
|
|
sectionTime = Math.pow(10, logSectionTime);
|
|
|
|
|
if (sectionTime * pixelsPerMillisecond >= 5 * minimumGap)
|
|
|
|
|
sectionTime = sectionTime / 5;
|
|
|
|
|
if (sectionTime * pixelsPerMillisecond >= 2 * minimumGap)
|
|
|
|
|
sectionTime = sectionTime / 2;
|
|
|
|
|
|
|
|
|
|
const firstDividerTime = boundaries.minimum;
|
|
|
|
|
let lastDividerTime = boundaries.maximum;
|
|
|
|
|
lastDividerTime += minimumGap / pixelsPerMillisecond;
|
|
|
|
|
dividerCount = Math.ceil((lastDividerTime - firstDividerTime) / sectionTime);
|
|
|
|
|
|
|
|
|
|
if (!sectionTime)
|
|
|
|
|
dividerCount = 0;
|
|
|
|
|
|
|
|
|
|
const offsets = [];
|
|
|
|
|
for (let i = 0; i < dividerCount; ++i) {
|
|
|
|
|
const time = firstDividerTime + sectionTime * i;
|
2021-01-15 18:30:55 -08:00
|
|
|
offsets.push({ position: timeToPosition(clientWidth, boundaries, time), time });
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
return offsets;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 18:30:55 -08:00
|
|
|
function timeToPosition(clientWidth: number, boundaries: Boundaries, time: number): number {
|
|
|
|
|
return (time - boundaries.minimum) / (boundaries.maximum - boundaries.minimum) * clientWidth;
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 20:16:02 -08:00
|
|
|
function positionToTime(clientWidth: number, boundaries: Boundaries, x: number): number {
|
2021-01-15 18:30:55 -08:00
|
|
|
return x / clientWidth * (boundaries.maximum - boundaries.minimum) + boundaries.minimum;
|
2021-01-14 20:16:02 -08:00
|
|
|
}
|
2021-06-03 21:52:29 -07:00
|
|
|
|
|
|
|
|
function trimRight(s: string, maxLength: number): string {
|
|
|
|
|
return s.length <= maxLength ? s : s.substring(0, maxLength - 1) + '\u2026';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const kBarHeight = 11;
|
|
|
|
|
function barTop(bar: TimelineBar): number {
|
|
|
|
|
return bar.event ? 22 : (bar.action?.metadata.method === 'waitForEventInfo' ? 0 : 11);
|
|
|
|
|
}
|