1 /*
2  * Copyright (c) 2021-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 "entities/user_entity.h"
17 
18 #include "ohos_account_kits_impl.h"
19 #include "stats_log.h"
20 
21 #include "battery_stats_parser.h"
22 #include "battery_stats_service.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 }
28 
UserEntity()29 UserEntity::UserEntity()
30 {
31     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_USER;
32 }
33 
GetEntityPowerMah(int32_t uidOrUserId)34 double UserEntity::GetEntityPowerMah(int32_t uidOrUserId)
35 {
36     double power = StatsUtils::DEFAULT_VALUE;
37     auto iter = userPowerMap_.find(uidOrUserId);
38     if (iter != userPowerMap_.end()) {
39         power = iter->second;
40         STATS_HILOGD(COMP_SVC, "Get user power consumption: %{public}lfmAh for user id: %{public}d",
41             power, uidOrUserId);
42     } else {
43         STATS_HILOGD(COMP_SVC,
44             "No user power consumption related with user id: %{public}d found, return 0", uidOrUserId);
45     }
46     return power;
47 }
48 
AggregateUserPowerMah(int32_t userId,double power)49 void UserEntity::AggregateUserPowerMah(int32_t userId, double power)
50 {
51     auto iter = userPowerMap_.find(userId);
52     if (iter != userPowerMap_.end()) {
53         iter->second += power;
54         STATS_HILOGD(COMP_SVC, "Add user power consumption: %{public}lfmAh for user id: %{public}d",
55             power, userId);
56     } else {
57         STATS_HILOGD(COMP_SVC, "Create user power consumption: %{public}lfmAh for user id: %{public}d",
58             power, userId);
59         userPowerMap_.insert(std::pair<int32_t, double>(userId, power));
60     }
61 }
62 
Calculate(int32_t uid)63 void UserEntity::Calculate(int32_t uid)
64 {
65     for (auto& iter : userPowerMap_) {
66         std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
67         statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_USER);
68         statsInfo->SetUserId(iter.first);
69         statsInfo->SetPower(iter.second);
70         statsInfoList_.push_back(statsInfo);
71     }
72 }
73 
Reset()74 void UserEntity::Reset()
75 {
76     // Reset app user total power consumption
77     for (auto& iter : userPowerMap_) {
78         iter.second = StatsUtils::DEFAULT_VALUE;
79     }
80 }
81 } // namespace PowerMgr
82 } // namespace OHOS
83 
84