From b85c05c0c2d818413525cb46246caca6ad3a70bb Mon Sep 17 00:00:00 2001 From: let_it_rot Date: Wed, 20 Aug 2025 20:08:07 +0800 Subject: [PATCH] update Signed-off-by: let_it_rot --- IBundleActiveService.idl | 2 ++ .../innerkits/include/bundle_active_client.h | 11 +++++++ .../innerkits/src/bundle_active_client.cpp | 11 +++++++ .../common/include/bundle_active_service.h | 11 +++++++ services/common/src/bundle_active_service.cpp | 32 +++++++++++++++++++ .../bundleactivecommon_fuzzer.cpp | 5 +++ .../device_usage_statistics_mock_test.cpp | 17 ++++++++++ .../device_usage_statistics_multi_test.cpp | 22 +++++++++++++ .../unittest/device_usage_statistics_test.cpp | 14 ++++++++ 9 files changed, 125 insertions(+) diff --git a/IBundleActiveService.idl b/IBundleActiveService.idl index 9278ddb..37de81b 100644 --- a/IBundleActiveService.idl +++ b/IBundleActiveService.idl @@ -30,6 +30,8 @@ interface OHOS.DeviceUsageStats.IBundleActiveService { [in] long endTime, [in] int userId); void QueryBundleStatsInfos([out] List bundleActivePackageStats, [in] int intervalType, [in] long beginTime, [in] long endTime); + void QueryHighFrequencyUsageBundleInfos([out] List packageStats, + [in] int userId, [in] int maxNum); void QueryCurrentBundleEvents([out] BundleActiveEventVecRawData bundleActiveEventVecRawData, [in] long beginTime, [in]long endTime); void QueryAppGroup([out] int appGroup, [in] String bundleName, [in] int userId); diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index bcce217..03bd57a 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -114,6 +114,17 @@ public: ErrCode QueryBundleStatsInfos(std::vector& bundleActivePackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime); + /** + * @brief Query the bundle stats data for the last 7 days + * + * @param packageStats bundleActivePackageStats, The result of QueryHighFrequencyUsageBundleInfos. + * @param userId the user id. + * @param maxNum packageStats max size + * @return errCode. + */ + ErrCode QueryHighFrequencyUsageBundleInfos(std::vector& packageStats, + const int32_t userId, const int32_t maxNum = 20); + /** * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle. * diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 9e9ab60..124c877 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -167,6 +167,17 @@ ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vectorQueryBundleStatsInfos(bundleActivePackageStats, intervalType, beginTime, endTime); } +ErrCode BundleActiveClient::QueryHighFrequencyUsageBundleInfos(std::vector& packageStats, + const int32_t userId, const int32_t maxNum) +{ + std::lock_guard lock(mutex_); + ErrCode ret = GetBundleActiveProxy(); + if (ret != ERR_OK) { + return ret; + } + return bundleActiveProxy_->QueryHighFrequencyUsageBundleInfos(packageStats, userId, maxNum); +} + ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime) { diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index b5e6528..d07aa1f 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -131,6 +131,17 @@ public: ErrCode QueryBundleStatsInfos(std::vector& bundleActivePackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime) override; + /** + * @brief Query the bundle stats data for the last 7 days + * + * @param packageStats bundleActivePackageStats, The result of QueryHighFrequencyUsageBundleInfos. + * @param userId the user id. + * @param maxNum packageStats max size + * @return errCode. + */ + ErrCode QueryHighFrequencyUsageBundleInfos(std::vector& packageStats, + const int32_t userId, const int32_t maxNum) override; + /** * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle. * diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 792319a..3e3b3eb 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -569,6 +569,38 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vector& packageStats, + const int32_t userId, const int32_t maxNum) +{ + // get uid + int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); + AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + int32_t ret = CheckSystemAppOrNativePermission(callingUid, tokenId); + if (ret != ERR_OK) { + BUNDLE_ACTIVE_LOGE("no premission."); + return ERR_PERMISSION_DENIED; + } + BUNDLE_ACTIVE_LOGD("QueryBundleStatsInfos userid is %{public}d", userId); + std::vector tempPackageStats; + constexpr int64_t SEVEN_DAYS = static_cast(7) * 24 * 60 * 60 * 1000; + int64_t endTime = BundleActiveUtil::GetSystemTimeMs(); + int64_t beginTime = endTime - SEVEN_DAYS; + ret = bundleActiveCore_->QueryBundleStatsInfos(tempPackageStats, userId, BundleActiveUtil::PERIOD_DAILY, + beginTime, endTime, ""); + std::vector results; + results = MergePackageStats(tempPackageStats); + std::sort(results.begin(), results.end(), + [](const BundleActivePackageStats& lhs, const BundleActivePackageStats& rhs) { + return lhs.startCount_ > rhs.startCount_; + }); + if (static_cast(results.size()) > maxNum) { + packageStats.assign(results.begin(), results.begin() + maxNum); + } else { + packageStats = std::move(results); + } + return ret; +} + ErrCode BundleActiveService::QueryCurrentBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime) { diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp index b96eb02..c329295 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp @@ -110,6 +110,9 @@ namespace DeviceUsageStats { auto bundleActiveService = std::make_shared(); BundleActiveEvent event; int32_t userId = fdp->ConsumeIntegral(); + int32_t maxNum = fdp->ConsumeIntegral(); + constexpr int32_t defaultNum = 20; + maxNum = maxNum > 0 ? maxNum : defaultNum; std::string bundleName = fdp->ConsumeRandomLengthString(); std::vector packageStats; int32_t intervalType = fdp->ConsumeIntegral(); @@ -124,6 +127,8 @@ namespace DeviceUsageStats { int32_t appGroup = 0; bundleActiveService->QueryAppGroup(appGroup, bundleName, userId); packageStats.clear(); + bundleActiveService->QueryHighFrequencyUsageBundleInfos(packageStats, userId, maxNum); + packageStats.clear(); bundleActiveService->QueryBundleStatsInfos(packageStats, intervalType, beginTime, endTime); bundleActiveService->QueryDeviceEventStats(beginTime, endTime, eventStats, userId); BundleActivePackageStats packageStats1; diff --git a/test/unittest/device_usage_statistics_mock_test.cpp b/test/unittest/device_usage_statistics_mock_test.cpp index 7e34762..def2a90 100644 --- a/test/unittest/device_usage_statistics_mock_test.cpp +++ b/test/unittest/device_usage_statistics_mock_test.cpp @@ -95,6 +95,8 @@ HWTEST_F(DeviceUsageStatisticsMockTest, DeviceUsageStatisticsMockTest_GetBundleA g_intervalType, 0, g_largeNum), ERR_OK); EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleStatsInfos(packageStats, g_intervalType, 0, g_largeNum), ERR_OK); + EXPECT_NE(BundleActiveClient::GetInstance().QueryHighFrequencyUsageBundleInfos(packageStats, + 100, 20), ERR_OK); std::vector event; EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleEvents(event, 0, g_largeNum, 100), ERR_OK); @@ -284,6 +286,21 @@ HWTEST_F(DeviceUsageStatisticsMockTest, DeviceUsageStatisticsMockTest_QueryBundl EXPECT_NE(code, ERR_OK); } +/* + * @tc.name: DeviceUsageStatisticsMockTest_QueryBundleStatsInfos_001 + * @tc.desc: test service queryBundleStatsInfos boundary condition + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsMockTest, DeviceUsageStatisticsMockTest_QueryHighFrequencyUsageBundleInfos_001, + Function | MediumTest | TestSize.Level0) +{ + std::vector packageStats; + ErrCode code = DelayedSingleton::GetInstance() + ->QueryHighFrequencyUsageBundleInfos(packageStats, 100, 20); + EXPECT_NE(code, ERR_OK); +} + /* * @tc.name: DeviceUsageStatisticsMockTest_QueryBundleStatsInfos_001 * @tc.desc: test service queryBundleStatsInfos boundary condition diff --git a/test/unittest/device_usage_statistics_multi_test.cpp b/test/unittest/device_usage_statistics_multi_test.cpp index 9c6114d..423a2c7 100644 --- a/test/unittest/device_usage_statistics_multi_test.cpp +++ b/test/unittest/device_usage_statistics_multi_test.cpp @@ -245,6 +245,28 @@ HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryBun GTEST_RUN_TASK(MultiTestQueryBundleStatsInfos); } +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryHighFrequencyUsageBundleInfos_001 + * @tc.desc: QueryHighFrequencyUsageBundleInfos + * @tc.type: FUNC + * @tc.require: issuesI5QJD9 + */ +void MultiTestQueryHighFrequencyUsageBundleInfos(void) +{ + std::vector result; + constexpr int32_t userId = 100; + constexpr int32_t maxNum = 20; + BundleActiveClient::GetInstance().QueryHighFrequencyUsageBundleInfos(result, userId, maxNum); + EXPECT_EQ(result.size(), 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryHighFrequencyUsageBundleInfos_001, + Function | MediumTest | TestSize.Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryHighFrequencyUsageBundleInfos); +} + /* * @tc.name: DeviceUsageStatisticsMultiTest_QueryModuleUsageRecords_001 * @tc.desc: QueryModuleUsageRecords diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index aad9ff6..af4d87e 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -209,6 +209,20 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryBundleStatsIn EXPECT_EQ(result.size(), 0); } +/* + * @tc.name: DeviceUsageStatisticsTest_QueryHighFrequencyUsageBundleInfos_001 + * @tc.desc: QueryHighFrequencyUsageBundleInfos + * @tc.type: FUNC + * @tc.require: issuesI5QJD9 + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryHighFrequencyUsageBundleInfos_001, + Function | MediumTest | TestSize.Level0) +{ + std::vector result; + BundleActiveClient::GetInstance().QueryHighFrequencyUsageBundleInfos(result, 100, 20); + EXPECT_EQ(result.size(), 0); +} + /* * @tc.name: DeviceUsageStatisticsTest_IsBundleIdle_001 * @tc.desc: isbundleidle -- Gitee