• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const flagInit = -100000000;
17
18export function filterDataByLayer(
19  list: unknown[],
20  layerKey: string,
21  startKey: string,
22  durKey: string,
23  startNS: number,
24  endNS: number,
25  width: number
26): unknown[] {
27  let pns = (endNS - startNS) / width; //每个像素多少ns
28  let sliceArray = findRange(list, { startKey, durKey, startNS, endNS });
29  let groups = groupBy(sliceArray, layerKey);
30  let res: unknown[] = [];
31  Reflect.ownKeys(
32    //@ts-ignore
33    groups
34  ).map((key: unknown) => {
35    //@ts-ignore
36    let it = groups[key] as unknown[];
37    if (it.length > 0) {
38      let sum = 0;
39      for (let i = 0; i < it.length; i++) {
40        if (i === it.length - 1) {
41          //@ts-ignore
42          if (it[i][durKey] === undefined || it[i][durKey] === null) {
43            //@ts-ignore
44            it[i][durKey] = (endNS || 0) - (it[i][startKey] || 0);
45          }
46        } else {
47          //@ts-ignore
48          if (it[i][durKey] === undefined || it[i][durKey] === null) {
49            //@ts-ignore
50            it[i][durKey] = (it[i + 1][startKey] || 0) - (it[i][startKey] || 0);
51          }
52        }
53        //@ts-ignore
54        if (it[i][durKey] >= pns || it.length < 100) {
55          //@ts-ignore
56          it[i].v = true;
57        } else {
58          if (i > 0) {
59            //@ts-ignore
60            let c = it[i][startKey] - it[i - 1][startKey] - it[i - 1][durKey];
61            if (c < pns && sum < pns) {
62              //@ts-ignore
63              sum += c + it[i - 1][durKey];
64              //@ts-ignore
65              it[i].v = false;
66            } else {
67              //@ts-ignore
68              it[i].v = true;
69              sum = 0;
70            }
71          }
72        }
73      }
74      //@ts-ignore
75      res.push(...it.filter((it) => it.v));
76    }
77  });
78  return res;
79}
80
81export function filterDataByGroup(
82  list: unknown[],
83  startKey: string,
84  durKey: string,
85  startNS: number,
86  endNS: number,
87  width: number,
88  valueKey?: string,
89  filter?: (a: unknown) => boolean,
90  fastFilter: boolean = true,
91  isDmaFence?: boolean,
92): unknown[] {
93  if (!fastFilter || filter) {
94    let arr = findRange(list, { startKey, durKey, startNS, endNS });
95    arr = arr.map((it) => {
96      //@ts-ignore
97      it.px = Math.floor(it[startKey] / ((endNS - startNS) / width));
98      return it;
99    });
100    let group = groupBy(arr, 'px');
101    let res: Set<unknown> = new Set();
102    //@ts-ignore
103    Reflect.ownKeys(group).map((key: unknown): void => {
104      //@ts-ignore
105      let arr = group[key] as unknown[];
106      if (arr.length > 0) {
107        //@ts-ignore
108        res.add(arr.reduce((p, c) => (p[durKey] > c[durKey] ? p : c)));
109        if (valueKey) {
110          //@ts-ignore
111          res.add(arr.reduce((p, c) => (p[valueKey] > c[valueKey] ? p : c)));
112        }
113        if (filter) {
114          let filterArr = arr.filter((a) => filter(a));
115          if (filterArr && filterArr.length > 0) {
116            //@ts-ignore
117            res.add(filterArr.reduce((p, c) => (p[durKey] > c[durKey] ? p : c)));
118          }
119        }
120      }
121    });
122    return [...res];
123  } else {
124    return filterDataByGroupWithoutValue(list, startKey, durKey, startNS, endNS, width, isDmaFence);
125  }
126}
127
128function filterDataByGroupWithoutValue(
129  list: unknown[],
130  startKey: string,
131  durKey: string,
132  startNS: number,
133  endNS: number,
134  width: number,
135  isDmaFence?: boolean,
136): unknown[] {
137  let arr: unknown[] = [];
138  // 标志位,判定何时进行新一轮数据统计处理
139  let flag: number = flagInit;
140  for (let i = 0; i < list.length; i++) {
141    // 筛选符合判断条件的数据,作进一步处理
142    //@ts-ignore
143    if (list[i][startKey] + list[i][durKey] >= startNS && list[i][startKey] <= endNS) {
144      // 获取当前数据的像素值
145      let px: number;
146      //@ts-ignore
147      if (isDmaFence && list[i][durKey] === 0) {
148        //如果是dmafence泳道,则不进行处理
149        //@ts-ignore
150        px = list[i][startKey] / ((endNS - startNS) / width);
151      } else {
152        //@ts-ignore
153        px = Math.floor(list[i][startKey] / ((endNS - startNS) / width));
154      } //@ts-ignore
155      list[i].px = px;
156      //@ts-ignore
157      if (flag === px && arr[arr.length - 1] && list[i][durKey] > arr[arr.length - 1][durKey]) {
158        arr[arr.length - 1] = list[i];
159      }
160      if (flag !== px) {
161        flag = px;
162        arr.push(list[i]);
163      }
164    }
165  }
166  return arr;
167}
168
169export function filterDataByGroupLayer(
170  list: unknown[],
171  layerKey: string,
172  startKey: string,
173  durKey: string,
174  startNS: number,
175  endNS: number,
176  width: number
177): unknown[] {
178  let arr = findRange(list, { startKey, durKey, startNS, endNS });
179  arr = arr.map((it) => {
180    //@ts-ignore
181    it.px = Math.floor(it[startKey] / ((endNS - startNS) / width) + it[layerKey] * width);
182    //设置临时变量durTmp 用于参与计算,分组后有dur为-1的数据按最长宽度显示
183    //@ts-ignore
184    it.durTmp =
185      //@ts-ignore
186      it[durKey] === -1 || it[durKey] === null || it[durKey] === undefined ? endNS - it[startKey] : it[durKey];
187    return it;
188  });
189  let group = groupBy(arr, 'px');
190  let res: unknown[] = [];
191  //@ts-ignore
192  Reflect.ownKeys(group).map((key: unknown) => {
193    //@ts-ignore
194    let childArray = (group[key] as unknown[]).reduce((p, c) => (p.durTmp > c.durTmp ? p : c));
195    res.push(childArray);
196  });
197  return res;
198}
199
200function groupBy(array: Array<unknown>, key: string): unknown {
201  return array.reduce((pre, current, index, arr) => {
202    //@ts-ignore
203    (pre[current[key]] = pre[current[key]] || []).push(current);
204    return pre;
205  }, {});
206}
207
208function findRange(
209  fullData: Array<unknown>,
210  condition: {
211    startKey: string;
212    startNS: number;
213    durKey: string;
214    endNS: number;
215  }
216): Array<unknown> {
217  return fullData.filter(
218    (
219      it
220      //@ts-ignore
221    ) => it[condition.startKey] + it[condition.durKey] >= condition.startNS && it[condition.startKey] <= condition.endNS
222  );
223}
224