1// Copyright (c) 2021 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
15import { getThreadPool } from '../SqlLite';
16import { TraceRow } from '../../component/trace/base/TraceRow';
17import { ClockStruct } from '../ui-worker/ProcedureWorkerClock';
18import { Utils } from '../../component/trace/base/Utils';
19
20export function clockDataSender(
21  clockName: string = '',
22  sqlType: string,
23  row: TraceRow<ClockStruct>,
24  args?: unknown,
25): Promise<ClockStruct[]> {
26  let trafic: number = TraficEnum.Memory;
27  let width = row.clientWidth - CHART_OFFSET_LEFT;
28  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
29    row.sharedArrayBuffers = {
30      filterId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
31      value: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
32      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
33      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34    };
35  }
36  return new Promise((resolve, reject): void => {
37    getThreadPool(row.traceId).submitProto(
38      QueryEnum.ClockData,
39      {
40        clockName: clockName,
41        sqlType: sqlType,
42        startNS: TraceRow.range?.startNS || 0,
43        endNS: TraceRow.range?.endNS || 0,
44        totalNS: TraceRow.range?.totalNS || 0,
45        recordStartNS: Utils.getInstance().getRecordStartNS(row.traceId),
46        recordEndNS: Utils.getInstance().getRecordEndNS(row.traceId),
47        // @ts-ignore
48        queryAll: args && args.queryAll,
49        // @ts-ignore
50        selectStartNS: args ? args.startNS : 0,
51        // @ts-ignore
52        selectEndNS: args ? args.endNS : 0,
53        // @ts-ignore
54        selectTotalNS: args ? args.endNS - args.startNS : 0,
55        t: Date.now(),
56        width: width,
57        trafic: trafic,
58        sharedArrayBuffers: row.sharedArrayBuffers,
59      },
60      (res: unknown, len: number, transfer: boolean): void => {
61        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
62      }
63    );
64  });
65}
66
67function arrayBufferHandler(buffers: unknown, len: number): ClockStruct[] {
68  let outArr: ClockStruct[] = [];
69  // @ts-ignore
70  let filterId = new Int32Array(buffers.filterId);
71  // @ts-ignore
72  let value = new Int32Array(buffers.value);
73  // @ts-ignore
74  let startNS = new Float64Array(buffers.startNS);
75  // @ts-ignore
76  let dur = new Float64Array(buffers.dur);
77  for (let i = 0; i < len; i++) {
78    outArr.push({
79      filterId: filterId[i],
80      value: value[i],
81      startNS: startNS[i],
82      dur: dur[i],
83    } as unknown as ClockStruct);
84  }
85  return outArr;
86}
87