1/*
2 * Copyright (C) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
17import { threadPool } from '../SqlLite';
18import { TraceRow } from '../../component/trace/base/TraceRow';
19import { HangStruct } from '../ui-worker/ProcedureWorkerHang';
20import { FlagsConfig } from '../../component/SpFlags';
21
22export function hangDataSender(
23  processId: number = 0,
24  row: TraceRow<HangStruct>,
25  args?: unknown,
26): Promise<HangStruct[]> {
27  let trafic: number = TraficEnum.Memory;
28  let width = row.clientWidth - CHART_OFFSET_LEFT;
29  return new Promise((resolve, reject): void => {
30    let flagsItemJson = JSON.parse(window.localStorage.getItem(FlagsConfig.FLAGS_CONFIG_KEY)!);
31    let minDur = parseInt(flagsItemJson.hangValue);
32    threadPool.submitProto(
33      QueryEnum.HangData,
34      {
35        pid: processId,
36        minDur: minDur,
37        // @ts-ignore
38        queryAll: args && args.queryAll,
39        // @ts-ignore
40        selectStartNS: args ? args.startNS : 0,
41        // @ts-ignore
42        selectEndNS: args ? args.endNS : 0,
43        // @ts-ignore
44        selectTotalNS: args ? args.endNS - args.startNS : 0,
45        trafic: trafic,
46        width: width,
47      },
48      (res: unknown, len: number, transfer: boolean): void => {
49        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
50      }
51    );
52  });
53}
54
55function arrayBufferHandler(buffers: unknown, len: number): HangStruct[] {
56  let outArr: HangStruct[] = [];
57  // @ts-ignore
58  let id = new Int32Array(buffers.id);
59  // @ts-ignore
60  let startNS = new Float64Array(buffers.startNS);
61  // @ts-ignore
62  let dur = new Float64Array(buffers.dur);
63  // @ts-ignore
64  let tid = new Int32Array(buffers.tid);
65  // @ts-ignore
66  let pid = new Int32Array(buffers.pid);
67  for (let i = 0; i < len; i += 1) {
68    outArr.push({
69      id: id[i],
70      startNS: startNS[i],
71      dur: dur[i],
72      tid: tid[i],
73      pid: pid[i],
74    } as HangStruct);
75  }
76  return outArr;
77}
78