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 #include "badge_number_callback_data.h" 23 24 namespace OHOS { 25 namespace Notification { 26 namespace { 27 const static std::string LINE = "_"; 28 const static std::string ANS_BUNDLE_BEGIN = "ans_bundle"; 29 const static std::string LIVE_VIEW_SLOT_ENABLE_END = "slot_type_5_enabled"; 30 const static std::string NAME = "name"; 31 const static std::string UID = "uid"; 32 } 33 34 enum EventSceneId { 35 SCENE_0 = 0, 36 SCENE_1 = 1, 37 SCENE_2 = 2, 38 SCENE_3 = 3, 39 SCENE_4 = 4, 40 SCENE_5 = 5, 41 SCENE_6 = 6, 42 SCENE_7 = 7, 43 SCENE_8 = 8, 44 SCENE_9 = 9, 45 SCENE_10 = 10, 46 SCENE_11 = 11, 47 SCENE_12 = 12, 48 SCENE_13 = 13, 49 SCENE_14 = 14, 50 SCENE_15 = 15, 51 SCENE_16 = 16, 52 SCENE_17 = 17, 53 SCENE_18 = 18, 54 SCENE_19 = 19, 55 SCENE_20 = 20, 56 SCENE_21 = 21, 57 SCENE_22 = 22, 58 SCENE_23 = 23, 59 SCENE_24 = 24, 60 SCENE_25 = 25, 61 }; 62 63 enum EventBranchId { 64 BRANCH_0 = 0, 65 BRANCH_1 = 1, 66 BRANCH_2 = 2, 67 BRANCH_3 = 3, 68 BRANCH_4 = 4, 69 BRANCH_5 = 5, 70 BRANCH_6 = 6, 71 BRANCH_7 = 7, 72 BRANCH_8 = 8, 73 BRANCH_9 = 9, 74 BRANCH_10 = 10, 75 BRANCH_11 = 11, 76 BRANCH_12 = 12, 77 BRANCH_13 = 13, 78 BRANCH_14 = 14, 79 BRANCH_15 = 15, 80 BRANCH_16 = 16, 81 BRANCH_17 = 17, 82 BRANCH_18 = 18, 83 BRANCH_19 = 19, 84 BRANCH_20 = 20, 85 }; 86 87 class OperationalMeta { 88 public: 89 void ToJson(nlohmann::json& jsonObject); 90 public: 91 int32_t syncTime; 92 int32_t delTime; 93 int32_t clickTime; 94 int32_t replyTime; 95 }; 96 97 class OperationalData { 98 public: 99 OperationalData(); 100 void ToJson(nlohmann::json& jsonObject); 101 public: 102 int32_t syncWatchHead = 0; 103 int32_t keyNode = 0; 104 int64_t time; 105 int64_t countTime = 0; 106 std::map<std::string, OperationalMeta> dataMap; 107 }; 108 109 class HaOperationMessage { 110 public: HaOperationMessage()111 HaOperationMessage() {} HaOperationMessage(bool isLiveView)112 HaOperationMessage(bool isLiveView) : isLiveView_(isLiveView) {} 113 void ResetData(); 114 std::string ToJson(); 115 bool DetermineWhetherToSend(); 116 HaOperationMessage& KeyNode(bool keyNodeFlag); 117 HaOperationMessage& SyncPublish(std::vector<std::string>& deviceTypes); 118 HaOperationMessage& SyncDelete(std::string deviceType, const std::string& reason); 119 HaOperationMessage& SyncClick(std::string deviceType); 120 HaOperationMessage& SyncReply(std::string deviceType); 121 public: 122 bool isLiveView_ = false; 123 static OperationalData notificationData; 124 static OperationalData liveViewData; 125 }; 126 127 class HaMetaMessage { 128 public: 129 HaMetaMessage() = default; 130 ~HaMetaMessage() = default; 131 132 explicit HaMetaMessage(uint32_t sceneId, uint32_t branchId); 133 134 HaMetaMessage& SceneId(uint32_t sceneId); 135 HaMetaMessage& BranchId(uint32_t branchId); 136 HaMetaMessage& ErrorCode(uint32_t errorCode); 137 HaMetaMessage& Message(const std::string& message, bool print = false); 138 HaMetaMessage& Path(const std::string &path); 139 HaMetaMessage& Append(const std::string& message); 140 HaMetaMessage& BundleName(const std::string& bundleName_); 141 HaMetaMessage& AgentBundleName(const std::string& agentBundleName); 142 HaMetaMessage& TypeCode(int32_t typeCode); 143 HaMetaMessage& NotificationId(int32_t notificationId); 144 HaMetaMessage& SlotType(int32_t slotType); 145 HaMetaMessage& DeleteReason(int32_t deleteReason); 146 std::string GetMessage() const; 147 HaMetaMessage& Checkfailed(bool checkfailed); 148 bool NeedReport() const; 149 150 std::string Build() const; 151 152 std::string bundleName_; 153 int32_t notificationId_ = -1; 154 std::string agentBundleName_ = ""; 155 int32_t typeCode_ = -1; 156 uint32_t slotType_ = -1; 157 uint32_t sceneId_; 158 uint32_t branchId_; 159 uint32_t errorCode_ = ERR_OK; 160 std::string message_; 161 std::string path_; 162 bool checkfailed_ = true; 163 int32_t deleteReason_ = -1; 164 }; 165 166 struct FlowControllerOption { 167 int32_t count; 168 int32_t time; 169 }; 170 171 struct ReportCache { 172 EventFwk::Want want; 173 int32_t eventCode; 174 }; 175 176 struct BadgeInfo { 177 std::int64_t startTime; 178 std::int32_t changeCount; 179 std::string badgeNum; 180 std::string time; 181 bool isNeedReport; 182 }; 183 184 struct ReportSlotMessage { 185 std::string bundleName; 186 int32_t uid; 187 int32_t slotType; 188 bool status; 189 }; 190 191 struct ReportLiveViewMessage { 192 int32_t successNum; 193 int32_t FailedNum; 194 int64_t startTime; 195 }; 196 197 class NotificationAnalyticsUtil { 198 public: 199 static void ReportTipsEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 200 201 static void ReportPublishFailedEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 202 203 static void ReportDeleteFailedEvent(const sptr<NotificationRequest>& request, HaMetaMessage& message); 204 205 static void ReportPublishSuccessEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 206 207 static void ReportSAPublishSuccessEvent(const sptr<NotificationRequest>& request, int32_t callUid); 208 209 static void ReportModifyEvent(const HaMetaMessage& message); 210 211 static void ReportDeleteFailedEvent(const HaMetaMessage& message); 212 213 static void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list, 214 const std::chrono::system_clock::time_point &now, int32_t time = 1); 215 216 static int64_t GetCurrentTime(); 217 218 static void ReportOperationsDotEvent(HaOperationMessage& message); 219 220 static void ReportPublishFailedEvent(const HaMetaMessage& message); 221 222 static void ReportSkipFailedEvent(const HaMetaMessage& message); 223 224 static void ReportPublishWithUserInput(const sptr<NotificationRequest>& request); 225 226 static void ReportPublishBadge(const sptr<NotificationRequest>& request); 227 228 static void ReportBadgeChange(const sptr<BadgeNumberCallbackData> &badgeData); 229 230 static bool ReportAllBundlesSlotEnabled(); 231 232 static void ReportLiveViewNumber(const sptr<NotificationRequest>& request, const int32_t reportType); 233 private: 234 static void ReportNotificationEvent(const sptr<NotificationRequest>& request, 235 EventFwk::Want want, int32_t eventCode, const std::string& reason); 236 static void CommonNotificationEvent(const sptr<NotificationRequest>& request, 237 int32_t eventCode, const HaMetaMessage& message); 238 239 static void CommonNotificationEvent(int32_t eventCode, const HaMetaMessage& message); 240 241 static void ReportNotificationEvent(EventFwk::Want want, int32_t eventCode, const std::string& reason); 242 243 static bool ReportFlowControl(const int32_t reportType); 244 245 static bool IsAllowedBundle(const sptr<NotificationRequest>& request); 246 247 static std::string BuildAnsData(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 248 249 static ReportCache Aggregate(); 250 251 static uint32_t SetControlFlags(const std::shared_ptr<NotificationFlags> &flags, uint32_t &controlFlags); 252 253 static std::string GetDeviceStatus(const sptr<NotificationRequest>& request); 254 255 static std::list<std::chrono::system_clock::time_point> GetFlowListByType(const int32_t reportType); 256 257 static FlowControllerOption GetFlowOptionByType(const int32_t reportType); 258 259 static std::string BuildExtraInfo(const HaMetaMessage& message); 260 261 static std::string BuildExtraInfoWithReq(const HaMetaMessage& message, 262 const sptr<NotificationRequest>& request); 263 264 static void SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo); 265 266 static void AddListCache(EventFwk::Want& want, int32_t eventCode); 267 268 static void AddSuccessListCache(EventFwk::Want& want, int32_t eventCode); 269 270 static void ExecuteCacheList(); 271 272 static void ExecuteSuccessCacheList(); 273 274 static void ReportCommonEvent(const ReportCache& reportCache); 275 276 static bool DetermineWhetherToSend(uint32_t slotType); 277 278 static void AddToBadgeInfos(std::string bundle, BadgeInfo& badgeInfo); 279 280 static void CheckBadgeReport(); 281 282 static void AggregateBadgeChange(); 283 284 static bool CheckSlotNeedReport(); 285 286 static bool GetAllSlotMessageCache(const int32_t &userId); 287 288 static bool GetReportSlotMessage(std::string& budleEntryKey, std::string& budleEntryValue, 289 ReportSlotMessage& reportSlotMessage, const int32_t &userId); 290 291 static bool CreateSlotTimerExecute(const int32_t &userId); 292 293 static void ExecuteSlotReportList(); 294 295 static bool ReportSlotEnable(); 296 297 static bool BuildSlotReportCache(ReportCache& reportCache, 298 std::list<ReportSlotMessage>& slotEnabledReportList); 299 300 static void AddLiveViewSuccessNum(std::string bundle, int32_t status); 301 302 static void AddLiveViewFailedNum(std::string bundle, int32_t status); 303 304 static void CreateLiveViewTimerExecute(); 305 306 static ReportCache AggregateLiveView(); 307 308 static void ExecuteLiveViewReport(); 309 310 static void AddLocalLiveViewSuccessNum(std::string bundle); 311 312 static void AddLocalLiveViewFailedNum(std::string bundle); 313 314 static void MakeRequestBundle(const sptr<NotificationRequest>& request); 315 316 static std::string GetTraceIdStr(); 317 }; 318 } // namespace Notification 319 } // namespace OHOS 320 321 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H 322