1/*
2 * Copyright (C) 2022 Huawei Device 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 */
15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
16import { threadPool } from '../SqlLite';
17import { TraceRow } from '../../component/trace/base/TraceRow';
18import { LogStruct } from '../ui-worker/ProcedureWorkerLog';
19
20export function LogDataSender(row: TraceRow<LogStruct>): Promise<LogStruct[]> {
21  let trafic: number = TraficEnum.ProtoBuffer;
22  let width = row.clientWidth - CHART_OFFSET_LEFT;
23  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
24    row.sharedArrayBuffers = {
25      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
27      pid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      tid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      dur: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
30      depth: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
31    };
32  }
33  return new Promise((resolve) => {
34    threadPool.submitProto(
35      QueryEnum.HilogData,
36      {
37        startNS: TraceRow.range?.startNS || 0,
38        endNS: TraceRow.range?.endNS || 0,
39        recordStartNS: window.recordStartNS,
40        recordEndNS: window.recordEndNS,
41        width: width,
42        trafic: trafic,
43        sharedArrayBuffers: row.sharedArrayBuffers,
44        oneDayTime: window.recordEndNS - ONE_DAY_NS,
45      },
46      (res: unknown, len: number, transfer: boolean) => {
47        // @ts-ignore
48        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
49      }
50    );
51  });
52}
53
54function arrayBufferHandler(res: unknown, len: number): unknown[] {
55  let outArr: LogStruct[] = [];
56  // @ts-ignore
57  let id = new Uint16Array(res.id);
58  // @ts-ignore
59  let startTs = new Float64Array(res.startTs);
60  // @ts-ignore
61  let pid = new Uint16Array(res.pid);
62  // @ts-ignore
63  let tid = new Uint16Array(res.tid);
64  // @ts-ignore
65  let dur = new Uint16Array(res.dur);
66  // @ts-ignore
67  let depth = new Uint16Array(res.depth);
68  for (let index = 0; index < len; index++) {
69    outArr.push({
70      id: id[index],
71      startTs: startTs[index],
72      pid: pid[index],
73      tid: tid[index],
74      dur: dur[index],
75      depth: depth[index],
76    } as unknown as LogStruct);
77  }
78  return outArr;
79}
80
81const ONE_DAY_NS = 86_400_000_000_000;
82