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& BundleName(const std::string& bundleName_);
60     HaMetaMessage& AgentBundleName(const std::string& agentBundleName);
61     HaMetaMessage& TypeCode(int32_t typeCode);
62     HaMetaMessage& NotificationId(int32_t notificationId);
63     HaMetaMessage& SlotType(int32_t slotType);
64     std::string GetMessage() const;
65     HaMetaMessage& Checkfailed(bool checkfailed);
66     bool NeedReport() const;
67 
68     std::string Build() const;
69 
70     std::string bundleName_;
71     int32_t notificationId_ = -1;
72     std::string agentBundleName_ = "";
73     int32_t typeCode_ = -1;
74     uint32_t slotType_ = -1;
75     uint32_t sceneId_;
76     uint32_t branchId_;
77     uint32_t errorCode_ = ERR_OK;
78     std::string message_;
79     bool checkfailed_ = true;
80 };
81 
82 
83 struct FlowControllerOption {
84     int32_t count;
85     int32_t time;
86 };
87 
88 struct ReportCache {
89     EventFwk::Want want;
90     int32_t eventCode;
91 };
92 
93 class NotificationAnalyticsUtil {
94 public:
95     static void ReportPublishFailedEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message);
96 
97     static void ReportDeleteFailedEvent(const sptr<NotificationRequest>& request, HaMetaMessage& message);
98 
99     static void ReportModifyEvent(const HaMetaMessage& message);
100 
101     static void ReportDeleteFailedEvent(const HaMetaMessage& message);
102 
103     static void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list,
104         const std::chrono::system_clock::time_point &now, int32_t time = 1);
105 
106     static int64_t GetCurrentTime();
107 private:
108     static void ReportNotificationEvent(const sptr<NotificationRequest>& request,
109         EventFwk::Want want, int32_t eventCode, const std::string& reason);
110     static void CommonNotificationEvent(const sptr<NotificationRequest>& request,
111         int32_t eventCode, const HaMetaMessage& message);
112 
113     static void CommonNotificationEvent(int32_t eventCode, const HaMetaMessage& message);
114 
115     static void ReportNotificationEvent(EventFwk::Want want, int32_t eventCode, const std::string& reason);
116 
117     static bool ReportFlowControl(const int32_t reportType);
118 
119     static std::list<std::chrono::system_clock::time_point> GetFlowListByType(const int32_t reportType);
120 
121     static FlowControllerOption GetFlowOptionByType(const int32_t reportType);
122 
123     static std::string BuildExtraInfo(const HaMetaMessage& message);
124 
125     static std::string BuildExtraInfoWithReq(const HaMetaMessage& message,
126         const sptr<NotificationRequest>& request);
127 
128     static void SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo);
129 
130     static void AddListCache(EventFwk::Want& want, int32_t eventCode);
131 
132     static void ExecuteCacheList();
133 
134     static void ReportCommonEvent(const ReportCache& reportCache);
135 };
136 } // namespace Notification
137 } // namespace OHOS
138 
139 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H
140