@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 12.5 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as Bindings from"../../models/bindings/bindings.js";import*as DataGrid from"../../ui/legacy/components/data_grid/data_grid.js";import*as PerfUI from"../../ui/legacy/components/perf_ui/perf_ui.js";import*as Components from"../../ui/legacy/components/utils/utils.js";import*as UI from"../../ui/legacy/legacy.js";import{BottomUpProfileDataGridTree}from"./BottomUpProfileDataGrid.js";import{CPUProfileFlameChart}from"./CPUProfileFlameChart.js";import{ProfileDataGridTree}from"./ProfileDataGrid.js";import{Events,ProfileHeader}from"./ProfileHeader.js";import{ProfileSidebarTreeElement}from"./ProfileSidebarTreeElement.js";import{TopDownProfileDataGridTree}from"./TopDownProfileDataGrid.js";const UIStrings={profile:"Profile",findByCostMsNameOrFile:"Find by cost (>50ms), name or file",function:"Function",profiler:"Profiler",profileViewMode:"Profile view mode",focusSelectedFunction:"Focus selected function",excludeSelectedFunction:"Exclude selected function",restoreAllFunctions:"Restore all functions",chart:"Chart",heavyBottomUp:"Heavy (Bottom Up)",treeTopDown:"Tree (Top Down)",profileD:"Profile {PH1}",loadingD:"Loading… {PH1}%",fileSReadErrorS:"File ''{PH1}'' read error: {PH2}",loading:"Loading…",failedToReadFile:"Failed to read file",parsing:"Parsing…",loaded:"Loaded"},str_=i18n.i18n.registerUIStrings("panels/profiler/ProfileView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ProfileView extends UI.View.SimpleView{profileInternal;searchableViewInternal;dataGrid;viewSelectComboBox;focusButton;excludeButton;resetButton;linkifierInternal;nodeFormatter;viewType;adjustedTotal;profileHeader;bottomUpProfileDataGridTree;topDownProfileDataGridTree;currentSearchResultIndex;dataProvider;flameChart;visibleView;searchableElement;profileDataGridTree;constructor(){super(i18nString(UIStrings.profile)),this.profileInternal=null,this.searchableViewInternal=new UI.SearchableView.SearchableView(this,null),this.searchableViewInternal.setPlaceholder(i18nString(UIStrings.findByCostMsNameOrFile)),this.searchableViewInternal.show(this.element);const e=[];e.push({id:"self",title:this.columnHeader("self"),width:"120px",fixedWidth:!0,sortable:!0,sort:DataGrid.DataGrid.Order.Descending,titleDOMFragment:void 0,align:void 0,editable:void 0,nonSelectable:void 0,longText:void 0,disclosure:void 0,weight:void 0,allowInSortByEvenWhenHidden:void 0,dataType:void 0,defaultWeight:void 0}),e.push({id:"total",title:this.columnHeader("total"),width:"120px",fixedWidth:!0,sortable:!0,sort:void 0,titleDOMFragment:void 0,align:void 0,editable:void 0,nonSelectable:void 0,longText:void 0,disclosure:void 0,weight:void 0,allowInSortByEvenWhenHidden:void 0,dataType:void 0,defaultWeight:void 0}),e.push({id:"function",title:i18nString(UIStrings.function),disclosure:!0,sortable:!0,sort:void 0,titleDOMFragment:void 0,align:void 0,editable:void 0,nonSelectable:void 0,longText:void 0,weight:void 0,allowInSortByEvenWhenHidden:void 0,dataType:void 0,defaultWeight:void 0,width:void 0,fixedWidth:void 0}),this.dataGrid=new DataGrid.DataGrid.DataGridImpl({displayName:i18nString(UIStrings.profiler),columns:e,editCallback:void 0,deleteCallback:void 0,refreshCallback:void 0}),this.dataGrid.addEventListener(DataGrid.DataGrid.Events.SortingChanged,this.sortProfile,this),this.dataGrid.addEventListener(DataGrid.DataGrid.Events.SelectedNode,this.nodeSelected.bind(this,!0)),this.dataGrid.addEventListener(DataGrid.DataGrid.Events.DeselectedNode,this.nodeSelected.bind(this,!1)),this.dataGrid.setRowContextMenuCallback(this.populateContextMenu.bind(this)),this.viewSelectComboBox=new UI.Toolbar.ToolbarComboBox(this.changeView.bind(this),i18nString(UIStrings.profileViewMode)),this.focusButton=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.focusSelectedFunction),"eye"),this.focusButton.setEnabled(!1),this.focusButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.focusClicked,this),this.excludeButton=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.excludeSelectedFunction),"cross"),this.excludeButton.setEnabled(!1),this.excludeButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.excludeClicked,this),this.resetButton=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.restoreAllFunctions),"refresh"),this.resetButton.setEnabled(!1),this.resetButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.resetClicked,this),this.linkifierInternal=new Components.Linkifier.Linkifier(maxLinkLength)}static buildPopoverTable(e){const t=document.createElement("table");for(const i of e){const e=t.createChild("tr");e.createChild("td").textContent=i.title,e.createChild("td").textContent=i.value}return t}setProfile(e){this.profileInternal=e,this.bottomUpProfileDataGridTree=null,this.topDownProfileDataGridTree=null,this.changeView(),this.refresh()}profile(){return this.profileInternal}initialize(e){this.nodeFormatter=e,this.viewType=Common.Settings.Settings.instance().createSetting("profileView","Heavy");const t=["Flame","Heavy","Tree"],i=new Map([["Flame",i18nString(UIStrings.chart)],["Heavy",i18nString(UIStrings.heavyBottomUp)],["Tree",i18nString(UIStrings.treeTopDown)]]),r=new Map(t.map((e=>[e,this.viewSelectComboBox.createOption(i.get(e),e)]))),o=this.viewType.get()||t[0],s=r.get(o)||r.get(t[0]);this.viewSelectComboBox.select(s),this.changeView(),this.flameChart&&this.flameChart.update()}focus(){this.flameChart?this.flameChart.focus():super.focus()}columnHeader(e){throw"Not implemented"}selectRange(e,t){this.flameChart&&this.flameChart.selectRange(e,t)}async toolbarItems(){return[this.viewSelectComboBox,this.focusButton,this.excludeButton,this.resetButton]}getBottomUpProfileDataGridTree(){return this.bottomUpProfileDataGridTree||(this.bottomUpProfileDataGridTree=new BottomUpProfileDataGridTree(this.nodeFormatter,this.searchableViewInternal,this.profileInternal.root,this.adjustedTotal)),this.bottomUpProfileDataGridTree}getTopDownProfileDataGridTree(){return this.topDownProfileDataGridTree||(this.topDownProfileDataGridTree=new TopDownProfileDataGridTree(this.nodeFormatter,this.searchableViewInternal,this.profileInternal.root,this.adjustedTotal)),this.topDownProfileDataGridTree}populateContextMenu(e,t){const i=t;i.linkElement&&!e.containsTarget(i.linkElement)&&e.appendApplicableItems(i.linkElement)}willHide(){this.currentSearchResultIndex=-1}refresh(){if(!this.profileDataGridTree)return;const e=this.dataGrid.selectedNode?this.dataGrid.selectedNode.profileNode:null;this.dataGrid.rootNode().removeChildren();const t=this.profileDataGridTree.children,i=t.length;for(let e=0;e<i;++e)this.dataGrid.rootNode().appendChild(t[e]);e&&(e.selected=!0)}refreshVisibleData(){let e=this.dataGrid.rootNode().children[0];for(;e;)e.refresh(),e=e.traverseNextNode(!1,null,!0)}searchableView(){return this.searchableViewInternal}supportsCaseSensitiveSearch(){return!0}supportsRegexSearch(){return!1}onSearchCanceled(){this.searchableElement&&this.searchableElement.onSearchCanceled()}performSearch(e,t,i){this.searchableElement&&this.searchableElement.performSearch(e,t,i)}jumpToNextSearchResult(){this.searchableElement&&this.searchableElement.jumpToNextSearchResult()}jumpToPreviousSearchResult(){this.searchableElement&&this.searchableElement.jumpToPreviousSearchResult()}linkifier(){return this.linkifierInternal}createFlameChartDataProvider(){throw"Not implemented"}ensureFlameChartCreated(){this.flameChart||(this.dataProvider=this.createFlameChartDataProvider(),this.flameChart=new CPUProfileFlameChart(this.searchableViewInternal,this.dataProvider),this.flameChart.addEventListener(PerfUI.FlameChart.Events.EntryInvoked,(e=>{this.onEntryInvoked(e)})))}async onEntryInvoked(e){if(!this.dataProvider)return;const t=e.data,i=this.dataProvider.entryNodes[t],r=this.profileHeader.debuggerModel;if(!i||!i.scriptId||!r)return;const o=r.scriptForId(i.scriptId);if(!o)return;const s=r.createRawLocation(o,i.lineNumber,i.columnNumber),a=await Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().rawLocationToUILocation(s);Common.Revealer.reveal(a)}changeView(){if(!this.profileInternal)return;switch(this.searchableViewInternal.closeSearch(),this.visibleView&&this.visibleView.detach(),this.viewType.set(this.viewSelectComboBox.selectedOption().value),this.viewType.get()){case"Flame":this.ensureFlameChartCreated(),this.visibleView=this.flameChart,this.searchableElement=this.flameChart;break;case"Tree":this.profileDataGridTree=this.getTopDownProfileDataGridTree(),this.sortProfile(),this.visibleView=this.dataGrid.asWidget(),this.searchableElement=this.profileDataGridTree;break;case"Heavy":this.profileDataGridTree=this.getBottomUpProfileDataGridTree(),this.sortProfile(),this.visibleView=this.dataGrid.asWidget(),this.searchableElement=this.profileDataGridTree}const e="Flame"===this.viewType.get();this.focusButton.setVisible(!e),this.excludeButton.setVisible(!e),this.resetButton.setVisible(!e),this.visibleView&&this.visibleView.show(this.searchableViewInternal.element)}nodeSelected(e){this.focusButton.setEnabled(e),this.excludeButton.setEnabled(e)}focusClicked(){this.dataGrid.selectedNode&&(this.resetButton.setEnabled(!0),this.resetButton.element.focus(),this.profileDataGridTree&&this.profileDataGridTree.focus(this.dataGrid.selectedNode),this.refresh(),this.refreshVisibleData(),Host.userMetrics.actionTaken(Host.UserMetrics.Action.CpuProfileNodeFocused))}excludeClicked(){const e=this.dataGrid.selectedNode;e&&(this.resetButton.setEnabled(!0),this.resetButton.element.focus(),e.deselect(),this.profileDataGridTree&&this.profileDataGridTree.exclude(e),this.refresh(),this.refreshVisibleData(),Host.userMetrics.actionTaken(Host.UserMetrics.Action.CpuProfileNodeExcluded))}resetClicked(){this.viewSelectComboBox.selectElement().focus(),this.resetButton.setEnabled(!1),this.profileDataGridTree&&this.profileDataGridTree.restore(),this.linkifierInternal.reset(),this.refresh(),this.refreshVisibleData()}sortProfile(){if(!this.profileDataGridTree)return;const e=this.dataGrid.isSortOrderAscending(),t=this.dataGrid.sortColumnId(),i="function"===t?"functionName":t||"";this.profileDataGridTree.sort(ProfileDataGridTree.propertyComparator(i,e),!1),this.refresh()}}export const maxLinkLength=30;export class WritableProfileHeader extends ProfileHeader{debuggerModel;fileName;jsonifiedProfile;profile;protocolProfileInternal;constructor(e,t,i){super(t,i||i18nString(UIStrings.profileD,{PH1:t.nextProfileUid()})),this.debuggerModel=e}onChunkTransferred(e){this.jsonifiedProfile&&this.updateStatus(i18nString(UIStrings.loadingD,{PH1:Platform.NumberUtilities.bytesToString(this.jsonifiedProfile.length)}))}onError(e){const t=e.error();t&&this.updateStatus(i18nString(UIStrings.fileSReadErrorS,{PH1:e.fileName(),PH2:t.message}))}async write(e){this.jsonifiedProfile+=e}async close(){}dispose(){this.removeTempFile()}createSidebarTreeElement(e){return new ProfileSidebarTreeElement(e,this,"profile-sidebar-tree-item")}canSaveToFile(){return!this.fromFile()&&Boolean(this.protocolProfileInternal)}async saveToFile(){const e=new Bindings.FileUtils.FileOutputStream;if(!this.fileName){const e=Platform.DateUtilities.toISO8601Compact(new Date),t=this.profileType().fileExtension();this.fileName=`${this.profileType().typeName()}-${e}${t}`}if(!await e.open(this.fileName)||!this.tempFile)return;const t=await this.tempFile.read();t&&await e.write(t),e.close()}async loadFromFile(e){this.updateStatus(i18nString(UIStrings.loading),!0);const t=new Bindings.FileUtils.ChunkedFileReader(e,1e7,this.onChunkTransferred.bind(this));this.jsonifiedProfile="";if(!await t.read(this))return this.onError(t),new Error(i18nString(UIStrings.failedToReadFile));this.updateStatus(i18nString(UIStrings.parsing),!0);let i=null;try{this.profile=JSON.parse(this.jsonifiedProfile),this.setProfile(this.profile),this.updateStatus(i18nString(UIStrings.loaded),!1)}catch(e){i=e,this.profileType().removeProfile(this)}return this.jsonifiedProfile=null,this.profileType().profileBeingRecorded()===this&&this.profileType().setProfileBeingRecorded(null),i}setProtocolProfile(e){this.setProfile(e),this.protocolProfileInternal=e,this.tempFile=new Bindings.TempFile.TempFile,this.tempFile.write([JSON.stringify(e)]),this.canSaveToFile()&&this.dispatchEventToListeners(Events.ProfileReceived)}}