1 /*
2  * Copyright (C) 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 #define MLOG_TAG "MultiStagesCaptureDfxTotalTime"
17 
18 #include "multistages_capture_dfx_total_time.h"
19 
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 #include "post_event_utils.h"
23 
24 namespace OHOS {
25 namespace Media {
MultiStagesCaptureDfxTotalTime()26 MultiStagesCaptureDfxTotalTime::MultiStagesCaptureDfxTotalTime() {}
27 
~MultiStagesCaptureDfxTotalTime()28 MultiStagesCaptureDfxTotalTime::~MultiStagesCaptureDfxTotalTime() {}
29 
GetInstance()30 MultiStagesCaptureDfxTotalTime& MultiStagesCaptureDfxTotalTime::GetInstance()
31 {
32     static MultiStagesCaptureDfxTotalTime instance;
33     return instance;
34 }
35 
AddStartTime(const std::string & photoId)36 void MultiStagesCaptureDfxTotalTime::AddStartTime(const std::string &photoId)
37 {
38     startTimes_.emplace(photoId, MediaFileUtils::UTCTimeMilliSeconds());
39 }
40 
RemoveStartTime(const std::string & photoId)41 void MultiStagesCaptureDfxTotalTime::RemoveStartTime(const std::string &photoId)
42 {
43     if (startTimes_.empty() || startTimes_.find(photoId) == startTimes_.end()) {
44         MEDIA_INFO_LOG("RemoveStartTime startTimes_ is empty or photoId is not in startTimes_");
45         return;
46     }
47 
48     startTimes_.erase(photoId);
49 }
50 
Report(const std::string & photoId)51 void MultiStagesCaptureDfxTotalTime::Report(const std::string &photoId)
52 {
53     if (startTimes_.empty() || startTimes_.find(photoId) == startTimes_.end()) {
54         MEDIA_INFO_LOG("startTimes_ is empty or photoId is not in startTimes_");
55         return;
56     }
57     int64_t startTime = startTimes_[photoId];
58     int64_t totalTime = MediaFileUtils::UTCTimeMilliSeconds() - startTime;
59     startTimes_.erase(photoId);
60     VariantMap map = {{KEY_PHOTO_ID, photoId}, {KEY_TOTAL_TIME_COST, totalTime}};
61     PostEventUtils::GetInstance().PostStatProcess(StatType::MSC_TOTAL_TIME_COST_STAT, map);
62 }
63 
64 } // namespace Media
65 } // namespace OHOS