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 { xpowerAppDetailList } from '../utils/AllMemoryCache'; 16import { Args } from '../CommonArgs'; 17 18export const chartXpowerAppDetailMemoryDataSql = (args: Args): string => { 19 return ` 20 SELECT a.start_time - b.start_ts AS startTime, 21 ifnull(a.count_1hz, 0) AS c1hz, 22 ifnull(a.count_5hz, 0) AS c5hz, 23 ifnull(a.count_10hz, 0) AS c10hz, 24 ifnull(a.count_15hz, 0) AS c15hz, 25 ifnull(a.count_24hz, 0) AS c24hz, 26 ifnull(a.count_30hz, 0) AS c30hz, 27 ifnull(a.count_45hz, 0) AS c45hz, 28 ifnull(a.count_60hz, 0) AS c60hz, 29 ifnull(a.count_90hz, 0) AS c90hz, 30 ifnull(a.count_120hz, 0) AS c120hz, 31 ifnull(a.count_180hz, 0) AS c180hz 32 FROM xpower_app_detail_display a, trace_range b`; 33}; 34 35export const chartXpowerStatisticDataSql = (args: Args): string => { 36 return ` 37 SELECT a.start_time - b.start_ts AS startTime, 38 ifnull(a.count_1hz, 0) AS c1hz, 39 ifnull(a.count_5hz, 0) AS c5hz, 40 ifnull(a.count_10hz, 0) AS c10hz, 41 ifnull(a.count_15hz, 0) AS c15hz, 42 ifnull(a.count_24hz, 0) AS c24hz, 43 ifnull(a.count_30hz, 0) AS c30hz, 44 ifnull(a.count_45hz, 0) AS c45hz, 45 ifnull(a.count_60hz, 0) AS c60hz, 46 ifnull(a.count_90hz, 0) AS c90hz, 47 ifnull(a.count_120hz, 0) AS c120hz, 48 ifnull(a.count_180hz, 0) AS c180hz 49 FROM xpower_app_detail_display a, trace_range b`; 50}; 51 52export function xpowerAppDetailDataReceiver(data: unknown, proc: Function): void { 53 // @ts-ignore 54 if (data.params.trafic === TraficEnum.Memory) { 55 let list: unknown[]; 56 // @ts-ignore 57 if (!xpowerAppDetailList.has(data.params.xpowerName)) { 58 // @ts-ignore 59 list = proc(chartXpowerAppDetailMemoryDataSql(data.params)); 60 // @ts-ignore 61 xpowerAppDetailList.set(data.params.xpowerName, list); 62 } else { 63 // @ts-ignore 64 list = xpowerAppDetailList.get(data.params.xpowerName) || []; 65 } 66 // @ts-ignore 67 if (data.params.queryAll) { 68 list = (list || []).filter( 69 // @ts-ignore 70 (it) => it.startTime + 3000000000 >= data.params.selectStartNS && it.startTime <= data.params.selectEndNS 71 ); 72 } 73 arrayBufferHandler(data, list, true); 74 } else { 75 // @ts-ignore 76 let sql = chartXpowerStatisticDataSql(data.params); 77 let res = proc(sql); 78 // @ts-ignore 79 arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer); 80 } 81} 82 83const keys = [ 84 'startTime', 85 'c1hz', 86 'c5hz', 87 'c10hz', 88 'c15hz', 89 'c24hz', 90 'c30hz', 91 'c45hz', 92 'c60hz', 93 'c90hz', 94 'c120hz', 95 'c180hz', 96]; 97 98function initializeArrays(res: unknown[], transfer: boolean, data: unknown): { [key: string]: Float64Array } { 99 return keys.reduce((acc, key) => { // @ts-ignore 100 acc[key] = new Float64Array(transfer ? res.length : (data as unknown).params.sharedArrayBuffers[key]); 101 return acc; 102 }, {} as { [key: string]: Float64Array }); 103} 104 105function fillArrays(arrays: { [key: string]: Float64Array }, res: unknown[], data: unknown): void { 106 let keysCopy = [...keys]; 107 keysCopy.shift(); 108 res.forEach((it, i) => { // @ts-ignore 109 if ((data as unknown).params.trafic === TraficEnum.ProtoBuffer) { // @ts-ignore 110 it = (it as unknown).xpowerAppDetailData; 111 } // @ts-ignore 112 arrays.startTime[i] = (it as unknown).startTime; 113 keysCopy.forEach((key) => { // @ts-ignore 114 arrays[key][i] = (it as unknown)[key]; 115 }); 116 }); 117} 118 119function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void { 120 const arrays = initializeArrays(res, transfer, data); 121 fillArrays(arrays, res, data); 122 (self as unknown as Worker).postMessage( 123 { // @ts-ignore 124 id: (data as unknown).id, // @ts-ignore 125 action: (data as unknown).action, 126 results: transfer 127 ? { 128 startTime: arrays.startTime.buffer, 129 c1hz: arrays.c1hz.buffer, 130 c5hz: arrays.c5hz.buffer, 131 c10hz: arrays.c10hz.buffer, 132 c15hz: arrays.c15hz.buffer, 133 c24hz: arrays.c24hz.buffer, 134 c30hz: arrays.c30hz.buffer, 135 c45hz: arrays.c45hz.buffer, 136 c60hz: arrays.c60hz.buffer, 137 c90hz: arrays.c90hz.buffer, 138 c120hz: arrays.c120hz.buffer, 139 c180hz: arrays.c180hz.buffer, 140 } 141 : {}, 142 len: res.length, 143 transfer: transfer, 144 }, 145 transfer 146 ? [ 147 arrays.startTime.buffer, 148 arrays.c1hz.buffer, 149 arrays.c5hz.buffer, 150 arrays.c10hz.buffer, 151 arrays.c15hz.buffer, 152 arrays.c24hz.buffer, 153 arrays.c30hz.buffer, 154 arrays.c45hz.buffer, 155 arrays.c60hz.buffer, 156 arrays.c90hz.buffer, 157 arrays.c120hz.buffer, 158 arrays.c180hz.buffer, 159 ] 160 : [] 161 ); 162} 163