• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
28 }
29 
UserEntity()30 UserEntity::UserEntity()
31 {
32     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_USER;
33 }
34 
GetEntityPowerMah(int32_t uidOrUserId)35 double UserEntity::GetEntityPowerMah(int32_t uidOrUserId)
36 {
37     double power = StatsUtils::DEFAULT_VALUE;
38     auto iter = userPowerMap_.find(uidOrUserId);
39     if (iter != userPowerMap_.end()) {
40         power = iter->second;
41         STATS_HILOGD(COMP_SVC, "Get user power consumption: %{public}lfmAh for user id: %{public}d",
42             power, uidOrUserId);
43     } else {
44         STATS_HILOGD(COMP_SVC,
45             "No user power consumption related with user id: %{public}d found, return 0", uidOrUserId);
46     }
47     return power;
48 }
49 
AggregateUserPowerMah(int32_t userId,double power)50 void UserEntity::AggregateUserPowerMah(int32_t userId, double power)
51 {
52     auto iter = userPowerMap_.find(userId);
53     if (iter != userPowerMap_.end()) {
54         iter->second += power;
55         STATS_HILOGD(COMP_SVC, "Add user power consumption: %{public}lfmAh for user id: %{public}d",
56             power, userId);
57     } else {
58         STATS_HILOGD(COMP_SVC, "Create user power consumption: %{public}lfmAh for user id: %{public}d",
59             power, userId);
60         userPowerMap_.insert(std::pair<int32_t, double>(userId, power));
61     }
62 }
63 
Calculate(int32_t uid)64 void UserEntity::Calculate(int32_t uid)
65 {
66     for (auto& iter : userPowerMap_) {
67         std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
68         statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_USER);
69         statsInfo->SetUserId(iter.first);
70         statsInfo->SetPower(iter.second);
71         statsInfoList_.push_back(statsInfo);
72     }
73 }
74 
Reset()75 void UserEntity::Reset()
76 {
77     // Reset app user total power consumption
78     for (auto& iter : userPowerMap_) {
79         iter.second = StatsUtils::DEFAULT_VALUE;
80     }
81 }
82 } // namespace PowerMgr
83 } // namespace OHOS
84 
85