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 "MultiStagesCaptureDfxFirstVisit"
17 
18 #include "multistages_capture_dfx_first_visit.h"
19 
20 #include <memory>
21 
22 #include "database_adapter.h"
23 #include "media_file_utils.h"
24 #include "media_log.h"
25 #include "medialibrary_command.h"
26 #include "medialibrary_data_manager_utils.h"
27 #include "post_event_utils.h"
28 #include "result_set_utils.h"
29 #include "values_bucket.h"
30 
31 using namespace std;
32 
33 namespace OHOS {
34 namespace Media {
MultiStagesCaptureDfxFirstVisit()35 MultiStagesCaptureDfxFirstVisit::MultiStagesCaptureDfxFirstVisit() {}
36 
~MultiStagesCaptureDfxFirstVisit()37 MultiStagesCaptureDfxFirstVisit::~MultiStagesCaptureDfxFirstVisit() {}
38 
GetInstance()39 MultiStagesCaptureDfxFirstVisit& MultiStagesCaptureDfxFirstVisit::GetInstance()
40 {
41     static MultiStagesCaptureDfxFirstVisit instance;
42     return instance;
43 }
44 
ReportInternal(AsyncTaskData * taskData)45 static void ReportInternal(AsyncTaskData *taskData)
46 {
47     if (taskData == nullptr) {
48         MEDIA_ERR_LOG("taskData is nullptr");
49         return;
50     }
51     FirstVisitAsyncTaskData *task = static_cast<FirstVisitAsyncTaskData*>(taskData);
52     if (task == nullptr) {
53         MEDIA_ERR_LOG("task is nullptr");
54         return;
55     }
56 
57     VariantMap map = {{KEY_PHOTO_ID, task->photoId_}, {KEY_TIME_INTERVAL, task->visitTime_ - task->startTime_}};
58     PostEventUtils::GetInstance().PostStatProcess(StatType::MSC_FIRST_VISIT_STAT, map);
59 
60     // update first_visit_time in Photos table
61     NativeRdb::ValuesBucket values;
62     values.PutLong(PhotoColumn::PHOTO_FIRST_VISIT_TIME, task->visitTime_);
63     MediaLibraryCommand updateCmd(OperationObject::FILESYSTEM_PHOTO, OperationType::UPDATE, values);
64     updateCmd.GetAbsRdbPredicates()->EqualTo(MediaColumn::MEDIA_ID, task->fileId_);
65     auto result = DatabaseAdapter::Update(updateCmd);
66     MEDIA_INFO_LOG("ReportInternal exit result: %{public}d", result);
67 }
68 
Report(const string & photoId)69 void MultiStagesCaptureDfxFirstVisit::Report(const string &photoId)
70 {
71     if (photoId.empty()) {
72         MEDIA_INFO_LOG("Report photoId is empty");
73         return;
74     }
75 
76     MediaLibraryCommand cmd(OperationObject::FILESYSTEM_PHOTO, OperationType::QUERY);
77     cmd.GetAbsRdbPredicates()->EqualTo(PhotoColumn::PHOTO_ID, photoId);
78     vector<string> columns { MediaColumn::MEDIA_ID, PhotoColumn::PHOTO_FIRST_VISIT_TIME,
79         PhotoColumn::PHOTO_LAST_VISIT_TIME };
80 
81     auto resultSet = DatabaseAdapter::Query(cmd, columns);
82     if (resultSet == nullptr || resultSet->GoToFirstRow() != 0) {
83         MEDIA_INFO_LOG("result set is empty");
84         return;
85     }
86 
87     int64_t firstVisitTime = GetInt64Val(PhotoColumn::PHOTO_FIRST_VISIT_TIME, resultSet);
88     if (firstVisitTime > 0) {
89         // had reported, do not need to report again
90         return;
91     }
92 
93     shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
94     if (asyncWorker == nullptr) {
95         MEDIA_INFO_LOG("can not get async worker");
96         return;
97     }
98 
99     int32_t fileId = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
100     int64_t lastVisitTime = GetInt64Val(PhotoColumn::PHOTO_LAST_VISIT_TIME, resultSet);
101     FirstVisitAsyncTaskData *taskData = new (std::nothrow) FirstVisitAsyncTaskData(fileId, photoId, lastVisitTime,
102         MediaFileUtils::UTCTimeMilliSeconds());
103     shared_ptr<MediaLibraryAsyncTask> asyncTask = make_shared<MediaLibraryAsyncTask>(ReportInternal, taskData);
104     if (asyncTask == nullptr) {
105         MEDIA_INFO_LOG("report first visit failed");
106         return;
107     }
108 
109     asyncWorker->AddTask(asyncTask, true);
110 }
111 
112 } // namespace Media
113 } // namespace OHOS