2021-01-08 01:15:34 +01:00
|
|
|
/**
|
|
|
|
|
* 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 23:22:34 +01:00
|
|
|
import { createGuid } from '../../../utils/utils';
|
2021-02-24 22:39:51 +01:00
|
|
|
import * as trace from '../common/traceEvents';
|
2021-03-09 04:49:57 +01:00
|
|
|
import { ContextResources, ResourceSnapshot } from '../../snapshot/snapshotTypes';
|
|
|
|
|
import { SnapshotStorage } from '../../snapshot/snapshotStorage';
|
2021-02-24 22:39:51 +01:00
|
|
|
export * as trace from '../common/traceEvents';
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-02-24 23:22:34 +01:00
|
|
|
export class TraceModel {
|
|
|
|
|
contextEntries = new Map<string, ContextEntry>();
|
|
|
|
|
pageEntries = new Map<string, { contextEntry: ContextEntry, pageEntry: PageEntry }>();
|
2021-02-25 03:38:04 +01:00
|
|
|
contextResources = new Map<string, ContextResources>();
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-03-09 04:49:57 +01:00
|
|
|
appendEvents(events: trace.TraceEvent[], snapshotStorage: SnapshotStorage) {
|
2021-02-24 23:22:34 +01:00
|
|
|
for (const event of events)
|
|
|
|
|
this.appendEvent(event);
|
2021-03-09 04:49:57 +01:00
|
|
|
const actions: ActionEntry[] = [];
|
|
|
|
|
for (const context of this.contextEntries.values()) {
|
|
|
|
|
for (const page of context.pages)
|
|
|
|
|
actions.push(...page.actions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resources = snapshotStorage.resources().reverse();
|
|
|
|
|
actions.reverse();
|
|
|
|
|
|
|
|
|
|
for (const action of actions) {
|
2021-03-10 20:43:26 +01:00
|
|
|
while (resources.length && resources[0].timestamp > action.timestamp)
|
2021-03-09 04:49:57 +01:00
|
|
|
action.resources.push(resources.shift()!);
|
|
|
|
|
action.resources.reverse();
|
|
|
|
|
}
|
2021-02-24 23:22:34 +01:00
|
|
|
}
|
2021-02-18 02:51:57 +01:00
|
|
|
|
2021-02-24 23:22:34 +01:00
|
|
|
appendEvent(event: trace.TraceEvent) {
|
2021-01-08 01:15:34 +01:00
|
|
|
switch (event.type) {
|
|
|
|
|
case 'context-created': {
|
2021-02-24 23:22:34 +01:00
|
|
|
this.contextEntries.set(event.contextId, {
|
|
|
|
|
name: event.debugName || createGuid(),
|
2021-01-08 01:15:34 +01:00
|
|
|
startTime: Number.MAX_VALUE,
|
|
|
|
|
endTime: Number.MIN_VALUE,
|
|
|
|
|
created: event,
|
|
|
|
|
destroyed: undefined as any,
|
|
|
|
|
pages: [],
|
|
|
|
|
});
|
2021-02-25 03:38:04 +01:00
|
|
|
this.contextResources.set(event.contextId, new Map());
|
2021-01-08 01:15:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'context-destroyed': {
|
2021-02-24 23:22:34 +01:00
|
|
|
this.contextEntries.get(event.contextId)!.destroyed = event;
|
2021-01-08 01:15:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'page-created': {
|
|
|
|
|
const pageEntry: PageEntry = {
|
|
|
|
|
created: event,
|
|
|
|
|
destroyed: undefined as any,
|
|
|
|
|
actions: [],
|
2021-01-16 03:30:55 +01:00
|
|
|
interestingEvents: [],
|
2021-04-07 23:32:12 +02:00
|
|
|
screencastFrames: [],
|
2021-01-08 01:15:34 +01:00
|
|
|
};
|
2021-02-24 23:22:34 +01:00
|
|
|
const contextEntry = this.contextEntries.get(event.contextId)!;
|
|
|
|
|
this.pageEntries.set(event.pageId, { pageEntry, contextEntry });
|
|
|
|
|
contextEntry.pages.push(pageEntry);
|
2021-01-08 01:15:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'page-destroyed': {
|
2021-02-24 23:22:34 +01:00
|
|
|
this.pageEntries.get(event.pageId)!.pageEntry.destroyed = event;
|
2021-01-08 01:15:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-07 23:32:12 +02:00
|
|
|
case 'page-screencast-frame': {
|
|
|
|
|
this.pageEntries.get(event.pageId)!.pageEntry.screencastFrames.push(event);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-08 01:15:34 +01:00
|
|
|
case 'action': {
|
2021-03-10 20:43:26 +01:00
|
|
|
const metadata = event.metadata;
|
|
|
|
|
if (metadata.method === 'waitForEventInfo')
|
2021-02-18 02:51:57 +01:00
|
|
|
break;
|
2021-03-10 20:43:26 +01:00
|
|
|
const { pageEntry } = this.pageEntries.get(metadata.pageId!)!;
|
|
|
|
|
const actionId = event.contextId + '/' + metadata.pageId + '/' + pageEntry.actions.length;
|
2021-01-08 01:15:34 +01:00
|
|
|
const action: ActionEntry = {
|
2021-01-21 17:29:01 +01:00
|
|
|
actionId,
|
2021-03-10 20:43:26 +01:00
|
|
|
resources: [],
|
|
|
|
|
...event,
|
2021-01-08 01:15:34 +01:00
|
|
|
};
|
|
|
|
|
pageEntry.actions.push(action);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-16 03:30:55 +01:00
|
|
|
case 'dialog-opened':
|
|
|
|
|
case 'dialog-closed':
|
|
|
|
|
case 'navigation':
|
|
|
|
|
case 'load': {
|
2021-02-24 23:22:34 +01:00
|
|
|
const { pageEntry } = this.pageEntries.get(event.pageId)!;
|
2021-01-16 03:30:55 +01:00
|
|
|
pageEntry.interestingEvents.push(event);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
2021-02-24 23:22:34 +01:00
|
|
|
const contextEntry = this.contextEntries.get(event.contextId)!;
|
2021-01-16 03:30:55 +01:00
|
|
|
contextEntry.startTime = Math.min(contextEntry.startTime, event.timestamp);
|
|
|
|
|
contextEntry.endTime = Math.max(contextEntry.endTime, event.timestamp);
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
2021-02-24 20:52:59 +01:00
|
|
|
|
2021-02-24 23:22:34 +01:00
|
|
|
actionById(actionId: string): { context: ContextEntry, page: PageEntry, action: ActionEntry } {
|
|
|
|
|
const [contextId, pageId, actionIndex] = actionId.split('/');
|
|
|
|
|
const context = this.contextEntries.get(contextId)!;
|
|
|
|
|
const page = context.pages.find(entry => entry.created.pageId === pageId)!;
|
|
|
|
|
const action = page.actions[+actionIndex];
|
|
|
|
|
return { context, page, action };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findPage(pageId: string): { contextEntry: ContextEntry | undefined, pageEntry: PageEntry | undefined } {
|
|
|
|
|
let contextEntry;
|
|
|
|
|
let pageEntry;
|
|
|
|
|
for (const c of this.contextEntries.values()) {
|
|
|
|
|
for (const p of c.pages) {
|
|
|
|
|
if (p.created.pageId === pageId) {
|
|
|
|
|
contextEntry = c;
|
|
|
|
|
pageEntry = p;
|
2021-02-24 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 23:22:34 +01:00
|
|
|
return { contextEntry, pageEntry };
|
|
|
|
|
}
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-24 23:22:34 +01:00
|
|
|
export type ContextEntry = {
|
|
|
|
|
name: string;
|
|
|
|
|
startTime: number;
|
|
|
|
|
endTime: number;
|
|
|
|
|
created: trace.ContextCreatedTraceEvent;
|
|
|
|
|
destroyed: trace.ContextDestroyedTraceEvent;
|
|
|
|
|
pages: PageEntry[];
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-24 23:22:34 +01:00
|
|
|
export type InterestingPageEvent = trace.DialogOpenedEvent | trace.DialogClosedEvent | trace.NavigationEvent | trace.LoadEvent;
|
|
|
|
|
|
|
|
|
|
export type PageEntry = {
|
|
|
|
|
created: trace.PageCreatedTraceEvent;
|
|
|
|
|
destroyed: trace.PageDestroyedTraceEvent;
|
|
|
|
|
actions: ActionEntry[];
|
|
|
|
|
interestingEvents: InterestingPageEvent[];
|
2021-04-07 23:32:12 +02:00
|
|
|
screencastFrames: { sha1: string, timestamp: number }[]
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
2021-02-24 23:22:34 +01:00
|
|
|
|
2021-03-10 20:43:26 +01:00
|
|
|
export type ActionEntry = trace.ActionTraceEvent & {
|
2021-02-24 23:22:34 +01:00
|
|
|
actionId: string;
|
2021-03-09 04:49:57 +01:00
|
|
|
resources: ResourceSnapshot[]
|
2021-02-24 23:22:34 +01:00
|
|
|
};
|