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
16import { Utils } from '../../trace/base/Utils';
17
18export const getFormatData = (data: Array<unknown>): unknown[] => {
19  let arrData: Array<unknown> = [];
20  data.forEach((item, idx): void => {
21    arrData.push({
22      index: idx + 1,
23      //@ts-ignore
24      ...item,
25      //@ts-ignore
26      avg: Utils.getProbablyTime(item.avg),
27      //@ts-ignore
28      max: Utils.getProbablyTime(item.max),
29      //@ts-ignore
30      min: Utils.getProbablyTime(item.min),
31      //@ts-ignore
32      sum: Utils.getProbablyTime(item.sum),
33    });
34  });
35  return arrData;
36};
37
38export const getDataNo = (data: Array<unknown>): unknown[] => {
39  let arrData: Array<unknown> = [];
40  data.forEach((item, idx): void => {
41    arrData.push({
42      index: idx + 1,
43      //@ts-ignore
44      ...item,
45    });
46  });
47  return arrData;
48};
49
50export const getInitializeTime = (ns: string): string => {
51  let hour1 = 3600_000_000_000;
52  let minute1 = 60_000_000_000;
53  let second1 = 1_000_000_000;
54  let millisecond1 = 1_000_000;
55  let microsecond1 = 1_000;
56
57  let res = '';
58  let currentNs = ns;
59  if (currentNs.indexOf('h') !== -1) {
60    res += Number(currentNs.slice(0, currentNs.length - 1)) * hour1;
61  } else if (currentNs.indexOf('m') !== -1) {
62    res += Number(currentNs.slice(0, currentNs.length - 1)) * minute1;
63  } else if (currentNs.indexOf('s') !== -1) {
64    res += Number(currentNs.slice(0, currentNs.length - 1)) * second1;
65  } else if (currentNs.indexOf('ms') !== -1) {
66    res += Number(currentNs.slice(0, currentNs.length - 2)) * millisecond1;
67  } else if (currentNs.indexOf('μs') !== -1) {
68    res += Number(currentNs.slice(0, currentNs.length - 2)) * microsecond1;
69  } else {
70    res += Number(currentNs);
71  }
72  return res;
73};
74