1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciexport class PluginConvertUtils {
17fb726d48Sopenharmony_ci  private static crlf: string = '\n';
18fb726d48Sopenharmony_ci  private static leftBrace: string = '{';
19fb726d48Sopenharmony_ci  private static rightBrace: string = '}';
20fb726d48Sopenharmony_ci  static pluginConfig: unknown[] = [];
21fb726d48Sopenharmony_ci
22fb726d48Sopenharmony_ci  public static createHdcCmd(requestString: string, outputPath: string, time: number): string {
23fb726d48Sopenharmony_ci    return `hiprofiler_cmd \\${this.crlf}  -c - \\${this.crlf}  -o ${outputPath} \\${this.crlf}  -t ${time} \\${this.crlf}  -s \\${this.crlf}  -k \\${this.crlf}<<CONFIG${requestString}CONFIG`;
24fb726d48Sopenharmony_ci  }
25fb726d48Sopenharmony_ci
26fb726d48Sopenharmony_ci  public static BeanToCmdTxt(bean: unknown, needColon: boolean): string {
27fb726d48Sopenharmony_ci    //@ts-ignore
28fb726d48Sopenharmony_ci    PluginConvertUtils.pluginConfig = bean.pluginConfigs;
29fb726d48Sopenharmony_ci    //@ts-ignore
30fb726d48Sopenharmony_ci    return this.handleObj(bean, 0, needColon, 1);
31fb726d48Sopenharmony_ci  }
32fb726d48Sopenharmony_ci
33fb726d48Sopenharmony_ci  public static BeanToCmdTxtWithObjName(
34fb726d48Sopenharmony_ci    bean: unknown,
35fb726d48Sopenharmony_ci    needColon: boolean,
36fb726d48Sopenharmony_ci    objName: string,
37fb726d48Sopenharmony_ci    spacesNumber: number
38fb726d48Sopenharmony_ci  ): string {
39fb726d48Sopenharmony_ci    //@ts-ignore
40fb726d48Sopenharmony_ci    return `${objName}: {${this.handleObj(bean, 0, needColon, spacesNumber)}}`;
41fb726d48Sopenharmony_ci  }
42fb726d48Sopenharmony_ci
43fb726d48Sopenharmony_ci  private static handleObj(bean: object, indentation: number, needColon: boolean, spacesNumber: number): string {
44fb726d48Sopenharmony_ci    let prefixText: string = '';
45fb726d48Sopenharmony_ci    if (indentation === 0) {
46fb726d48Sopenharmony_ci      prefixText = prefixText + this.crlf;
47fb726d48Sopenharmony_ci    } else {
48fb726d48Sopenharmony_ci      prefixText = prefixText + ' '.repeat(spacesNumber) + this.leftBrace + this.crlf;
49fb726d48Sopenharmony_ci    }
50fb726d48Sopenharmony_ci    if (bean) {
51fb726d48Sopenharmony_ci      prefixText = this.getPrefixText(prefixText, indentation, needColon, spacesNumber, bean);
52fb726d48Sopenharmony_ci    }
53fb726d48Sopenharmony_ci    if (indentation === 0) {
54fb726d48Sopenharmony_ci      return prefixText;
55fb726d48Sopenharmony_ci    } else {
56fb726d48Sopenharmony_ci      return prefixText + ' '.repeat(spacesNumber).repeat(indentation) + this.rightBrace;
57fb726d48Sopenharmony_ci    }
58fb726d48Sopenharmony_ci  }
59fb726d48Sopenharmony_ci
60fb726d48Sopenharmony_ci  private static getPrefixText(
61fb726d48Sopenharmony_ci    prefixText: string,
62fb726d48Sopenharmony_ci    indentation: number,
63fb726d48Sopenharmony_ci    needColon: boolean,
64fb726d48Sopenharmony_ci    spacesNumber: number,
65fb726d48Sopenharmony_ci    bean: unknown
66fb726d48Sopenharmony_ci  ): string {
67fb726d48Sopenharmony_ci    // @ts-ignore
68fb726d48Sopenharmony_ci    for (const [key, value] of Object.entries(bean)) {
69fb726d48Sopenharmony_ci      const repeatedKey = Array.isArray(value);
70fb726d48Sopenharmony_ci      if (repeatedKey) {
71fb726d48Sopenharmony_ci        prefixText = prefixText + this.handleArray(key, value, indentation, needColon, spacesNumber);
72fb726d48Sopenharmony_ci      } else {
73fb726d48Sopenharmony_ci        switch (typeof value) {
74fb726d48Sopenharmony_ci          case 'bigint':
75fb726d48Sopenharmony_ci            prefixText = this.getMontageStrings(prefixText, spacesNumber, indentation, key, value);
76fb726d48Sopenharmony_ci            break;
77fb726d48Sopenharmony_ci          case 'boolean':
78fb726d48Sopenharmony_ci            prefixText = this.getMontageStrings(prefixText, spacesNumber, indentation, key, value);
79fb726d48Sopenharmony_ci            break;
80fb726d48Sopenharmony_ci          case 'number':
81fb726d48Sopenharmony_ci            if (value === 0 && !needColon) {
82fb726d48Sopenharmony_ci              break;
83fb726d48Sopenharmony_ci            }
84fb726d48Sopenharmony_ci            prefixText = this.getMontageStrings(prefixText, spacesNumber, indentation, key, value);
85fb726d48Sopenharmony_ci            break;
86fb726d48Sopenharmony_ci          case 'string':
87fb726d48Sopenharmony_ci            if (value === '') {
88fb726d48Sopenharmony_ci              break;
89fb726d48Sopenharmony_ci            }
90fb726d48Sopenharmony_ci            prefixText = this.handleObjByStr(prefixText, value, spacesNumber, key, indentation);
91fb726d48Sopenharmony_ci            break;
92fb726d48Sopenharmony_ci          case 'object':
93fb726d48Sopenharmony_ci          default:
94fb726d48Sopenharmony_ci            prefixText = this.handleObjByDefault(prefixText, value, spacesNumber, key, indentation, needColon);
95fb726d48Sopenharmony_ci            break;
96fb726d48Sopenharmony_ci        }
97fb726d48Sopenharmony_ci      }
98fb726d48Sopenharmony_ci    }
99fb726d48Sopenharmony_ci    return prefixText;
100fb726d48Sopenharmony_ci  }
101fb726d48Sopenharmony_ci
102fb726d48Sopenharmony_ci  private static handleObjByDefault(
103fb726d48Sopenharmony_ci    prefixText: string,
104fb726d48Sopenharmony_ci    value: unknown,
105fb726d48Sopenharmony_ci    spacesNumber: number,
106fb726d48Sopenharmony_ci    key: string,
107fb726d48Sopenharmony_ci    indentation: number,
108fb726d48Sopenharmony_ci    needColon: boolean
109fb726d48Sopenharmony_ci  ): string {
110fb726d48Sopenharmony_ci    if (needColon) {
111fb726d48Sopenharmony_ci      prefixText = `${
112fb726d48Sopenharmony_ci        prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)
113fb726d48Sopenharmony_ci        //@ts-ignore
114fb726d48Sopenharmony_ci      }: ${this.handleObj(value, indentation + 1, needColon, spacesNumber)}${this.crlf}`;
115fb726d48Sopenharmony_ci    } else {
116fb726d48Sopenharmony_ci      prefixText = `${
117fb726d48Sopenharmony_ci        prefixText +
118fb726d48Sopenharmony_ci        ' '.repeat(spacesNumber).repeat(indentation + 1) +
119fb726d48Sopenharmony_ci        this.humpToSnake(key) +
120fb726d48Sopenharmony_ci        //@ts-ignore
121fb726d48Sopenharmony_ci        this.handleObj(value, indentation + 1, needColon, spacesNumber)
122fb726d48Sopenharmony_ci      }${this.crlf}`;
123fb726d48Sopenharmony_ci    }
124fb726d48Sopenharmony_ci    return prefixText;
125fb726d48Sopenharmony_ci  }
126fb726d48Sopenharmony_ci
127fb726d48Sopenharmony_ci  private static handleObjByStr(
128fb726d48Sopenharmony_ci    prefixText: string,
129fb726d48Sopenharmony_ci    value: unknown,
130fb726d48Sopenharmony_ci    spacesNumber: number,
131fb726d48Sopenharmony_ci    key: string,
132fb726d48Sopenharmony_ci    indentation: number
133fb726d48Sopenharmony_ci  ): string {
134fb726d48Sopenharmony_ci    //@ts-ignore
135fb726d48Sopenharmony_ci    if (configEnumList.indexOf(value) >= 0 || value.startsWith('IO_REPORT')) {
136fb726d48Sopenharmony_ci      prefixText = `${prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + //@ts-ignore
137fb726d48Sopenharmony_ci      this.humpToSnake(key)}: ${value.toString()}${this.crlf}`;
138fb726d48Sopenharmony_ci    } else {
139fb726d48Sopenharmony_ci      prefixText = `${prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: "${
140fb726d48Sopenharmony_ci        //@ts-ignore
141fb726d48Sopenharmony_ci        value.toString()
142fb726d48Sopenharmony_ci      }"${this.crlf}`;
143fb726d48Sopenharmony_ci    }
144fb726d48Sopenharmony_ci    return prefixText;
145fb726d48Sopenharmony_ci  }
146fb726d48Sopenharmony_ci
147fb726d48Sopenharmony_ci  private static handleArray(
148fb726d48Sopenharmony_ci    key: string,
149fb726d48Sopenharmony_ci    arr: Array<Object>,
150fb726d48Sopenharmony_ci    indentation: number,
151fb726d48Sopenharmony_ci    needColon: boolean,
152fb726d48Sopenharmony_ci    spacesNumber: number
153fb726d48Sopenharmony_ci  ): string {
154fb726d48Sopenharmony_ci    let text = '';
155fb726d48Sopenharmony_ci    arr.forEach((arrValue): void => {
156fb726d48Sopenharmony_ci      switch (typeof arrValue) {
157fb726d48Sopenharmony_ci        case 'bigint':
158fb726d48Sopenharmony_ci          text = this.handleArrayByBigint(text, spacesNumber, indentation, key, arrValue);
159fb726d48Sopenharmony_ci          break;
160fb726d48Sopenharmony_ci        case 'boolean':
161fb726d48Sopenharmony_ci          text = this.handleArrayByBoolean(text, spacesNumber, indentation, key, arrValue);
162fb726d48Sopenharmony_ci          break;
163fb726d48Sopenharmony_ci        case 'number':
164fb726d48Sopenharmony_ci          text = this.handleArrayByNumber(text, spacesNumber, indentation, key, arrValue);
165fb726d48Sopenharmony_ci          break;
166fb726d48Sopenharmony_ci        case 'string':
167fb726d48Sopenharmony_ci          if (arrValue === '') {
168fb726d48Sopenharmony_ci            break;
169fb726d48Sopenharmony_ci          }
170fb726d48Sopenharmony_ci          text = this.handleArrayByStr(text, spacesNumber, indentation, key, arrValue);
171fb726d48Sopenharmony_ci          break;
172fb726d48Sopenharmony_ci        case 'object':
173fb726d48Sopenharmony_ci        default:
174fb726d48Sopenharmony_ci          text = this.handleArrayByDefault(text, spacesNumber, indentation, key, arrValue, needColon);
175fb726d48Sopenharmony_ci      }
176fb726d48Sopenharmony_ci    });
177fb726d48Sopenharmony_ci    return text;
178fb726d48Sopenharmony_ci  }
179fb726d48Sopenharmony_ci
180fb726d48Sopenharmony_ci  private static handleArrayByBigint(
181fb726d48Sopenharmony_ci    text: string,
182fb726d48Sopenharmony_ci    spacesNumber: number,
183fb726d48Sopenharmony_ci    indentation: number,
184fb726d48Sopenharmony_ci    key: string,
185fb726d48Sopenharmony_ci    arrValue: unknown
186fb726d48Sopenharmony_ci  ): string {
187fb726d48Sopenharmony_ci    //@ts-ignore
188fb726d48Sopenharmony_ci    return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${
189fb726d48Sopenharmony_ci      this.crlf
190fb726d48Sopenharmony_ci    }`;
191fb726d48Sopenharmony_ci  }
192fb726d48Sopenharmony_ci
193fb726d48Sopenharmony_ci  private static handleArrayByBoolean(
194fb726d48Sopenharmony_ci    text: string,
195fb726d48Sopenharmony_ci    spacesNumber: number,
196fb726d48Sopenharmony_ci    indentation: number,
197fb726d48Sopenharmony_ci    key: string,
198fb726d48Sopenharmony_ci    arrValue: unknown
199fb726d48Sopenharmony_ci  ): string {
200fb726d48Sopenharmony_ci    //@ts-ignore
201fb726d48Sopenharmony_ci    return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${
202fb726d48Sopenharmony_ci      this.crlf
203fb726d48Sopenharmony_ci    }`;
204fb726d48Sopenharmony_ci  }
205fb726d48Sopenharmony_ci
206fb726d48Sopenharmony_ci  private static handleArrayByNumber(
207fb726d48Sopenharmony_ci    text: string,
208fb726d48Sopenharmony_ci    spacesNumber: number,
209fb726d48Sopenharmony_ci    indentation: number,
210fb726d48Sopenharmony_ci    key: string,
211fb726d48Sopenharmony_ci    arrValue: unknown
212fb726d48Sopenharmony_ci  ): string {
213fb726d48Sopenharmony_ci    //@ts-ignore
214fb726d48Sopenharmony_ci    return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${
215fb726d48Sopenharmony_ci      this.crlf
216fb726d48Sopenharmony_ci    }`;
217fb726d48Sopenharmony_ci  }
218fb726d48Sopenharmony_ci
219fb726d48Sopenharmony_ci  private static handleArrayByStr(
220fb726d48Sopenharmony_ci    text: string,
221fb726d48Sopenharmony_ci    spacesNumber: number,
222fb726d48Sopenharmony_ci    indentation: number,
223fb726d48Sopenharmony_ci    key: string,
224fb726d48Sopenharmony_ci    arrValue: unknown
225fb726d48Sopenharmony_ci  ): string {
226fb726d48Sopenharmony_ci    //@ts-ignore
227fb726d48Sopenharmony_ci    if (arrValue.startsWith('VMEMINFO') || arrValue.startsWith('PMEM')) {
228fb726d48Sopenharmony_ci      text = `${
229fb726d48Sopenharmony_ci        text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)
230fb726d48Sopenharmony_ci        //@ts-ignore
231fb726d48Sopenharmony_ci      }: ${arrValue.toString()}${this.crlf}`;
232fb726d48Sopenharmony_ci      //@ts-ignore
233fb726d48Sopenharmony_ci    } else if (arrValue.startsWith('REAL_BATTERY') || arrValue.startsWith('THERMAL_REPORT')) { 
234fb726d48Sopenharmony_ci      text = `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)
235fb726d48Sopenharmony_ci        //@ts-ignore
236fb726d48Sopenharmony_ci        }: ${arrValue.toString()}${this.crlf}`;
237fb726d48Sopenharmony_ci    } else {
238fb726d48Sopenharmony_ci      text = `${
239fb726d48Sopenharmony_ci        text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)
240fb726d48Sopenharmony_ci        //@ts-ignore
241fb726d48Sopenharmony_ci      }: "${arrValue.trim().toString()}"${this.crlf}`;
242fb726d48Sopenharmony_ci    }
243fb726d48Sopenharmony_ci    return text;
244fb726d48Sopenharmony_ci  }
245fb726d48Sopenharmony_ci
246fb726d48Sopenharmony_ci  private static handleArrayByDefault(
247fb726d48Sopenharmony_ci    text: string,
248fb726d48Sopenharmony_ci    spacesNumber: number,
249fb726d48Sopenharmony_ci    indentation: number,
250fb726d48Sopenharmony_ci    key: string,
251fb726d48Sopenharmony_ci    arrValue: unknown,
252fb726d48Sopenharmony_ci    needColon: boolean
253fb726d48Sopenharmony_ci  ): string {
254fb726d48Sopenharmony_ci    if (needColon) {
255fb726d48Sopenharmony_ci      text = `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${this.handleObj(
256fb726d48Sopenharmony_ci        //@ts-ignore
257fb726d48Sopenharmony_ci        arrValue,
258fb726d48Sopenharmony_ci        indentation + 1,
259fb726d48Sopenharmony_ci        needColon,
260fb726d48Sopenharmony_ci        spacesNumber
261fb726d48Sopenharmony_ci      )}${this.crlf}`;
262fb726d48Sopenharmony_ci    } else {
263fb726d48Sopenharmony_ci      text = `${
264fb726d48Sopenharmony_ci        text +
265fb726d48Sopenharmony_ci        ' '.repeat(spacesNumber).repeat(indentation + 1) +
266fb726d48Sopenharmony_ci        this.humpToSnake(key) +
267fb726d48Sopenharmony_ci        //@ts-ignore
268fb726d48Sopenharmony_ci        this.handleObj(arrValue, indentation + 1, needColon, spacesNumber)
269fb726d48Sopenharmony_ci      }${this.crlf}`;
270fb726d48Sopenharmony_ci    }
271fb726d48Sopenharmony_ci    return text;
272fb726d48Sopenharmony_ci  }
273fb726d48Sopenharmony_ci
274fb726d48Sopenharmony_ci  // 驼峰转snake
275fb726d48Sopenharmony_ci  private static humpToSnake(humpString: string): string {
276fb726d48Sopenharmony_ci    return humpString.replace(/[A-Z]/g, (value) => `_${value.toLowerCase()}`);
277fb726d48Sopenharmony_ci  }
278fb726d48Sopenharmony_ci
279fb726d48Sopenharmony_ci  private static getMontageStrings<T extends Object>(
280fb726d48Sopenharmony_ci    prefixText: string,
281fb726d48Sopenharmony_ci    spacesNumber: number,
282fb726d48Sopenharmony_ci    indentation: number,
283fb726d48Sopenharmony_ci    key: string,
284fb726d48Sopenharmony_ci    value: T
285fb726d48Sopenharmony_ci  ): string {
286fb726d48Sopenharmony_ci    return `${
287fb726d48Sopenharmony_ci      prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)
288fb726d48Sopenharmony_ci    }: ${value.toString()}${this.crlf}`;
289fb726d48Sopenharmony_ci  }
290fb726d48Sopenharmony_ci}
291fb726d48Sopenharmony_ci
292fb726d48Sopenharmony_ciconst LevelConfigEnumList: string[] = ['LEVEL_UNSPECIFIED', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'];
293fb726d48Sopenharmony_ci
294fb726d48Sopenharmony_ciexport const ffrtEnumList: string[] = ['BOOTTIME', 'REALTIME', 'REALTIME_COARSE', 'MONOTONIC',
295fb726d48Sopenharmony_ci  'MONOTONIC_COARSE', 'MONOTONIC_RAW'];
296fb726d48Sopenharmony_ci
297fb726d48Sopenharmony_ciconst configEnumList: string[] = [...LevelConfigEnumList, ...ffrtEnumList];
298