• 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 
16 #include "statistics_componment.h"
17 
18 #include <algorithm>
19 #include <iomanip>
20 #include <iostream>
21 #include <sstream>
22 #include <stdlib.h>
23 
24 namespace OHOS {
25 namespace WuKong {
26 namespace {
27 const uint32_t DECIMAL_LENGTH = 2;
28 const float PERCENTAGE = 100.0;
29 }  // namespace
30 
StatisticsDetail(std::vector<std::map<std::string,std::string>> srcDatas,std::map<std::string,std::shared_ptr<Table>> & destTables)31 void StatisticsComponment::StatisticsDetail(std::vector<std::map<std::string, std::string>> srcDatas,
32                                             std::map<std::string, std::shared_ptr<Table>> &destTables)
33 {
34     SrcDatasPretreatment(srcDatas);
35     // loop bundle store componment statistics msg
36     for (auto bundle : componmentStatisticsMsg_) {
37         DEBUG_LOG_STR("start bundlename{%s}", bundle.first.c_str());
38         std::shared_ptr<ComponmentStatisticsMsg> curComponmentStatisticsMsgPtr = bundle.second;
39         uint32_t curComonmentTypeLength = curComponmentStatisticsMsgPtr->componmentTypes_.size();
40         std::vector<std::string> line;
41         std::shared_ptr<ComponmentStatisticsRecord> curComponmentStatisticsRecordPtr = nullptr;
42         std::shared_ptr<ComponmentStatisticsRecord> curBundleAllStatisticsPtr =
43             std::make_shared<ComponmentStatisticsRecord>();
44         curBundleAllStatisticsPtr->componmentType_ = "total";
45         for (uint32_t i = 0; i < curComonmentTypeLength; i++) {
46             curComponmentStatisticsRecordPtr = curComponmentStatisticsMsgPtr->componmentTypeRecord_[i];
47             UpdateLine(curComponmentStatisticsRecordPtr, curComponmentStatisticsMsgPtr->componmentTypeTotal_, line);
48             record_.push_back(line);
49             curBundleAllStatisticsPtr->execTimes_ += curComponmentStatisticsRecordPtr->execTimes_;
50             curBundleAllStatisticsPtr->inputedTimes_ += curComponmentStatisticsRecordPtr->inputedTimes_;
51             curBundleAllStatisticsPtr->expectInputTimes_ += curComponmentStatisticsRecordPtr->expectInputTimes_;
52         }
53 
54         UpdateLine(curBundleAllStatisticsPtr, curComponmentStatisticsMsgPtr->componmentTypeTotal_, line);
55         record_.push_back(line);
56         std::shared_ptr<Table> table = std::make_shared<Table>(headers_, record_);
57         table->SetName(bundle.first);
58         table->SetDetail("componment");
59         destTables[bundle.first] = table;
60         record_.clear();
61     }
62 
63     GlobalComponmentTypeStatistics();
64     std::shared_ptr<Table> globalTable = std::make_shared<Table>(headers_, record_);
65     globalTable->SetName("all");
66     globalTable->SetDetail("componment");
67     destTables["all"] = globalTable;
68     record_.clear();
69 }
70 
SrcDatasPretreatment(std::vector<std::map<std::string,std::string>> srcDatas)71 void StatisticsComponment::SrcDatasPretreatment(std::vector<std::map<std::string, std::string>> srcDatas)
72 {
73     for (auto srcData : srcDatas) {
74         DEBUG_LOG_STR("bundlename{%s} | componment{%s} | inputedTimes{%s} | componmentTotals{%s}",
75                       srcData["bundleName"].c_str(), srcData["componment"].c_str(), srcData["inputedTimes"].c_str(),
76                       srcData["componmentTotals"].c_str());
77         std::vector<std::string>::iterator globalComponmentTypesIter =
78             find(globalComponmentTypes_.begin(), globalComponmentTypes_.end(), srcData["componment"]);
79         if (globalComponmentTypesIter == globalComponmentTypes_.end()) {
80             DEBUG_LOG_STR("push componment{%s} to globalComponmentTypes_", srcData["componment"].c_str());
81             globalComponmentTypes_.push_back(srcData["componment"]);
82         }
83 
84         // check whether bundle is entered resolve create or reuse already exist  StatisticsMsgPtr
85         std::shared_ptr<ComponmentStatisticsMsg> curStatisticsMsgPtr = std::make_shared<ComponmentStatisticsMsg>();
86         std::map<std::string, std::shared_ptr<ComponmentStatisticsMsg>>::iterator componmentStatisticsMsgIter =
87             componmentStatisticsMsg_.find(srcData["bundleName"]);
88         if (componmentStatisticsMsgIter != componmentStatisticsMsg_.end()) {
89             DEBUG_LOG_STR("use inited curStatisticsMsgPtr by bundleName{%s}", srcData["bundleName"].c_str());
90             curStatisticsMsgPtr = componmentStatisticsMsg_[srcData["bundleName"]];
91         }
92         // check whether componmentType is entered resolve create or reuse already exist ComponmentStatisticsRecordPtr
93         std::shared_ptr<ComponmentStatisticsRecord> curComponmentStatisticsRecordPtr =
94             std::make_shared<ComponmentStatisticsRecord>();
95         uint32_t index = curStatisticsMsgPtr->ComponmentTypesIndex(srcData["componment"]);
96         uint32_t curComponmentTypeTotal = curStatisticsMsgPtr->componmentTypeTotal_;
97         if (index != curStatisticsMsgPtr->componmentTypes_.size()) {
98             curComponmentStatisticsRecordPtr = curStatisticsMsgPtr->componmentTypeRecord_[index];
99             DEBUG_LOG_STR("use inited curComponmentStatisticsRecordPtr in index{%d} | componment{%s}", index,
100                           srcData["componment"].c_str());
101         }
102         // update record msg
103         curComponmentStatisticsRecordPtr->componmentType_ = srcData["componment"];
104         curComponmentStatisticsRecordPtr->execTimes_++;
105         curComponmentStatisticsRecordPtr->inputedTimes_ = (unsigned int)atoi(srcData["inputedTimes"].c_str());
106         curComponmentStatisticsRecordPtr->expectInputTimes_ = (unsigned int)atoi(srcData["componmentTotals"].c_str());
107 
108         if (curStatisticsMsgPtr->componmentTypeRecord_.size() > index) {
109             curStatisticsMsgPtr->componmentTypeRecord_[index] = curComponmentStatisticsRecordPtr;
110             curStatisticsMsgPtr->componmentTypes_[index] = srcData["componment"];
111         } else {
112             curStatisticsMsgPtr->componmentTypeRecord_.push_back(curComponmentStatisticsRecordPtr);
113             curStatisticsMsgPtr->componmentTypes_.push_back(srcData["componment"]);
114         }
115 
116         curComponmentTypeTotal++;
117         DEBUG_LOG_STR("curComponmentTypeTotal{%d}", curComponmentTypeTotal);
118         curStatisticsMsgPtr->componmentTypeTotal_ = curComponmentTypeTotal;
119         componmentStatisticsMsg_[srcData["bundleName"]] = curStatisticsMsgPtr;
120         execCount_++;
121     }
122 }
123 
UpdateLine(std::shared_ptr<ComponmentStatisticsRecord> ComponmentStatisticsRecordPtr,unsigned int componmentTypeTotal,std::vector<std::string> & line)124 void StatisticsComponment::UpdateLine(std::shared_ptr<ComponmentStatisticsRecord> ComponmentStatisticsRecordPtr,
125                                       unsigned int componmentTypeTotal, std::vector<std::string> &line)
126 {
127     std::stringstream bufferStream;
128     std::string curComponmentType = ComponmentStatisticsRecordPtr->componmentType_;
129     std::string curExecTimes = std::to_string(ComponmentStatisticsRecordPtr->execTimes_);
130     std::string curProportionStr = "";
131     std::string curInputedTimes = std::to_string(ComponmentStatisticsRecordPtr->inputedTimes_);
132     std::string curExpectInputTimes = std::to_string(ComponmentStatisticsRecordPtr->expectInputTimes_);
133     std::string curCoverageStr = "";
134     if (componmentTypeTotal > 0) {
135         float proportion = (ComponmentStatisticsRecordPtr->execTimes_ * PERCENTAGE) / componmentTypeTotal;
136         bufferStream.str("");
137         bufferStream << std::setiosflags(std::ios::fixed) << std::setprecision(DECIMAL_LENGTH) << proportion;
138         curProportionStr = bufferStream.str() + "%";
139     }
140     if (ComponmentStatisticsRecordPtr->expectInputTimes_ > 0) {
141         float coverage = (ComponmentStatisticsRecordPtr->inputedTimes_ * PERCENTAGE) /
142                          ComponmentStatisticsRecordPtr->expectInputTimes_;
143         bufferStream.str("");
144         bufferStream << std::setiosflags(std::ios::fixed) << std::setprecision(DECIMAL_LENGTH) << coverage;
145         curCoverageStr = bufferStream.str() + "%";
146     }
147     DEBUG_LOG_STR(
148         "line content curComponmentType{%s} | curExecTimes{%s} | curProportionStr{%s} | curInputedTimes{%s} | "
149         "curExpectInputTimes{%s} | curCoverageStr{%s}",
150         curComponmentType.c_str(), curExecTimes.c_str(), curProportionStr.c_str(), curInputedTimes.c_str(),
151         curExpectInputTimes.c_str(), curCoverageStr.c_str());
152     line = {curComponmentType, curExecTimes, curProportionStr, curInputedTimes, curExpectInputTimes, curCoverageStr};
153 }
154 
GlobalComponmentTypeStatistics()155 void StatisticsComponment::GlobalComponmentTypeStatistics()
156 {
157     std::vector<std::string> line;
158     std::shared_ptr<ComponmentStatisticsRecord> globalAllStatisticsPtr = std::make_shared<ComponmentStatisticsRecord>();
159     globalAllStatisticsPtr->componmentType_ = "total";
160     for (auto comonmentType : globalComponmentTypes_) {
161         std::shared_ptr<ComponmentStatisticsRecord> componmentTypeRecordPtr =
162             std::make_shared<ComponmentStatisticsRecord>();
163         componmentTypeRecordPtr->componmentType_ = comonmentType;
164         for (auto bundle : componmentStatisticsMsg_) {
165             std::shared_ptr<ComponmentStatisticsMsg> curComponmentStatisticsMsgPtr = bundle.second;
166             uint32_t index = curComponmentStatisticsMsgPtr->ComponmentTypesIndex(comonmentType);
167             if (curComponmentStatisticsMsgPtr->componmentTypeRecord_.size() > index) {
168                 std::shared_ptr<ComponmentStatisticsRecord> curComponmentStatisticsRecordPtr =
169                     curComponmentStatisticsMsgPtr->componmentTypeRecord_[index];
170                 componmentTypeRecordPtr->execTimes_ += curComponmentStatisticsRecordPtr->execTimes_;
171                 componmentTypeRecordPtr->inputedTimes_ += curComponmentStatisticsRecordPtr->inputedTimes_;
172                 componmentTypeRecordPtr->expectInputTimes_ += curComponmentStatisticsRecordPtr->expectInputTimes_;
173             }
174         }
175         globalAllStatisticsPtr->execTimes_ += componmentTypeRecordPtr->execTimes_;
176         globalAllStatisticsPtr->inputedTimes_ += componmentTypeRecordPtr->inputedTimes_;
177         globalAllStatisticsPtr->expectInputTimes_ += componmentTypeRecordPtr->expectInputTimes_;
178         UpdateLine(componmentTypeRecordPtr, execCount_, line);
179         record_.push_back(line);
180     }
181     UpdateLine(globalAllStatisticsPtr, execCount_, line);
182     record_.push_back(line);
183 }
184 }  // namespace WuKong
185 }  // namespace OHOS
186