1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF unknown KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { SpSystemTrace } from './SpSystemTrace';
17fb726d48Sopenharmony_ciimport { TabPaneFrequencySample } from './trace/sheet/cpu/TabPaneFrequencySample';
18fb726d48Sopenharmony_ciimport { TabPaneCounterSample } from './trace/sheet/cpu/TabPaneCounterSample';
19fb726d48Sopenharmony_ciimport { RangeSelect } from './trace/base/RangeSelect';
20fb726d48Sopenharmony_ciimport { TraceRow } from './trace/base/TraceRow';
21fb726d48Sopenharmony_ciimport { SportRuler } from './trace/timer-shaft/SportRuler';
22fb726d48Sopenharmony_ciimport { SelectionParam } from '../bean/BoxSelection';
23fb726d48Sopenharmony_ciimport { error, info } from '../../log/Log';
24fb726d48Sopenharmony_ciimport { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil';
25fb726d48Sopenharmony_ciimport { queryEbpfSamplesCount } from '../database/sql/Memory.sql';
26fb726d48Sopenharmony_ciimport { SpChartManager } from './chart/SpChartManager';
27fb726d48Sopenharmony_ciimport { ThreadStruct } from '../database/ui-worker/ProcedureWorkerThread';
28fb726d48Sopenharmony_ciimport { FlagsConfig } from './SpFlags';
29fb726d48Sopenharmony_ciimport { threadPool, threadPool2 } from '../database/SqlLite';
30fb726d48Sopenharmony_ciimport { JankStruct } from '../database/ui-worker/ProcedureWorkerJank';
31fb726d48Sopenharmony_ciimport { CpuStruct } from '../database/ui-worker/cpu/ProcedureWorkerCPU';
32fb726d48Sopenharmony_ciimport { PairPoint } from '../database/ui-worker/ProcedureWorkerCommon';
33fb726d48Sopenharmony_ciimport { TraceSheet } from './trace/base/TraceSheet';
34fb726d48Sopenharmony_ciimport { TimerShaftElement } from './trace/TimerShaftElement';
35fb726d48Sopenharmony_ciimport { SpChartList } from './trace/SpChartList';
36fb726d48Sopenharmony_citype HTMLElementAlias = HTMLElement | null | undefined;
37fb726d48Sopenharmony_ciimport { Utils } from './trace/base/Utils';
38fb726d48Sopenharmony_ciimport { fuzzyQueryFuncRowData, queryFuncRowData } from '../database/sql/Func.sql';
39fb726d48Sopenharmony_ci
40fb726d48Sopenharmony_cifunction rightButtonOnClick(sp: SpSystemTrace, rightStar: HTMLElementAlias): unknown {
41fb726d48Sopenharmony_ci  Object.assign(sp, {
42fb726d48Sopenharmony_ci    ext(): string {
43fb726d48Sopenharmony_ci      return 'Handle the right button click event';
44fb726d48Sopenharmony_ci    },
45fb726d48Sopenharmony_ci  });
46fb726d48Sopenharmony_ci
47fb726d48Sopenharmony_ci  return function (event: unknown): void {
48fb726d48Sopenharmony_ci    if (SpSystemTrace.btnTimer) {
49fb726d48Sopenharmony_ci      return;
50fb726d48Sopenharmony_ci    }
51fb726d48Sopenharmony_ci    sp.checkclick = true;
52fb726d48Sopenharmony_ci    // 唤醒树有值则不再重复添加
53fb726d48Sopenharmony_ci    const startIndex = CpuStruct.selectCpuStruct!.displayProcess?.indexOf('[');
54fb726d48Sopenharmony_ci    if (SpSystemTrace.wakeupList.length === 0) {
55fb726d48Sopenharmony_ci      SpSystemTrace.wakeupList.unshift(CpuStruct.wakeupBean!);
56fb726d48Sopenharmony_ci      sp.queryCPUWakeUpList(CpuStruct.wakeupBean!);
57fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.ts = CpuStruct.selectCpuStruct!.startTime;
58fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.thread = CpuStruct.selectCpuStruct!.name;
59fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.pid = CpuStruct.selectCpuStruct!.processId;
60fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.process = CpuStruct.selectCpuStruct!.displayProcess?.substring(0, startIndex).trim();
61fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.itid = CpuStruct.wakeupBean!.itid;
62fb726d48Sopenharmony_ci      sessionStorage.setItem('saveselectcpustruct', JSON.stringify(CpuStruct.selectCpuStruct));
63fb726d48Sopenharmony_ci    } else {
64fb726d48Sopenharmony_ci      sp.wakeupListNull();
65fb726d48Sopenharmony_ci      SpSystemTrace.wakeupList.unshift(CpuStruct.wakeupBean!);
66fb726d48Sopenharmony_ci      sp.queryCPUWakeUpList(CpuStruct.wakeupBean!);
67fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.ts = CpuStruct.selectCpuStruct!.startTime;
68fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.thread = CpuStruct.selectCpuStruct!.name;
69fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.pid = CpuStruct.selectCpuStruct!.processId;
70fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.process = CpuStruct.selectCpuStruct!.displayProcess?.substring(0, startIndex).trim();
71fb726d48Sopenharmony_ci      CpuStruct.selectCpuStruct!.itid = CpuStruct.wakeupBean!.itid;
72fb726d48Sopenharmony_ci      sessionStorage.setItem('saveselectcpustruct', JSON.stringify(CpuStruct.selectCpuStruct));
73fb726d48Sopenharmony_ci    }
74fb726d48Sopenharmony_ci    setTimeout(() => {
75fb726d48Sopenharmony_ci      requestAnimationFrame(() => sp.refreshCanvas(false));
76fb726d48Sopenharmony_ci    }, 300);
77fb726d48Sopenharmony_ci    rightStar!.style.visibility = 'visible';
78fb726d48Sopenharmony_ci    rightStar!.style.cursor = 'pointer';
79fb726d48Sopenharmony_ci    SpSystemTrace.btnTimer = setTimeout((): void => {
80fb726d48Sopenharmony_ci      SpSystemTrace.btnTimer = null; // 2.清空节流阀,方便下次开启定时器
81fb726d48Sopenharmony_ci    }, 2000);
82fb726d48Sopenharmony_ci  };
83fb726d48Sopenharmony_ci}
84fb726d48Sopenharmony_cifunction rightStarOnClick(sp: SpSystemTrace) {
85fb726d48Sopenharmony_ci  return function (ev: unknown): void {
86fb726d48Sopenharmony_ci    let wakeupLists = [];
87fb726d48Sopenharmony_ci    wakeupLists.push(CpuStruct.selectCpuStruct?.cpu);
88fb726d48Sopenharmony_ci    for (let wakeupBean of SpSystemTrace.wakeupList) {
89fb726d48Sopenharmony_ci      wakeupLists.push(wakeupBean.cpu);
90fb726d48Sopenharmony_ci    }
91fb726d48Sopenharmony_ci    let wakeupCpuLists = Array.from(new Set(wakeupLists)).sort();
92fb726d48Sopenharmony_ci    for (let wakeupCpu of wakeupCpuLists) {
93fb726d48Sopenharmony_ci      // @ts-ignore
94fb726d48Sopenharmony_ci      let cpuFavoriteRow: unknown = sp.shadowRoot?.querySelector<TraceRow<unknown>>(
95fb726d48Sopenharmony_ci        `trace-row[row-type='cpu-data'][row-id='${Utils.getDistributedRowId(wakeupCpu)}']`
96fb726d48Sopenharmony_ci      );
97fb726d48Sopenharmony_ci      if (cpuFavoriteRow === null || cpuFavoriteRow === undefined) {
98fb726d48Sopenharmony_ci        continue;
99fb726d48Sopenharmony_ci      }
100fb726d48Sopenharmony_ci      // @ts-ignore
101fb726d48Sopenharmony_ci      cpuFavoriteRow!.setAttribute('collect-type', '');
102fb726d48Sopenharmony_ci      let replaceRow = document.createElement('div');
103fb726d48Sopenharmony_ci      // @ts-ignore
104fb726d48Sopenharmony_ci      replaceRow.setAttribute('row-id', `${cpuFavoriteRow.rowId}-${cpuFavoriteRow.rowType}`);
105fb726d48Sopenharmony_ci      replaceRow.setAttribute('type', 'replaceRow');
106fb726d48Sopenharmony_ci      // @ts-ignore
107fb726d48Sopenharmony_ci      replaceRow.setAttribute('row-parent-id', cpuFavoriteRow.rowParentId);
108fb726d48Sopenharmony_ci      replaceRow.style.display = 'none';
109fb726d48Sopenharmony_ci      // @ts-ignore
110fb726d48Sopenharmony_ci      cpuFavoriteRow.rowHidden = !cpuFavoriteRow.hasAttribute('scene');
111fb726d48Sopenharmony_ci      // @ts-ignore
112fb726d48Sopenharmony_ci      if (sp.rowsEL!.contains(cpuFavoriteRow)) {
113fb726d48Sopenharmony_ci        // @ts-ignore
114fb726d48Sopenharmony_ci        sp.rowsEL!.replaceChild(replaceRow, cpuFavoriteRow);
115fb726d48Sopenharmony_ci      }
116fb726d48Sopenharmony_ci      // @ts-ignore
117fb726d48Sopenharmony_ci      cpuFavoriteRow.tampName = cpuFavoriteRow.name;
118fb726d48Sopenharmony_ci      // @ts-ignore
119fb726d48Sopenharmony_ci      sp.favoriteChartListEL!.insertRow(cpuFavoriteRow, cpuFavoriteRow.traceId || sp.currentCollectGroup, true);
120fb726d48Sopenharmony_ci      // @ts-ignore
121fb726d48Sopenharmony_ci      sp.collectRows.push(cpuFavoriteRow);
122fb726d48Sopenharmony_ci      sp.timerShaftEL?.displayCollect(sp.collectRows.length !== 0);
123fb726d48Sopenharmony_ci      sp.currentClickRow = null;
124fb726d48Sopenharmony_ci      // @ts-ignore
125fb726d48Sopenharmony_ci      cpuFavoriteRow.setAttribute('draggable', 'true');
126fb726d48Sopenharmony_ci      // @ts-ignore
127fb726d48Sopenharmony_ci      cpuFavoriteRow.addEventListener('dragstart', cpuFavoriteRowDragStart(sp, cpuFavoriteRow));
128fb726d48Sopenharmony_ci      // @ts-ignore
129fb726d48Sopenharmony_ci      cpuFavoriteRow.addEventListener('dragover', cpuFavoriteRowDragOver(sp));
130fb726d48Sopenharmony_ci      // @ts-ignore
131fb726d48Sopenharmony_ci      cpuFavoriteRow.addEventListener('drop', cpuFavoriteRowDropHandler(sp, cpuFavoriteRow));
132fb726d48Sopenharmony_ci      // @ts-ignore
133fb726d48Sopenharmony_ci      cpuFavoriteRow.addEventListener('dragend', cpuFavoriteRowDragendHandler(sp));
134fb726d48Sopenharmony_ci    }
135fb726d48Sopenharmony_ci    sp.refreshFavoriteCanvas();
136fb726d48Sopenharmony_ci    sp.refreshCanvas(true);
137fb726d48Sopenharmony_ci  };
138fb726d48Sopenharmony_ci}
139fb726d48Sopenharmony_cifunction cpuFavoriteRowDragStart(sp: SpSystemTrace, cpuFavoriteRow: unknown) {
140fb726d48Sopenharmony_ci  return function (): void {
141fb726d48Sopenharmony_ci    // @ts-ignore
142fb726d48Sopenharmony_ci    sp.currentClickRow = cpuFavoriteRow;
143fb726d48Sopenharmony_ci  };
144fb726d48Sopenharmony_ci}
145fb726d48Sopenharmony_cifunction cpuFavoriteRowDragOver(sp: SpSystemTrace) {
146fb726d48Sopenharmony_ci  return function (ev: unknown): void {
147fb726d48Sopenharmony_ci    // @ts-ignore
148fb726d48Sopenharmony_ci    ev.preventDefault();
149fb726d48Sopenharmony_ci    // @ts-ignore
150fb726d48Sopenharmony_ci    ev.dataTransfer.dropEffect = 'move';
151fb726d48Sopenharmony_ci  };
152fb726d48Sopenharmony_ci}
153fb726d48Sopenharmony_cifunction cpuFavoriteRowDropHandler(sp: SpSystemTrace, cpuFavoriteRow: unknown) {
154fb726d48Sopenharmony_ci  return function (ev: unknown): void {
155fb726d48Sopenharmony_ci    if (sp.favoriteChartListEL && sp.currentClickRow && sp.currentClickRow !== cpuFavoriteRow) {
156fb726d48Sopenharmony_ci      // @ts-ignore
157fb726d48Sopenharmony_ci      let rect = cpuFavoriteRow.getBoundingClientRect();
158fb726d48Sopenharmony_ci      // @ts-ignore
159fb726d48Sopenharmony_ci      if (ev.clientY >= rect.top && ev.clientY < rect.top + rect.height / 2) {
160fb726d48Sopenharmony_ci        //向上移动
161fb726d48Sopenharmony_ci        // @ts-ignore
162fb726d48Sopenharmony_ci        sp.favoriteChartListEL.insertRowBefore(sp.currentClickRow, cpuFavoriteRow);
163fb726d48Sopenharmony_ci        // @ts-ignore
164fb726d48Sopenharmony_ci      } else if (ev.clientY <= rect.bottom && ev.clientY > rect.top + rect.height / 2) {
165fb726d48Sopenharmony_ci        //向下移动
166fb726d48Sopenharmony_ci        // @ts-ignore
167fb726d48Sopenharmony_ci        sp.favoriteChartListEL.insertRowBefore(sp.currentClickRow, cpuFavoriteRow.nextSibling);
168fb726d48Sopenharmony_ci      }
169fb726d48Sopenharmony_ci      sp.refreshFavoriteCanvas();
170fb726d48Sopenharmony_ci    }
171fb726d48Sopenharmony_ci  };
172fb726d48Sopenharmony_ci}
173fb726d48Sopenharmony_cifunction cpuFavoriteRowDragendHandler(sp: SpSystemTrace): () => void {
174fb726d48Sopenharmony_ci  return function (): void {
175fb726d48Sopenharmony_ci    sp.linkNodes.forEach((itln) => {
176fb726d48Sopenharmony_ci      if (itln[0].rowEL.collect) {
177fb726d48Sopenharmony_ci        itln[0].rowEL.translateY = itln[0].rowEL.getBoundingClientRect().top - 195;
178fb726d48Sopenharmony_ci      } else {
179fb726d48Sopenharmony_ci        itln[0].rowEL.translateY = itln[0].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
180fb726d48Sopenharmony_ci      }
181fb726d48Sopenharmony_ci      if (itln[1].rowEL.collect) {
182fb726d48Sopenharmony_ci        itln[1].rowEL.translateY = itln[1].rowEL.getBoundingClientRect().top - 195;
183fb726d48Sopenharmony_ci      } else {
184fb726d48Sopenharmony_ci        itln[1].rowEL.translateY = itln[1].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
185fb726d48Sopenharmony_ci      }
186fb726d48Sopenharmony_ci      itln[0].y = itln[0].rowEL.translateY + itln[0].offsetY;
187fb726d48Sopenharmony_ci      itln[1].y = itln[1].rowEL.translateY + itln[1].offsetY;
188fb726d48Sopenharmony_ci    });
189fb726d48Sopenharmony_ci    sp.currentClickRow = null;
190fb726d48Sopenharmony_ci  };
191fb726d48Sopenharmony_ci}
192fb726d48Sopenharmony_cifunction triangleFlagHandler(sp: SpSystemTrace): (event: unknown) => void {
193fb726d48Sopenharmony_ci  return function (event: unknown): void {
194fb726d48Sopenharmony_ci    //@ts-ignore
195fb726d48Sopenharmony_ci    let temporaryTime = sp.timerShaftEL?.drawTriangle(event.detail.time, event.detail.type);
196fb726d48Sopenharmony_ci    //@ts-ignore
197fb726d48Sopenharmony_ci    if (event.detail.timeCallback && temporaryTime) {
198fb726d48Sopenharmony_ci      //@ts-ignore
199fb726d48Sopenharmony_ci      event.detail.timeCallback(temporaryTime);
200fb726d48Sopenharmony_ci    }
201fb726d48Sopenharmony_ci  };
202fb726d48Sopenharmony_ci}
203fb726d48Sopenharmony_cifunction numberCalibrationHandler(sp: SpSystemTrace): (event: unknown) => void {
204fb726d48Sopenharmony_ci  return function (event: unknown): void {
205fb726d48Sopenharmony_ci    // @ts-ignore
206fb726d48Sopenharmony_ci    sp.timerShaftEL!.sportRuler!.times = event.detail.time;
207fb726d48Sopenharmony_ci    // @ts-ignore
208fb726d48Sopenharmony_ci    sp.timerShaftEL!.sportRuler!.counts = event.detail.counts;
209fb726d48Sopenharmony_ci    // @ts-ignore
210fb726d48Sopenharmony_ci    sp.timerShaftEL!.sportRuler!.durations = event.detail.durations;
211fb726d48Sopenharmony_ci    sp.timerShaftEL!.sportRuler?.draw();
212fb726d48Sopenharmony_ci  };
213fb726d48Sopenharmony_ci}
214fb726d48Sopenharmony_cifunction flagChangeHandler(sp: SpSystemTrace): (event: unknown) => void {
215fb726d48Sopenharmony_ci  return function (event: unknown): void {
216fb726d48Sopenharmony_ci    // @ts-ignore
217fb726d48Sopenharmony_ci    sp.timerShaftEL?.modifyFlagList(event.detail);
218fb726d48Sopenharmony_ci    // @ts-ignore
219fb726d48Sopenharmony_ci    if (event.detail.hidden) {
220fb726d48Sopenharmony_ci      //@ts-ignore
221fb726d48Sopenharmony_ci      sp.selectFlag = undefined;
222fb726d48Sopenharmony_ci      if (sp._flagList.length <= 0) {
223fb726d48Sopenharmony_ci        let showTab = sp.getShowTab();
224fb726d48Sopenharmony_ci        showTab = showTab.filter((it) => it !== 'box-flag');
225fb726d48Sopenharmony_ci        if (TraceRow.rangeSelectObject && showTab.length > 0) {
226fb726d48Sopenharmony_ci          sp.traceSheetEL?.displayTab(...showTab);
227fb726d48Sopenharmony_ci        } else {
228fb726d48Sopenharmony_ci          sp.traceSheetEL?.setMode('hidden');
229fb726d48Sopenharmony_ci        }
230fb726d48Sopenharmony_ci      }
231fb726d48Sopenharmony_ci      sp.refreshCanvas(true);
232fb726d48Sopenharmony_ci    }
233fb726d48Sopenharmony_ci  };
234fb726d48Sopenharmony_ci}
235fb726d48Sopenharmony_cifunction slicesChangeHandler(sp: SpSystemTrace): (event: unknown) => void {
236fb726d48Sopenharmony_ci  return function (event: unknown): void {
237fb726d48Sopenharmony_ci    // @ts-ignore
238fb726d48Sopenharmony_ci    sp.timerShaftEL?.modifySlicesList(event.detail);
239fb726d48Sopenharmony_ci    // @ts-ignore
240fb726d48Sopenharmony_ci    if (event.detail.hidden) {
241fb726d48Sopenharmony_ci      sp.slicestime = null;
242fb726d48Sopenharmony_ci      if (sp._slicesList.length <= 0) {
243fb726d48Sopenharmony_ci        let showTab = sp.getShowTab();
244fb726d48Sopenharmony_ci        showTab = showTab.filter((it) => it !== 'tabpane-current');
245fb726d48Sopenharmony_ci        if (TraceRow.rangeSelectObject && showTab.length > 0) {
246fb726d48Sopenharmony_ci          sp.traceSheetEL?.displayTab(...showTab);
247fb726d48Sopenharmony_ci        } else {
248fb726d48Sopenharmony_ci          sp.traceSheetEL?.setMode('hidden');
249fb726d48Sopenharmony_ci        }
250fb726d48Sopenharmony_ci      }
251fb726d48Sopenharmony_ci      sp.refreshCanvas(true);
252fb726d48Sopenharmony_ci    }
253fb726d48Sopenharmony_ci  };
254fb726d48Sopenharmony_ci}
255fb726d48Sopenharmony_cifunction collectHandler(sp: SpSystemTrace): (event: unknown) => void {
256fb726d48Sopenharmony_ci  return function (event: unknown): void {
257fb726d48Sopenharmony_ci    // @ts-ignore
258fb726d48Sopenharmony_ci    let currentRow = event.detail.row;
259fb726d48Sopenharmony_ci    if (currentRow.collect) {
260fb726d48Sopenharmony_ci      collectHandlerYes(sp, currentRow, event);
261fb726d48Sopenharmony_ci    } else {
262fb726d48Sopenharmony_ci      collectHandlerNo(sp, currentRow, event);
263fb726d48Sopenharmony_ci    }
264fb726d48Sopenharmony_ci    sp.timerShaftEL?.displayCollect(sp.collectRows.length !== 0);
265fb726d48Sopenharmony_ci    sp.refreshFavoriteCanvas();
266fb726d48Sopenharmony_ci    sp.refreshCanvas(true);
267fb726d48Sopenharmony_ci    sp.linkNodes.forEach((itln) => {
268fb726d48Sopenharmony_ci      if (itln[0].rowEL === currentRow) {
269fb726d48Sopenharmony_ci        if (itln[0].rowEL.collect) {
270fb726d48Sopenharmony_ci          itln[0].rowEL.translateY = itln[0].rowEL.getBoundingClientRect().top - 195;
271fb726d48Sopenharmony_ci        } else {
272fb726d48Sopenharmony_ci          itln[0].rowEL.translateY = itln[0].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
273fb726d48Sopenharmony_ci        }
274fb726d48Sopenharmony_ci        itln[0].y = itln[0].rowEL.translateY + itln[0].offsetY;
275fb726d48Sopenharmony_ci      } else if (itln[1].rowEL === currentRow) {
276fb726d48Sopenharmony_ci        if (itln[1].rowEL.collect) {
277fb726d48Sopenharmony_ci          itln[1].rowEL.translateY = itln[1].rowEL.getBoundingClientRect().top - 195;
278fb726d48Sopenharmony_ci        } else {
279fb726d48Sopenharmony_ci          itln[1].rowEL.translateY = itln[1].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
280fb726d48Sopenharmony_ci        }
281fb726d48Sopenharmony_ci        itln[1].y = itln[1].rowEL.translateY + itln[1].offsetY;
282fb726d48Sopenharmony_ci      }
283fb726d48Sopenharmony_ci    });
284fb726d48Sopenharmony_ci    // 收藏夹元素拖动排序功能
285fb726d48Sopenharmony_ci    sp.currentClickRow = null;
286fb726d48Sopenharmony_ci    currentRow.setAttribute('draggable', 'true');
287fb726d48Sopenharmony_ci    currentRow.addEventListener('dragstart', () => {
288fb726d48Sopenharmony_ci      sp.currentClickRow = currentRow;
289fb726d48Sopenharmony_ci    });
290fb726d48Sopenharmony_ci    currentRow.addEventListener('dragover', (ev: unknown) => {
291fb726d48Sopenharmony_ci      // @ts-ignore
292fb726d48Sopenharmony_ci      ev.preventDefault();
293fb726d48Sopenharmony_ci      // @ts-ignore
294fb726d48Sopenharmony_ci      ev.dataTransfer.dropEffect = 'move';
295fb726d48Sopenharmony_ci    });
296fb726d48Sopenharmony_ci    currentRow.addEventListener('drop', collectHandlerDrop(sp, currentRow));
297fb726d48Sopenharmony_ci    currentRow.addEventListener('dragend', collectHandlerDragEnd(sp));
298fb726d48Sopenharmony_ci  };
299fb726d48Sopenharmony_ci}
300fb726d48Sopenharmony_cifunction collectHandlerNo(sp: SpSystemTrace, currentRow: unknown, event: unknown): void {
301fb726d48Sopenharmony_ci  // @ts-ignore
302fb726d48Sopenharmony_ci  sp.favoriteChartListEL?.deleteRow(currentRow, event.detail.type !== 'auto-collect');
303fb726d48Sopenharmony_ci  // @ts-ignore
304fb726d48Sopenharmony_ci  if (event.detail.type !== 'auto-collect') {
305fb726d48Sopenharmony_ci    // @ts-ignore
306fb726d48Sopenharmony_ci    let rowIndex = sp.collectRows.indexOf(currentRow);
307fb726d48Sopenharmony_ci    if (rowIndex !== -1) {
308fb726d48Sopenharmony_ci      sp.collectRows.splice(rowIndex, 1);
309fb726d48Sopenharmony_ci    }
310fb726d48Sopenharmony_ci  }
311fb726d48Sopenharmony_ci  let row = currentRow;
312fb726d48Sopenharmony_ci  let allowExpansionRow = [];
313fb726d48Sopenharmony_ci  // @ts-ignore
314fb726d48Sopenharmony_ci  while (row.hasParentRowEl) {
315fb726d48Sopenharmony_ci    // @ts-ignore
316fb726d48Sopenharmony_ci    let parent = row.parentRowEl;
317fb726d48Sopenharmony_ci    allowExpansionRow.push(parent);
318fb726d48Sopenharmony_ci    row = parent;
319fb726d48Sopenharmony_ci  }
320fb726d48Sopenharmony_ci  if (allowExpansionRow.length === 1) {
321fb726d48Sopenharmony_ci    for (let index: number = allowExpansionRow.length - 1; index >= 0; index--) {
322fb726d48Sopenharmony_ci      if (allowExpansionRow[index]?.hasAttribute('scene')) {
323fb726d48Sopenharmony_ci        if (allowExpansionRow[index]!.expansion) {
324fb726d48Sopenharmony_ci          allowExpansionRow[index].updateChildRowStatus();
325fb726d48Sopenharmony_ci        } else {
326fb726d48Sopenharmony_ci          allowExpansionRow[index].expansion = true;
327fb726d48Sopenharmony_ci        }
328fb726d48Sopenharmony_ci      }
329fb726d48Sopenharmony_ci    }
330fb726d48Sopenharmony_ci  } else {
331fb726d48Sopenharmony_ci    for (let index: number = allowExpansionRow.length - 1; index >= 0; index--) {
332fb726d48Sopenharmony_ci      let currentItemRow = allowExpansionRow[index];
333fb726d48Sopenharmony_ci      if (currentItemRow.hasAttribute('scene')) {
334fb726d48Sopenharmony_ci        if (currentItemRow.rowParentId !== '') {
335fb726d48Sopenharmony_ci          if (currentItemRow.expansion) {
336fb726d48Sopenharmony_ci            currentItemRow.updateChildRowStatus();
337fb726d48Sopenharmony_ci          } else {
338fb726d48Sopenharmony_ci            currentItemRow.expansion = true;
339fb726d48Sopenharmony_ci          }
340fb726d48Sopenharmony_ci        }
341fb726d48Sopenharmony_ci        else {
342fb726d48Sopenharmony_ci          currentItemRow.expansion = true;
343fb726d48Sopenharmony_ci          let number = currentItemRow.childrenList.indexOf(currentRow);
344fb726d48Sopenharmony_ci          if (number !== -1) {// 确保 currentRow 在 childrenList 中
345fb726d48Sopenharmony_ci            let childrenEl = currentItemRow.childrenList[number];
346fb726d48Sopenharmony_ci            let childrenNextEl = currentItemRow.childrenList[number + 1];
347fb726d48Sopenharmony_ci            if (childrenEl) {
348fb726d48Sopenharmony_ci              if (childrenNextEl) {
349fb726d48Sopenharmony_ci                currentItemRow.parentNode.insertBefore(childrenEl, currentItemRow.childrenList[number + 1]);
350fb726d48Sopenharmony_ci              } else if (childrenEl.nextSibling) {
351fb726d48Sopenharmony_ci                currentItemRow.parentNode.insertBefore(childrenEl, childrenEl.nextSibling);
352fb726d48Sopenharmony_ci              } else {
353fb726d48Sopenharmony_ci                currentItemRow.parentNode.appendChild(childrenEl);
354fb726d48Sopenharmony_ci              }
355fb726d48Sopenharmony_ci            }
356fb726d48Sopenharmony_ci          }
357fb726d48Sopenharmony_ci        }
358fb726d48Sopenharmony_ci      }
359fb726d48Sopenharmony_ci    }
360fb726d48Sopenharmony_ci  }
361fb726d48Sopenharmony_ci  allowExpansionRow.length = 0;
362fb726d48Sopenharmony_ci  // @ts-ignore
363fb726d48Sopenharmony_ci  let traceId = currentRow.traceId ? `${currentRow.traceId}-` : '';
364fb726d48Sopenharmony_ci  let replaceRow = sp.rowsEL!.querySelector<HTMLCanvasElement>(
365fb726d48Sopenharmony_ci    // @ts-ignore
366fb726d48Sopenharmony_ci    `div[row-id='${traceId}${currentRow.rowId}-${currentRow.rowType}']`
367fb726d48Sopenharmony_ci  );
368fb726d48Sopenharmony_ci  // 取消收藏时,删除父亲ID
369fb726d48Sopenharmony_ci  // @ts-ignore
370fb726d48Sopenharmony_ci  currentRow.name = currentRow.tampName;
371fb726d48Sopenharmony_ci  if (replaceRow !== null) {
372fb726d48Sopenharmony_ci    // @ts-ignore
373fb726d48Sopenharmony_ci    sp.rowsEL!.replaceChild(currentRow, replaceRow);
374fb726d48Sopenharmony_ci    // @ts-ignore
375fb726d48Sopenharmony_ci    currentRow.style.boxShadow = '0 10px 10px #00000000';
376fb726d48Sopenharmony_ci  }
377fb726d48Sopenharmony_ci}
378fb726d48Sopenharmony_cifunction collectHandlerYes(sp: SpSystemTrace, currentRow: unknown, event: unknown): void {
379fb726d48Sopenharmony_ci  if (!sp.collectRows.find((find) => find === currentRow)) {
380fb726d48Sopenharmony_ci    // @ts-ignore
381fb726d48Sopenharmony_ci    sp.collectRows.push(currentRow);
382fb726d48Sopenharmony_ci  }
383fb726d48Sopenharmony_ci  let replaceRow = document.createElement('div');
384fb726d48Sopenharmony_ci  // @ts-ignore
385fb726d48Sopenharmony_ci  let traceId = currentRow.traceId ? `${currentRow.traceId}-` : '';
386fb726d48Sopenharmony_ci  // @ts-ignore
387fb726d48Sopenharmony_ci  replaceRow.setAttribute('row-id', `${traceId}${currentRow.rowId}-${currentRow.rowType}`);
388fb726d48Sopenharmony_ci  replaceRow.setAttribute('type', 'replaceRow');
389fb726d48Sopenharmony_ci  // @ts-ignore
390fb726d48Sopenharmony_ci  replaceRow.setAttribute('row-parent-id', currentRow.rowParentId);
391fb726d48Sopenharmony_ci  replaceRow.style.display = 'none';
392fb726d48Sopenharmony_ci  // @ts-ignore
393fb726d48Sopenharmony_ci  if (!currentRow.hasAttribute('scene')) {
394fb726d48Sopenharmony_ci    // @ts-ignore
395fb726d48Sopenharmony_ci    currentRow.setAttribute('row-hidden', '');
396fb726d48Sopenharmony_ci  } else {
397fb726d48Sopenharmony_ci    // @ts-ignore
398fb726d48Sopenharmony_ci    currentRow.removeAttribute('row-hidden');
399fb726d48Sopenharmony_ci  }
400fb726d48Sopenharmony_ci  // 添加收藏时,在线程名前面追加父亲ID
401fb726d48Sopenharmony_ci  // @ts-ignore
402fb726d48Sopenharmony_ci  let rowParentId = currentRow.rowParentId;
403fb726d48Sopenharmony_ci  // @ts-ignore
404fb726d48Sopenharmony_ci  currentRow.tampName = currentRow.name;
405fb726d48Sopenharmony_ci  if (rowParentId) {
406fb726d48Sopenharmony_ci    // @ts-ignore
407fb726d48Sopenharmony_ci    let parentRows = sp.shadowRoot?.querySelectorAll<TraceRow<unknown>>(`trace-row[row-id='${rowParentId}']`);
408fb726d48Sopenharmony_ci    parentRows?.forEach((parentRow) => {
409fb726d48Sopenharmony_ci      if (
410fb726d48Sopenharmony_ci        parentRow?.name &&
411fb726d48Sopenharmony_ci        // @ts-ignore
412fb726d48Sopenharmony_ci        parentRow?.name !== currentRow.name &&
413fb726d48Sopenharmony_ci        !parentRow.rowType!.startsWith('cpu') &&
414fb726d48Sopenharmony_ci        !parentRow.rowType!.startsWith('thread') &&
415fb726d48Sopenharmony_ci        !parentRow.rowType!.startsWith('func') &&
416fb726d48Sopenharmony_ci        // @ts-ignore
417fb726d48Sopenharmony_ci        !currentRow.name.includes(parentRow.name)
418fb726d48Sopenharmony_ci      ) {
419fb726d48Sopenharmony_ci        //@ts-ignore
420fb726d48Sopenharmony_ci        currentRow.name = currentRow.protoParentId ? `${currentRow.name} (${currentRow.protoParentId})` :
421fb726d48Sopenharmony_ci          //@ts-ignore
422fb726d48Sopenharmony_ci          `${currentRow.name} (${parentRow.name})`;
423fb726d48Sopenharmony_ci      }
424fb726d48Sopenharmony_ci    });
425fb726d48Sopenharmony_ci  }
426fb726d48Sopenharmony_ci  // @ts-ignore
427fb726d48Sopenharmony_ci  if (!currentRow.hasParentRowEl) {
428fb726d48Sopenharmony_ci    // @ts-ignore
429fb726d48Sopenharmony_ci    sp.rowsEL!.replaceChild(replaceRow, currentRow);
430fb726d48Sopenharmony_ci  }
431fb726d48Sopenharmony_ci  // @ts-ignore
432fb726d48Sopenharmony_ci  let group = currentRow.traceId || sp.currentCollectGroup;
433fb726d48Sopenharmony_ci  // @ts-ignore
434fb726d48Sopenharmony_ci  sp.favoriteChartListEL?.insertRow(currentRow, group, event.detail.type !== 'auto-collect');
435fb726d48Sopenharmony_ci}
436fb726d48Sopenharmony_cifunction collectHandlerDrop(sp: SpSystemTrace, currentRow: HTMLDivElement | undefined | null): (ev: unknown) => void {
437fb726d48Sopenharmony_ci  return function (ev: unknown) {
438fb726d48Sopenharmony_ci    if (sp.favoriteChartListEL !== null && sp.currentClickRow !== null && sp.currentClickRow !== currentRow) {
439fb726d48Sopenharmony_ci      // @ts-ignore
440fb726d48Sopenharmony_ci      let rect = currentRow!.getBoundingClientRect();
441fb726d48Sopenharmony_ci      // @ts-ignore
442fb726d48Sopenharmony_ci      if (ev.clientY >= rect.top && ev.clientY < rect.top + rect.height / 2) {
443fb726d48Sopenharmony_ci        //向上移动
444fb726d48Sopenharmony_ci        sp.favoriteChartListEL!.insertRowBefore(sp.currentClickRow!, currentRow!);
445fb726d48Sopenharmony_ci        // @ts-ignore
446fb726d48Sopenharmony_ci      } else if (ev.clientY <= rect.bottom && ev.clientY > rect.top + rect.height / 2) {
447fb726d48Sopenharmony_ci        //向下移动
448fb726d48Sopenharmony_ci        sp.favoriteChartListEL!.insertRowBefore(sp.currentClickRow!, currentRow!.nextSibling!);
449fb726d48Sopenharmony_ci      }
450fb726d48Sopenharmony_ci      sp.refreshFavoriteCanvas();
451fb726d48Sopenharmony_ci    }
452fb726d48Sopenharmony_ci  };
453fb726d48Sopenharmony_ci}
454fb726d48Sopenharmony_cifunction collectHandlerDragEnd(sp: SpSystemTrace): (ev: unknown) => void {
455fb726d48Sopenharmony_ci  return function (ev: unknown): void {
456fb726d48Sopenharmony_ci    sp.linkNodes.forEach((itln) => {
457fb726d48Sopenharmony_ci      if (itln[0].rowEL.collect) {
458fb726d48Sopenharmony_ci        if (sp.timerShaftEL?._checkExpand) {
459fb726d48Sopenharmony_ci          itln[0].rowEL.translateY =
460fb726d48Sopenharmony_ci            itln[0].rowEL.getBoundingClientRect().top - 195 + sp.timerShaftEL._usageFoldHeight!;
461fb726d48Sopenharmony_ci        } else {
462fb726d48Sopenharmony_ci          itln[0].rowEL.translateY = itln[0].rowEL.getBoundingClientRect().top - 195;
463fb726d48Sopenharmony_ci        }
464fb726d48Sopenharmony_ci      } else {
465fb726d48Sopenharmony_ci        itln[0].rowEL.translateY = itln[0].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
466fb726d48Sopenharmony_ci      }
467fb726d48Sopenharmony_ci      if (itln[1].rowEL.collect) {
468fb726d48Sopenharmony_ci        if (sp.timerShaftEL?._checkExpand) {
469fb726d48Sopenharmony_ci          itln[1].rowEL.translateY =
470fb726d48Sopenharmony_ci            itln[1].rowEL.getBoundingClientRect().top - 195 + sp.timerShaftEL._usageFoldHeight!;
471fb726d48Sopenharmony_ci        } else {
472fb726d48Sopenharmony_ci          itln[1].rowEL.translateY = itln[1].rowEL.getBoundingClientRect().top - 195;
473fb726d48Sopenharmony_ci        }
474fb726d48Sopenharmony_ci      } else {
475fb726d48Sopenharmony_ci        itln[1].rowEL.translateY = itln[1].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
476fb726d48Sopenharmony_ci      }
477fb726d48Sopenharmony_ci      itln[0].y = itln[0].rowEL.translateY + itln[0].offsetY;
478fb726d48Sopenharmony_ci      itln[1].y = itln[1].rowEL.translateY + itln[1].offsetY;
479fb726d48Sopenharmony_ci    });
480fb726d48Sopenharmony_ci    sp.currentClickRow = null;
481fb726d48Sopenharmony_ci  };
482fb726d48Sopenharmony_ci}
483fb726d48Sopenharmony_cifunction selectHandler(sp: SpSystemTrace): void {
484fb726d48Sopenharmony_ci  sp.rangeSelect.selectHandler = (rows, refreshCheckBox): void => {
485fb726d48Sopenharmony_ci    rows.forEach((item) => {
486fb726d48Sopenharmony_ci      sp.setAttribute('clickRow', item.rowType!);
487fb726d48Sopenharmony_ci      sp.setAttribute('rowName', item.name);
488fb726d48Sopenharmony_ci      sp.setAttribute('rowId', item.rowId!);
489fb726d48Sopenharmony_ci    });
490fb726d48Sopenharmony_ci    if (rows.length === 0) {
491fb726d48Sopenharmony_ci      const allRows = [
492fb726d48Sopenharmony_ci        // @ts-ignore
493fb726d48Sopenharmony_ci        ...sp.shadowRoot!.querySelectorAll<TraceRow<unknown>>('trace-row'),
494fb726d48Sopenharmony_ci        ...sp.favoriteChartListEL!.getAllCollectRows(),
495fb726d48Sopenharmony_ci      ];
496fb726d48Sopenharmony_ci      for (const row of allRows) {
497fb726d48Sopenharmony_ci        row.checkType = '-1';
498fb726d48Sopenharmony_ci        if (row.folder) {
499fb726d48Sopenharmony_ci          row.childrenList.forEach((item) => {
500fb726d48Sopenharmony_ci            row.checkType = '-1';
501fb726d48Sopenharmony_ci          });
502fb726d48Sopenharmony_ci        }
503fb726d48Sopenharmony_ci      }
504fb726d48Sopenharmony_ci      sp.refreshCanvas(true);
505fb726d48Sopenharmony_ci      if (!SportRuler.isMouseInSportRuler) {
506fb726d48Sopenharmony_ci        sp.traceSheetEL?.setMode('max');
507fb726d48Sopenharmony_ci        sp.traceSheetEL?.setMode('hidden');
508fb726d48Sopenharmony_ci      }
509fb726d48Sopenharmony_ci      return;
510fb726d48Sopenharmony_ci    }
511fb726d48Sopenharmony_ci    let checkRows = rows;
512fb726d48Sopenharmony_ci    if (!refreshCheckBox) {
513fb726d48Sopenharmony_ci      checkRows = [
514fb726d48Sopenharmony_ci        ...rows,
515fb726d48Sopenharmony_ci        // @ts-ignore
516fb726d48Sopenharmony_ci        ...sp.shadowRoot!.querySelectorAll<TraceRow<unknown>>(`trace-row[check-type='2']`),
517fb726d48Sopenharmony_ci        ...sp.favoriteChartListEL!.getAllSelectCollectRows(),
518fb726d48Sopenharmony_ci      ];
519fb726d48Sopenharmony_ci    }
520fb726d48Sopenharmony_ci    selectHandlerRefreshCheckBox(sp, checkRows, refreshCheckBox);
521fb726d48Sopenharmony_ci    if (!sp.isSelectClick) {
522fb726d48Sopenharmony_ci      sp.rangeTraceRow = [];
523fb726d48Sopenharmony_ci    }
524fb726d48Sopenharmony_ci    selectHandlerRows(sp, checkRows);
525fb726d48Sopenharmony_ci  };
526fb726d48Sopenharmony_ci}
527fb726d48Sopenharmony_ci// @ts-ignore
528fb726d48Sopenharmony_cifunction selectHandlerRefreshCheckBox(sp: SpSystemTrace, rows: Array<TraceRow<unknown>>, refreshCheckBox: boolean): void {
529fb726d48Sopenharmony_ci  if (refreshCheckBox) {
530fb726d48Sopenharmony_ci    if (rows.length > 0) {
531fb726d48Sopenharmony_ci      sp.queryAllTraceRow().forEach((row) => (row.checkType = '0'));
532fb726d48Sopenharmony_ci      rows.forEach((it) => (it.checkType = '2'));
533fb726d48Sopenharmony_ci    } else {
534fb726d48Sopenharmony_ci      sp.queryAllTraceRow().forEach((row) => (row.checkType = '-1'));
535fb726d48Sopenharmony_ci      return;
536fb726d48Sopenharmony_ci    }
537fb726d48Sopenharmony_ci  }
538fb726d48Sopenharmony_ci}
539fb726d48Sopenharmony_ci// @ts-ignore
540fb726d48Sopenharmony_cifunction selectHandlerRows(sp: SpSystemTrace, rows: Array<TraceRow<unknown>>): void {
541fb726d48Sopenharmony_ci  let selection = new SelectionParam();
542fb726d48Sopenharmony_ci  selection.traceId = Utils.currentSelectTrace;
543fb726d48Sopenharmony_ci  selection.cpuStateRowsId = sp.stateRowsId;
544fb726d48Sopenharmony_ci  selection.leftNs = TraceRow.rangeSelectObject?.startNS || 0;
545fb726d48Sopenharmony_ci  selection.rightNs = TraceRow.rangeSelectObject?.endNS || 0;
546fb726d48Sopenharmony_ci  selection.recordStartNs = Utils.getInstance().getRecordStartNS(Utils.currentSelectTrace);
547fb726d48Sopenharmony_ci  rows.forEach((it) => {
548fb726d48Sopenharmony_ci    selection.pushSelection(it, sp);
549fb726d48Sopenharmony_ci    if (sp.rangeTraceRow!.length !== rows.length) {
550fb726d48Sopenharmony_ci      let event = sp.createPointEvent(it);
551fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.addOrdinaryVisitAction({
552fb726d48Sopenharmony_ci        action: 'trace_row', // @ts-ignore
553fb726d48Sopenharmony_ci        event: event,
554fb726d48Sopenharmony_ci      });
555fb726d48Sopenharmony_ci    }
556fb726d48Sopenharmony_ci    sp.setParentCheckStatus(it);
557fb726d48Sopenharmony_ci  });
558fb726d48Sopenharmony_ci  if (selection.diskIOipids.length > 0 && !selection.diskIOLatency) {
559fb726d48Sopenharmony_ci    selection.promiseList.push(
560fb726d48Sopenharmony_ci      queryEbpfSamplesCount(
561fb726d48Sopenharmony_ci        TraceRow.rangeSelectObject?.startNS || 0,
562fb726d48Sopenharmony_ci        TraceRow.rangeSelectObject?.endNS || 0,
563fb726d48Sopenharmony_ci        selection.diskIOipids
564fb726d48Sopenharmony_ci      ).then((res) => {
565fb726d48Sopenharmony_ci        if (res.length > 0) {
566fb726d48Sopenharmony_ci          //@ts-ignore
567fb726d48Sopenharmony_ci          selection.fsCount = res[0].fsCount;
568fb726d48Sopenharmony_ci          //@ts-ignore
569fb726d48Sopenharmony_ci          selection.vmCount = res[0].vmCount;
570fb726d48Sopenharmony_ci        }
571fb726d48Sopenharmony_ci        return new Promise((resolve) => resolve(1));
572fb726d48Sopenharmony_ci      })
573fb726d48Sopenharmony_ci    );
574fb726d48Sopenharmony_ci  }
575fb726d48Sopenharmony_ci  sp.rangeTraceRow = rows;
576fb726d48Sopenharmony_ci  sp.isSelectClick = false;
577fb726d48Sopenharmony_ci  sp.selectStructNull();
578fb726d48Sopenharmony_ci  sp.timerShaftEL?.removeTriangle('inverted');
579fb726d48Sopenharmony_ci  if (selection.promiseList.length > 0) {
580fb726d48Sopenharmony_ci    Promise.all(selection.promiseList).then(() => {
581fb726d48Sopenharmony_ci      selection.promiseList = [];
582fb726d48Sopenharmony_ci      sp.traceSheetEL?.rangeSelect(selection);
583fb726d48Sopenharmony_ci    });
584fb726d48Sopenharmony_ci  } else {
585fb726d48Sopenharmony_ci    sp.traceSheetEL?.rangeSelect(selection);
586fb726d48Sopenharmony_ci  }
587fb726d48Sopenharmony_ci  sp.timerShaftEL!.selectionList.push(selection); // 保持选中对象,为后面的再次选中该框选区域做准备。
588fb726d48Sopenharmony_ci  sp.selectionParam = selection;
589fb726d48Sopenharmony_ci  sp.refreshCanvas(true);
590fb726d48Sopenharmony_ci}
591fb726d48Sopenharmony_cifunction resizeObserverHandler(sp: SpSystemTrace): void {
592fb726d48Sopenharmony_ci  // @ts-ignore
593fb726d48Sopenharmony_ci  new ResizeObserver((entries) => {
594fb726d48Sopenharmony_ci    TraceRow.FRAME_WIDTH = sp.clientWidth - 249 - sp.getScrollWidth();
595fb726d48Sopenharmony_ci    requestAnimationFrame(() => {
596fb726d48Sopenharmony_ci      sp.timerShaftEL?.updateWidth(sp.clientWidth - 1 - sp.getScrollWidth());
597fb726d48Sopenharmony_ci      // @ts-ignore
598fb726d48Sopenharmony_ci      sp.shadowRoot!.querySelectorAll<TraceRow<unknown>>('trace-row').forEach((it) => {
599fb726d48Sopenharmony_ci        it.updateWidth(sp.clientWidth);
600fb726d48Sopenharmony_ci      });
601fb726d48Sopenharmony_ci    });
602fb726d48Sopenharmony_ci  }).observe(sp);
603fb726d48Sopenharmony_ci
604fb726d48Sopenharmony_ci  new ResizeObserver((entries) => {
605fb726d48Sopenharmony_ci    sp.canvasPanelConfig();
606fb726d48Sopenharmony_ci    if (sp.traceSheetEL!.getAttribute('mode') === 'hidden') {
607fb726d48Sopenharmony_ci      sp.timerShaftEL?.removeTriangle('triangle');
608fb726d48Sopenharmony_ci    }
609fb726d48Sopenharmony_ci    if (sp.favoriteChartListEL?.style.display === 'flex') {
610fb726d48Sopenharmony_ci      sp.refreshFavoriteCanvas();
611fb726d48Sopenharmony_ci    }
612fb726d48Sopenharmony_ci    sp.refreshCanvas(true);
613fb726d48Sopenharmony_ci  }).observe(sp.rowsPaneEL!);
614fb726d48Sopenharmony_ci}
615fb726d48Sopenharmony_cifunction mutationObserverHandler(sp: SpSystemTrace): void {
616fb726d48Sopenharmony_ci  new MutationObserver((mutations, observer) => {
617fb726d48Sopenharmony_ci    for (const mutation of mutations) {
618fb726d48Sopenharmony_ci      if (mutation.type === 'attributes') {
619fb726d48Sopenharmony_ci        if (sp.style.visibility === 'visible') {
620fb726d48Sopenharmony_ci          if (TraceRow.rangeSelectObject && SpSystemTrace.sliceRangeMark) {
621fb726d48Sopenharmony_ci            sp.timerShaftEL?.setSlicesMark(
622fb726d48Sopenharmony_ci              TraceRow.rangeSelectObject.startNS || 0,
623fb726d48Sopenharmony_ci              TraceRow.rangeSelectObject.endNS || 0,
624fb726d48Sopenharmony_ci              false
625fb726d48Sopenharmony_ci            );
626fb726d48Sopenharmony_ci            SpSystemTrace.sliceRangeMark = undefined;
627fb726d48Sopenharmony_ci            window.publish(window.SmartEvent.UI.RefreshCanvas, {});
628fb726d48Sopenharmony_ci          }
629fb726d48Sopenharmony_ci        }
630fb726d48Sopenharmony_ci      }
631fb726d48Sopenharmony_ci    }
632fb726d48Sopenharmony_ci  }).observe(sp, {
633fb726d48Sopenharmony_ci    attributes: true,
634fb726d48Sopenharmony_ci    childList: false,
635fb726d48Sopenharmony_ci    subtree: false,
636fb726d48Sopenharmony_ci  });
637fb726d48Sopenharmony_ci}
638fb726d48Sopenharmony_cifunction intersectionObserverHandler(sp: SpSystemTrace): void {
639fb726d48Sopenharmony_ci  sp.intersectionObserver = new IntersectionObserver(
640fb726d48Sopenharmony_ci    (entries) => {
641fb726d48Sopenharmony_ci      entries.forEach((it) => {
642fb726d48Sopenharmony_ci        // @ts-ignore
643fb726d48Sopenharmony_ci        let tr = it.target as TraceRow<unknown>;
644fb726d48Sopenharmony_ci        // 目标元素的可见比例
645fb726d48Sopenharmony_ci        tr.intersectionRatio = it.intersectionRatio;
646fb726d48Sopenharmony_ci        // 判断目标元素是否可见 isIntersecting为true是可见
647fb726d48Sopenharmony_ci        if (!it.isIntersecting) {
648fb726d48Sopenharmony_ci          tr.sleeping = true;
649fb726d48Sopenharmony_ci          sp.invisibleRows.indexOf(tr) === -1 && sp.invisibleRows.push(tr);
650fb726d48Sopenharmony_ci        } else {
651fb726d48Sopenharmony_ci          tr.sleeping = false;
652fb726d48Sopenharmony_ci          sp.visibleRows.indexOf(tr) === -1 && sp.visibleRows.push(tr);
653fb726d48Sopenharmony_ci        }
654fb726d48Sopenharmony_ci      });
655fb726d48Sopenharmony_ci      //更新可见泳道及不可见泳道值
656fb726d48Sopenharmony_ci      sp.visibleRows = sp.visibleRows.filter((it) => !it.sleeping);
657fb726d48Sopenharmony_ci      sp.invisibleRows = sp.invisibleRows.filter((it) => it.sleeping);
658fb726d48Sopenharmony_ci      if (sp.handler === -1) {
659fb726d48Sopenharmony_ci        cancelAnimationFrame(sp.handler);
660fb726d48Sopenharmony_ci      }
661fb726d48Sopenharmony_ci      sp.handler = requestAnimationFrame(() => sp.refreshCanvas(false));
662fb726d48Sopenharmony_ci    },
663fb726d48Sopenharmony_ci    { threshold: [0, 0.01, 0.99, 1] }
664fb726d48Sopenharmony_ci  );
665fb726d48Sopenharmony_ci}
666fb726d48Sopenharmony_cifunction observerHandler(sp: SpSystemTrace): void {
667fb726d48Sopenharmony_ci  resizeObserverHandler(sp);
668fb726d48Sopenharmony_ci  mutationObserverHandler(sp);
669fb726d48Sopenharmony_ci  intersectionObserverHandler(sp);
670fb726d48Sopenharmony_ci}
671fb726d48Sopenharmony_cifunction windowKeyDownHandler(sp: SpSystemTrace): (ev: KeyboardEvent) => void {
672fb726d48Sopenharmony_ci  return function (ev: KeyboardEvent) {
673fb726d48Sopenharmony_ci    if (ev.key.toLocaleLowerCase() === 'escape') {
674fb726d48Sopenharmony_ci      sp.queryAllTraceRow().forEach((it) => {
675fb726d48Sopenharmony_ci        it.checkType = '-1';
676fb726d48Sopenharmony_ci      });
677fb726d48Sopenharmony_ci      TraceRow.rangeSelectObject = undefined;
678fb726d48Sopenharmony_ci      sp.rangeSelect.rangeTraceRow = [];
679fb726d48Sopenharmony_ci      sp.selectStructNull();
680fb726d48Sopenharmony_ci      sp.timerShaftEL?.setSlicesMark();
681fb726d48Sopenharmony_ci      sp.traceSheetEL?.setMode('hidden');
682fb726d48Sopenharmony_ci      sp.removeLinkLinesByBusinessType('janks', 'task');
683fb726d48Sopenharmony_ci    }
684fb726d48Sopenharmony_ci  };
685fb726d48Sopenharmony_ci}
686fb726d48Sopenharmony_cifunction smartEventSubscribe(sp: SpSystemTrace): void {
687fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.SliceMark, (data) => sp.sliceMarkEventHandler(data));
688fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.TraceRowComplete, (tr) => { });
689fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.RefreshCanvas, () => sp.refreshCanvas(false));
690fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.KeyboardEnable, (tr) => {
691fb726d48Sopenharmony_ci    //@ts-ignore
692fb726d48Sopenharmony_ci    sp.keyboardEnable = tr.enable;
693fb726d48Sopenharmony_ci    if (!sp.keyboardEnable) {
694fb726d48Sopenharmony_ci      sp.stopWASD();
695fb726d48Sopenharmony_ci    }
696fb726d48Sopenharmony_ci  }); //@ts-ignore
697fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.CollapseAllLane, (collapse: boolean) => {
698fb726d48Sopenharmony_ci    if (!collapse) {
699fb726d48Sopenharmony_ci      // 一键折叠之前,记录当前打开的泳道图
700fb726d48Sopenharmony_ci      // @ts-ignore
701fb726d48Sopenharmony_ci      sp.expandRowList = Array.from(sp.rowsEL!.querySelectorAll<TraceRow<unknown>>('trace-row[folder][expansion]')) || [];
702fb726d48Sopenharmony_ci    }
703fb726d48Sopenharmony_ci    sp.collapseAll = true;
704fb726d48Sopenharmony_ci    sp.setAttribute('disable', '');
705fb726d48Sopenharmony_ci    sp.expandRowList!.forEach((it) => (it.expansion = collapse));
706fb726d48Sopenharmony_ci    sp.collapseAll = false;
707fb726d48Sopenharmony_ci    sp.removeAttribute('disable');
708fb726d48Sopenharmony_ci    sp.refreshCanvas(true);
709fb726d48Sopenharmony_ci  });
710fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.MouseEventEnable, (tr) => {
711fb726d48Sopenharmony_ci    //@ts-ignore
712fb726d48Sopenharmony_ci    sp.mouseEventEnable = tr.mouseEnable;
713fb726d48Sopenharmony_ci    if (sp.mouseEventEnable) {
714fb726d48Sopenharmony_ci      sp.removeAttribute('disable');
715fb726d48Sopenharmony_ci    } else {
716fb726d48Sopenharmony_ci      sp.setAttribute('disable', '');
717fb726d48Sopenharmony_ci    }
718fb726d48Sopenharmony_ci  }); //@ts-ignore
719fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.CollectGroupChange, (group: string) => (sp.currentCollectGroup = group));
720fb726d48Sopenharmony_ci}
721fb726d48Sopenharmony_ci
722fb726d48Sopenharmony_ciexport function documentInitEvent(sp: SpSystemTrace): void {
723fb726d48Sopenharmony_ci  if (!document) {
724fb726d48Sopenharmony_ci    return;
725fb726d48Sopenharmony_ci  }
726fb726d48Sopenharmony_ci  document.addEventListener('triangle-flag', triangleFlagHandler(sp));
727fb726d48Sopenharmony_ci  document.addEventListener('number_calibration', numberCalibrationHandler(sp));
728fb726d48Sopenharmony_ci  document.addEventListener('flag-change', flagChangeHandler(sp));
729fb726d48Sopenharmony_ci  document.addEventListener('slices-change', slicesChangeHandler(sp));
730fb726d48Sopenharmony_ci  if (sp.timerShaftEL?.collecBtn) {
731fb726d48Sopenharmony_ci    sp.timerShaftEL.collecBtn.onclick = (): void => {
732fb726d48Sopenharmony_ci      if (sp.timerShaftEL!.collecBtn!.hasAttribute('close')) {
733fb726d48Sopenharmony_ci        sp.timerShaftEL!.collecBtn!.removeAttribute('close');
734fb726d48Sopenharmony_ci        sp.favoriteChartListEL?.showCollectArea();
735fb726d48Sopenharmony_ci      } else {
736fb726d48Sopenharmony_ci        sp.timerShaftEL!.collecBtn!.setAttribute('close', '');
737fb726d48Sopenharmony_ci        sp.favoriteChartListEL?.hideCollectArea();
738fb726d48Sopenharmony_ci      }
739fb726d48Sopenharmony_ci    };
740fb726d48Sopenharmony_ci  }
741fb726d48Sopenharmony_ci  document.addEventListener('collect', collectHandler(sp));
742fb726d48Sopenharmony_ci}
743fb726d48Sopenharmony_ci
744fb726d48Sopenharmony_ciexport function spSystemTraceInitElement(sp: SpSystemTrace): void {
745fb726d48Sopenharmony_ci  window.subscribe(window.SmartEvent.UI.LoadFinishFrame, () => sp.drawAllLines());
746fb726d48Sopenharmony_ci  sp.traceSheetEL = sp.shadowRoot?.querySelector<TraceSheet>('.trace-sheet');
747fb726d48Sopenharmony_ci  if (!sp || !sp.shadowRoot || !sp.traceSheetEL) {
748fb726d48Sopenharmony_ci    return;
749fb726d48Sopenharmony_ci  }
750fb726d48Sopenharmony_ci  let rightButton: HTMLElement | null | undefined = sp.traceSheetEL.shadowRoot
751fb726d48Sopenharmony_ci    ?.querySelector('#current-selection > tabpane-current-selection')
752fb726d48Sopenharmony_ci    ?.shadowRoot?.querySelector('#rightButton');
753fb726d48Sopenharmony_ci  let rightStar: HTMLElement | null | undefined = sp.traceSheetEL.shadowRoot
754fb726d48Sopenharmony_ci    ?.querySelector('#current-selection > tabpane-current-selection')
755fb726d48Sopenharmony_ci    ?.shadowRoot?.querySelector('#right-star');
756fb726d48Sopenharmony_ci  sp.tipEL = sp.shadowRoot.querySelector<HTMLDivElement>('.tip');
757fb726d48Sopenharmony_ci  sp.rowsPaneEL = sp.shadowRoot.querySelector<HTMLDivElement>('.rows-pane');
758fb726d48Sopenharmony_ci  sp.rowsEL = sp.rowsPaneEL;
759fb726d48Sopenharmony_ci  sp.spacerEL = sp.shadowRoot.querySelector<HTMLDivElement>('.spacer');
760fb726d48Sopenharmony_ci  sp.timerShaftEL = sp.shadowRoot.querySelector<TimerShaftElement>('.timer-shaft');
761fb726d48Sopenharmony_ci  sp.favoriteChartListEL = sp.shadowRoot.querySelector<SpChartList>('#favorite-chart-list');
762fb726d48Sopenharmony_ci  if (!sp.traceSheetEL.shadowRoot) {
763fb726d48Sopenharmony_ci    return;
764fb726d48Sopenharmony_ci  }
765fb726d48Sopenharmony_ci  sp.tabCpuFreq = sp.traceSheetEL.shadowRoot.querySelector<TabPaneFrequencySample>('tabpane-frequency-sample');
766fb726d48Sopenharmony_ci  sp.tabCpuState = sp.traceSheetEL.shadowRoot.querySelector<TabPaneCounterSample>('tabpane-counter-sample');
767fb726d48Sopenharmony_ci  sp.wakeupListTbl = sp.traceSheetEL.shadowRoot?.querySelector('#current-selection > tabpane-current-selection')?.
768fb726d48Sopenharmony_ci    shadowRoot?.querySelector('#wakeupListTbl');
769fb726d48Sopenharmony_ci  sp.rangeSelect = new RangeSelect(sp);
770fb726d48Sopenharmony_ci  // @ts-ignore
771fb726d48Sopenharmony_ci  rightButton?.addEventListener('click', rightButtonOnClick(sp, rightStar));
772fb726d48Sopenharmony_ci  rightStar?.addEventListener('click', rightStarOnClick(sp));
773fb726d48Sopenharmony_ci  documentInitEvent(sp);
774fb726d48Sopenharmony_ci  SpSystemTrace.scrollViewWidth = sp.getScrollWidth();
775fb726d48Sopenharmony_ci  selectHandler(sp);
776fb726d48Sopenharmony_ci  observerHandler(sp);
777fb726d48Sopenharmony_ci  window.addEventListener('keydown', windowKeyDownHandler(sp));
778fb726d48Sopenharmony_ci  sp.chartManager = new SpChartManager(sp);
779fb726d48Sopenharmony_ci  sp.canvasPanel = sp.shadowRoot.querySelector<HTMLCanvasElement>('#canvas-panel')!;
780fb726d48Sopenharmony_ci  sp.canvasPanelCtx = sp.canvasPanel.getContext('2d');
781fb726d48Sopenharmony_ci  sp.canvasFavoritePanelCtx = sp.favoriteChartListEL!.context();
782fb726d48Sopenharmony_ci  sp.canvasPanelConfig();
783fb726d48Sopenharmony_ci  smartEventSubscribe(sp);
784fb726d48Sopenharmony_ci}
785fb726d48Sopenharmony_ci
786fb726d48Sopenharmony_cifunction moveRangeToCenterAndHighlight(sp: SpSystemTrace, findEntry: unknown, currentEntry: unknown): void {
787fb726d48Sopenharmony_ci  if (findEntry) {
788fb726d48Sopenharmony_ci    //findEntry不在range范围内,会把它移动到泳道最左侧
789fb726d48Sopenharmony_ci    // @ts-ignore
790fb726d48Sopenharmony_ci    if (findEntry.startTime + findEntry.dur > TraceRow.range!.endNS || findEntry.startTime < TraceRow.range!.startNS) {
791fb726d48Sopenharmony_ci      // @ts-ignore
792fb726d48Sopenharmony_ci      sp.moveRangeToLeft(findEntry.startTime!, findEntry.dur!);
793fb726d48Sopenharmony_ci    }
794fb726d48Sopenharmony_ci    cancelCurrentTraceRowHighlight(sp, currentEntry);
795fb726d48Sopenharmony_ci    // @ts-ignore
796fb726d48Sopenharmony_ci    if (findEntry.type === 'cpu') {
797fb726d48Sopenharmony_ci      findEntryTypeCpu(sp, findEntry);
798fb726d48Sopenharmony_ci      // @ts-ignore
799fb726d48Sopenharmony_ci    } else if (findEntry.type === 'func') {
800fb726d48Sopenharmony_ci      findEntryTypeFunc(sp, findEntry);
801fb726d48Sopenharmony_ci      // @ts-ignore
802fb726d48Sopenharmony_ci    } else if (findEntry.type === 'thread||process') {
803fb726d48Sopenharmony_ci      findEntryTypeThreadProcess(sp, findEntry);
804fb726d48Sopenharmony_ci      // @ts-ignore
805fb726d48Sopenharmony_ci    } else if (findEntry.type === 'sdk') {
806fb726d48Sopenharmony_ci      findEntryTypeSdk(sp, findEntry);
807fb726d48Sopenharmony_ci    }
808fb726d48Sopenharmony_ci  }
809fb726d48Sopenharmony_ci}
810fb726d48Sopenharmony_ci
811fb726d48Sopenharmony_ciexport function cancelCurrentTraceRowHighlight(sp: SpSystemTrace, currentEntry: unknown): void {
812fb726d48Sopenharmony_ci  // @ts-ignore
813fb726d48Sopenharmony_ci  if (currentEntry?.type === 'cpu') {
814fb726d48Sopenharmony_ci    // @ts-ignore
815fb726d48Sopenharmony_ci    sp.queryAllTraceRow(`trace-row[row-type='cpu-data'][row-id='${currentEntry.cpu}']`,
816fb726d48Sopenharmony_ci      // @ts-ignore
817fb726d48Sopenharmony_ci      (row) => row.rowType === 'cpu-data' && row.rowId === `${currentEntry.cpu}`)[0].highlight = false;
818fb726d48Sopenharmony_ci    // @ts-ignore
819fb726d48Sopenharmony_ci  } else if (currentEntry?.type === 'func') {
820fb726d48Sopenharmony_ci    // @ts-ignore
821fb726d48Sopenharmony_ci    let funId = (currentEntry.rowId === null || currentEntry.rowId === undefined) ? `${currentEntry.funName}-${currentEntry.pid}` : currentEntry.rowId;
822fb726d48Sopenharmony_ci    // @ts-ignore
823fb726d48Sopenharmony_ci    let funcRowID = (currentEntry.cookie === null || currentEntry.cookie === undefined) ? `${Utils.getDistributedRowId(currentEntry.tid)}` : funId;
824fb726d48Sopenharmony_ci    // @ts-ignore
825fb726d48Sopenharmony_ci    let parentRow = sp.queryAllTraceRow(`trace-row[row-id='${Utils.getDistributedRowId(currentEntry.pid)}'][folder]`,
826fb726d48Sopenharmony_ci      // @ts-ignore
827fb726d48Sopenharmony_ci      (row) => row.rowId === `trace-row[row-id='${Utils.getDistributedRowId(currentEntry.pid)}'][folder]`)[0];
828fb726d48Sopenharmony_ci    if (!parentRow) {
829fb726d48Sopenharmony_ci      return;
830fb726d48Sopenharmony_ci    }
831fb726d48Sopenharmony_ci    let filterRow = parentRow.childrenList.filter((child) => child.rowId === funcRowID && child.rowType === 'func')[0];
832fb726d48Sopenharmony_ci    filterRow.highlight = false;
833fb726d48Sopenharmony_ci    // @ts-ignore
834fb726d48Sopenharmony_ci  } else if (currentEntry?.type === 'sdk') {
835fb726d48Sopenharmony_ci    // @ts-ignore
836fb726d48Sopenharmony_ci    let parentRow = sp.shadowRoot!.querySelector<TraceRow<unknown>>("trace-row[row-type='sdk'][folder]");
837fb726d48Sopenharmony_ci    if (parentRow) {
838fb726d48Sopenharmony_ci      let sdkRow = parentRow.childrenList.filter(
839fb726d48Sopenharmony_ci        // @ts-ignore
840fb726d48Sopenharmony_ci        (child) => child.rowId === currentEntry.rowId && child.rowType === currentEntry.rowType
841fb726d48Sopenharmony_ci      )[0];
842fb726d48Sopenharmony_ci      sdkRow!.highlight = false;
843fb726d48Sopenharmony_ci    }
844fb726d48Sopenharmony_ci  }
845fb726d48Sopenharmony_ci}
846fb726d48Sopenharmony_ci
847fb726d48Sopenharmony_ciexport function spSystemTraceShowStruct(
848fb726d48Sopenharmony_ci  sp: SpSystemTrace,
849fb726d48Sopenharmony_ci  previous: boolean,
850fb726d48Sopenharmony_ci  currentIndex: number,
851fb726d48Sopenharmony_ci  structs: Array<unknown>,
852fb726d48Sopenharmony_ci  retargetIndex?: number
853fb726d48Sopenharmony_ci): number {
854fb726d48Sopenharmony_ci  if (structs.length === 0) {
855fb726d48Sopenharmony_ci    return 0;
856fb726d48Sopenharmony_ci  }
857fb726d48Sopenharmony_ci  let findIndex = spSystemTraceShowStructFindIndex(previous, currentIndex, structs, retargetIndex);
858fb726d48Sopenharmony_ci  let findEntry: unknown = structs[findIndex];
859fb726d48Sopenharmony_ci  let currentEntry: unknown = undefined;
860fb726d48Sopenharmony_ci  if (currentIndex >= 0) {
861fb726d48Sopenharmony_ci    currentEntry = structs[currentIndex];
862fb726d48Sopenharmony_ci  }
863fb726d48Sopenharmony_ci  moveRangeToCenterAndHighlight(sp, findEntry, currentEntry);
864fb726d48Sopenharmony_ci  return findIndex;
865fb726d48Sopenharmony_ci}
866fb726d48Sopenharmony_ci
867fb726d48Sopenharmony_cifunction spSystemTraceShowStructFindIndex(
868fb726d48Sopenharmony_ci  previous: boolean,
869fb726d48Sopenharmony_ci  currentIndex: number,
870fb726d48Sopenharmony_ci  structs: Array<unknown>,
871fb726d48Sopenharmony_ci  retargetIndex: number | undefined
872fb726d48Sopenharmony_ci): number {
873fb726d48Sopenharmony_ci  const rangeStart = TraceRow.range!.startNS;
874fb726d48Sopenharmony_ci  const rangeEnd = TraceRow.range!.endNS;
875fb726d48Sopenharmony_ci  let findIndex = -1;
876fb726d48Sopenharmony_ci  if (retargetIndex) {//如果Go有值,直接跳转
877fb726d48Sopenharmony_ci    findIndex = retargetIndex - 1;
878fb726d48Sopenharmony_ci  } else if (previous) {
879fb726d48Sopenharmony_ci    //case1:current.start在start边界以右,需要从当前项往第一项遍历,找到structs[index].start < end
880fb726d48Sopenharmony_ci    //@ts-ignore
881fb726d48Sopenharmony_ci    if (structs[currentIndex].startTime! >= Math.round(rangeStart)) {
882fb726d48Sopenharmony_ci      findIndex = findPreviousOne(currentIndex - 1, 0, structs);
883fb726d48Sopenharmony_ci      //处理当前项如果是第一项
884fb726d48Sopenharmony_ci      findIndex = findIndex === -1 ? structs.length - 1 : findIndex;
885fb726d48Sopenharmony_ci    } else {
886fb726d48Sopenharmony_ci      //case2:current.start在start边界以左,需要从最后一项到当前项遍历,找到structs[index].start < end
887fb726d48Sopenharmony_ci      findIndex = findPreviousOne(structs.length - 1, currentIndex + 1, structs);
888fb726d48Sopenharmony_ci    }
889fb726d48Sopenharmony_ci  } else {//向后查找
890fb726d48Sopenharmony_ci    if (currentIndex === -1) {//输入框内输入内容后第一次搜索
891fb726d48Sopenharmony_ci      findIndex = findNextOne(0, structs.length - 1, structs);
892fb726d48Sopenharmony_ci      //处理当所有的项都在start以左
893fb726d48Sopenharmony_ci      return findIndex === -1 ? 0 : findIndex;
894fb726d48Sopenharmony_ci    }
895fb726d48Sopenharmony_ci    //case1:current.start 在end左侧  从当前项到最后一项遍历,找到startTime>start
896fb726d48Sopenharmony_ci    //@ts-ignore
897fb726d48Sopenharmony_ci    if (structs[currentIndex].startTime! < Math.round(rangeEnd)) {//case1
898fb726d48Sopenharmony_ci      findIndex = findNextOne(currentIndex + 1, structs.length - 1, structs);
899fb726d48Sopenharmony_ci      //处理当前项是最后一项
900fb726d48Sopenharmony_ci      findIndex = findIndex === -1 ? 0 : findIndex;
901fb726d48Sopenharmony_ci    } else {
902fb726d48Sopenharmony_ci      //case2: current.start 在end右侧  从第一项到当前项遍历,找到startTime>start
903fb726d48Sopenharmony_ci      findIndex = findNextOne(0, currentIndex - 1, structs);
904fb726d48Sopenharmony_ci    }
905fb726d48Sopenharmony_ci  }
906fb726d48Sopenharmony_ci  return findIndex;
907fb726d48Sopenharmony_ci}
908fb726d48Sopenharmony_ci//向前查找逻辑
909fb726d48Sopenharmony_cifunction findPreviousOne(start: number, end: number, structs: Array<unknown>): number {
910fb726d48Sopenharmony_ci  let findIndex = -1;
911fb726d48Sopenharmony_ci  const rangeEnd = TraceRow.range!.endNS;
912fb726d48Sopenharmony_ci  for (let i = start; i >= end; i--) {
913fb726d48Sopenharmony_ci    let it = structs[i];
914fb726d48Sopenharmony_ci    //@ts-ignore
915fb726d48Sopenharmony_ci    if (it.startTime! < rangeEnd) {
916fb726d48Sopenharmony_ci      findIndex = i;
917fb726d48Sopenharmony_ci      break;
918fb726d48Sopenharmony_ci    }
919fb726d48Sopenharmony_ci  }
920fb726d48Sopenharmony_ci  return findIndex;
921fb726d48Sopenharmony_ci}
922fb726d48Sopenharmony_ci//向后查找
923fb726d48Sopenharmony_cifunction findNextOne(start: number, end: number, structs: Array<unknown>): number {
924fb726d48Sopenharmony_ci  let findIndex = -1;
925fb726d48Sopenharmony_ci  const rangeStart = TraceRow.range!.startNS;
926fb726d48Sopenharmony_ci  for (let i = start; i <= end; i++) {
927fb726d48Sopenharmony_ci    let it = structs[i];
928fb726d48Sopenharmony_ci    //@ts-ignore
929fb726d48Sopenharmony_ci    if (it.startTime > rangeStart) {
930fb726d48Sopenharmony_ci      findIndex = i;
931fb726d48Sopenharmony_ci      break;
932fb726d48Sopenharmony_ci    }
933fb726d48Sopenharmony_ci  }
934fb726d48Sopenharmony_ci  return findIndex;
935fb726d48Sopenharmony_ci}
936fb726d48Sopenharmony_cifunction findEntryTypeCpu(sp: SpSystemTrace, findEntry: unknown): void {
937fb726d48Sopenharmony_ci  // @ts-ignore
938fb726d48Sopenharmony_ci  CpuStruct.selectCpuStruct = findEntry;
939fb726d48Sopenharmony_ci  CpuStruct.hoverCpuStruct = CpuStruct.selectCpuStruct;
940fb726d48Sopenharmony_ci  sp.queryAllTraceRow(`trace-row[row-type='cpu-data']`, (row): boolean => row.rowType === 'cpu-data').forEach(
941fb726d48Sopenharmony_ci    (item): void => {
942fb726d48Sopenharmony_ci      // @ts-ignore
943fb726d48Sopenharmony_ci      if (item.rowId === `${Utils.getDistributedRowId(findEntry.cpu)}`) {
944fb726d48Sopenharmony_ci        sp.rechargeCpuData(
945fb726d48Sopenharmony_ci          // @ts-ignore
946fb726d48Sopenharmony_ci          findEntry, // @ts-ignore
947fb726d48Sopenharmony_ci          item.dataListCache.find((it) => it.startTime > findEntry.startTime)
948fb726d48Sopenharmony_ci        );
949fb726d48Sopenharmony_ci        let _findEntry = JSON.parse(JSON.stringify(findEntry));
950fb726d48Sopenharmony_ci        _findEntry.type = 'thread';
951fb726d48Sopenharmony_ci        item.fixedList = [_findEntry];
952fb726d48Sopenharmony_ci      }
953fb726d48Sopenharmony_ci      // @ts-ignore
954fb726d48Sopenharmony_ci      item.highlight = item.rowId === `${Utils.getDistributedRowId(findEntry.cpu)}`;
955fb726d48Sopenharmony_ci      item.draw(true);
956fb726d48Sopenharmony_ci    }
957fb726d48Sopenharmony_ci  );
958fb726d48Sopenharmony_ci  // @ts-ignore
959fb726d48Sopenharmony_ci  sp.scrollToProcess(`${findEntry.cpu}`, '', 'cpu-data', true);
960fb726d48Sopenharmony_ci  sp.onClickHandler(TraceRow.ROW_TYPE_CPU);
961fb726d48Sopenharmony_ci}
962fb726d48Sopenharmony_cifunction findEntryTypeFunc(sp: SpSystemTrace, findEntry: unknown): void {
963fb726d48Sopenharmony_ci  sp.observerScrollHeightEnable = true;
964fb726d48Sopenharmony_ci  sp.scrollToActFunc(
965fb726d48Sopenharmony_ci    {
966fb726d48Sopenharmony_ci      // @ts-ignore
967fb726d48Sopenharmony_ci      startTs: findEntry.startTime,
968fb726d48Sopenharmony_ci      // @ts-ignore
969fb726d48Sopenharmony_ci      dur: findEntry.dur,
970fb726d48Sopenharmony_ci      // @ts-ignore
971fb726d48Sopenharmony_ci      tid: findEntry.tid,
972fb726d48Sopenharmony_ci      // @ts-ignore
973fb726d48Sopenharmony_ci      pid: findEntry.pid,
974fb726d48Sopenharmony_ci      // @ts-ignore
975fb726d48Sopenharmony_ci      depth: findEntry.depth,
976fb726d48Sopenharmony_ci      // @ts-ignore
977fb726d48Sopenharmony_ci      argsetid: findEntry.argsetid,
978fb726d48Sopenharmony_ci      // @ts-ignore
979fb726d48Sopenharmony_ci      funName: findEntry.funName,
980fb726d48Sopenharmony_ci      // @ts-ignore
981fb726d48Sopenharmony_ci      cookie: findEntry.cookie,
982fb726d48Sopenharmony_ci      // @ts-ignore
983fb726d48Sopenharmony_ci      //因异步trace分类出的rowId类型有三种,故新增row_id字段,该字段为异步方法的对应的rowId,支持搜索查询定位到该方法属于那个row,只有缓存的异步trace数据中含该字段
984fb726d48Sopenharmony_ci      row_id: findEntry.rowId ? findEntry.rowId : null,
985fb726d48Sopenharmony_ci    },
986fb726d48Sopenharmony_ci    true
987fb726d48Sopenharmony_ci  );
988fb726d48Sopenharmony_ci}
989fb726d48Sopenharmony_cifunction findEntryTypeThreadProcess(sp: SpSystemTrace, findEntry: unknown): void {
990fb726d48Sopenharmony_ci  let threadProcessRow = sp.rowsEL?.querySelectorAll<TraceRow<ThreadStruct>>('trace-row')[0];
991fb726d48Sopenharmony_ci  if (threadProcessRow) {
992fb726d48Sopenharmony_ci    let filterRow = threadProcessRow.childrenList.filter(
993fb726d48Sopenharmony_ci      // @ts-ignore
994fb726d48Sopenharmony_ci      (row) => row.rowId === Utils.getDistributedRowId(findEntry.rowId) && row.rowId === findEntry.rowType
995fb726d48Sopenharmony_ci    )[0];
996fb726d48Sopenharmony_ci    filterRow!.highlight = true;
997fb726d48Sopenharmony_ci    // @ts-ignore
998fb726d48Sopenharmony_ci    sp.closeAllExpandRows(Utils.getDistributedRowId(findEntry.rowParentId));
999fb726d48Sopenharmony_ci    // @ts-ignore
1000fb726d48Sopenharmony_ci    sp.scrollToProcess(`${findEntry.rowId}`, `${findEntry.rowParentId}`, findEntry.rowType, true);
1001fb726d48Sopenharmony_ci    let completeEntry = (): void => {
1002fb726d48Sopenharmony_ci      sp.hoverStructNull();
1003fb726d48Sopenharmony_ci      sp.selectStructNull();
1004fb726d48Sopenharmony_ci      sp.wakeupListNull();
1005fb726d48Sopenharmony_ci      // @ts-ignore
1006fb726d48Sopenharmony_ci      sp.scrollToProcess(`${findEntry.rowId}`, `${findEntry.rowParentId}`, findEntry.rowType, true);
1007fb726d48Sopenharmony_ci    };
1008fb726d48Sopenharmony_ci    if (filterRow!.isComplete) {
1009fb726d48Sopenharmony_ci      completeEntry();
1010fb726d48Sopenharmony_ci    } else {
1011fb726d48Sopenharmony_ci      filterRow!.onComplete = completeEntry;
1012fb726d48Sopenharmony_ci    }
1013fb726d48Sopenharmony_ci  }
1014fb726d48Sopenharmony_ci}
1015fb726d48Sopenharmony_cifunction findEntryTypeSdk(sp: SpSystemTrace, findEntry: unknown): void {
1016fb726d48Sopenharmony_ci  // @ts-ignore
1017fb726d48Sopenharmony_ci  let parentRow = sp.shadowRoot!.querySelector<TraceRow<unknown>>(`trace-row[row-type='sdk'][folder]`);
1018fb726d48Sopenharmony_ci  if (parentRow) {
1019fb726d48Sopenharmony_ci    let sdkRow = parentRow.childrenList.filter(
1020fb726d48Sopenharmony_ci      // @ts-ignore
1021fb726d48Sopenharmony_ci      (child) => child.rowId === findEntry.rowId && child.rowType === findEntry.rowType
1022fb726d48Sopenharmony_ci    )[0];
1023fb726d48Sopenharmony_ci    sdkRow!.highlight = true;
1024fb726d48Sopenharmony_ci  }
1025fb726d48Sopenharmony_ci  sp.hoverStructNull();
1026fb726d48Sopenharmony_ci  sp.selectStructNull();
1027fb726d48Sopenharmony_ci  sp.wakeupListNull();
1028fb726d48Sopenharmony_ci  // @ts-ignore
1029fb726d48Sopenharmony_ci  sp.onClickHandler(findEntry.rowType!);
1030fb726d48Sopenharmony_ci  // @ts-ignore
1031fb726d48Sopenharmony_ci  sp.closeAllExpandRows(findEntry.rowParentId);
1032fb726d48Sopenharmony_ci  // @ts-ignore
1033fb726d48Sopenharmony_ci  sp.scrollToProcess(`${findEntry.rowId}`, `${findEntry.rowParentId}`, findEntry.rowType, true);
1034fb726d48Sopenharmony_ci}
1035fb726d48Sopenharmony_ciasync function spSystemTraceInitBuffer(
1036fb726d48Sopenharmony_ci  sp: SpSystemTrace,
1037fb726d48Sopenharmony_ci  param: { buf?: ArrayBuffer; Url?: string; buf2?: ArrayBuffer },
1038fb726d48Sopenharmony_ci  wasmConfigUri: string,
1039fb726d48Sopenharmony_ci  progress: Function
1040fb726d48Sopenharmony_ci): Promise<{
1041fb726d48Sopenharmony_ci  status: boolean;
1042fb726d48Sopenharmony_ci  msg: string;
1043fb726d48Sopenharmony_ci} | null> {
1044fb726d48Sopenharmony_ci  if (param.buf) {
1045fb726d48Sopenharmony_ci    let configJson = '';
1046fb726d48Sopenharmony_ci    try {
1047fb726d48Sopenharmony_ci      configJson = await fetch(wasmConfigUri).then((res) => res.text());
1048fb726d48Sopenharmony_ci    } catch (e) {
1049fb726d48Sopenharmony_ci      error('getWasmConfigFailed', e);
1050fb726d48Sopenharmony_ci    }
1051fb726d48Sopenharmony_ci    let parseConfig = FlagsConfig.getSpTraceStreamParseConfig();
1052fb726d48Sopenharmony_ci    let { status, msg, sdkConfigMap } = await threadPool.initSqlite(param.buf, parseConfig, configJson, progress);
1053fb726d48Sopenharmony_ci    if (!status) {
1054fb726d48Sopenharmony_ci      return { status: false, msg: msg };
1055fb726d48Sopenharmony_ci    }
1056fb726d48Sopenharmony_ci    SpSystemTrace.SDK_CONFIG_MAP = sdkConfigMap;
1057fb726d48Sopenharmony_ci    if (param.buf2) {
1058fb726d48Sopenharmony_ci      let { status, msg } = await threadPool2.initSqlite(param.buf2, parseConfig, configJson, progress);
1059fb726d48Sopenharmony_ci      if (!status) {
1060fb726d48Sopenharmony_ci        return { status: false, msg: msg };
1061fb726d48Sopenharmony_ci      }
1062fb726d48Sopenharmony_ci    }
1063fb726d48Sopenharmony_ci    return null;
1064fb726d48Sopenharmony_ci  } else {
1065fb726d48Sopenharmony_ci    return null;
1066fb726d48Sopenharmony_ci  }
1067fb726d48Sopenharmony_ci}
1068fb726d48Sopenharmony_ciasync function spSystemTraceInitUrl(
1069fb726d48Sopenharmony_ci  sp: SpSystemTrace,
1070fb726d48Sopenharmony_ci  param: { buf?: ArrayBuffer; url?: string },
1071fb726d48Sopenharmony_ci  wasmConfigUri: string,
1072fb726d48Sopenharmony_ci  progress: Function
1073fb726d48Sopenharmony_ci): Promise<{
1074fb726d48Sopenharmony_ci  status: boolean;
1075fb726d48Sopenharmony_ci  msg: string;
1076fb726d48Sopenharmony_ci} | null> {
1077fb726d48Sopenharmony_ci  if (param.url) {
1078fb726d48Sopenharmony_ci    let { status, msg } = await threadPool.initServer(param.url, progress);
1079fb726d48Sopenharmony_ci    if (!status) {
1080fb726d48Sopenharmony_ci      return { status: false, msg: msg };
1081fb726d48Sopenharmony_ci    } else {
1082fb726d48Sopenharmony_ci      return null;
1083fb726d48Sopenharmony_ci    }
1084fb726d48Sopenharmony_ci  } else {
1085fb726d48Sopenharmony_ci    return null;
1086fb726d48Sopenharmony_ci  }
1087fb726d48Sopenharmony_ci}
1088fb726d48Sopenharmony_ciexport async function spSystemTraceInit(
1089fb726d48Sopenharmony_ci  sp: SpSystemTrace,
1090fb726d48Sopenharmony_ci  param: { buf?: ArrayBuffer; url?: string; buf2?: ArrayBuffer; fileName1?: string; fileName2?: string },
1091fb726d48Sopenharmony_ci  wasmConfigUri: string,
1092fb726d48Sopenharmony_ci  progress: Function,
1093fb726d48Sopenharmony_ci  isDistributed: boolean
1094fb726d48Sopenharmony_ci): Promise<unknown> {
1095fb726d48Sopenharmony_ci  progress('Load database', 6);
1096fb726d48Sopenharmony_ci  sp.rowsPaneEL!.scroll({ top: 0, left: 0 });
1097fb726d48Sopenharmony_ci  let rsBuf = await spSystemTraceInitBuffer(sp, param, wasmConfigUri, progress);
1098fb726d48Sopenharmony_ci  if (rsBuf) {
1099fb726d48Sopenharmony_ci    return rsBuf;
1100fb726d48Sopenharmony_ci  }
1101fb726d48Sopenharmony_ci  let rsUrl = await spSystemTraceInitUrl(sp, param, wasmConfigUri, progress);
1102fb726d48Sopenharmony_ci  if (rsUrl) {
1103fb726d48Sopenharmony_ci    return rsUrl;
1104fb726d48Sopenharmony_ci  }
1105fb726d48Sopenharmony_ci  if (isDistributed) {
1106fb726d48Sopenharmony_ci    await sp.chartManager?.initDistributedChart(progress, param.fileName1 || 'Trace 1', param.fileName2 || 'Trace 2');
1107fb726d48Sopenharmony_ci  } else {
1108fb726d48Sopenharmony_ci    await sp.chartManager?.init(progress);
1109fb726d48Sopenharmony_ci  }
1110fb726d48Sopenharmony_ci  let rowId: string = '';
1111fb726d48Sopenharmony_ci  // @ts-ignore
1112fb726d48Sopenharmony_ci  sp.rowsEL?.querySelectorAll<TraceRow<unknown>>('trace-row').forEach((it) => {
1113fb726d48Sopenharmony_ci    if (it.name.includes('Ark Ts')) {
1114fb726d48Sopenharmony_ci      rowId = it.rowId!;
1115fb726d48Sopenharmony_ci    }
1116fb726d48Sopenharmony_ci    if (it.folder) {
1117fb726d48Sopenharmony_ci      it.addEventListener('expansion-change', sp.extracted(it));
1118fb726d48Sopenharmony_ci    }
1119fb726d48Sopenharmony_ci  });
1120fb726d48Sopenharmony_ci  progress('completed', 100);
1121fb726d48Sopenharmony_ci  info('All TraceRow Data initialized');
1122fb726d48Sopenharmony_ci  sp.loadTraceCompleted = true;
1123fb726d48Sopenharmony_ci  // @ts-ignore
1124fb726d48Sopenharmony_ci  sp.rowsEL!.querySelectorAll<TraceRow<unknown>>('trace-row').forEach((it) => {
1125fb726d48Sopenharmony_ci    if (rowId !== '' && (it.rowId?.includes(rowId) || it.name.includes(rowId))) {
1126fb726d48Sopenharmony_ci      it.addTemplateTypes('Ark Ts');
1127fb726d48Sopenharmony_ci      for (let child of it.childrenList) {
1128fb726d48Sopenharmony_ci        child.addTemplateTypes('Ark Ts');
1129fb726d48Sopenharmony_ci      }
1130fb726d48Sopenharmony_ci    }
1131fb726d48Sopenharmony_ci    if (it.folder) {
1132fb726d48Sopenharmony_ci      let offsetYTimeOut: unknown = undefined;
1133fb726d48Sopenharmony_ci      it.addEventListener('expansion-change', expansionChangeHandler(sp, offsetYTimeOut));
1134fb726d48Sopenharmony_ci    }
1135fb726d48Sopenharmony_ci    if (sp.loadTraceCompleted) {
1136fb726d48Sopenharmony_ci      sp.traceSheetEL?.displaySystemLogsData();
1137fb726d48Sopenharmony_ci      sp.traceSheetEL?.displayHangsData();
1138fb726d48Sopenharmony_ci      sp.traceSheetEL?.displaySystemStatesData();
1139fb726d48Sopenharmony_ci    }
1140fb726d48Sopenharmony_ci    // 如果有render_service进程,查询该进程下对应泳道的方法存起来,以便框选时直接使用
1141fb726d48Sopenharmony_ci    if (it.getAttribute('name')?.includes('render_service') && it.getAttribute('row-type') === 'process') {
1142fb726d48Sopenharmony_ci      queryRowsData(sp, it.childrenList);
1143fb726d48Sopenharmony_ci    }
1144fb726d48Sopenharmony_ci    sp.intersectionObserver?.observe(it);
1145fb726d48Sopenharmony_ci  });
1146fb726d48Sopenharmony_ci  // trace文件加载完毕,将动效json文件读取并存入缓存
1147fb726d48Sopenharmony_ci  let funDetailUrl = `https://${window.location.host.split(':')[0]}:${window.location.port
1148fb726d48Sopenharmony_ci    }/application/doc/funDetail.json`;
1149fb726d48Sopenharmony_ci  let xhr = new XMLHttpRequest();
1150fb726d48Sopenharmony_ci  // 创建XMLHttpRequest对象
1151fb726d48Sopenharmony_ci  xhr.open('GET', funDetailUrl);
1152fb726d48Sopenharmony_ci  xhr.onreadystatechange = function (): void {
1153fb726d48Sopenharmony_ci    if (xhr.readyState === 4 && xhr.status === 200) {
1154fb726d48Sopenharmony_ci      let content = xhr.responseText;
1155fb726d48Sopenharmony_ci      caches.open('/funDetail').then((cache) => {
1156fb726d48Sopenharmony_ci        let headers = new Headers();
1157fb726d48Sopenharmony_ci        headers.append('Content-Type', 'application/json');
1158fb726d48Sopenharmony_ci        return cache
1159fb726d48Sopenharmony_ci          .put(
1160fb726d48Sopenharmony_ci            '/funDetail',
1161fb726d48Sopenharmony_ci            new Response(content, {
1162fb726d48Sopenharmony_ci              status: 200,
1163fb726d48Sopenharmony_ci              headers,
1164fb726d48Sopenharmony_ci            })
1165fb726d48Sopenharmony_ci          )
1166fb726d48Sopenharmony_ci          .then();
1167fb726d48Sopenharmony_ci      });
1168fb726d48Sopenharmony_ci    }
1169fb726d48Sopenharmony_ci  };
1170fb726d48Sopenharmony_ci  xhr.send(); // 发送请求
1171fb726d48Sopenharmony_ci  return { status: true, msg: 'success' };
1172fb726d48Sopenharmony_ci}
1173fb726d48Sopenharmony_cifunction expansionChangeHandler(sp: SpSystemTrace, offsetYTimeOut: unknown): (event: unknown) => void {
1174fb726d48Sopenharmony_ci  return function (event: unknown) {
1175fb726d48Sopenharmony_ci    sp.scrollH = sp.rowsPaneEL!.scrollHeight;
1176fb726d48Sopenharmony_ci    let max = [...sp.rowsPaneEL!.querySelectorAll('trace-row')].reduce((pre, cur) => pre + cur.clientHeight!, 0);
1177fb726d48Sopenharmony_ci    let offset = sp.rowsPaneEL!.scrollHeight - max;
1178fb726d48Sopenharmony_ci    sp.rowsPaneEL!.scrollTop = sp.rowsPaneEL!.scrollTop - offset;
1179fb726d48Sopenharmony_ci    JankStruct.delJankLineFlag = false;
1180fb726d48Sopenharmony_ci    if (offsetYTimeOut) {
1181fb726d48Sopenharmony_ci      // @ts-ignore
1182fb726d48Sopenharmony_ci      clearTimeout(offsetYTimeOut);
1183fb726d48Sopenharmony_ci    }
1184fb726d48Sopenharmony_ci    // @ts-ignore
1185fb726d48Sopenharmony_ci    if (event.detail.expansion) {
1186fb726d48Sopenharmony_ci      offsetYTimeOut = setTimeout(() => {
1187fb726d48Sopenharmony_ci        sp.linkNodes.forEach((linkNode) => {
1188fb726d48Sopenharmony_ci          JankStruct.selectJankStructList?.forEach((selectStruct: unknown) => {
1189fb726d48Sopenharmony_ci            // @ts-ignore
1190fb726d48Sopenharmony_ci            if (event.detail.rowId === selectStruct.pid) {
1191fb726d48Sopenharmony_ci              // @ts-ignore
1192fb726d48Sopenharmony_ci              JankStruct.selectJankStruct = selectStruct;
1193fb726d48Sopenharmony_ci              // @ts-ignore
1194fb726d48Sopenharmony_ci              JankStruct.hoverJankStruct = selectStruct;
1195fb726d48Sopenharmony_ci            }
1196fb726d48Sopenharmony_ci          });
1197fb726d48Sopenharmony_ci          linkNodeHandler(linkNode, sp);
1198fb726d48Sopenharmony_ci        });
1199fb726d48Sopenharmony_ci      }, 300);
1200fb726d48Sopenharmony_ci    } else {
1201fb726d48Sopenharmony_ci      if (JankStruct!.selectJankStruct) {
1202fb726d48Sopenharmony_ci        JankStruct.selectJankStructList?.push(<JankStruct>JankStruct!.selectJankStruct);
1203fb726d48Sopenharmony_ci      }
1204fb726d48Sopenharmony_ci      offsetYTimeOut = setTimeout(() => {
1205fb726d48Sopenharmony_ci        sp.linkNodes?.forEach((linkNode) => linkNodeHandler(linkNode, sp));
1206fb726d48Sopenharmony_ci      }, 300);
1207fb726d48Sopenharmony_ci    }
1208fb726d48Sopenharmony_ci    let refreshTimeOut = setTimeout(() => {
1209fb726d48Sopenharmony_ci      sp.refreshCanvas(true);
1210fb726d48Sopenharmony_ci      clearTimeout(refreshTimeOut);
1211fb726d48Sopenharmony_ci    }, 360);
1212fb726d48Sopenharmony_ci  };
1213fb726d48Sopenharmony_ci}
1214fb726d48Sopenharmony_ci// 查询render_service对应方法行的所有数据
1215fb726d48Sopenharmony_ci// @ts-ignore
1216fb726d48Sopenharmony_cifunction queryRowsData(sp: SpSystemTrace, rowList: Array<TraceRow<unknown>>): void {
1217fb726d48Sopenharmony_ci  rowList.forEach((row): void => {
1218fb726d48Sopenharmony_ci    if (row.getAttribute('row-type') === 'func') {
1219fb726d48Sopenharmony_ci      if (row.getAttribute('name')?.startsWith('render_service')) {
1220fb726d48Sopenharmony_ci        saveFrameRateData(sp, row, 'H:RSMainThread::DoComposition');
1221fb726d48Sopenharmony_ci      } else if (row.getAttribute('name')?.startsWith('RSHardwareThrea')) {
1222fb726d48Sopenharmony_ci        saveFrameRateData(sp, row, 'H:Repaint');
1223fb726d48Sopenharmony_ci      } else if (row.getAttribute('name')?.startsWith('Present')) {
1224fb726d48Sopenharmony_ci        savePresentData(sp, row, 'H:Waiting for Present Fence');
1225fb726d48Sopenharmony_ci      }
1226fb726d48Sopenharmony_ci    }
1227fb726d48Sopenharmony_ci  });
1228fb726d48Sopenharmony_ci}
1229fb726d48Sopenharmony_ci
1230fb726d48Sopenharmony_ci// 查到所有的数据存储起来
1231fb726d48Sopenharmony_ci// @ts-ignore
1232fb726d48Sopenharmony_cifunction saveFrameRateData(sp: SpSystemTrace, row: TraceRow<unknown>, funcName: string): void {
1233fb726d48Sopenharmony_ci  let dataList: unknown = [];
1234fb726d48Sopenharmony_ci  queryFuncRowData(funcName, Number(row?.getAttribute('row-id'))).then((res): void => {
1235fb726d48Sopenharmony_ci    if (res.length) {
1236fb726d48Sopenharmony_ci      res.forEach((item): void => {
1237fb726d48Sopenharmony_ci        // @ts-ignore
1238fb726d48Sopenharmony_ci        dataList?.push({ startTime: item.startTime!, tid: item.tid });
1239fb726d48Sopenharmony_ci      });
1240fb726d48Sopenharmony_ci      if (funcName === 'H:RSMainThread::DoComposition') {
1241fb726d48Sopenharmony_ci        // @ts-ignore
1242fb726d48Sopenharmony_ci        sp.docomList = dataList;
1243fb726d48Sopenharmony_ci      } else {
1244fb726d48Sopenharmony_ci        // @ts-ignore
1245fb726d48Sopenharmony_ci        sp.repaintList = dataList;
1246fb726d48Sopenharmony_ci      }
1247fb726d48Sopenharmony_ci    }
1248fb726d48Sopenharmony_ci  });
1249fb726d48Sopenharmony_ci}
1250fb726d48Sopenharmony_ci// 查到present泳道所有的数据存储起来
1251fb726d48Sopenharmony_ci// @ts-ignore
1252fb726d48Sopenharmony_cifunction savePresentData(sp: SpSystemTrace, row: TraceRow<unknown>, funcName: string): void {
1253fb726d48Sopenharmony_ci  let dataList: unknown = [];
1254fb726d48Sopenharmony_ci  fuzzyQueryFuncRowData(funcName, Number(row?.getAttribute('row-id'))).then((res): void => {
1255fb726d48Sopenharmony_ci    if (res.length) {
1256fb726d48Sopenharmony_ci      res.forEach((item): void => {
1257fb726d48Sopenharmony_ci        // @ts-ignore
1258fb726d48Sopenharmony_ci        dataList?.push({ endTime: item.endTime!, tid: item.tid });
1259fb726d48Sopenharmony_ci      }); // @ts-ignore
1260fb726d48Sopenharmony_ci      sp.presentList = dataList;
1261fb726d48Sopenharmony_ci    }
1262fb726d48Sopenharmony_ci  });
1263fb726d48Sopenharmony_ci}
1264fb726d48Sopenharmony_cifunction linkNodeHandler(linkNode: PairPoint[], sp: SpSystemTrace): void {
1265fb726d48Sopenharmony_ci  if (linkNode[0].rowEL.collect) {
1266fb726d48Sopenharmony_ci    linkNode[0].rowEL.translateY = linkNode[0].rowEL.getBoundingClientRect().top - 195;
1267fb726d48Sopenharmony_ci  } else {
1268fb726d48Sopenharmony_ci    linkNode[0].rowEL.translateY = linkNode[0].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
1269fb726d48Sopenharmony_ci  }
1270fb726d48Sopenharmony_ci  linkNode[0].y = linkNode[0].rowEL!.translateY! + linkNode[0].offsetY;
1271fb726d48Sopenharmony_ci  if (linkNode[1].rowEL.collect) {
1272fb726d48Sopenharmony_ci    linkNode[1].rowEL.translateY = linkNode[1].rowEL.getBoundingClientRect().top - 195;
1273fb726d48Sopenharmony_ci  } else {
1274fb726d48Sopenharmony_ci    linkNode[1].rowEL.translateY = linkNode[1].rowEL.offsetTop - sp.rowsPaneEL!.scrollTop;
1275fb726d48Sopenharmony_ci  }
1276fb726d48Sopenharmony_ci  linkNode[1].y = linkNode[1].rowEL!.translateY! + linkNode[1].offsetY;
1277fb726d48Sopenharmony_ci}
1278fb726d48Sopenharmony_ci
1279fb726d48Sopenharmony_ciconst eventMap = {
1280fb726d48Sopenharmony_ci  'cpu-data': 'Cpu',
1281fb726d48Sopenharmony_ci  'cpu-state': 'Cpu State',
1282fb726d48Sopenharmony_ci  'cpu-freq': 'Cpu Frequency',
1283fb726d48Sopenharmony_ci  'cpu-limit-freq': 'Cpu Freq Limit',
1284fb726d48Sopenharmony_ci  process: 'Process',
1285fb726d48Sopenharmony_ci  'native-memory': 'Native Memory',
1286fb726d48Sopenharmony_ci  thread: 'Thread',
1287fb726d48Sopenharmony_ci  func: 'Func',
1288fb726d48Sopenharmony_ci  mem: 'Memory',
1289fb726d48Sopenharmony_ci  'virtual-memory-cell': 'Virtual Memory',
1290fb726d48Sopenharmony_ci  'virtual-memory-group': 'Virtual Memory',
1291fb726d48Sopenharmony_ci  fps: 'FPS',
1292fb726d48Sopenharmony_ci  'ability-monitor': 'Ability Monitor',
1293fb726d48Sopenharmony_ci  'cpu-ability': 'Cpu Ability',
1294fb726d48Sopenharmony_ci  'memory-ability': 'Memory Ability',
1295fb726d48Sopenharmony_ci  'disk-ability': 'DiskIO Ability',
1296fb726d48Sopenharmony_ci  'network-ability': 'Network Ability',
1297fb726d48Sopenharmony_ci  sdk: 'Sdk',
1298fb726d48Sopenharmony_ci  'sdk-counter': 'SDK Counter',
1299fb726d48Sopenharmony_ci  'sdk-slice': 'Sdk Slice',
1300fb726d48Sopenharmony_ci  energy: 'Energy',
1301fb726d48Sopenharmony_ci  'power-energy': 'Power Event',
1302fb726d48Sopenharmony_ci  'system-energy': 'System Event',
1303fb726d48Sopenharmony_ci  'anomaly-energy': 'Anomaly Event',
1304fb726d48Sopenharmony_ci  'clock-group': 'Clocks',
1305fb726d48Sopenharmony_ci  clock: 'clock',
1306fb726d48Sopenharmony_ci  'irq-group': 'Irqs',
1307fb726d48Sopenharmony_ci  irq: 'irq',
1308fb726d48Sopenharmony_ci  hiperf: 'HiPerf (All)',
1309fb726d48Sopenharmony_ci  'hiperf-event': 'HiPerf Event',
1310fb726d48Sopenharmony_ci  'hiperf-report': 'HiPerf Report',
1311fb726d48Sopenharmony_ci  'hiperf-process': 'HiPerf Process',
1312fb726d48Sopenharmony_ci  'hiperf-thread': 'HiPerf Thread',
1313fb726d48Sopenharmony_ci  'js-memory': 'Js Memory',
1314fb726d48Sopenharmony_ci};
1315fb726d48Sopenharmony_ciexport function spSystemTraceInitPointToEvent(sp: SpSystemTrace): void {
1316fb726d48Sopenharmony_ci  sp.eventMap = eventMap;
1317fb726d48Sopenharmony_ci}
1318fb726d48Sopenharmony_ci
1319fb726d48Sopenharmony_ciexport function spSystemTraceParentRowSticky(sp: SpSystemTrace, deltaY: number): void {
1320fb726d48Sopenharmony_ci  if (deltaY > 0) {
1321fb726d48Sopenharmony_ci    // 从上往下划
1322fb726d48Sopenharmony_ci    const expandRowList = sp.visibleRows.filter((vr) => vr.expansion);
1323fb726d48Sopenharmony_ci    // @ts-ignore
1324fb726d48Sopenharmony_ci    expandRowList.forEach((vr: TraceRow<unknown>) => {
1325fb726d48Sopenharmony_ci      // @ts-ignore
1326fb726d48Sopenharmony_ci      const visibleNotCollectList = vr.childrenList.filter((child: TraceRow<unknown>) => !child.collect && !child.sleeping);
1327fb726d48Sopenharmony_ci      vr.sticky = visibleNotCollectList.length > 0;
1328fb726d48Sopenharmony_ci    });
1329fb726d48Sopenharmony_ci  } else if (deltaY < 0) {
1330fb726d48Sopenharmony_ci    // 从下往上划
1331fb726d48Sopenharmony_ci    sp.visibleRows
1332fb726d48Sopenharmony_ci      .filter((vr) => !vr.folder && vr.parentRowEl && vr.parentRowEl.expansion && !vr.collect)
1333fb726d48Sopenharmony_ci      .forEach((vr) => (vr.parentRowEl!.sticky = true));
1334fb726d48Sopenharmony_ci  } else {
1335fb726d48Sopenharmony_ci    return;
1336fb726d48Sopenharmony_ci  }
1337fb726d48Sopenharmony_ci}
1338