From 11ea47141d10f0808cf94e620db51ba3d2fab1a6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Wed, 16 Apr 2025 10:44:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=9C=AA=E8=B6=85=E9=99=90=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- .../common/include/bundle_active_service.h | 7 +- services/common/src/bundle_active_service.cpp | 86 ++++++++++++++----- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index df7fdef..19f145d 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -85,14 +85,14 @@ public: /** * @brief QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user. * - * @param PackageStats . + * @param packageStats . * @param intervalType . * @param beginTime . * @param endTime . * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. * @return errCode. */ - ErrCode QueryBundleStatsInfoByInterval(std::vector& PackageStats, + ErrCode QueryBundleStatsInfoByInterval(std::vector& packageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) override; /** @@ -255,6 +255,9 @@ private: int32_t DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo); int32_t DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo); int32_t GetNameAndIndexForUid(int32_t uid); + std::vector MergePackageStats( + const std::vector& packageStats); + void MergeSamePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 736ef28..0a37e30 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -335,7 +335,7 @@ ErrCode BundleActiveService::IsBundleUsePeriod(bool& IsUsePeriod, const std::str return ERR_OK; } -ErrCode BundleActiveService::QueryBundleStatsInfoByInterval(std::vector& PackageStats, +ErrCode BundleActiveService::QueryBundleStatsInfoByInterval(std::vector& packageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) { BUNDLE_ACTIVE_LOGD("QueryBundleStatsInfoByInterval stats called, intervaltype is %{public}d", intervalType); @@ -350,14 +350,16 @@ ErrCode BundleActiveService::QueryBundleStatsInfoByInterval(std::vectorQueryBundleStatsInfos( - PackageStats, userId, convertedIntervalType, beginTime, endTime, ""); - for (auto& packageStat : PackageStats) { - packageStat.appIndex_ = GetNameAndIndexForUid(packageStat.uid_); - } + if (ret != ERR_OK) { + return ret; + } + int32_t convertedIntervalType = ConvertIntervalType(intervalType); + ret = bundleActiveCore_->QueryBundleStatsInfos( + tempPackageStats, userId, convertedIntervalType, beginTime, endTime, ""); + for (auto& packageStat : tempPackageStats) { + packageStat.appIndex_ = GetNameAndIndexForUid(packageStat.uid_); } + packageStats = MergePackageStats(tempPackageStats); return ret; } @@ -443,17 +445,20 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vectorGetNameForUid(callingUid, bundleName); - ErrCode isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId); - if (!bundleName.empty() && isSystemAppAndHasPermission == ERR_OK) { - int32_t convertedIntervalType = ConvertIntervalType(intervalType); - ret = bundleActiveCore_->QueryBundleStatsInfos(bundleActivePackageStats, userId, convertedIntervalType, - beginTime, endTime, bundleName); - } + if (ret != ERR_OK || userId == -1) { + return ret; + } + std::vector tempPackageStats; + BUNDLE_ACTIVE_LOGD("QueryBundleStatsInfos userid is %{public}d", userId); + std::string bundleName = ""; + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); + ErrCode isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId); + if (!bundleName.empty() && isSystemAppAndHasPermission == ERR_OK) { + int32_t convertedIntervalType = ConvertIntervalType(intervalType); + ret = bundleActiveCore_->QueryBundleStatsInfos(tempPackageStats, userId, convertedIntervalType, + beginTime, endTime, bundleName); } + packageStats = MergePackageStats(tempPackageStats); return ret; } @@ -799,7 +804,7 @@ int32_t BundleActiveService::DumpPackageUsage(const std::vector &du std::vector &dumpInfo) { int32_t ret = -1; - std::vector packageUsageResult; + std::vector tempPackageUsage; if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { return ret; } @@ -808,7 +813,8 @@ int32_t BundleActiveService::DumpPackageUsage(const std::vector &du int64_t endTime = BundleActiveUtil::StringToInt64(dumpOption[4]); int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[5]); bundleActiveCore_->QueryBundleStatsInfos( - packageUsageResult, userId, intervalType, beginTime, endTime, ""); + tempPackageUsage, userId, intervalType, beginTime, endTime, ""); + auto packageUsageResult = MergePackageStats(tempPackageUsage); for (auto& onePackageRecord : packageUsageResult) { dumpInfo.emplace_back(onePackageRecord.ToString()); } @@ -852,6 +858,46 @@ void BundleActiveService::DumpUsage(std::string &result) " ModuleUsage [maxNum] [userId] get module usage for one user\n"; result.append(dumpHelpMsg); } + +std::vector BundleActiveService::MergePackageStats( + const std::vector& packageStats) +{ + if (packageStats.empty()) { + return packageStats; + } + std::vector tempPackageStats; + std::shared_ptr>> mergedPackageStats = + std::make_shared>>(); + for (auto packageStat : packageStats) { + std::string mergedPackageStatsKey = packageStat.bundleName_ + std::to_string(packageStat.uid_); + std::map::iterator iter = + mergedPackageStats->find(mergedPackageStatsKey); + if (iter != mergedPackageStats->end()) { + MergeSamePackageStats(iter->second, packageStat); + } else { + mergedPackageStats-> + insert(std::pair(mergedPackageStatsKey, packageStat)); + } + } + for (auto pair : *mergedPackageStats) { + tempPackageStats.push_back(pair.second); + } + return tempPackageStats; +} + +void BundleActiveService::MergeSamePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right) +{ + if (left.bundleName_ != right.bundleName_) { + BUNDLE_ACTIVE_LOGE("Merge package stats failed, existing packageName : %{public}s," + " new packageName : %{public}s,", left.bundleName_.c_str(), right.bundleName_.c_str()); + return; + } + left.lastTimeUsed_ = std::max(left.lastTimeUsed_, right.lastTimeUsed_); + left.lastContiniousTaskUsed_ = std::max(left.lastContiniousTaskUsed_, right.lastContiniousTaskUsed_); + left.totalInFrontTime_ += right.totalInFrontTime_; + left.totalContiniousTaskUsedTime_ += right.totalContiniousTaskUsedTime_; + left.bundleStartedCount_ += right.bundleStartedCount_; +} } // namespace DeviceUsageStats } // namespace OHOS -- Gitee From b054c4bd0dfe72b2e5a8db05903be87d19d97f6e Mon Sep 17 00:00:00 2001 From: fengyang Date: Wed, 16 Apr 2025 11:15:51 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=9C=AA=E8=B6=85=E9=99=90=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 0a37e30..a683f11 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -353,6 +353,7 @@ ErrCode BundleActiveService::QueryBundleStatsInfoByInterval(std::vector tempPackageStats; int32_t convertedIntervalType = ConvertIntervalType(intervalType); ret = bundleActiveCore_->QueryBundleStatsInfos( tempPackageStats, userId, convertedIntervalType, beginTime, endTime, ""); @@ -866,12 +867,11 @@ std::vector BundleActiveService::MergePackageStats( return packageStats; } std::vector tempPackageStats; - std::shared_ptr>> mergedPackageStats = - std::make_shared>>(); + std::shared_ptr> mergedPackageStats = + std::make_shared>(); for (auto packageStat : packageStats) { std::string mergedPackageStatsKey = packageStat.bundleName_ + std::to_string(packageStat.uid_); - std::map::iterator iter = - mergedPackageStats->find(mergedPackageStatsKey); + auto iter = mergedPackageStats->find(mergedPackageStatsKey); if (iter != mergedPackageStats->end()) { MergeSamePackageStats(iter->second, packageStat); } else { -- Gitee From 246f86eba34532cc9d2b897ebb87a2c065912105 Mon Sep 17 00:00:00 2001 From: fengyang Date: Wed, 16 Apr 2025 16:03:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=9C=AA=E8=B6=85=E9=99=90=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 2 +- .../device_usage_statistics_service_test.cpp | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index a683f11..e6a3701 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -459,7 +459,7 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vectorQueryBundleStatsInfos(tempPackageStats, userId, convertedIntervalType, beginTime, endTime, bundleName); } - packageStats = MergePackageStats(tempPackageStats); + bundleActivePackageStats = MergePackageStats(tempPackageStats); return ret; } diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 777c9bd..6a2c2a0 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -1587,5 +1587,32 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Conf EXPECT_EQ(result, false); } +/* + * @tc.name: DeviceUsageStatisticsServiceTest_MergePackageStats_001 + * @tc.desc: MergePackageStats + * @tc.type: FUNC + * @tc.require: IC0GWV + */ +HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_MergePackageStats_001, + Function | MediumTest | Level0) +{ + std::vector bundleActivePackageStatsVector; + BundleActivePackageStats bundleActivePackageStats; + bundleActivePackageStats.bundleName_ = "test"; + bundleActivePackageStats.uid_ = 1; + bundleActivePackageStatsVector.push_back(bundleActivePackageStats); + BundleActivePackageStats bundleActivePackageStats2; + bundleActivePackageStats2.bundleName_ = "test"; + bundleActivePackageStats2.uid_ = 1; + bundleActivePackageStatsVector.push_back(bundleActivePackageStats2); + BundleActivePackageStats bundleActivePackageStats3; + bundleActivePackageStats3.bundleName_ = "test"; + bundleActivePackageStats3.uid_ = 2; + bundleActivePackageStatsVector.push_back(bundleActivePackageStats3); + auto bundleActiveService = std::make_shared(); + auto MergeResult = bundleActiveService->MergePackageStats(bundleActivePackageStatsVector); + EXPECT_EQ(MergeResult.size(), 2); +} + } // namespace DeviceUsageStats } // namespace OHOS \ No newline at end of file -- Gitee