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 
16 #ifndef HIVIEW_BASE_EVENT_EXPORT_EXPORTED_EVENT_WRITE_HANDLER_H
17 #define HIVIEW_BASE_EVENT_EXPORT_EXPORTED_EVENT_WRITE_HANDLER_H
18 
19 #include <functional>
20 #include <list>
21 #include <tuple>
22 #include <unordered_map>
23 
24 #include "export_base_handler.h"
25 #include "export_db_storage.h"
26 #include "export_json_file_writer.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 struct CachedEvent {
31     // event version
32     EventVersion version;
33 
34     // event domain
35     std::string domain;
36 
37     // event name
38     std::string name;
39 
40     // event json string
41     std::string eventStr;
42 
CachedEventCachedEvent43     CachedEvent(EventVersion& version, std::string& domain, std::string& name, std::string& eventStr)
44         : version(version), domain(domain), name(name), eventStr(eventStr) {}
45 };
46 
47 struct EventWriteRequest : public BaseRequest {
48     // name of export module
49     std::string moduleName;
50 
51     // item: <system version, domain, sequecen, sysevent content>
52     std::list<std::shared_ptr<CachedEvent>> sysEvents;
53 
54     // directory configured for export event file to store
55     std::string exportDir;
56 
57     // tag whether the query is completed
58     bool isQueryCompleted = false;
59 
60     // max size of a single event file
61     int64_t maxSingleFileSize = 0;
62 
EventWriteRequestEventWriteRequest63     EventWriteRequest(std::string& moduleName, std::list<std::shared_ptr<CachedEvent>>& sysEvents,
64         std::string& exportDir, bool isQueryCompleted, int64_t maxSingleFileSize)
65         : moduleName(moduleName), sysEvents(sysEvents), exportDir(exportDir), isQueryCompleted(isQueryCompleted),
66         maxSingleFileSize(maxSingleFileSize) {}
67 };
68 
69 class EventWriteHandler : public ExportBaseHandler {
70 public:
71     bool HandleRequest(RequestPtr req) override;
72 
73 private:
74     std::shared_ptr<ExportJsonFileWriter> GetEventWriter(const EventVersion& eventVersion,
75         std::shared_ptr<EventWriteRequest> writeReq);
76     void CopyTmpZipFilesToDest();
77     void Rollback();
78 
79 private:
80     // <key: <module name, event version>, value: writer>
81     std::map<std::pair<std::string, std::string>, std::shared_ptr<ExportJsonFileWriter>> allJsonFileWriters_;
82     // <tmpZipFilePath, destZipFilePath>
83     std::unordered_map<std::string, std::string> zippedExportFileMap_;
84 };
85 } // namespace HiviewDFX
86 } // namespace OHOS
87 
88 #endif // HIVIEW_BASE_EVENT_EXPORT_EXPORTED_EVENT_WRITE_HANDLER_H