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
16#include "statistics_exception.h"
17
18#include <iomanip>
19#include <iostream>
20#include <map>
21#include <sstream>
22
23#include "wukong_util.h"
24
25namespace OHOS {
26namespace WuKong {
27namespace {
28const uint32_t DECIMAL_LENGTH = 2;
29const float PERCENTAGE = 100.0;
30}  // namespace
31void StatisticsException::StatisticsDetail(std::vector<std::map<std::string, std::string>> srcDatas,
32                                           std::map<std::string, std::shared_ptr<Table>> &destTables)
33{
34    std::stringstream bufferStream;
35    for (auto srcDatasIter : srcDatas) {
36        std::string crashType;
37        // check exception name
38        if (srcDatasIter.count("exception") == 0) {
39            return;
40        }
41        crashType = srcDatasIter["exception"];
42        // check app is insert apps
43        std::vector<std::string>::iterator crashTypesIter = find(crashTypes_.begin(), crashTypes_.end(), crashType);
44        if (crashTypesIter == crashTypes_.end()) {
45            DEBUG_LOG_STR("crashType{%s} init", crashType.c_str());
46            crashTypes_.push_back(crashType);
47            exceptionTypeCount_[crashType] = 1;
48        } else {
49            exceptionTypeCount_[crashType]++;
50        }
51        exceptionTotal_++;
52    }
53
54    std::vector<std::string> line;
55    for (auto crashTypesIter : crashTypes_) {
56        std::string proportionStr;
57        line.push_back(crashTypesIter);
58        int curExceptionTypeCount = exceptionTypeCount_[crashTypesIter];
59        DEBUG_LOG_STR("curExceptionTypeCount{%d}", curExceptionTypeCount);
60        line.push_back(std::to_string(curExceptionTypeCount));
61        if (exceptionTotal_ <= 0) {
62            ERROR_LOG("statistics error");
63            return;
64        }
65        float proportion = (curExceptionTypeCount * PERCENTAGE) / exceptionTotal_;
66        bufferStream.str("");
67        bufferStream << std::setiosflags(std::ios::fixed) << std::setprecision(DECIMAL_LENGTH) << proportion;
68        proportionStr = bufferStream.str() + "%";
69        line.push_back(proportionStr);
70        record_.push_back(line);
71        line.clear();
72    }
73    if (exceptionTotal_ != 0) {
74        line = {"total", std::to_string(exceptionTotal_), "100%"};
75        record_.push_back(line);
76    }
77    std::shared_ptr<Table> table = std::make_shared<Table>(headers_, record_);
78    record_.clear();
79    table->SetName("exception");
80    table->SetDetail("statistics");
81    destTables["exception"] = table;
82}
83}  // namespace WuKong
84}  // namespace OHOS
85