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 INPUT_AGGREGATOR_H
17 #define INPUT_AGGREGATOR_H
18 
19 #include <iostream>
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <utility>
24 
25 #include "mmi_log.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class Aggregator {
30 public:
31     Aggregator(std::function<int32_t(int32_t, int32_t, std::function<void()>)> addTimer,
32                std::function<int32_t(int32_t)> resetTimer, uint32_t maxRecordCount = 100)
33         : addTimer_(std::move(addTimer)), resetTimer_(std::move(resetTimer)), maxRecordCount_(maxRecordCount)
34     {}
35 
36     bool Record(const LogHeader &lh, const std::string &key, const std::string &record);
37 
38     ~Aggregator();
39 
40 private:
41     struct RecordInfo {
42         std::string record;
43         std::chrono::system_clock::time_point timestamp;
44     };
45     std::string key_;
46     std::vector<RecordInfo> records_;
47     std::function<int32_t(int32_t, int32_t, std::function<void()>)> addTimer_;
48     std::function<int32_t(int32_t)> resetTimer_;
49     int32_t timerId_ { -1 };
50     uint32_t maxRecordCount_ { 0 };
51 
52     void FlushRecords(const LogHeader &lh, const std::string &key = "", const std::string &extraRecord = "");
53 };
54 } // namespace MMI
55 } // namespace OHOS
56 #endif // INPUT_AGGREGATOR_H
57