• 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 */
15export function filterData(
16  list: any[],
17  startKey: string,
18  durKey: string,
19  startNS: number,
20  endNS: number,
21  width: number
22): any[] {
23  let pns = (endNS - startNS) / width; //每个像素多少ns
24  let slice = findRange(list, {startKey, durKey, startNS, endNS});
25  let sum = 0;
26  for (let i = 0; i < slice.length; i++) {
27    if (i === slice.length - 1) {
28      if (slice[i][durKey] === undefined || slice[i][durKey] === null) {
29        slice[i][durKey] = (endNS || 0) - (slice[i][startKey] || 0);
30      }
31    } else {
32      if (slice[i][durKey] === undefined || slice[i][durKey] === null) {
33        slice[i][durKey] = (slice[i + 1][startKey] || 0) - (slice[i][startKey] || 0);
34      }
35    }
36    if (slice[i][durKey] >= pns || slice.length < 100) {
37      slice[i].v = true;
38    } else {
39      if (i > 0) {
40        let c = slice[i][startKey] - slice[i - 1][startKey] - slice[i - 1][durKey];
41        if (c < pns && sum < pns) {
42          sum += c + slice[i - 1][durKey];
43          slice[i].v = false;
44        } else {
45          slice[i].v = true;
46          sum = 0;
47        }
48      }
49    }
50  }
51  return slice.filter((it) => it.v);
52}
53
54export function filterDataByLayer(
55  list: any[],
56  layerKey: string,
57  startKey: string,
58  durKey: string,
59  startNS: number,
60  endNS: number,
61  width: number
62): any[] {
63  let pns = (endNS - startNS) / width; //每个像素多少ns
64  let sliceArray = findRange(list, {startKey, durKey, startNS, endNS});
65  let groups = groupBy(sliceArray, layerKey);
66  let res: any[] = [];
67  Reflect.ownKeys(groups).map((key: any) => {
68    let slice = groups[key] as any[];
69    if (slice.length > 0) {
70      let sum = 0;
71      for (let i = 0; i < slice.length; i++) {
72        if (i === slice.length - 1) {
73          if (slice[i][durKey] === undefined || slice[i][durKey] === null) {
74            slice[i][durKey] = (endNS || 0) - (slice[i][startKey] || 0);
75          }
76        } else {
77          if (slice[i][durKey] === undefined || slice[i][durKey] === null) {
78            slice[i][durKey] = (slice[i + 1][startKey] || 0) - (slice[i][startKey] || 0);
79          }
80        }
81        if (slice[i][durKey] >= pns || slice.length < 100) {
82          slice[i].v = true;
83        } else {
84          if (i > 0) {
85            let c = slice[i][startKey] - slice[i - 1][startKey] - slice[i - 1][durKey];
86            if (c < pns && sum < pns) {
87              sum += c + slice[i - 1][durKey];
88              slice[i].v = false;
89            } else {
90              slice[i].v = true;
91              sum = 0;
92            }
93          }
94        }
95      }
96      res.push(...slice.filter((it) => it.v));
97    }
98  });
99  return res;
100}
101
102export function filterDataByGroup(
103  list: any[],
104  startKey: string,
105  durKey: string,
106  startNS: number,
107  endNS: number,
108  width: number,
109  valueKey?: string,
110  filter?: (a: any) => boolean): any[] {
111  let arr = findRange(list, {startKey, durKey, startNS, endNS})
112  arr = arr.map((it) => {
113    it.px = Math.floor(it[startKey] / ((endNS - startNS) / width));
114    return it;
115  });
116  let group = groupBy(arr, 'px');
117  let res: Set<any> = new Set();
118  Reflect.ownKeys(group).map((key: any): void => {
119    let arr = group[key] as any[];
120    if (arr.length > 0) {
121      res.add(arr.reduce((p, c) => (p[durKey] > c[durKey]) ? p : c));
122      if (valueKey) {
123        res.add(arr.reduce((p, c) => (p[valueKey] > c[valueKey]) ? p : c));
124      }
125      if (filter) {
126        let filterArr = arr.filter(a => filter(a));
127        if (filterArr && filterArr.length > 0) {
128          res.add(filterArr.reduce((p, c) => (p[durKey] > c[durKey]) ? p : c));
129        }
130      }
131    }
132  });
133  return [...res];
134}
135
136export function filterDataByGroupLayer(
137  list: any[],
138  layerKey: string,
139  startKey: string,
140  durKey: string,
141  startNS: number,
142  endNS: number,
143  width: number
144): any[] {
145  let arr = findRange(list, {startKey, durKey, startNS, endNS});
146  arr = arr.map((it) => {
147    it.px = Math.floor(it[startKey] / ((endNS - startNS) / width) + it[layerKey] * width);
148    //设置临时变量durTmp 用于参与计算,分组后有dur为-1的数据按最长宽度显示
149    it.durTmp = it[durKey] === -1 ? (endNS - it[startKey]) : it[durKey];
150    return it;
151  });
152  let group = groupBy(arr, 'px');
153  let res: any[] = [];
154  Reflect.ownKeys(group).map((key: any) => {
155    let childArray = (group[key] as any[]).reduce((p, c) => (p.durTmp > c.durTmp) ? p : c);
156    res.push(childArray);
157  });
158  return res;
159}
160
161function groupBy(array: Array<any>, key: string): any {
162  return array.reduce((pre, current, index, arr) => {
163    (pre[current[key]] = pre[current[key]] || []).push(current);
164    return pre;
165  }, {});
166}
167
168function findRange(
169  fullData: Array<any>,
170  condition: {
171    startKey: string;
172    startNS: number;
173    durKey: string;
174    endNS: number;
175  }
176): Array<any> {
177  return fullData.filter(it => it[condition.startKey] + it[condition.durKey] >= condition.startNS && it[condition.startKey] <= condition.endNS);
178}
179