2021-12-10 02:37:01 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
import {Component, OnDestroy, OnInit} from '@angular/core';
|
2023-08-30 22:41:16 +00:00
|
|
|
import {MatDialog} from '@angular/material/dialog';
|
2021-12-09 05:44:17 +00:00
|
|
|
import {Events, MessageBus, ProfilerFrame} from 'protocol';
|
|
|
|
|
import {Subject, Subscription} from 'rxjs';
|
2020-01-27 18:40:18 +00:00
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
import {FileApiService} from './file-api-service';
|
|
|
|
|
import {ProfilerImportDialogComponent} from './profiler-import-dialog.component';
|
|
|
|
|
|
2024-01-16 21:35:47 +00:00
|
|
|
type State = 'idle' | 'recording' | 'visualizing';
|
2020-01-27 18:40:18 +00:00
|
|
|
|
2020-03-02 20:30:55 +00:00
|
|
|
const SUPPORTED_VERSIONS = [1];
|
|
|
|
|
const PROFILER_VERSION = 1;
|
|
|
|
|
|
2020-01-27 18:40:18 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'ng-profiler',
|
2020-02-25 12:44:22 +00:00
|
|
|
templateUrl: './profiler.component.html',
|
2020-03-29 03:28:36 +00:00
|
|
|
styleUrls: ['./profiler.component.scss'],
|
2020-01-27 18:40:18 +00:00
|
|
|
})
|
2023-12-04 19:44:53 +00:00
|
|
|
export class ProfilerComponent implements OnInit {
|
2020-01-27 18:40:18 +00:00
|
|
|
state: State = 'idle';
|
2020-05-04 23:40:55 +00:00
|
|
|
stream = new Subject<ProfilerFrame[]>();
|
2020-01-27 18:40:18 +00:00
|
|
|
|
2020-05-04 23:40:55 +00:00
|
|
|
// We collect this buffer so we can have it available for export.
|
|
|
|
|
private _buffer: ProfilerFrame[] = [];
|
2020-01-27 18:40:18 +00:00
|
|
|
|
2020-03-18 00:48:29 +00:00
|
|
|
constructor(
|
2024-01-16 21:35:47 +00:00
|
|
|
private _fileApiService: FileApiService,
|
|
|
|
|
private _messageBus: MessageBus<Events>,
|
|
|
|
|
public dialog: MatDialog,
|
2023-12-04 19:44:53 +00:00
|
|
|
) {
|
|
|
|
|
this._fileApiService.uploadedData.subscribe((importedFile) => {
|
2020-02-27 22:07:22 +00:00
|
|
|
if (importedFile.error) {
|
|
|
|
|
console.error('Could not process uploaded file');
|
|
|
|
|
console.error(importedFile.error);
|
2020-03-02 20:30:55 +00:00
|
|
|
this.dialog.open(ProfilerImportDialogComponent, {
|
|
|
|
|
width: '600px',
|
2021-12-09 05:44:17 +00:00
|
|
|
data: {status: 'ERROR', errorMessage: importedFile.error},
|
2020-03-02 20:30:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!SUPPORTED_VERSIONS.includes(importedFile.version)) {
|
|
|
|
|
const processDataDialog = this.dialog.open(ProfilerImportDialogComponent, {
|
|
|
|
|
width: '600px',
|
2021-12-09 05:44:17 +00:00
|
|
|
data: {
|
|
|
|
|
importedVersion: importedFile.version,
|
|
|
|
|
profilerVersion: PROFILER_VERSION,
|
2023-12-04 19:44:53 +00:00
|
|
|
status: 'INVALID_VERSION',
|
2021-12-09 05:44:17 +00:00
|
|
|
},
|
2020-03-02 20:30:55 +00:00
|
|
|
});
|
|
|
|
|
|
2020-03-28 19:15:54 +00:00
|
|
|
processDataDialog.afterClosed().subscribe((result) => {
|
2020-03-02 20:30:55 +00:00
|
|
|
if (result) {
|
2020-05-04 23:40:55 +00:00
|
|
|
this.state = 'visualizing';
|
|
|
|
|
this._buffer = importedFile.buffer;
|
|
|
|
|
setTimeout(() => this.stream.next(importedFile.buffer));
|
2020-03-02 20:30:55 +00:00
|
|
|
}
|
|
|
|
|
});
|
2020-02-27 22:07:22 +00:00
|
|
|
} else {
|
2020-05-04 23:40:55 +00:00
|
|
|
this.state = 'visualizing';
|
|
|
|
|
this._buffer = importedFile.buffer;
|
|
|
|
|
setTimeout(() => this.stream.next(importedFile.buffer));
|
2020-02-27 22:07:22 +00:00
|
|
|
}
|
2020-02-18 21:13:57 +00:00
|
|
|
});
|
2020-02-14 20:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:44:53 +00:00
|
|
|
startRecording(): void {
|
|
|
|
|
this.state = 'recording';
|
|
|
|
|
this._messageBus.emit('startProfiling');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopRecording(): void {
|
|
|
|
|
this.state = 'visualizing';
|
|
|
|
|
this._messageBus.emit('stopProfiling');
|
|
|
|
|
this.stream.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
this._messageBus.on('profilerResults', (remainingRecords) => {
|
|
|
|
|
if (remainingRecords.duration > 0 && remainingRecords.source) {
|
|
|
|
|
this.stream.next([remainingRecords]);
|
|
|
|
|
this._buffer.push(remainingRecords);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this._messageBus.on('sendProfilerChunk', (chunkOfRecords: ProfilerFrame) => {
|
|
|
|
|
this.stream.next([chunkOfRecords]);
|
|
|
|
|
this._buffer.push(chunkOfRecords);
|
|
|
|
|
});
|
2020-02-18 21:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportProfilerResults(): void {
|
2020-02-27 22:07:22 +00:00
|
|
|
const fileToExport = {
|
2020-03-02 20:30:55 +00:00
|
|
|
version: PROFILER_VERSION,
|
2020-05-04 23:40:55 +00:00
|
|
|
buffer: this._buffer,
|
2020-02-27 22:07:22 +00:00
|
|
|
};
|
|
|
|
|
this._fileApiService.saveObjectAsJSON(fileToExport);
|
2020-02-18 21:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-27 22:07:22 +00:00
|
|
|
importProfilerResults(event: InputEvent): void {
|
2020-02-18 21:13:57 +00:00
|
|
|
this._fileApiService.publishFileUpload(event);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 20:42:20 +00:00
|
|
|
discardRecording(): void {
|
2020-05-04 23:40:55 +00:00
|
|
|
this.stream = new Subject<ProfilerFrame[]>();
|
2020-01-27 18:40:18 +00:00
|
|
|
this.state = 'idle';
|
2020-05-04 23:40:55 +00:00
|
|
|
this._buffer = [];
|
2020-01-27 18:40:18 +00:00
|
|
|
}
|
|
|
|
|
}
|