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_ciimport { PerfCall, PerfFile } from '../../bean/PerfProfile';
17fb726d48Sopenharmony_ciimport { info } from '../../../log/Log';
18fb726d48Sopenharmony_ciimport { SpHiPerf } from './SpHiPerf';
19fb726d48Sopenharmony_ciimport { procedurePool } from '../../database/Procedure';
20fb726d48Sopenharmony_ciimport { SpSystemTrace } from '../SpSystemTrace';
21fb726d48Sopenharmony_ciimport { queryPerfFiles } from '../../database/sql/Perf.sql';
22fb726d48Sopenharmony_ci
23fb726d48Sopenharmony_ciexport class PerfDataQuery {
24fb726d48Sopenharmony_ci  filesData: unknown = {};
25fb726d48Sopenharmony_ci  callChainMap: Map<number, PerfCall> = new Map<number, PerfCall>();
26fb726d48Sopenharmony_ci
27fb726d48Sopenharmony_ci  async initPerfCache(): Promise<void> {
28fb726d48Sopenharmony_ci    await this.initPerfCallChainMap();
29fb726d48Sopenharmony_ci    await this.initPerfFiles();
30fb726d48Sopenharmony_ci  }
31fb726d48Sopenharmony_ci
32fb726d48Sopenharmony_ci  async initPerfCallChainMap(): Promise<void> {
33fb726d48Sopenharmony_ci    this.callChainMap.clear();
34fb726d48Sopenharmony_ci  }
35fb726d48Sopenharmony_ci
36fb726d48Sopenharmony_ci  async initPerfFiles(): Promise<void> {
37fb726d48Sopenharmony_ci    let files = await queryPerfFiles();
38fb726d48Sopenharmony_ci    info('PerfFiles Data size is: ', files!.length);
39fb726d48Sopenharmony_ci    files.forEach((file) => {
40fb726d48Sopenharmony_ci      // @ts-ignore
41fb726d48Sopenharmony_ci      this.filesData[file.fileId] = this.filesData[file.fileId] || [];
42fb726d48Sopenharmony_ci      PerfFile.setFileName(file); // @ts-ignore
43fb726d48Sopenharmony_ci      this.filesData[file.fileId].push(file);
44fb726d48Sopenharmony_ci    });
45fb726d48Sopenharmony_ci    const data = {
46fb726d48Sopenharmony_ci      fValue: SpHiPerf.stringResult?.fValue,
47fb726d48Sopenharmony_ci    };
48fb726d48Sopenharmony_ci    let results = await new Promise<unknown>((resolve, reject) => {
49fb726d48Sopenharmony_ci      procedurePool.submitWithName('logic0', 'perf-init', data, undefined, (res: unknown) => {
50fb726d48Sopenharmony_ci        resolve(res);
51fb726d48Sopenharmony_ci      });
52fb726d48Sopenharmony_ci    }); // @ts-ignore
53fb726d48Sopenharmony_ci    this.callChainMap = results as unknown;
54fb726d48Sopenharmony_ci    info('Perf Files Data initialized');
55fb726d48Sopenharmony_ci  }
56fb726d48Sopenharmony_ci
57fb726d48Sopenharmony_ci  getLibName(fileId: number, symbolId: number): string {
58fb726d48Sopenharmony_ci    let name = 'unknown';
59fb726d48Sopenharmony_ci    if (symbolId === -1) {
60fb726d48Sopenharmony_ci      // @ts-ignore
61fb726d48Sopenharmony_ci      if (this.filesData[fileId] && this.filesData[fileId].length > 0) {
62fb726d48Sopenharmony_ci        // @ts-ignore
63fb726d48Sopenharmony_ci        name = this.filesData[fileId][0].fileName;
64fb726d48Sopenharmony_ci      }
65fb726d48Sopenharmony_ci    } else {
66fb726d48Sopenharmony_ci      // @ts-ignore
67fb726d48Sopenharmony_ci      if (this.filesData[fileId] && this.filesData[fileId].length > symbolId) {
68fb726d48Sopenharmony_ci        // @ts-ignore
69fb726d48Sopenharmony_ci        name = this.filesData[fileId][symbolId].fileName;
70fb726d48Sopenharmony_ci      }
71fb726d48Sopenharmony_ci    }
72fb726d48Sopenharmony_ci    return name.replace(/</g, '&lt;').replace(/>/g, '&gt;');
73fb726d48Sopenharmony_ci  }
74fb726d48Sopenharmony_ci}
75fb726d48Sopenharmony_ci
76fb726d48Sopenharmony_ciexport const perfDataQuery = new PerfDataQuery();
77