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", "Stopped"); 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 /** 46 * Get the last status description 47 * 48 * @param state state 49 * @return String 50 */ 51 public static getEndState(state: string): string | null | undefined { 52 if (Utils.getInstance().getStatusMap().has(state)) { 53 return Utils.getInstance().getStatusMap().get(state); 54 } else { 55 if ("" == state || state == null) { 56 return ""; 57 } 58 return "Unknown State"; 59 } 60 } 61 62 public static getStateColor(state: string): string { 63 if (state == "D" || state == "DK") { 64 return "#f19b38" 65 } else if (state == "R" || state == "R+") { 66 return "#a0b84d" 67 } else if (state == "I") { 68 return "#673ab7" 69 } else if (state == "Running") { 70 return "#467b3b" 71 } else if (state == "S") { 72 return "#e0e0e0" 73 } else { 74 return "#ff6e40" 75 } 76 } 77 78 public static getTimeString(ns: number): string { 79 let currentNs = ns 80 let hour1 = 3600_000_000_000 81 let minute1 = 60_000_000_000 82 let second1 = 1_000_000_000; 83 let millisecond1 = 1_000_000; 84 let microsecond1 = 1_000; 85 let res = ""; 86 if (currentNs >= hour1) { 87 res += Math.floor(currentNs / hour1) + "h "; 88 currentNs = currentNs - Math.floor(currentNs / hour1) * hour1 89 } 90 if (currentNs >= minute1) { 91 res += Math.floor(currentNs / minute1) + "m "; 92 currentNs = currentNs - Math.floor(ns / minute1) * minute1 93 } 94 if (currentNs >= second1) { 95 res += Math.floor(currentNs / second1) + "s "; 96 currentNs = currentNs - Math.floor(currentNs / second1) * second1 97 } 98 if (currentNs >= millisecond1) { 99 res += Math.floor(currentNs / millisecond1) + "ms "; 100 currentNs = currentNs - Math.floor(currentNs / millisecond1) * millisecond1 101 } 102 if (currentNs >= microsecond1) { 103 res += Math.floor(currentNs / microsecond1) + "μs "; 104 currentNs = currentNs - Math.floor(currentNs / microsecond1) * microsecond1 105 } 106 if (currentNs > 0) { 107 res += currentNs + "ns "; 108 } 109 if (res == "") { 110 res = ns + ""; 111 } 112 return res 113 } 114 115 public static getByteWithUnit(bytes: number): string { 116 let currentBytes = bytes 117 let kb1 = 1000 118 let mb1 = 1000_000 119 let gb1 = 1_000_000_000; 120 let res = "" 121 if (currentBytes > gb1) { 122 res += (currentBytes / gb1).toFixed(2) + "Gb"; 123 } else if (currentBytes > mb1) { 124 res += (currentBytes / mb1).toFixed(2) + "Mb"; 125 } else if (currentBytes > kb1) { 126 res += (currentBytes / kb1).toFixed(2) + "kb"; 127 } else { 128 res += currentBytes + "byte"; 129 } 130 return res 131 } 132 133 public getStatusMap(): Map<string, string> { 134 return Utils.statusMap; 135 } 136} 137