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';
15
16export const chartProcessTouchEventDispatchDataSql = (args: unknown): string => {
17  return `
18  select 
19      c.ts-${//@ts-ignore
20        args.recordStartNS} as startTs,
21      c.dur,
22      tid,
23      P.pid,
24      c.parent_id as parentId,
25      c.id,
26      c.depth,
27      ((c.ts - ${//@ts-ignore
28        args.recordStartNS}) / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px,
29      c.name as funName,
30      A.name as threadName
31  from thread A
32  left join process P on P.id = A.ipid
33  left join callstack C on A.id = C.callid
34  where startTs not null and cookie not null
35  and (c.name = 'H:touchEventDispatch' OR c.name = 'H:TouchEventDispatch')  
36  and tid = ${//@ts-ignore
37    args.tid}
38  and startTs + dur >= ${Math.floor(//@ts-ignore
39    args.startNS)}
40  and startTs <= ${Math.floor(//@ts-ignore
41    args.endNS)}
42    group by px;
43  `;
44};
45
46export function processTouchEventDispatchDataReceiver(data: unknown, proc: Function): void {
47  //@ts-ignore
48  if (data.params.trafic === TraficEnum.Memory) {
49    //@ts-ignore
50    let sql = chartProcessTouchEventDispatchDataSql(data.params);
51    let res = proc(sql);
52    //@ts-ignore
53    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
54  }
55}
56
57function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
58  let processTouchEventDispatch = new ProcessTouchEventDispatch(data, transfer, res.length);
59  res.forEach((it, i) => {
60    //@ts-ignore
61    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processEventDispatchData);
62    //@ts-ignore
63    processTouchEventDispatch.tid[i] = it.tid;
64    //@ts-ignore
65    processTouchEventDispatch.dur[i] = it.dur;
66    //@ts-ignore
67    processTouchEventDispatch.startTs[i] = it.startTs;
68    //@ts-ignore
69    processTouchEventDispatch.pid[i] = it.pid;
70    //@ts-ignore
71    processTouchEventDispatch.id[i] = it.id;
72    //@ts-ignore
73    processTouchEventDispatch.depth[i] = it.depth;
74  });
75  postMessage(data, transfer, processTouchEventDispatch, res.length);
76}
77function postMessage(data: unknown, transfer: boolean, processTouchEventDispatch: ProcessTouchEventDispatch, len: number): void {
78  (self as unknown as Worker).postMessage(
79    {
80      transfer: transfer,
81      //@ts-ignore
82      id: data.id,
83      
84      //@ts-ignore
85      action: data.action,
86      results: transfer
87        ? {
88          tid: processTouchEventDispatch.tid.buffer,
89          dur: processTouchEventDispatch.dur.buffer,
90          startTs: processTouchEventDispatch.startTs.buffer,
91          pid: processTouchEventDispatch.pid.buffer,
92          id: processTouchEventDispatch.id.buffer,
93          depth: processTouchEventDispatch.depth.buffer,
94        }
95        : {},
96      len: len,
97    },
98    transfer
99      ? [
100        processTouchEventDispatch.tid.buffer,
101        processTouchEventDispatch.dur.buffer,
102        processTouchEventDispatch.startTs.buffer,
103        processTouchEventDispatch.pid.buffer,
104        processTouchEventDispatch.id.buffer,
105        processTouchEventDispatch.depth.buffer,
106      ]
107      : []
108  );
109}
110class ProcessTouchEventDispatch {
111  tid: Int32Array;
112  pid: Int32Array;
113  startTs: Float64Array;
114  dur: Float64Array;
115  id: Int32Array;
116  depth: Int32Array;
117  constructor(data: unknown, transfer: boolean, len: number) {
118    //@ts-ignore
119    this.tid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.tid);
120    //@ts-ignore
121    this.pid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.pid);
122    //@ts-ignore
123    this.startTs = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.startTs);
124    //@ts-ignore
125    this.dur = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.dur);
126    //@ts-ignore
127    this.id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.id);
128    //@ts-ignore
129    this.depth = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.depth);
130  }
131}
132