From c9c2c799fd889225694c431264d2ce4b8d1fd5e3 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Tue, 31 May 2022 13:42:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=B0=86event=E5=92=8C=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BF=A1=E6=81=AF=E8=90=BD=E7=9B=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- .../include/bundle_active_usage_database.h | 3 ++- .../src/bundle_active_usage_database.cpp | 21 ++++++++++++------- .../src/bundle_active_group_controller.cpp | 3 ++- .../include/bundle_active_event.h | 6 ++++++ .../packageusage/src/bundle_active_event.cpp | 13 ++++++++++++ .../src/bundle_active_period_stats.cpp | 2 +- .../src/bundle_active_user_service.cpp | 8 ++++--- 7 files changed, 43 insertions(+), 13 deletions(-) diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index 23cfe58..e544d6c 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -41,7 +41,8 @@ public: ~BundleActiveUsageDatabase(); void InitDatabaseTableInfo(int64_t currentTime); void InitUsageGroupDatabase(const int32_t databaseType, const bool forModuleRecords); - void UpdateUsageData(int32_t databaseType, BundleActivePeriodStats &stats); + void UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats); + void UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats); std::shared_ptr GetCurrentUsageData(int32_t databaseType, int32_t userId); void RenewTableTime(int64_t timeDiffMillis); int32_t GetOptimalIntervalType(int64_t beginTime, int64_t endTime); diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index aa8118f..a809cb9 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -1167,7 +1167,20 @@ void BundleActiveUsageDatabase::RenewTableTime(int64_t changedTime) } } -void BundleActiveUsageDatabase::UpdateUsageData(int32_t databaseType, BundleActivePeriodStats &stats) +void BundleActiveUsageDatabase::UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats) +{ + lock_guard lock(databaseMutex_); + CheckDatabaseFile(databaseType); + if (databaseType != DAILY_DATABASE_INDEX) { + return; + } + if (stats.events_.Size() != 0) { + CheckDatabaseFile(EVENT_DATABASE_INDEX); + FlushEventInfo(EVENT_DATABASE_INDEX, stats); + } +} + +void BundleActiveUsageDatabase::UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats) { lock_guard lock(databaseMutex_); if (databaseType < 0 || databaseType >= EVENT_DATABASE_INDEX) { @@ -1175,12 +1188,6 @@ void BundleActiveUsageDatabase::UpdateUsageData(int32_t databaseType, BundleActi return; } CheckDatabaseFile(databaseType); - if (databaseType == DAILY_DATABASE_INDEX) { - if (stats.events_.Size() != 0) { - CheckDatabaseFile(EVENT_DATABASE_INDEX); - FlushEventInfo(EVENT_DATABASE_INDEX, stats); - } - } int32_t packageTableIndex = BundleActiveBinarySearch::GetInstance()->BinarySearch( sortedTableArray_.at(databaseType), stats.beginTime_); if (packageTableIndex < 0) { diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 54dc3d1..5d22086 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -224,6 +224,7 @@ void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, co } std::lock_guard lock(mutex_); if (IsBundleInstalled(event.bundleName_, userId) == false) { + BUNDLE_ACTIVE_LOGE("Report an uninstalled package event, return!"); return; } int32_t eventId = event.eventId_; @@ -374,7 +375,7 @@ int32_t BundleActiveGroupController::QueryPackageGroup(const std::string& bundle BUNDLE_ACTIVE_LOGI("QueryPackageGroup is not bundleInstalled"); return -1; } - + int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle( bundleName, userId, bootBasedTimeStamp, false); diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index 963ca49..e92dda6 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -120,6 +120,12 @@ public: * return: string of bundlename, timestamp, eventid. */ std::string ToString(); + /** + * @brief get if the event is reported by bundle usage. + * + * @return return true if event reported by bundle usage. + */ + static bool IsBundleEvent(const int32_t eventId); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/src/bundle_active_event.cpp b/services/packageusage/src/bundle_active_event.cpp index c8451b7..e005984 100644 --- a/services/packageusage/src/bundle_active_event.cpp +++ b/services/packageusage/src/bundle_active_event.cpp @@ -183,6 +183,19 @@ std::string BundleActiveEvent::ToString() return "bundle name is " + this->bundleName_ + ", event is " + std::to_string(this->eventId_) + ", timestamp is " + std::to_string(this->timeStamp_) + "\n"; } + +bool BundleActiveEvent::IsBundleEvent(const int32_t eventId) +{ + if (eventId == ABILITY_BACKGROUND || + eventId == ABILITY_FOREGROUND || + eventId == ABILITY_STOP || + eventId == LONG_TIME_TASK_STARTTED || + eventId == LONG_TIME_TASK_ENDED || + eventId == END_OF_THE_DAY) { + return true; + } + return false; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/src/bundle_active_period_stats.cpp b/services/packageusage/src/bundle_active_period_stats.cpp index d223d62..a6c4414 100644 --- a/services/packageusage/src/bundle_active_period_stats.cpp +++ b/services/packageusage/src/bundle_active_period_stats.cpp @@ -53,7 +53,7 @@ void BundleActivePeriodStats::Update(const std::string bundleName, const std::st tmpUsageStats->Update("", timeStamp, eventId, abilityId); } } - } else { + } else if (BundleActiveEvent::IsBundleEvent(eventId)){ auto usageStats = GetOrCreateUsageStats(bundleName); usageStats->Update(longTimeTaskName, timeStamp, eventId, abilityId); } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 9084d78..7f091db 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -173,10 +173,12 @@ void BundleActiveUserService::RestoreStats(bool forced) BUNDLE_ACTIVE_LOGI("RestoreStats() stat changed is true"); for (uint32_t i = 0; i < currentStats_.size(); i++) { if (currentStats_[i]) { - if (currentStats_[i]->bundleStats_.empty() && currentStats_[i]->events_.events_.empty()) { - continue; + if (!currentStats_[i]->bundleStats_.empty()) { + database_.UpdateBundleUsageData(i, *(currentStats_[i])); + } + if (!currentStats_[i]->events_.events_.empty() && i == BundleActivePeriodStats::PERIOD_DAILY) { + database_.UpdateEventData(i, *(currentStats_[i])); } - database_.UpdateUsageData(i, *(currentStats_[i])); } } if (!moduleRecords_.empty()) { -- Gitee From b8a21e20d618ca3693ee89b773f6cbcd174c1456 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Tue, 31 May 2022 14:53:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86event=E5=92=8C=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BF=A1=E6=81=AF=E8=90=BD=E7=9B=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/packagegroup/src/bundle_active_group_controller.cpp | 2 -- services/packageusage/src/bundle_active_period_stats.cpp | 2 +- services/packageusage/src/bundle_active_user_service.cpp | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 5d22086..518bf1b 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -218,7 +218,6 @@ uint32_t BundleActiveGroupController::EventToGroupReason(const int32_t eventId) void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp, const int32_t userId) { - BUNDLE_ACTIVE_LOGI("ReportEvent called"); if (bundleGroupEnable_ == false) { return; } @@ -375,7 +374,6 @@ int32_t BundleActiveGroupController::QueryPackageGroup(const std::string& bundle BUNDLE_ACTIVE_LOGI("QueryPackageGroup is not bundleInstalled"); return -1; } - int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle( bundleName, userId, bootBasedTimeStamp, false); diff --git a/services/packageusage/src/bundle_active_period_stats.cpp b/services/packageusage/src/bundle_active_period_stats.cpp index a6c4414..c8b6994 100644 --- a/services/packageusage/src/bundle_active_period_stats.cpp +++ b/services/packageusage/src/bundle_active_period_stats.cpp @@ -53,7 +53,7 @@ void BundleActivePeriodStats::Update(const std::string bundleName, const std::st tmpUsageStats->Update("", timeStamp, eventId, abilityId); } } - } else if (BundleActiveEvent::IsBundleEvent(eventId)){ + } else if (BundleActiveEvent::IsBundleEvent(eventId)) { auto usageStats = GetOrCreateUsageStats(bundleName); usageStats->Update(longTimeTaskName, timeStamp, eventId, abilityId); } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 7f091db..8d88bf5 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -176,7 +176,7 @@ void BundleActiveUserService::RestoreStats(bool forced) if (!currentStats_[i]->bundleStats_.empty()) { database_.UpdateBundleUsageData(i, *(currentStats_[i])); } - if (!currentStats_[i]->events_.events_.empty() && i == BundleActivePeriodStats::PERIOD_DAILY) { + if (!currentStats_[i]->events_.events_.empty()) { database_.UpdateEventData(i, *(currentStats_[i])); } } -- Gitee From 3d1b637818ab06e0166402563746dcd985746a85 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Tue, 31 May 2022 15:12:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=86event=E5=92=8C=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BF=A1=E6=81=AF=E8=90=BD=E7=9B=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/packageusage/src/bundle_active_user_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 8d88bf5..7f091db 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -176,7 +176,7 @@ void BundleActiveUserService::RestoreStats(bool forced) if (!currentStats_[i]->bundleStats_.empty()) { database_.UpdateBundleUsageData(i, *(currentStats_[i])); } - if (!currentStats_[i]->events_.events_.empty()) { + if (!currentStats_[i]->events_.events_.empty() && i == BundleActivePeriodStats::PERIOD_DAILY) { database_.UpdateEventData(i, *(currentStats_[i])); } } -- Gitee