• 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
16export class Utils {
17    private static statusMap: Map<string, string> = new Map<string, string>();
18    private static instance: Utils | null = null;
19
20    constructor() {
21        Utils.statusMap.set("D", "Uninterruptible Sleep");
22        Utils.statusMap.set("S", "Sleeping");
23        Utils.statusMap.set("R", "Runnable");
24        Utils.statusMap.set("Running", "Running");
25        Utils.statusMap.set("R+", "Runnable (Preempted)");
26        Utils.statusMap.set("DK", "Uninterruptible Sleep + Wake Kill");
27        Utils.statusMap.set("I", "Task Dead");
28        Utils.statusMap.set("T", "Traced");
29        Utils.statusMap.set("t", "Traced");
30        Utils.statusMap.set("X", "Exit (Dead)");
31        Utils.statusMap.set("Z", "Exit (Zombie)");
32        Utils.statusMap.set("K", "Wake Kill");
33        Utils.statusMap.set("W", "Waking");
34        Utils.statusMap.set("P", "Parked");
35        Utils.statusMap.set("N", "No Load");
36    }
37
38    public static getInstance(): Utils {
39        if (Utils.instance == null) {
40            Utils.instance = new Utils();
41        }
42        return Utils.instance
43    }
44
45    public static getEndState(state: string): string | null | undefined {
46        if (Utils.getInstance().getStatusMap().has(state)) {
47            return Utils.getInstance().getStatusMap().get(state);
48        } else {
49            if ("" == state || state == null) {
50                return "";
51            }
52            return "Unknown State";
53        }
54    }
55
56    public static getStateColor(state: string): string {
57        if (state == "D" || state == "DK") {
58            return "#f19b38"
59        } else if (state == "R" || state == "R+") {
60            return "#a0b84d"
61        } else if (state == "I") {
62            return "#673ab7"
63        } else if (state == "Running") {
64            return "#467b3b"
65        } else if (state == "S") {
66            return "#e0e0e0"
67        } else {
68            return "#ff6e40"
69        }
70    }
71
72    public static getTimeString(ns: number): string {
73        let currentNs = ns
74        let hour1 = 3600_000_000_000
75        let minute1 = 60_000_000_000
76        let second1 = 1_000_000_000;
77        let millisecond1 = 1_000_000;
78        let microsecond1 = 1_000;
79        let res = "";
80        if (currentNs >= hour1) {
81            res += Math.floor(currentNs / hour1) + "h ";
82            currentNs = currentNs - Math.floor(currentNs / hour1) * hour1
83        }
84        if (currentNs >= minute1) {
85            res += Math.floor(currentNs / minute1) + "m ";
86            currentNs = currentNs - Math.floor(ns / minute1) * minute1
87        }
88        if (currentNs >= second1) {
89            res += Math.floor(currentNs / second1) + "s ";
90            currentNs = currentNs - Math.floor(currentNs / second1) * second1
91        }
92        if (currentNs >= millisecond1) {
93            res += Math.floor(currentNs / millisecond1) + "ms ";
94            currentNs = currentNs - Math.floor(currentNs / millisecond1) * millisecond1
95        }
96        if (currentNs >= microsecond1) {
97            res += Math.floor(currentNs / microsecond1) + "μs ";
98            currentNs = currentNs - Math.floor(currentNs / microsecond1) * microsecond1
99        }
100        if (currentNs > 0) {
101            res += currentNs + "ns ";
102        }
103        if (res == "") {
104            res = ns + "";
105        }
106        return res
107    }
108
109    public static getProbablyTime(ns: number): string {
110        let currentNs = ns
111        let hour1 = 3600_000_000_000
112        let minute1 = 60_000_000_000
113        let second1 = 1_000_000_000;
114        let millisecond1 = 1_000_000;
115        let microsecond1 = 1_000;
116        let res = "";
117        if (currentNs >= hour1) {
118            res += (currentNs / hour1).toFixed(2) + "h ";
119        }else if (currentNs >= minute1) {
120            res += (currentNs / minute1).toFixed(2) + "m ";
121        }else if (currentNs >= second1) {
122            res += (currentNs / second1).toFixed(2) + "s ";
123        }else if (currentNs >= millisecond1) {
124            res += (currentNs / millisecond1).toFixed(2) + "ms ";
125        }else if (currentNs >= microsecond1) {
126            res += (currentNs / microsecond1).toFixed(2) + "μs ";
127        }else if (currentNs > 0) {
128            res += currentNs + "ns ";
129        }else if (res == "") {
130            res = ns + "";
131        }
132        return res
133    }
134
135    public static getTimeStringHMS(ns: number): string {
136        let currentNs = ns
137        let hour1 = 3600_000_000_000
138        let minute1 = 60_000_000_000
139        let second1 = 1_000_000_000; // 1 second
140        let millisecond1 = 1_000_000; // 1 millisecond
141        let microsecond1 = 1_000; // 1 microsecond
142        let res = "";
143        if (currentNs >= hour1) {
144            res += Math.floor(currentNs / hour1) + ":";
145            currentNs = currentNs - Math.floor(currentNs / hour1) * hour1
146        }
147        if (currentNs >= minute1) {
148            res += Math.floor(currentNs / minute1) + ":";
149            currentNs = currentNs - Math.floor(ns / minute1) * minute1
150        }
151        if (currentNs >= second1) {
152            res += Math.floor(currentNs / second1) + ":";
153            currentNs = currentNs - Math.floor(currentNs / second1) * second1
154        }
155        if (currentNs >= millisecond1) {
156            res += Math.floor(currentNs / millisecond1) + ".";
157            currentNs = currentNs - Math.floor(currentNs / millisecond1) * millisecond1
158        }
159        if (currentNs >= microsecond1) {
160            res += Math.floor(currentNs / microsecond1) + ".";
161            currentNs = currentNs - Math.floor(currentNs / microsecond1) * microsecond1
162        }
163        if (currentNs > 0) {
164            res += currentNs + "";
165        }
166        if (res == "") {
167            res = ns + "";
168        }
169        return res
170    }
171
172    public static getByteWithUnit(bytes: number): string {
173        if (bytes < 0) {
174            return "-" + this.getByteWithUnit(Math.abs(bytes))
175        }
176        let currentBytes = bytes
177        let kb1 = 1 << 10;
178        let mb1 = 1 << 10 << 10;
179        let gb1 = 1 << 10 << 10 << 10; // 1 gb
180        let res = ""
181        if (currentBytes > gb1) {
182            res += (currentBytes / gb1).toFixed(2) + " Gb";
183        } else if (currentBytes > mb1) {
184            res += (currentBytes / mb1).toFixed(2) + " Mb";
185        } else if (currentBytes > kb1) {
186            res += (currentBytes / kb1).toFixed(2) + " Kb";
187        } else {
188            res += Math.round(currentBytes) + " byte";
189        }
190        return res
191    }
192
193    public static groupByMap(array: Array<any>, key: string) {
194        let result = new Map();
195        array.forEach(item => {
196            let value = item[key];
197            if (!result.has(value)) {
198                result.set(value, [])
199            }
200            result.get(value).push(item);
201        })
202        return result;
203    }
204
205    public static groupBy(array: Array<any>, key: string) {
206        return array.reduce((pre, current, index, arr) => {
207            (pre[current[key]] = pre[current[key]] || []).push(current);
208            return pre;
209        }, {});
210    }
211
212    public static timeMsFormat2p(ns: number) {
213        let currentNs = ns
214        let hour1 = 3600_000
215        let minute1 = 60_000
216        let second1 = 1_000; // 1 second
217        let res = ""
218        if (currentNs >= hour1) {
219            res += Math.floor(currentNs / hour1).toFixed(2) + "h"
220            return res
221        }
222        if (currentNs >= minute1) {
223            res += Math.floor(currentNs / minute1).toFixed(2) + "min"
224            return res
225        }
226        if (currentNs >= second1) {
227            res += Math.floor(currentNs / second1).toFixed(2) + "s"
228            return res
229        }
230        if (currentNs > 0) {
231            res += currentNs.toFixed(2) + "ms";
232            return res
233        }
234        if (res == "") {
235            res = "0s";
236        }
237        return res
238    }
239
240    public static uuid(): string {
241        // @ts-ignore
242        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
243    }
244
245    public static getBinaryByteWithUnit(bytes: number): string {
246        if (bytes == 0) {
247            return "0Bytes"
248        }
249        let currentBytes = bytes
250        let kib1 = 1024
251        let mib1 = 1024 * 1024
252        let gib1 = 1024 * 1024 * 1024;
253        let res = ""
254        if (currentBytes >= gib1) {
255            res += (currentBytes / gib1).toFixed(2) + "Gib";
256        } else if (currentBytes >= mib1) {
257            res += (currentBytes / mib1).toFixed(2) + "Mib";
258        } else if (currentBytes >= kib1) {
259            res += (currentBytes / kib1).toFixed(2) + "kib";
260        } else {
261            res += currentBytes.toFixed(2) + "Bytes";
262        }
263        return res
264    }
265
266    public static getTimeStampHMS(ns: number): string {
267        let currentNs = ns
268        let hour1 = 3600_000_000_000
269        let minute1 = 60_000_000_000
270        let second1 = 1_000_000_000; // 1 second
271        let millisecond1 = 1_000_000; // 1 millisecond
272        let microsecond1 = 1_000; // 1 microsecond
273        let res = "";
274        if (currentNs >= hour1) {
275            res += this.getCompletionTime(Math.floor(currentNs / hour1), 2) + ":";
276            currentNs = currentNs - Math.floor(currentNs / hour1) * hour1
277        }
278        if (currentNs >= minute1) {
279            res += this.getCompletionTime(Math.floor(currentNs / minute1), 2) + ":";
280            currentNs = currentNs - Math.floor(ns / minute1) * minute1
281        }
282        if (currentNs >= second1) {
283            res += this.getCompletionTime(Math.floor(currentNs / second1), 2) + ":";
284            currentNs = currentNs - Math.floor(currentNs / second1) * second1
285        } else {
286            res += '00:'
287        }
288        if (currentNs >= millisecond1) {
289            res += this.getCompletionTime(Math.floor(currentNs / millisecond1), 3) + ".";
290            currentNs = currentNs - Math.floor(currentNs / millisecond1) * millisecond1
291        } else {
292            res += "000."
293        }
294        if (currentNs >= microsecond1) {
295            res += this.getCompletionTime(Math.floor(currentNs / microsecond1), 3) + ".";
296            currentNs = currentNs - Math.floor(currentNs / microsecond1) * microsecond1
297        } else {
298            res += "000"
299        }
300        if (currentNs > 0) {
301            res += this.getCompletionTime(currentNs, 3);
302        }
303        if (res == "") {
304            res = ns + "";
305        }
306        return res
307    }
308
309    public static getDurString(ns: number): string {
310        let currentNs = ns
311        let second1 = 1000000000;
312        let millisecond1 = 1000000;
313        let res = "";
314        if (currentNs >= second1) {
315            let cu = currentNs / second1
316            res += cu.toFixed(3) + " s "
317            return res;
318        }
319        if (currentNs >= millisecond1) {
320            res += Math.floor(currentNs / millisecond1) + " ms ";
321            return res;
322        }
323        if (res == "") {
324            res = ns + "";
325        }
326        return res
327    }
328
329    private static getCompletionTime(time: number, maxLength: number): string {
330        if (maxLength == 2) {
331            if (time.toString().length == 2) {
332                return '' + time;
333            } else {
334                return '0' + time;
335            }
336        } else if (maxLength == 3) {
337            if (time.toString().length == 3) {
338                return time.toString();
339            } else if (time.toString().length == 2) {
340                return '0' + time;
341            } else {
342                return '00' + time;
343            }
344        } else {
345            return '0'
346        }
347    }
348
349    public getStatusMap(): Map<string, string> {
350        return Utils.statusMap;
351    }
352
353    public static removeDuplicates(array1:any[],array2:any[],key:string){
354        let obj:any = {};
355        return array1.concat(array2).reduce(function (total, item) {
356            if (!obj[item[key]]) {
357                obj[item[key]] = true
358                total.push(item)
359            }
360            return total;
361        }, []);
362    }
363
364}
365