1 /*
2  * Copyright (c) 2024 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 #ifndef HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_DB_INCLUDE_DAILY_DB_HELPER_H
16 #define HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_DB_INCLUDE_DAILY_DB_HELPER_H
17 
18 #include "rdb_helper.h"
19 #include "rdb_store.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 class DailyDbHelper {
24 public:
25     struct EventInfo {
26         std::string domain;
27         std::string name;
28         int32_t count = 0;
29         int64_t exceedTime = 0;
30     };
31 
32 public:
33     DailyDbHelper(const std::string& workPath);
34     ~DailyDbHelper() = default;
35 
36     int32_t InsertEventInfo(const EventInfo& info);
37     int32_t UpdateEventInfo(const EventInfo& info);
38     int32_t QueryEventInfo(EventInfo& info);
39 
40     bool NeedReport(int64_t nowTime);
41     void Report();
42 
43 private:
44     void InitDb();
45     bool InitDbPath();
46     void InitDbStore();
47     void CloseDbStore();
48     void PrepareOldDbFilesBeforeReport();
49     void ReportDailyEvent();
50     void PrepareNewDbFilesAfterReport();
51 
52 private:
53     class DailyDbOpenCallback : public NativeRdb::RdbOpenCallback {
54     public:
55         int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
56         int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
57     };
58 
59 private:
60     std::string workPath_;
61     std::string dbPath_;
62     std::string uploadPath_;
63     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
64 };
65 } // namespace HiviewDFX
66 } // namespace OHOS
67 #endif // HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_DB_INCLUDE_DAILY_DB_HELPER_H
68