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