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