1 /*
2 * Copyright (c) 2022-2023 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 "usage_event_cacher.h"
16
17 #include "hiview_logger.h"
18
19 namespace OHOS {
20 namespace HiviewDFX {
21 DEFINE_LOG_TAG("HiView-UsageEventCacher");
22
UsageEventCacher(const std::string & workPath)23 UsageEventCacher::UsageEventCacher(const std::string& workPath) : dbHelper_(nullptr)
24 {
25 dbHelper_ = std::make_unique<EventDbHelper>(workPath);
26 }
27
GetPluginStatsEvents(std::vector<std::shared_ptr<LoggerEvent>> & events) const28 void UsageEventCacher::GetPluginStatsEvents(std::vector<std::shared_ptr<LoggerEvent>>& events) const
29 {
30 if (dbHelper_->QueryPluginStatsEvent(events) < 0) {
31 HIVIEW_LOGI("failed to query plugin stats events from db");
32 return;
33 }
34 }
35
GetSysUsageEvent(const std::string & table) const36 std::shared_ptr<LoggerEvent> UsageEventCacher::GetSysUsageEvent(const std::string& table) const
37 {
38 std::shared_ptr<LoggerEvent> event = nullptr;
39 if (dbHelper_->QuerySysUsageEvent(event, table) < 0) {
40 HIVIEW_LOGI("failed to query sys usage event from db");
41 return nullptr;
42 }
43 return event;
44 }
45
DeletePluginStatsEventsFromDb() const46 void UsageEventCacher::DeletePluginStatsEventsFromDb() const
47 {
48 std::vector<std::shared_ptr<LoggerEvent>> events;
49 if (dbHelper_->QueryPluginStatsEvent(events) >= 0) {
50 dbHelper_->DeletePluginStatsEvent();
51 }
52 }
53
DeleteSysUsageEventFromDb(const std::string & table) const54 void UsageEventCacher::DeleteSysUsageEventFromDb(const std::string& table) const
55 {
56 std::shared_ptr<LoggerEvent> event = nullptr;
57 if (dbHelper_->QuerySysUsageEvent(event, table) >= 0) {
58 dbHelper_->DeleteSysUsageEvent(table);
59 }
60 }
61
SavePluginStatsEventsToDb(const std::vector<std::shared_ptr<LoggerEvent>> & events) const62 void UsageEventCacher::SavePluginStatsEventsToDb(const std::vector<std::shared_ptr<LoggerEvent>>& events) const
63 {
64 HIVIEW_LOGI("start to save plugin stats event to db");
65 for (auto event : events) {
66 if (dbHelper_->InsertPluginStatsEvent(event) < 0) {
67 HIVIEW_LOGE("failed to save event to db");
68 }
69 }
70 }
71
SaveSysUsageEventToDb(const std::shared_ptr<LoggerEvent> & event,const std::string & table) const72 void UsageEventCacher::SaveSysUsageEventToDb(const std::shared_ptr<LoggerEvent>& event, const std::string& table) const
73 {
74 if (event == nullptr) {
75 return;
76 }
77 if (dbHelper_->InsertSysUsageEvent(event, table) < 0) {
78 HIVIEW_LOGE("failed to save sys usage event to db");
79 }
80 }
81 } // namespace HiviewDFX
82 } // namespace OHOS
83