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 PluginConvertUtils { 17 private static crlf: string = "\n"; 18 private static leftBrace: string = "{" 19 private static rightBrace: string = "}" 20 21 public static createHdcCmd(requestString: string, outputPath: string, time: number) { 22 return "hiprofiler_cmd \\" + this.crlf 23 + " -c - \\" + this.crlf 24 + " -o " + outputPath + " \\" + this.crlf 25 + " -t " + time + " \\" + this.crlf 26 + " -s \\" + this.crlf 27 + " -k \\" + this.crlf 28 + "<<CONFIG" 29 + requestString 30 + "CONFIG" 31 } 32 33 public static BeanToCmdTxt(bean: any, needColon: boolean): string { 34 return this.handleObj(bean, 0, needColon, 1); 35 } 36 37 public static BeanToCmdTxtWithObjName(bean: any, needColon: boolean, objName: string, 38 spacesNumber: number): string { 39 return objName + ": {" + this.handleObj(bean, 0, needColon, spacesNumber) + "}"; 40 } 41 42 private static handleObj(bean: object, indentation: number, needColon: boolean, spacesNumber: number): string { 43 let prefixText: string = ""; 44 if (indentation == 0) { 45 prefixText = prefixText + this.crlf; 46 } else { 47 prefixText = prefixText + " ".repeat(spacesNumber) + this.leftBrace + this.crlf; 48 } 49 if (bean) { 50 for (const [key, value] of Object.entries(bean)) { 51 const repeatedKey = Array.isArray(value); 52 if (repeatedKey) { 53 prefixText = prefixText + this.handleArray(key, value, indentation, needColon, spacesNumber); 54 } else { 55 switch (typeof value) { 56 case "bigint": 57 prefixText = prefixText 58 + ' '.repeat(spacesNumber).repeat(indentation + 1) 59 + this.humpToSnake(key) 60 + ": " 61 + value.toString() 62 + this.crlf 63 break 64 case "boolean": 65 prefixText = prefixText 66 + ' '.repeat(spacesNumber).repeat(indentation + 1) 67 + this.humpToSnake(key) 68 + ": " 69 + value.toString() 70 + this.crlf 71 break 72 case "number": 73 if (value == 0 && !needColon) { 74 break; 75 } 76 prefixText = prefixText 77 + ' '.repeat(spacesNumber).repeat(indentation + 1) 78 + this.humpToSnake(key) 79 + ": " 80 + value.toString() 81 + this.crlf 82 break 83 case "string": 84 if (value == '') { 85 break 86 } 87 if (value.startsWith("LOG_") || value.startsWith("IO_REPORT")) { 88 prefixText = prefixText 89 + ' '.repeat(spacesNumber).repeat(indentation + 1) 90 + this.humpToSnake(key) 91 + ": " 92 + value.toString() 93 + this.crlf 94 } else { 95 prefixText = prefixText 96 + ' '.repeat(spacesNumber).repeat(indentation + 1) 97 + this.humpToSnake(key) 98 + ": \"" 99 + value.toString() 100 + "\"" 101 + this.crlf 102 } 103 break 104 case "object": 105 default: 106 if (needColon) { 107 prefixText = prefixText 108 + ' '.repeat(spacesNumber).repeat(indentation + 1) 109 + this.humpToSnake(key) 110 + ": " 111 + this.handleObj(value, indentation + 1, needColon, spacesNumber) 112 + "" 113 + this.crlf 114 } else { 115 prefixText = prefixText 116 + ' '.repeat(spacesNumber).repeat(indentation + 1) 117 + this.humpToSnake(key) 118 + this.handleObj(value, indentation + 1, needColon, spacesNumber) 119 + "" 120 + this.crlf 121 } 122 } 123 } 124 } 125 } 126 if (indentation == 0) { 127 return prefixText 128 } else { 129 return prefixText + ' '.repeat(spacesNumber).repeat(indentation) + this.rightBrace; 130 } 131 } 132 133 private static handleArray(key: string, arr: Array<any>, indentation: number, 134 needColon: boolean, spacesNumber: number): string { 135 let text = ""; 136 arr.forEach(arrValue => { 137 switch (typeof arrValue) { 138 case "bigint": 139 text = text 140 + ' '.repeat(spacesNumber).repeat(indentation + 1) 141 + this.humpToSnake(key) 142 + ": " 143 + arrValue.toString() 144 + this.crlf 145 break 146 case "boolean": 147 text = text 148 + ' '.repeat(spacesNumber).repeat(indentation + 1) 149 + this.humpToSnake(key) 150 + ": " 151 + arrValue.toString() 152 + this.crlf 153 break 154 case "number": 155 text = text 156 + ' '.repeat(spacesNumber).repeat(indentation + 1) 157 + this.humpToSnake(key) 158 + ": " 159 + arrValue.toString() 160 + this.crlf 161 break 162 case "string": 163 if (arrValue == '') { 164 break 165 } 166 if (arrValue.startsWith("VMEMINFO") || arrValue.startsWith("PMEM")) { 167 text = text 168 + ' '.repeat(spacesNumber).repeat(indentation + 1) 169 + this.humpToSnake(key) 170 + ": " 171 + arrValue.toString() 172 + this.crlf 173 } else { 174 text = text 175 + ' '.repeat(spacesNumber).repeat(indentation + 1) 176 + this.humpToSnake(key) 177 + ": \"" 178 + arrValue.toString() 179 + "\"" 180 + this.crlf 181 } 182 break 183 case "object": 184 default: 185 if (needColon) { 186 text = text 187 + ' '.repeat(spacesNumber).repeat(indentation + 1) 188 + this.humpToSnake(key) 189 + ": " 190 + this.handleObj(arrValue, indentation + 1, needColon, spacesNumber) 191 + "" 192 + this.crlf 193 } else { 194 text = text 195 + ' '.repeat(spacesNumber).repeat(indentation + 1) 196 + this.humpToSnake(key) 197 + this.handleObj(arrValue, indentation + 1, needColon, spacesNumber) 198 + "" 199 + this.crlf 200 } 201 } 202 }) 203 return text; 204 } 205 206 // 驼峰转snake 207 private static humpToSnake(humpString: string): string { 208 return humpString.replace(/[A-Z]/g, (value) => '_' + value.toLowerCase()); 209 } 210} 211 212