1 /*
2 * Copyright (c) 2021-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 
16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H
18 
19 #include <string>
20 #include <map>
21 #include "notification_request.h"
22 
23 namespace OHOS {
24 namespace Notification {
25 
26 enum EventSceneId {
27     SCENE_0 = 0,
28     SCENE_1 = 1,
29     SCENE_2 = 2,
30     SCENE_3 = 3,
31     SCENE_4 = 4,
32     SCENE_5 = 5,
33     SCENE_6 = 6,
34     SCENE_7 = 7,
35     SCENE_8 = 8,
36     SCENE_9 = 9,
37 };
38 
39 enum EventBranchId {
40     BRANCH_0 = 0,
41     BRANCH_1 = 1,
42     BRANCH_2 = 2,
43     BRANCH_3 = 3,
44     BRANCH_4 = 4,
45     BRANCH_5 = 5,
46     BRANCH_6 = 6,
47 };
48 class HaMetaMessage {
49 public:
50     HaMetaMessage() = default;
51     ~HaMetaMessage() = default;
52 
53     explicit HaMetaMessage(uint32_t sceneId, uint32_t branchId);
54 
55     HaMetaMessage& SceneId(uint32_t sceneId);
56     HaMetaMessage& BranchId(uint32_t branchId);
57     HaMetaMessage& ErrorCode(uint32_t errorCode);
58     HaMetaMessage& Message(const std::string& message, bool print = false);
59     HaMetaMessage& Append(const std::string& message);
60     HaMetaMessage& BundleName(const std::string& bundleName_);
61     HaMetaMessage& AgentBundleName(const std::string& agentBundleName);
62     HaMetaMessage& TypeCode(int32_t typeCode);
63     HaMetaMessage& NotificationId(int32_t notificationId);
64     HaMetaMessage& SlotType(int32_t slotType);
65     std::string GetMessage() const;
66     HaMetaMessage& Checkfailed(bool checkfailed);
67     bool NeedReport() const;
68 
69     std::string Build() const;
70 
71     std::string bundleName_;
72     int32_t notificationId_ = -1;
73     std::string agentBundleName_ = "";
74     int32_t typeCode_ = -1;
75     uint32_t slotType_ = -1;
76     uint32_t sceneId_;
77     uint32_t branchId_;
78     uint32_t errorCode_ = ERR_OK;
79     std::string message_;
80     bool checkfailed_ = true;
81 };
82 
83 
84 struct FlowControllerOption {
85     int32_t count;
86     int32_t time;
87 };
88 
89 struct ReportCache {
90     EventFwk::Want want;
91     int32_t eventCode;
92 };
93 
94 class NotificationAnalyticsUtil {
95 public:
96     static void ReportPublishFailedEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message);
97 
98     static void ReportDeleteFailedEvent(const sptr<NotificationRequest>& request, HaMetaMessage& message);
99 
100     static void ReportModifyEvent(const HaMetaMessage& message);
101 
102     static void ReportDeleteFailedEvent(const HaMetaMessage& message);
103 
104     static void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list,
105         const std::chrono::system_clock::time_point &now, int32_t time = 1);
106 
107     static int64_t GetCurrentTime();
108 private:
109     static void ReportNotificationEvent(const sptr<NotificationRequest>& request,
110         EventFwk::Want want, int32_t eventCode, const std::string& reason);
111     static void CommonNotificationEvent(const sptr<NotificationRequest>& request,
112         int32_t eventCode, const HaMetaMessage& message);
113 
114     static void CommonNotificationEvent(int32_t eventCode, const HaMetaMessage& message);
115 
116     static void ReportNotificationEvent(EventFwk::Want want, int32_t eventCode, const std::string& reason);
117 
118     static bool ReportFlowControl(const int32_t reportType);
119 
120     static FlowControllerOption GetFlowOptionByType(const int32_t reportType);
121 
122     static std::string BuildExtraInfo(const HaMetaMessage& message);
123 
124     static std::string BuildExtraInfoWithReq(const HaMetaMessage& message,
125         const sptr<NotificationRequest>& request);
126 
127     static void SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo);
128 
129     static void AddListCache(EventFwk::Want& want, int32_t eventCode);
130 
131     static void ExecuteCacheList();
132 
133     static void ReportCommonEvent(const ReportCache& reportCache);
134 };
135 } // namespace Notification
136 } // namespace OHOS
137 
138 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H
139