1 /*
2  * Copyright (c) 2023-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 OHOS_CAMERA_DPS_DEFERRED_EVENT_REPORT_H
17 #define OHOS_CAMERA_DPS_DEFERRED_EVENT_REPORT_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <string>
22 #include <schedule/photo_processor/photo_job_repository/deferred_photo_job.h>
23 #include "deferred_processing_service_ipc_interface_code.h"
24 #include <shared_mutex>
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 namespace DeferredProcessing {
29 const std::string EVENT_KEY_IMAGEID = "IMAGEID";
30 const std::string EVENT_KEY_USERID = "USERID";
31 const std::string EVENT_KEY_DEFEVENTTYPE = "DEFEVENTTYPE";
32 const std::string EVENT_KEY_DISCARDABLE = "DISCARDABLE";
33 const std::string EVENT_KEY_TRIGGERMODE = "TRIGGERMODE";
34 const std::string EVENT_KEY_HIGHJOBNUM = "HIGHJOBNUM";
35 const std::string EVENT_KEY_NORMALJOBNUM = "NORMALJOBNUM";
36 const std::string EVENT_KEY_LOWJOBNUM = "LOWJOBNUM";
37 const std::string EVENT_KEY_TEMPERATURELEVEL = "TEMPERATURELEVEL";
38 const std::string EVENT_KEY_SYNCHRONIZETIME = "SYNCHRONIZETIME";
39 const std::string EVENT_KEY_DISPATCHTIME = "DISPATCHTIME";
40 const std::string EVENT_KEY_PROCESSTIME = "PROCESSTIME";
41 const std::string EVENT_KEY_IMAGEDONETIME = "IMAGEDONETIME";
42 const std::string EVENT_KEY_RESTORETIME = "RESTORETIME";
43 const std::string EVENT_KEY_REMOVETIME = "CANCELTIME";
44 const std::string EVENT_KEY_TRAILINGTIME = "TRAILINGTIME";
45 const std::string EVENT_KEY_PHOTOJOBTYPE = "PHOTOJOBTYPE";
46 const std::string EVENT_KEY_EXCUTIONMODE = "EXCUTIONMODE";
47 const std::string EVENT_KEY_CHANGEREASON = "CHANGEREASON";
48 const std::string EVENT_KEY_EXCEPTIONSOURCE = "EXCEPTIONSOURCE";
49 const std::string EVENT_KEY_EXCEPTIONCAUSE = "EXCEPTIONCAUSE";
50 
51 struct DPSEventInfo {
52     std::string imageId;
53     int32_t userId;
54     TrigerMode triggerMode;
55     uint32_t operatorStage;
56     bool discardable;
57     uint64_t synchronizeTimeBeginTime;
58     uint64_t synchronizeTimeEndTime;
59     uint64_t dispatchTimeBeginTime;
60     uint64_t dispatchTimeEndTime;
61     uint64_t processTimeBeginTime;
62     uint64_t processTimeEndTime;
63     uint64_t imageDoneTimeBeginTime;
64     uint64_t imageDoneTimeEndTime;
65     uint64_t restoreTimeBeginTime;
66     uint64_t restoreTimeEndTime;
67     uint64_t removeTimeBeginTime;
68     uint64_t removeTimeEndTime;
69     uint64_t trailingTimeBeginTime;
70     uint64_t trailingTimeEndTime;
71     int32_t highJobNum;
72     int32_t normalJobNum;
73     int32_t lowJobNum;
74     int temperatureLevel;
75     ExecutionMode executionMode;
76     PhotoJobType photoJobType;
77     EventType changeReason;
78     ExceptionSource exceptionSource;
79     ExceptionCause exceptionCause;
80 };
81 
82 class DPSEventReport {
83 public:
GetInstance()84     static DPSEventReport &GetInstance()
85     {
86         static DPSEventReport instance;
87         return instance;
88     }
89     void ReportOperateImage(const std::string& imageId, int32_t userId, DPSEventInfo& dpsEventInfo);
90     void ReportImageProcessResult(const std::string& imageId, int32_t userId, uint64_t endTime = 0);
91     void ReportImageModeChange(ExecutionMode executionMode);
92     void ReportImageException(const std::string& imageId, int32_t userId);
93     void SetEventInfo(const std::string& imageId, int32_t userId);
94     void SetEventInfo(DPSEventInfo& dpsEventInfo);
95     void RemoveEventInfo(const std::string& imageId, int32_t userId);
96 
97     void SetTemperatureLevel(int temperatureLevel);
98     void SetExecutionMode(ExecutionMode executionMode);
99     void SetEventType(EventType eventType_);
100     void UpdateEventInfo(DPSEventInfo& dpsEventInfo);
101     void UpdateProcessDoneTime(const std::string& imageId, int32_t userId);
102     void UpdateRemoveTime(const std::string& imageId, int32_t userId);
103 
104 private:
105     DPSEventInfo GetEventInfo(const std::string& imageId, int32_t userId);
106     void UpdateSynchronizeTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
107     void UpdateDispatchTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
108     void UpdateProcessTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
109     void UpdateImageDoneTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
110     void UpdateRestoreTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
111     void UpdateRemoveTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
112     void UpdateTrailingTime(DPSEventInfo& dpsEventInfo, DPSEventInfo& dpsEventInfoSrc);
113     int GetTotalTime (uint64_t beginTime, uint64_t endTime);
114     std::mutex mutex_;
115     std::map<int32_t, std::map<std::string, DPSEventInfo>> userIdToImageIdEventInfo; //userid--imageid--eventinfo
116     ExecutionMode executionMode_;
117     int temperatureLevel_;
118     EventType eventType_;
119 };
120 } // namespace DeferredProcessingService
121 } // namespace CameraStandard
122 } // namespace OHOS
123 #endif // OHOS_CAMERA_DPS_DEFERRED_PROCESSING_SERVICE_H