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 { TraficEnum } from '../utils/QueryEnum';
15import { memList } from '../utils/AllMemoryCache';
16import { Args } from '../CommonArgs';
17
18export const chartProcessMemDataSql = (args: Args): string => {
19  return `
20      select 
21             filter_id as       trackId,
22             value,
23             c.ts - ${args.recordStartNS} as startTime,
24             ts
25      from process_measure c,
26           trace_range tb
27      where filter_id = ${args.trackId};`;
28};
29
30export function processMemDataReceiver(data: unknown, proc: Function): void {
31  let res: unknown[];
32  let list: unknown[]; //@ts-ignore
33  if (!memList.has(data.params.trackId)) {
34    //@ts-ignore
35    list = proc(chartProcessMemDataSql(data.params)); //@ts-ignore
36    memList.set(data.params.trackId, list);
37  } else {
38    //@ts-ignore
39    list = memList.get(data.params.trackId) || [];
40  }
41  res = list; //@ts-ignore
42  arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
43}
44
45function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
46  //@ts-ignore
47  let startTime = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTime); //@ts-ignore
48  let ts = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.ts); //@ts-ignore
49  let value = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.value); //@ts-ignore
50  let track_id = new Uint8Array(transfer ? res.length : data.params.sharedArrayBuffers.track_id);
51  res.forEach((it, i) => {
52    //@ts-ignore
53    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processMemData); //@ts-ignore
54    ts[i] = it.ts; //@ts-ignore
55    startTime[i] = it.startTime; //@ts-ignore
56    track_id[i] = it.trackId; //@ts-ignore
57    value[i] = it.value;
58  });
59  (self as unknown as Worker).postMessage(
60    {
61      //@ts-ignore
62      id: data.id, //@ts-ignore
63      action: data.action,
64      results: transfer
65        ? {
66            ts: ts.buffer,
67            startTime: startTime.buffer,
68            value: value.buffer,
69            track_id: track_id.buffer,
70          }
71        : {},
72      len: res.length,
73      transfer: transfer,
74    },
75    transfer ? [ts.buffer, startTime.buffer, value.buffer, track_id.buffer] : []
76  );
77}
78