From 09d80bb654f639829761e17df39d0bceb0f2c939 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 22 Apr 2025 10:29:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BA=94=E7=94=A8=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E6=9C=AA=E8=B6=85=E9=99=90=EF=BC=8C=E4=BD=86=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=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 | 80 +++++++++++++++---- .../device_usage_statistics_service_test.cpp | 27 +++++++ 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index cf33fee..82b18aa 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; /** @@ -254,6 +254,9 @@ private: int32_t DumpEvents(const std::vector &dumpOption, std::vector &dumpInfo); int32_t DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo); int32_t DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo); + 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 d9203ca..6a4f089 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -334,7 +334,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); @@ -349,11 +349,14 @@ ErrCode BundleActiveService::QueryBundleStatsInfoByInterval(std::vectorQueryBundleStatsInfos( - PackageStats, userId, convertedIntervalType, beginTime, endTime, ""); + if (ret != ERR_OK) { + return ret; } + std::vector tempPackageStats; + int32_t convertedIntervalType = ConvertIntervalType(intervalType); + ret = bundleActiveCore_->QueryBundleStatsInfos( + tempPackageStats, userId, convertedIntervalType, beginTime, endTime, ""); + packageStats = MergePackageStats(tempPackageStats); return ret; } @@ -416,17 +419,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); } + bundleActivePackageStats = MergePackageStats(tempPackageStats); return ret; } @@ -773,7 +779,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; } @@ -782,7 +788,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()); } @@ -826,6 +833,45 @@ 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_); + auto 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 diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 134203b..57efffd 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -1503,5 +1503,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