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 SCENE_10 = 10, 38 SCENE_11 = 11, 39 SCENE_20 = 20, 40 SCENE_21 = 21, 41 }; 42 43 enum EventBranchId { 44 BRANCH_0 = 0, 45 BRANCH_1 = 1, 46 BRANCH_2 = 2, 47 BRANCH_3 = 3, 48 BRANCH_4 = 4, 49 BRANCH_5 = 5, 50 BRANCH_6 = 6, 51 BRANCH_7 = 7, 52 BRANCH_8 = 8, 53 }; 54 class HaMetaMessage { 55 public: 56 HaMetaMessage() = default; 57 ~HaMetaMessage() = default; 58 59 explicit HaMetaMessage(uint32_t sceneId, uint32_t branchId); 60 61 HaMetaMessage& SceneId(uint32_t sceneId); 62 HaMetaMessage& BranchId(uint32_t branchId); 63 HaMetaMessage& ErrorCode(uint32_t errorCode); 64 HaMetaMessage& Message(const std::string& message, bool print = false); 65 HaMetaMessage& Append(const std::string& message); 66 HaMetaMessage& BundleName(const std::string& bundleName_); 67 HaMetaMessage& AgentBundleName(const std::string& agentBundleName); 68 HaMetaMessage& TypeCode(int32_t typeCode); 69 HaMetaMessage& NotificationId(int32_t notificationId); 70 HaMetaMessage& SlotType(int32_t slotType); 71 HaMetaMessage& SyncWatch(bool isLiveView); 72 HaMetaMessage& SyncHeadSet(bool isLiveView); 73 HaMetaMessage& SyncWatchHeadSet(bool isLiveView); 74 HaMetaMessage& KeyNode(bool isKeyNode); 75 HaMetaMessage& DelByWatch(bool isLiveView); 76 HaMetaMessage& DeleteReason(int32_t deleteReason); 77 HaMetaMessage& ClickByWatch(); 78 HaMetaMessage& ReplyByWatch(); 79 std::string GetMessage() const; 80 HaMetaMessage& Checkfailed(bool checkfailed); 81 bool NeedReport() const; 82 83 std::string Build() const; 84 85 std::string bundleName_; 86 int32_t notificationId_ = -1; 87 std::string agentBundleName_ = ""; 88 int32_t typeCode_ = -1; 89 uint32_t slotType_ = -1; 90 uint32_t sceneId_; 91 uint32_t branchId_; 92 uint32_t errorCode_ = ERR_OK; 93 std::string message_; 94 bool checkfailed_ = true; 95 int32_t deleteReason_ = -1; 96 static int32_t syncWatch_; 97 static int32_t syncHeadSet_; 98 static int32_t syncWatchHeadSet_; 99 static int32_t delByWatch_; 100 static int32_t clickByWatch_; 101 static int32_t replyByWatch_; 102 static int32_t keyNode_; 103 static int64_t time_; 104 static int32_t syncLiveViewWatch_; 105 static int32_t syncLiveViewHeadSet_; 106 static int32_t syncLiveViewWatchHeadSet_; 107 static int64_t liveViewTime_; 108 static int32_t liveViewDelByWatch_; 109 }; 110 111 struct FlowControllerOption { 112 int32_t count; 113 int32_t time; 114 }; 115 116 struct ReportCache { 117 EventFwk::Want want; 118 int32_t eventCode; 119 }; 120 121 class NotificationAnalyticsUtil { 122 public: 123 static void ReportPublishFailedEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 124 125 static void ReportDeleteFailedEvent(const sptr<NotificationRequest>& request, HaMetaMessage& message); 126 127 static void ReportPublishSuccessEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 128 129 static void ReportModifyEvent(const HaMetaMessage& message); 130 131 static void ReportDeleteFailedEvent(const HaMetaMessage& message); 132 133 static void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list, 134 const std::chrono::system_clock::time_point &now, int32_t time = 1); 135 136 static int64_t GetCurrentTime(); 137 138 static void ReportOperationsDotEvent(const HaMetaMessage& message); 139 140 static void ReportPublishFailedEvent(const HaMetaMessage& message); 141 142 static void ReportSkipFailedEvent(const HaMetaMessage& message); 143 144 private: 145 static void ReportNotificationEvent(const sptr<NotificationRequest>& request, 146 EventFwk::Want want, int32_t eventCode, const std::string& reason); 147 static void CommonNotificationEvent(const sptr<NotificationRequest>& request, 148 int32_t eventCode, const HaMetaMessage& message); 149 150 static void CommonNotificationEvent(int32_t eventCode, const HaMetaMessage& message); 151 152 static void ReportNotificationEvent(EventFwk::Want want, int32_t eventCode, const std::string& reason); 153 154 static bool ReportFlowControl(const int32_t reportType); 155 156 static bool IsAllowedBundle(const sptr<NotificationRequest>& request); 157 158 static std::string BuildAnsData(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 159 160 static ReportCache Aggregate(); 161 162 static uint32_t SetControlFlags(const std::shared_ptr<NotificationFlags> &flags, uint32_t &controlFlags); 163 164 static std::string GetDeviceStatus(const sptr<NotificationRequest>& request); 165 166 static std::list<std::chrono::system_clock::time_point> GetFlowListByType(const int32_t reportType); 167 168 static FlowControllerOption GetFlowOptionByType(const int32_t reportType); 169 170 static std::string BuildExtraInfo(const HaMetaMessage& message); 171 172 static std::string BuildExtraInfoWithReq(const HaMetaMessage& message, 173 const sptr<NotificationRequest>& request); 174 175 static void SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo); 176 177 static void AddListCache(EventFwk::Want& want, int32_t eventCode); 178 179 static void AddSuccessListCache(EventFwk::Want& want, int32_t eventCode); 180 181 static void ExecuteCacheList(); 182 183 static void ExecuteSuccessCacheList(); 184 185 static void ReportCommonEvent(const ReportCache& reportCache); 186 187 static bool DetermineWhetherToSend(uint32_t slotType); 188 189 static std::string BuildAnsData(const HaMetaMessage& message); 190 }; 191 } // namespace Notification 192 } // namespace OHOS 193 194 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H 195