1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
17fb726d48Sopenharmony_ciimport { threadPool } from '../SqlLite';
18fb726d48Sopenharmony_ciimport { TraceRow } from '../../component/trace/base/TraceRow';
19fb726d48Sopenharmony_ciimport { EnergySystemStruct } from '../ui-worker/ProcedureWorkerEnergySystem';
20fb726d48Sopenharmony_ciimport { EnergyAnomalyStruct } from '../ui-worker/ProcedureWorkerEnergyAnomaly';
21fb726d48Sopenharmony_ciimport { EnergyPowerStruct } from '../ui-worker/ProcedureWorkerEnergyPower';
22fb726d48Sopenharmony_ciimport { EnergyStateStruct } from '../ui-worker/ProcedureWorkerEnergyState';
23fb726d48Sopenharmony_ci
24fb726d48Sopenharmony_ciexport function energySysEventSender(row: TraceRow<EnergySystemStruct>): Promise<EnergySystemStruct[]> {
25fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.ProtoBuffer;
26fb726d48Sopenharmony_ci  let width = row.clientWidth - 248;
27fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
28fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
29fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
30fb726d48Sopenharmony_ci      startNs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
31fb726d48Sopenharmony_ci      count: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
32fb726d48Sopenharmony_ci      type: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
33fb726d48Sopenharmony_ci      token: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34fb726d48Sopenharmony_ci      dataType: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
35fb726d48Sopenharmony_ci    };
36fb726d48Sopenharmony_ci  }
37fb726d48Sopenharmony_ci  return new Promise((resolve) => {
38fb726d48Sopenharmony_ci    threadPool.submitProto(
39fb726d48Sopenharmony_ci      QueryEnum.EnergySystemData,
40fb726d48Sopenharmony_ci      {
41fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
42fb726d48Sopenharmony_ci        trafic: trafic,
43fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
44fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
45fb726d48Sopenharmony_ci        width: width,
46fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
47fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
48fb726d48Sopenharmony_ci      },
49fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
50fb726d48Sopenharmony_ci        // @ts-ignore
51fb726d48Sopenharmony_ci        resolve(systemBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
52fb726d48Sopenharmony_ci      }
53fb726d48Sopenharmony_ci    );
54fb726d48Sopenharmony_ci  });
55fb726d48Sopenharmony_ci}
56fb726d48Sopenharmony_ci
57fb726d48Sopenharmony_ciexport function hiSysEnergyAnomalyDataSender(row: TraceRow<EnergyAnomalyStruct>): Promise<EnergyAnomalyStruct[]> {
58fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.ProtoBuffer;
59fb726d48Sopenharmony_ci  let width = row.clientWidth - 248;
60fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
61fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
62fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
63fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
64fb726d48Sopenharmony_ci    };
65fb726d48Sopenharmony_ci  }
66fb726d48Sopenharmony_ci  return new Promise((resolve) => {
67fb726d48Sopenharmony_ci    threadPool.submitProto(
68fb726d48Sopenharmony_ci      QueryEnum.EnergyAnomalyData,
69fb726d48Sopenharmony_ci      {
70fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
71fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
72fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
73fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
74fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
75fb726d48Sopenharmony_ci        width: width,
76fb726d48Sopenharmony_ci        trafic: trafic,
77fb726d48Sopenharmony_ci      },
78fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
79fb726d48Sopenharmony_ci        resolve(anomalyBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
80fb726d48Sopenharmony_ci      }
81fb726d48Sopenharmony_ci    );
82fb726d48Sopenharmony_ci  });
83fb726d48Sopenharmony_ci}
84fb726d48Sopenharmony_ci
85fb726d48Sopenharmony_ciexport function hiSysEnergyPowerSender(row: TraceRow<EnergyPowerStruct>): Promise<EnergyPowerStruct[]> {
86fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.ProtoBuffer;
87fb726d48Sopenharmony_ci  let width = row.clientWidth - 248;
88fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
89fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
90fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
91fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
92fb726d48Sopenharmony_ci    };
93fb726d48Sopenharmony_ci  }
94fb726d48Sopenharmony_ci  return new Promise((resolve, reject): void => {
95fb726d48Sopenharmony_ci    threadPool.submitProto(
96fb726d48Sopenharmony_ci      QueryEnum.EnergyPowerData,
97fb726d48Sopenharmony_ci      {
98fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
99fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
100fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
101fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
102fb726d48Sopenharmony_ci        width: width,
103fb726d48Sopenharmony_ci        trafic: trafic,
104fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
105fb726d48Sopenharmony_ci      },
106fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
107fb726d48Sopenharmony_ci        resolve(powerBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
108fb726d48Sopenharmony_ci      }
109fb726d48Sopenharmony_ci    );
110fb726d48Sopenharmony_ci  });
111fb726d48Sopenharmony_ci}
112fb726d48Sopenharmony_ci
113fb726d48Sopenharmony_ciexport function hiSysEnergyStateSender(
114fb726d48Sopenharmony_ci  eventName: string[],
115fb726d48Sopenharmony_ci  index: number,
116fb726d48Sopenharmony_ci  row: TraceRow<EnergyStateStruct>
117fb726d48Sopenharmony_ci): Promise<EnergyStateStruct[]> {
118fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.ProtoBuffer;
119fb726d48Sopenharmony_ci  let width = row.clientWidth - 248;
120fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
121fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
122fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
123fb726d48Sopenharmony_ci      startNs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
124fb726d48Sopenharmony_ci      value: new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * MAX_COUNT),
125fb726d48Sopenharmony_ci    };
126fb726d48Sopenharmony_ci  }
127fb726d48Sopenharmony_ci  return new Promise((resolve, reject): void => {
128fb726d48Sopenharmony_ci    threadPool.submitProto(
129fb726d48Sopenharmony_ci      QueryEnum.EnergyStateData,
130fb726d48Sopenharmony_ci      {
131fb726d48Sopenharmony_ci        eventName: eventName[index],
132fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
133fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
134fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
135fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
136fb726d48Sopenharmony_ci        width: width,
137fb726d48Sopenharmony_ci        trafic: trafic,
138fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
139fb726d48Sopenharmony_ci      },
140fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
141fb726d48Sopenharmony_ci        resolve(stateArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
142fb726d48Sopenharmony_ci      }
143fb726d48Sopenharmony_ci    );
144fb726d48Sopenharmony_ci  });
145fb726d48Sopenharmony_ci}
146fb726d48Sopenharmony_ci
147fb726d48Sopenharmony_cifunction systemBufferHandler(res: unknown, len: number): unknown[] {
148fb726d48Sopenharmony_ci  let outArr: EnergySystemStruct[] = [];
149fb726d48Sopenharmony_ci  // @ts-ignore
150fb726d48Sopenharmony_ci  let startNs = new Float64Array(res.startNs);
151fb726d48Sopenharmony_ci  // @ts-ignore
152fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
153fb726d48Sopenharmony_ci  // @ts-ignore
154fb726d48Sopenharmony_ci  let count = new Uint32Array(res.count);
155fb726d48Sopenharmony_ci  // @ts-ignore
156fb726d48Sopenharmony_ci  let type = new Uint32Array(res.type);
157fb726d48Sopenharmony_ci  // @ts-ignore
158fb726d48Sopenharmony_ci  let token = new Float64Array(res.token);
159fb726d48Sopenharmony_ci  // @ts-ignore
160fb726d48Sopenharmony_ci  let dataType = new Uint16Array(res.dataType);
161fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
162fb726d48Sopenharmony_ci    outArr.push({
163fb726d48Sopenharmony_ci      id: id[index],
164fb726d48Sopenharmony_ci      startNs: startNs[index],
165fb726d48Sopenharmony_ci      count: count[index],
166fb726d48Sopenharmony_ci      type: type[index],
167fb726d48Sopenharmony_ci      token: token[index],
168fb726d48Sopenharmony_ci      dataType: dataType[index],
169fb726d48Sopenharmony_ci    } as unknown as EnergySystemStruct);
170fb726d48Sopenharmony_ci  }
171fb726d48Sopenharmony_ci  return outArr;
172fb726d48Sopenharmony_ci}
173fb726d48Sopenharmony_ci
174fb726d48Sopenharmony_cifunction anomalyBufferHandler(res: unknown, len: number): EnergyAnomalyStruct[] {
175fb726d48Sopenharmony_ci  let outArr: EnergyAnomalyStruct[] = [];
176fb726d48Sopenharmony_ci  // @ts-ignore
177fb726d48Sopenharmony_ci  let startNs = new Float64Array(res.startNs);
178fb726d48Sopenharmony_ci  // @ts-ignore
179fb726d48Sopenharmony_ci  let id = new Int32Array(res.id);
180fb726d48Sopenharmony_ci  for (let index = 0; index < len; index++) {
181fb726d48Sopenharmony_ci    outArr.push({
182fb726d48Sopenharmony_ci      id: id[index],
183fb726d48Sopenharmony_ci      startNS: startNs[index],
184fb726d48Sopenharmony_ci    } as unknown as EnergyAnomalyStruct);
185fb726d48Sopenharmony_ci  }
186fb726d48Sopenharmony_ci  return outArr;
187fb726d48Sopenharmony_ci}
188fb726d48Sopenharmony_ci
189fb726d48Sopenharmony_cifunction powerBufferHandler(buffers: unknown, len: number): EnergyPowerStruct[] {
190fb726d48Sopenharmony_ci  let outArr: EnergyPowerStruct[] = [];
191fb726d48Sopenharmony_ci  // @ts-ignore
192fb726d48Sopenharmony_ci  let startNs = new Float64Array(buffers.startNs);
193fb726d48Sopenharmony_ci  // @ts-ignore
194fb726d48Sopenharmony_ci  let id = new Uint32Array(buffers.id);
195fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
196fb726d48Sopenharmony_ci    outArr.push({
197fb726d48Sopenharmony_ci      id: id[i],
198fb726d48Sopenharmony_ci      startNS: startNs[i],
199fb726d48Sopenharmony_ci    } as unknown as EnergyPowerStruct);
200fb726d48Sopenharmony_ci  }
201fb726d48Sopenharmony_ci  return outArr;
202fb726d48Sopenharmony_ci}
203fb726d48Sopenharmony_ci
204fb726d48Sopenharmony_cifunction stateArrayBufferHandler(buffers: unknown, len: number): EnergyStateStruct[] {
205fb726d48Sopenharmony_ci  let outArr: EnergyStateStruct[] = [];
206fb726d48Sopenharmony_ci  // @ts-ignore
207fb726d48Sopenharmony_ci  let startNs = new Float64Array(buffers.startNs);
208fb726d48Sopenharmony_ci  // @ts-ignore
209fb726d48Sopenharmony_ci  let eventValue = new Float32Array(buffers.eventValue);
210fb726d48Sopenharmony_ci  // @ts-ignore
211fb726d48Sopenharmony_ci  let id = new Uint32Array(buffers.id);
212fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
213fb726d48Sopenharmony_ci    outArr.push({
214fb726d48Sopenharmony_ci      id: id[i],
215fb726d48Sopenharmony_ci      startNs: startNs[i],
216fb726d48Sopenharmony_ci      value: eventValue[i],
217fb726d48Sopenharmony_ci    } as unknown as EnergyStateStruct);
218fb726d48Sopenharmony_ci  }
219fb726d48Sopenharmony_ci  return outArr;
220fb726d48Sopenharmony_ci}
221