1d590543dSopenharmony_ci/*
2d590543dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d590543dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d590543dSopenharmony_ci * you may not use this file except in compliance with the License.
5d590543dSopenharmony_ci * You may obtain a copy of the License at
6d590543dSopenharmony_ci *
7d590543dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d590543dSopenharmony_ci *
9d590543dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d590543dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d590543dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d590543dSopenharmony_ci * See the License for the specific language governing permissions and
13d590543dSopenharmony_ci * limitations under the License.
14d590543dSopenharmony_ci */
15d590543dSopenharmony_ci
16d590543dSopenharmony_ci#include "entities/user_entity.h"
17d590543dSopenharmony_ci
18d590543dSopenharmony_ci#include "ohos_account_kits_impl.h"
19d590543dSopenharmony_ci#include "stats_log.h"
20d590543dSopenharmony_ci
21d590543dSopenharmony_ci#include "battery_stats_parser.h"
22d590543dSopenharmony_ci#include "battery_stats_service.h"
23d590543dSopenharmony_ci
24d590543dSopenharmony_cinamespace OHOS {
25d590543dSopenharmony_cinamespace PowerMgr {
26d590543dSopenharmony_cinamespace {
27d590543dSopenharmony_ci}
28d590543dSopenharmony_ci
29d590543dSopenharmony_ciUserEntity::UserEntity()
30d590543dSopenharmony_ci{
31d590543dSopenharmony_ci    consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_USER;
32d590543dSopenharmony_ci}
33d590543dSopenharmony_ci
34d590543dSopenharmony_cidouble UserEntity::GetEntityPowerMah(int32_t uidOrUserId)
35d590543dSopenharmony_ci{
36d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
37d590543dSopenharmony_ci    auto iter = userPowerMap_.find(uidOrUserId);
38d590543dSopenharmony_ci    if (iter != userPowerMap_.end()) {
39d590543dSopenharmony_ci        power = iter->second;
40d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC, "Get user power consumption: %{public}lfmAh for user id: %{public}d",
41d590543dSopenharmony_ci            power, uidOrUserId);
42d590543dSopenharmony_ci    } else {
43d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC,
44d590543dSopenharmony_ci            "No user power consumption related with user id: %{public}d found, return 0", uidOrUserId);
45d590543dSopenharmony_ci    }
46d590543dSopenharmony_ci    return power;
47d590543dSopenharmony_ci}
48d590543dSopenharmony_ci
49d590543dSopenharmony_civoid UserEntity::AggregateUserPowerMah(int32_t userId, double power)
50d590543dSopenharmony_ci{
51d590543dSopenharmony_ci    auto iter = userPowerMap_.find(userId);
52d590543dSopenharmony_ci    if (iter != userPowerMap_.end()) {
53d590543dSopenharmony_ci        iter->second += power;
54d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC, "Add user power consumption: %{public}lfmAh for user id: %{public}d",
55d590543dSopenharmony_ci            power, userId);
56d590543dSopenharmony_ci    } else {
57d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC, "Create user power consumption: %{public}lfmAh for user id: %{public}d",
58d590543dSopenharmony_ci            power, userId);
59d590543dSopenharmony_ci        userPowerMap_.insert(std::pair<int32_t, double>(userId, power));
60d590543dSopenharmony_ci    }
61d590543dSopenharmony_ci}
62d590543dSopenharmony_ci
63d590543dSopenharmony_civoid UserEntity::Calculate(int32_t uid)
64d590543dSopenharmony_ci{
65d590543dSopenharmony_ci    for (auto& iter : userPowerMap_) {
66d590543dSopenharmony_ci        std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
67d590543dSopenharmony_ci        statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_USER);
68d590543dSopenharmony_ci        statsInfo->SetUserId(iter.first);
69d590543dSopenharmony_ci        statsInfo->SetPower(iter.second);
70d590543dSopenharmony_ci        statsInfoList_.push_back(statsInfo);
71d590543dSopenharmony_ci    }
72d590543dSopenharmony_ci}
73d590543dSopenharmony_ci
74d590543dSopenharmony_civoid UserEntity::Reset()
75d590543dSopenharmony_ci{
76d590543dSopenharmony_ci    // Reset app user total power consumption
77d590543dSopenharmony_ci    for (auto& iter : userPowerMap_) {
78d590543dSopenharmony_ci        iter.second = StatsUtils::DEFAULT_VALUE;
79d590543dSopenharmony_ci    }
80d590543dSopenharmony_ci}
81d590543dSopenharmony_ci} // namespace PowerMgr
82d590543dSopenharmony_ci} // namespace OHOS
83d590543dSopenharmony_ci
84