1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { Args } from '../CommonArgs';
15fb726d48Sopenharmony_ciimport { TraficEnum } from '../utils/QueryEnum';
16fb726d48Sopenharmony_ci
17fb726d48Sopenharmony_ciexport const chartProcessStartupDataSql = (args: Args): string => {
18fb726d48Sopenharmony_ci  return `
19fb726d48Sopenharmony_ci      select P.pid,
20fb726d48Sopenharmony_ci             A.tid,
21fb726d48Sopenharmony_ci             A.call_id                                                                         as itid,
22fb726d48Sopenharmony_ci             (case when A.start_time < ${args.recordStartNS} then 0 else (A.start_time - ${args.recordStartNS}) end) as startTime,
23fb726d48Sopenharmony_ci             (case
24fb726d48Sopenharmony_ci                  when A.start_time < ${args.recordStartNS} then (A.end_time - ${args.recordStartNS})
25fb726d48Sopenharmony_ci                  when A.end_time = -1 then 0
26fb726d48Sopenharmony_ci                  else (A.end_time - A.start_time) end)                                        as dur,
27fb726d48Sopenharmony_ci             A.start_name                                                                      as startName
28fb726d48Sopenharmony_ci      from app_startup A
29fb726d48Sopenharmony_ci               left join process P on A.ipid = P.ipid
30fb726d48Sopenharmony_ci      where P.pid = ${args.pid}
31fb726d48Sopenharmony_ci      order by start_name;`;
32fb726d48Sopenharmony_ci};
33fb726d48Sopenharmony_ci
34fb726d48Sopenharmony_ciexport function processStartupDataReceiver(data: unknown, proc: Function): void {
35fb726d48Sopenharmony_ci  //@ts-ignore
36fb726d48Sopenharmony_ci  let sql = chartProcessStartupDataSql(data.params);
37fb726d48Sopenharmony_ci  let res = proc(sql); //@ts-ignore
38fb726d48Sopenharmony_ci  arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
39fb726d48Sopenharmony_ci}
40fb726d48Sopenharmony_ci
41fb726d48Sopenharmony_cifunction arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
42fb726d48Sopenharmony_ci  //@ts-ignore
43fb726d48Sopenharmony_ci  let startTs = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTime); //@ts-ignore
44fb726d48Sopenharmony_ci  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur); //@ts-ignore
45fb726d48Sopenharmony_ci  let pid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.pid); //@ts-ignore
46fb726d48Sopenharmony_ci  let tid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.tid); //@ts-ignore
47fb726d48Sopenharmony_ci  let itid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.itid); //@ts-ignore
48fb726d48Sopenharmony_ci  let startName = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.startName);
49fb726d48Sopenharmony_ci  res.forEach((it, i) => {
50fb726d48Sopenharmony_ci    //@ts-ignore
51fb726d48Sopenharmony_ci    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processStartupData); //@ts-ignore
52fb726d48Sopenharmony_ci    dur[i] = it.dur || 0; //@ts-ignore
53fb726d48Sopenharmony_ci    startTs[i] = it.startTime || 0; //@ts-ignore
54fb726d48Sopenharmony_ci    pid[i] = it.pid || 0; //@ts-ignore
55fb726d48Sopenharmony_ci    tid[i] = it.tid || 0; //@ts-ignore
56fb726d48Sopenharmony_ci    itid[i] = it.itid || 0; //@ts-ignore
57fb726d48Sopenharmony_ci    startName[i] = it.startName || 0;
58fb726d48Sopenharmony_ci  });
59fb726d48Sopenharmony_ci  (self as unknown as Worker).postMessage(
60fb726d48Sopenharmony_ci    {
61fb726d48Sopenharmony_ci      //@ts-ignore
62fb726d48Sopenharmony_ci      id: data.id, //@ts-ignore
63fb726d48Sopenharmony_ci      action: data.action,
64fb726d48Sopenharmony_ci      results: transfer
65fb726d48Sopenharmony_ci        ? {
66fb726d48Sopenharmony_ci            dur: dur.buffer,
67fb726d48Sopenharmony_ci            startTs: startTs.buffer,
68fb726d48Sopenharmony_ci            pid: pid.buffer,
69fb726d48Sopenharmony_ci            tid: tid.buffer,
70fb726d48Sopenharmony_ci            itid: itid.buffer,
71fb726d48Sopenharmony_ci            startName: startName.buffer,
72fb726d48Sopenharmony_ci          }
73fb726d48Sopenharmony_ci        : {},
74fb726d48Sopenharmony_ci      len: res.length,
75fb726d48Sopenharmony_ci      transfer: transfer,
76fb726d48Sopenharmony_ci    },
77fb726d48Sopenharmony_ci    transfer ? [dur.buffer, startTs.buffer, pid.buffer, tid.buffer, itid.buffer] : []
78fb726d48Sopenharmony_ci  );
79fb726d48Sopenharmony_ci}
80