1/*
2 * Copyright (c) 2024 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
16import { ApiStatisticsInfo } from '../../typedef/statistics/ApiStatistics';
17import { ApiCountInfo } from '../../typedef/count/ApiCount';
18import { FunctionUtils, KitData } from '../../utils/FunctionUtils';
19
20export class ApiCountHelper {
21  /**
22   * 对api统计工具的结果进行处理,计算同一个api文件里的api个数
23   * 
24   * @param staticApiInfos 
25   * @returns { ApiCountInfo[] }
26   */
27  static countApi(staticApiInfos: ApiStatisticsInfo[]): ApiCountInfo[] {
28    const apiCountInfos: ApiCountInfo[] = [];
29    const subSystemData: KitData = FunctionUtils.readKitFile();
30    const filePathSet: Set<string> = subSystemData.filePathSet;
31    const subsystemMap: Map<string, string> = subSystemData.subsystemMap;
32    const kitNameMap: Map<string, string> = subSystemData.kitNameMap;
33    filePathSet.forEach((filePath: string) => {
34      let apiNumber: number = 0;
35      let kitName: string = '';
36      let apiCountInfo: ApiCountInfo = new ApiCountInfo();
37      staticApiInfos.forEach((staticApiInfo: ApiStatisticsInfo) => {
38        if (filePath === staticApiInfo.getFilePath().replace(/\\/g, '/')) {
39          apiNumber++;
40          kitName = staticApiInfo.getKitInfo();
41        }
42      });
43      if (apiNumber > 0) {
44        apiCountInfo
45          .setFilePath(`api/${filePath}`)
46          .setApiNumber(apiNumber)
47          .setKitName(kitName === '' ? kitNameMap.get(filePath) : kitName)
48          .setsubSystem(subsystemMap.get(filePath));
49        apiCountInfos.push(apiCountInfo);
50      }
51    });
52    return apiCountInfos;
53  }
54}
55
56