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 ANY 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_ciimport { type JsCpuProfilerChartFrame } from '../bean/JsStruct';
16fb726d48Sopenharmony_ciimport { type SnapshotStruct } from '../database/ui-worker/ProcedureWorkerSnapshot';
17fb726d48Sopenharmony_ciimport { type RangeSelectStruct, TraceRow } from './trace/base/TraceRow';
18fb726d48Sopenharmony_ciimport { KeyPathStruct } from '../bean/KeyPathStruct';
19fb726d48Sopenharmony_ciimport { warn } from '../../log/Log';
20fb726d48Sopenharmony_ci
21fb726d48Sopenharmony_ciexport function setSelectState(
22fb726d48Sopenharmony_ci  data: JsCpuProfilerChartFrame,
23fb726d48Sopenharmony_ci  frameSelectDataIdArr: Array<number>,
24fb726d48Sopenharmony_ci  parent?: JsCpuProfilerChartFrame
25fb726d48Sopenharmony_ci): void {
26fb726d48Sopenharmony_ci  if (TraceRow.rangeSelectObject?.startNS && TraceRow.rangeSelectObject?.endNS) {
27fb726d48Sopenharmony_ci    let startTime = 0;
28fb726d48Sopenharmony_ci    let endTime = 0;
29fb726d48Sopenharmony_ci    if (data.startTime < TraceRow.rangeSelectObject?.startNS) {
30fb726d48Sopenharmony_ci      startTime = TraceRow.rangeSelectObject?.startNS;
31fb726d48Sopenharmony_ci    } else {
32fb726d48Sopenharmony_ci      startTime = data.startTime;
33fb726d48Sopenharmony_ci    }
34fb726d48Sopenharmony_ci    if (data.endTime > TraceRow.rangeSelectObject.endNS) {
35fb726d48Sopenharmony_ci      endTime = TraceRow.rangeSelectObject?.endNS;
36fb726d48Sopenharmony_ci    } else {
37fb726d48Sopenharmony_ci      endTime = data.endTime;
38fb726d48Sopenharmony_ci    }
39fb726d48Sopenharmony_ci    data.totalTime = endTime - startTime;
40fb726d48Sopenharmony_ci    data.selfTime = data.totalTime;
41fb726d48Sopenharmony_ci    if (parent) {
42fb726d48Sopenharmony_ci      parent.selfTime -= data.totalTime;
43fb726d48Sopenharmony_ci    }
44fb726d48Sopenharmony_ci  }
45fb726d48Sopenharmony_ci
46fb726d48Sopenharmony_ci  data.isSelect = true;
47fb726d48Sopenharmony_ci  if (data.children.length > 0) {
48fb726d48Sopenharmony_ci    for (let child of data.children) {
49fb726d48Sopenharmony_ci      if (child === null) {
50fb726d48Sopenharmony_ci        continue;
51fb726d48Sopenharmony_ci      }
52fb726d48Sopenharmony_ci      if (frameSelectDataIdArr.includes(child.id)) {
53fb726d48Sopenharmony_ci        setSelectState(child, frameSelectDataIdArr, data);
54fb726d48Sopenharmony_ci      }
55fb726d48Sopenharmony_ci    }
56fb726d48Sopenharmony_ci  }
57fb726d48Sopenharmony_ci}
58fb726d48Sopenharmony_ci
59fb726d48Sopenharmony_ciexport function intersectData(row: TraceRow<any>): any[] {
60fb726d48Sopenharmony_ci  let isIntersect = (snapshotStruct: SnapshotStruct, rangeSelectStruct: RangeSelectStruct): boolean =>
61fb726d48Sopenharmony_ci    Math.max(snapshotStruct.startNs! + snapshotStruct.dur!, rangeSelectStruct!.endNS || 0) -
62fb726d48Sopenharmony_ci      Math.min(snapshotStruct.startNs!, rangeSelectStruct!.startNS || 0) <
63fb726d48Sopenharmony_ci    snapshotStruct.dur! + (rangeSelectStruct!.endNS || 0) - (rangeSelectStruct!.startNS || 0);
64fb726d48Sopenharmony_ci  let intersectData = row.dataListCache.filter((struct: SnapshotStruct) => {
65fb726d48Sopenharmony_ci    return isIntersect(struct, TraceRow.rangeSelectObject!);
66fb726d48Sopenharmony_ci  });
67fb726d48Sopenharmony_ci  return intersectData;
68fb726d48Sopenharmony_ci}
69fb726d48Sopenharmony_ciexport function isExistPidInArray(arr: Array<{ pid: number; ipid: number }>, pid: number): boolean {
70fb726d48Sopenharmony_ci  return arr.some((item) => item.pid === pid);
71fb726d48Sopenharmony_ci}
72fb726d48Sopenharmony_ci
73fb726d48Sopenharmony_ci/**
74fb726d48Sopenharmony_ci * 校验导入的json, 导出数据结构, 不匹配的不导入
75fb726d48Sopenharmony_ci * @param content json 内容
76fb726d48Sopenharmony_ci * @returns Array<KeyPathStruct>
77fb726d48Sopenharmony_ci */
78fb726d48Sopenharmony_ciexport function parseKeyPathJson(content: string): Array<KeyPathStruct> {
79fb726d48Sopenharmony_ci  const threads = JSON.parse(content);
80fb726d48Sopenharmony_ci  const parseResult = [];
81fb726d48Sopenharmony_ci  for (let threadKey in threads) {
82fb726d48Sopenharmony_ci    const tsArray = threads[threadKey];
83fb726d48Sopenharmony_ci    threadKey = threadKey.trim();
84fb726d48Sopenharmony_ci    const regex = /\[(\d+)\]/;
85fb726d48Sopenharmony_ci    const matches = threadKey.match(regex);
86fb726d48Sopenharmony_ci    const tid = matches ? parseInt(matches[1]) : -1;
87fb726d48Sopenharmony_ci    const spaceIndex = threadKey.indexOf(' ');
88fb726d48Sopenharmony_ci    const threadName = spaceIndex !== -1 ? threadKey.substring(0, spaceIndex) : '';
89fb726d48Sopenharmony_ci    if (tid && threadName && tsArray.length > 0) {
90fb726d48Sopenharmony_ci      const keyPath = new KeyPathStruct(tid, threadName, tsArray);
91fb726d48Sopenharmony_ci      parseResult.push(keyPath);
92fb726d48Sopenharmony_ci    } else {
93fb726d48Sopenharmony_ci      warn('parse key path fail ', threadKey);
94fb726d48Sopenharmony_ci    }
95fb726d48Sopenharmony_ci  }
96fb726d48Sopenharmony_ci  return parseResult;
97fb726d48Sopenharmony_ci}
98fb726d48Sopenharmony_ci
99fb726d48Sopenharmony_ciexport function debounce(func: any, delay: number) {
100fb726d48Sopenharmony_ci  let timer: any = null;
101fb726d48Sopenharmony_ci  return function () {
102fb726d48Sopenharmony_ci    clearTimeout(timer);
103fb726d48Sopenharmony_ci    timer = setTimeout(() => {
104fb726d48Sopenharmony_ci      func();
105fb726d48Sopenharmony_ci    }, delay);
106fb726d48Sopenharmony_ci  };
107fb726d48Sopenharmony_ci}
108