1 /*
2  * Copyright (c) 2022-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 #include "app_event_db_cleaner.h"
16 
17 #include "app_event_store.h"
18 #include "file_util.h"
19 #include "hiappevent_config.h"
20 #include "hilog/log.h"
21 
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD002D07
24 
25 #undef LOG_TAG
26 #define LOG_TAG "DbCleaner"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 const std::string DATABASE_NAME = "databases/appevent.db";
32 constexpr int RESERVED_NUM = 1000;
33 constexpr int RESERVED_NUM_OS = 150;
34 
ClearAllData()35 void ClearAllData()
36 {
37     if (AppEventStore::GetInstance().DeleteEvent() < 0) {
38         HILOG_WARN(LOG_CORE, "failed to clear event table");
39     }
40     if (AppEventStore::GetInstance().DeleteCustomEventParams() < 0) {
41         HILOG_WARN(LOG_CORE, "failed to clear custom event params table");
42     }
43     if (AppEventStore::GetInstance().DeleteEventMapping() < 0) {
44         HILOG_WARN(LOG_CORE, "failed to clear event mapping table");
45     }
46     if (AppEventStore::GetInstance().DeleteUserId() < 0) {
47         HILOG_WARN(LOG_CORE, "failed to clear user id table");
48     }
49     if (AppEventStore::GetInstance().DeleteUserProperty() < 0) {
50         HILOG_WARN(LOG_CORE, "failed to clear user propertie table");
51     }
52 }
53 
ClearHistoryData()54 void ClearHistoryData()
55 {
56     if (AppEventStore::GetInstance().DeleteHistoryEvent(RESERVED_NUM, RESERVED_NUM_OS) < 0) {
57         HILOG_WARN(LOG_CORE, "failed to delete history events");
58         return;
59     }
60     if (AppEventStore::GetInstance().DeleteUnusedEventMapping() < 0) {
61         HILOG_WARN(LOG_CORE, "failed to delete unused event map");
62         return;
63     }
64     std::string runningId = HiAppEventConfig::GetInstance().GetRunningId();
65     if (!runningId.empty() && AppEventStore::GetInstance().DeleteUnusedParamsExceptCurId(runningId) < 0) {
66         HILOG_WARN(LOG_CORE, "failed to delete unused params");
67     }
68 }
69 }
GetFilesSize()70 uint64_t AppEventDbCleaner::GetFilesSize()
71 {
72     return FileUtil::GetFileSize(path_ + DATABASE_NAME);
73 }
74 
ClearSpace(uint64_t curSize,uint64_t maxSize)75 uint64_t AppEventDbCleaner::ClearSpace(uint64_t curSize, uint64_t maxSize)
76 {
77     HILOG_INFO(LOG_CORE, "start to clear the space occupied by database files");
78     if (curSize <= maxSize) {
79         return curSize;
80     }
81     ClearHistoryData();
82     return 0;
83 }
84 
ClearData()85 void AppEventDbCleaner::ClearData()
86 {
87     HILOG_INFO(LOG_CORE, "start to clear the db data");
88     ClearAllData();
89 }
90 } // namespace HiviewDFX
91 } // namespace OHOS
92