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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-24 13:39:51 -08:00
|
|
|
import { ContextEntry, InterestingPageEvent, ActionEntry, trace } from '../../../server/trace/viewer/traceModel';
|
2021-01-07 16:15:34 -08:00
|
|
|
import './timeline.css';
|
|
|
|
|
import { Boundaries } from '../geometry';
|
|
|
|
|
import * as React from 'react';
|
2021-02-17 22:10:13 -08:00
|
|
|
import { useMeasure } from './helpers';
|
|
|
|
|
import { msToString } from '../../uiUtils';
|
2021-04-08 05:32:12 +08:00
|
|
|
import { FilmStrip } from './filmStrip';
|
2021-01-15 18:30:55 -08:00
|
|
|
|
|
|
|
|
type TimelineBar = {
|
|
|
|
|
entry?: ActionEntry;
|
|
|
|
|
event?: InterestingPageEvent;
|
|
|
|
|
leftPosition: number;
|
|
|
|
|
rightPosition: number;
|
|
|
|
|
leftTime: number;
|
|
|
|
|
rightTime: number;
|
|
|
|
|
type: string;
|
|
|
|
|
label: string;
|
|
|
|
|
priority: number;
|
|
|
|
|
};
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
|
export const Timeline: React.FunctionComponent<{
|
|
|
|
|
context: ContextEntry,
|
|
|
|
|
boundaries: Boundaries,
|
2021-01-14 20:16:02 -08:00
|
|
|
selectedAction: ActionEntry | undefined,
|
|
|
|
|
highlightedAction: ActionEntry | undefined,
|
|
|
|
|
onSelected: (action: ActionEntry) => void,
|
2021-04-19 19:50:11 -07:00
|
|
|
onHighlighted: (action: ActionEntry | undefined) => void,
|
|
|
|
|
}> = ({ context, boundaries, selectedAction, highlightedAction, onSelected, onHighlighted }) => {
|
2021-01-07 16:15:34 -08:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
|
|
|
|
const [previewX, setPreviewX] = React.useState<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[] = [];
|
|
|
|
|
for (const page of context.pages) {
|
|
|
|
|
for (const entry of page.actions) {
|
2021-04-08 22:59:05 +08:00
|
|
|
if (!entry.metadata.params)
|
|
|
|
|
console.log(entry);
|
2021-03-10 11:43:26 -08:00
|
|
|
let detail = entry.metadata.params.selector || '';
|
|
|
|
|
if (entry.metadata.method === 'goto')
|
|
|
|
|
detail = entry.metadata.params.url || '';
|
2021-01-15 18:30:55 -08:00
|
|
|
bars.push({
|
|
|
|
|
entry,
|
2021-03-10 11:43:26 -08:00
|
|
|
leftTime: entry.metadata.startTime,
|
|
|
|
|
rightTime: entry.metadata.endTime,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, entry.metadata.startTime),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, entry.metadata.endTime),
|
2021-04-18 17:02:34 -10:00
|
|
|
label: entry.metadata.apiName + ' ' + detail,
|
2021-03-10 11:43:26 -08:00
|
|
|
type: entry.metadata.method,
|
2021-01-15 18:30:55 -08:00
|
|
|
priority: 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
let lastDialogOpened: trace.DialogOpenedEvent | undefined;
|
|
|
|
|
for (const event of page.interestingEvents) {
|
|
|
|
|
if (event.type === 'dialog-opened') {
|
|
|
|
|
lastDialogOpened = event;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (event.type === 'dialog-closed' && lastDialogOpened) {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: lastDialogOpened.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, lastDialogOpened.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: lastDialogOpened.message ? `${event.dialogType} "${lastDialogOpened.message}"` : event.dialogType,
|
|
|
|
|
type: 'dialog',
|
|
|
|
|
priority: -1,
|
|
|
|
|
});
|
|
|
|
|
} else if (event.type === 'navigation') {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: event.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: `navigated to ${event.url}`,
|
|
|
|
|
type: event.type,
|
|
|
|
|
priority: 1,
|
|
|
|
|
});
|
|
|
|
|
} else if (event.type === 'load') {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: event.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: `load`,
|
|
|
|
|
type: event.type,
|
|
|
|
|
priority: 1,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bars.sort((a, b) => a.priority - b.priority);
|
|
|
|
|
return bars;
|
|
|
|
|
}, [context, boundaries, measure.width]);
|
|
|
|
|
|
2021-02-17 17:51:57 -08:00
|
|
|
const hoveredBar = hoveredBarIndex !== undefined ? bars[hoveredBarIndex] : undefined;
|
|
|
|
|
let targetBar: TimelineBar | undefined = bars.find(bar => bar.entry === (highlightedAction || selectedAction));
|
|
|
|
|
targetBar = hoveredBar || targetBar;
|
|
|
|
|
|
|
|
|
|
const findHoveredBarIndex = (x: 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-01-14 20:16:02 -08:00
|
|
|
let distance: number | undefined;
|
2021-02-17 17:51:57 -08:00
|
|
|
for (let i = 0; i < bars.length; i++) {
|
|
|
|
|
const bar = bars[i];
|
|
|
|
|
const left = Math.max(bar.leftTime, time1);
|
|
|
|
|
const right = Math.min(bar.rightTime, time2);
|
|
|
|
|
const middle = (bar.leftTime + bar.rightTime) / 2;
|
2021-01-14 20:16:02 -08:00
|
|
|
const d = Math.abs(time - middle);
|
2021-02-17 17:51:57 -08:00
|
|
|
if (left <= right && (index === undefined || d < distance!)) {
|
|
|
|
|
index = i;
|
2021-01-14 20:16:02 -08:00
|
|
|
distance = d;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-02-17 17:51:57 -08:00
|
|
|
if (!ref.current)
|
|
|
|
|
return;
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
2021-04-19 19:50:11 -07:00
|
|
|
const index = findHoveredBarIndex(x);
|
2021-02-17 17:51:57 -08:00
|
|
|
setPreviewX(x);
|
2021-04-19 19:50:11 -07:00
|
|
|
setHoveredBarIndex(index);
|
|
|
|
|
if (typeof index === 'number')
|
|
|
|
|
onHighlighted(bars[index].entry);
|
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 = () => {
|
|
|
|
|
setPreviewX(undefined);
|
2021-04-19 19:50:11 -07:00
|
|
|
setHoveredBarIndex(undefined);
|
|
|
|
|
onHighlighted(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-04-19 19:50:11 -07:00
|
|
|
setPreviewX(undefined);
|
2021-02-17 17:51:57 -08:00
|
|
|
if (!ref.current)
|
|
|
|
|
return;
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
|
|
|
|
const index = findHoveredBarIndex(x);
|
|
|
|
|
if (index === undefined)
|
|
|
|
|
return;
|
|
|
|
|
const entry = bars[index].entry;
|
|
|
|
|
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}
|
|
|
|
|
className={'timeline-label ' + bar.type + (targetBar === bar ? ' selected' : '')}
|
2021-01-14 20:16:02 -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-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-02-17 17:51:57 -08:00
|
|
|
<div className='timeline-lane timeline-bars'>{
|
2021-01-15 18:30:55 -08:00
|
|
|
bars.map((bar, index) => {
|
|
|
|
|
return <div key={index}
|
|
|
|
|
className={'timeline-bar ' + bar.type + (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-01-07 16:15:34 -08:00
|
|
|
}}
|
|
|
|
|
></div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-04-08 05:32:12 +08:00
|
|
|
<FilmStrip context={context} boundaries={boundaries} previewX={previewX} />
|
2021-01-15 18:30:55 -08:00
|
|
|
<div className='timeline-marker timeline-marker-hover' style={{
|
2021-01-07 16:15:34 -08:00
|
|
|
display: (previewX !== undefined) ? 'block' : 'none',
|
|
|
|
|
left: (previewX || 0) + 'px',
|
|
|
|
|
}}></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
|
|
|
}
|