1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "RdbStatReporter"
16
17 #include "rdb_stat_reporter.h"
18
19 #include <chrono>
20
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "rdb_time_utils.h"
24 #include "sqlite_global_config.h"
25 #include "sqlite_utils.h"
26 #include "task_executor.h"
27
28 namespace OHOS::NativeRdb {
29 using namespace OHOS::Rdb;
30 constexpr int32_t WAIT_TIME = 600;
31 constexpr int32_t TIMEOUT_FIRST = 1500;
32 constexpr int32_t TIMEOUT_SECOND = 5000;
33 constexpr int32_t TIMEOUT_THIRD = 10000;
34 std::atomic<std::chrono::steady_clock::time_point> RdbStatReporter::reportTime_ =
35 std::chrono::steady_clock::time_point();
36
RdbStatReporter(StatType statType,SubType subType,const RdbStoreConfig & config,std::shared_ptr<ReportFunc> func)37 RdbStatReporter::RdbStatReporter(
38 StatType statType, SubType subType, const RdbStoreConfig &config, std::shared_ptr<ReportFunc> func)
39 {
40 if (func == nullptr) {
41 return;
42 }
43 startTime_ = std::chrono::steady_clock::now();
44 statEvent_.statType = static_cast<uint32_t>(statType);
45 statEvent_.bundleName = config.GetBundleName();
46 statEvent_.storeName = SqliteUtils::Anonymous(config.GetName());
47 statEvent_.subType = static_cast<uint32_t>(subType);
48 reportFunc_ = std::move(func);
49 }
50
~RdbStatReporter()51 RdbStatReporter::~RdbStatReporter()
52 {
53 if (reportFunc_ == nullptr) {
54 return;
55 }
56 auto endTime = std::chrono::steady_clock::now();
57 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime_).count();
58 auto loadedReportTime = reportTime_.load();
59 auto reportInterval = std::chrono::duration_cast<std::chrono::seconds>(endTime - loadedReportTime).count();
60 statEvent_.costTime = GetTimeType(static_cast<uint32_t>(duration));
61 if (statEvent_.costTime > TIME_LEVEL_NONE &&
62 (loadedReportTime == std::chrono::steady_clock::time_point() || reportInterval >= WAIT_TIME)) {
63 auto pool = TaskExecutor::GetInstance().GetExecutor();
64 if (pool == nullptr) {
65 LOG_WARN("task pool err when RdbStatReporter");
66 return;
67 }
68 pool->Execute([report = std::move(reportFunc_), statEvent = std::move(statEvent_)]() {
69 (*report)(statEvent);
70 });
71 reportTime_.store(std::chrono::steady_clock::now());
72 }
73 }
74
GetTimeType(uint32_t costTime)75 TimeType RdbStatReporter::GetTimeType(uint32_t costTime)
76 {
77 if (costTime < TIMEOUT_FIRST) {
78 return TIME_LEVEL_NONE;
79 } else if (costTime < TIMEOUT_SECOND) {
80 return TIME_LEVEL_FIRST;
81 } else if (costTime < TIMEOUT_THIRD) {
82 return TIME_LEVEL_SECOND;
83 }
84 return TIME_LEVEL_THIRD;
85 }
86
87 } // namespace OHOS::NativeRdb