1 /*
2  * Copyright (c) 2021-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 
16 #include "sys_event_store.h"
17 
18 #include <cstdio>
19 #include <memory>
20 
21 #include "event.h"
22 #include "event_export_engine.h"
23 #include "file_util.h"
24 #include "focused_event_util.h"
25 #include "hiview_global.h"
26 #include "hiview_logger.h"
27 #include "hiview_platform.h"
28 #include "parameter_ex.h"
29 #include "plugin_factory.h"
30 #include "sys_event.h"
31 #include "sys_event_dao.h"
32 #include "sys_event_db_mgr.h"
33 #include "sys_event_sequence_mgr.h"
34 #include "time_util.h"
35 
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace {
39 REGISTER(SysEventStore);
40 DEFINE_LOG_TAG("HiView-SysEventStore");
41 const std::string PROP_LAST_BACKUP = "persist.hiviewdfx.priv.sysevent.backup_time";
42 }
43 
SysEventStore()44 SysEventStore::SysEventStore() : hasLoaded_(false)
45 {
46     sysEventDbMgr_ = std::make_unique<SysEventDbMgr>();
47 }
48 
~SysEventStore()49 SysEventStore::~SysEventStore() {}
50 
OnLoad()51 void SysEventStore::OnLoad()
52 {
53     HIVIEW_LOGI("sys event service load");
54     sysEventDbMgr_->StartCheckStoreTask(this->workLoop_);
55 
56     lastBackupTime_ = Parameter::GetString(PROP_LAST_BACKUP, "");
57     EventExportEngine::GetInstance().Start();
58     hasLoaded_ = true;
59 }
60 
OnUnload()61 void SysEventStore::OnUnload()
62 {
63     HIVIEW_LOGI("sys event service unload");
64     EventExportEngine::GetInstance().Stop();
65 }
66 
IsNeedBackup(const std::string & dateStr)67 bool SysEventStore::IsNeedBackup(const std::string& dateStr)
68 {
69     if (lastBackupTime_ == dateStr) {
70         return false;
71     }
72     if (lastBackupTime_.empty()) {
73         // first time boot, no need to backup
74         lastBackupTime_ = dateStr;
75         Parameter::SetProperty(PROP_LAST_BACKUP, dateStr);
76         HIVIEW_LOGI("first time boot, record backup time: %{public}s.", dateStr.c_str());
77         return false;
78     }
79     return true;
80 }
81 
Convert2SysEvent(std::shared_ptr<Event> & event)82 std::shared_ptr<SysEvent> SysEventStore::Convert2SysEvent(std::shared_ptr<Event>& event)
83 {
84     if (event == nullptr) {
85         HIVIEW_LOGE("event is null");
86         return nullptr;
87     }
88     if (event->messageType_ != Event::MessageType::SYS_EVENT) {
89         HIVIEW_LOGE("receive out of sys event type");
90         return nullptr;
91     }
92     std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
93     if (sysEvent == nullptr) {
94         HIVIEW_LOGE("sysevent is null");
95     }
96     return sysEvent;
97 }
98 
OnEvent(std::shared_ptr<Event> & event)99 bool SysEventStore::OnEvent(std::shared_ptr<Event>& event)
100 {
101     if (!hasLoaded_) {
102         HIVIEW_LOGE("SysEventService not ready");
103         return false;
104     }
105 
106     std::shared_ptr<SysEvent> sysEvent = Convert2SysEvent(event);
107     if (sysEvent != nullptr && sysEvent->preserve_) {
108         // add seq to sys event and save it to local file
109         int64_t eventSeq = EventStore::SysEventSequenceManager::GetInstance().GetSequence();
110         sysEvent->SetEventSeq(eventSeq);
111         if (FocusedEventUtil::IsFocusedEvent(sysEvent->domain_, sysEvent->eventName_)) {
112             HIVIEW_LOGI("event[%{public}s|%{public}s|%{public}" PRId64 "] is valid.",
113                 sysEvent->domain_.c_str(), sysEvent->eventName_.c_str(), eventSeq);
114         }
115         EventStore::SysEventSequenceManager::GetInstance().SetSequence(++eventSeq);
116         sysEventDbMgr_->SaveToStore(sysEvent);
117 
118         std::string dateStr(TimeUtil::TimestampFormatToDate(TimeUtil::GetSeconds(), "%Y%m%d"));
119         if (IsNeedBackup(dateStr)) {
120             EventStore::SysEventDao::Backup();
121             lastBackupTime_ = dateStr;
122             Parameter::SetProperty(PROP_LAST_BACKUP, dateStr);
123         }
124     }
125     return true;
126 }
127 } // namespace HiviewDFX
128 } // namespace OHOS
129