1 /*
2  * Copyright (c) 2022 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 #include "bundle_active_event_tracker.h"
17 
18 namespace OHOS {
19 namespace DeviceUsageStats {
BundleActiveEventTracker()20 BundleActiveEventTracker::BundleActiveEventTracker()
21 {
22     curStartTime_ = 0;
23     lastEventTime_ = 0;
24     duration_ = 0;
25     count_ =0;
26 }
27 
CommitTime(const int64_t timeStamp)28 void BundleActiveEventTracker::CommitTime(const int64_t timeStamp)
29 {
30     if (curStartTime_) {
31         duration_ += timeStamp - curStartTime_;
32         curStartTime_ = 0;
33     }
34 }
35 
Update(int64_t timeStamp)36 void BundleActiveEventTracker::Update(int64_t timeStamp)
37 {
38     if (!curStartTime_) {
39         count_++;
40     }
41     CommitTime(timeStamp);
42     curStartTime_ = timeStamp;
43     lastEventTime_ = timeStamp;
44 }
45 
AddToEventStats(std::vector<BundleActiveEventStats> & eventStatsList,int32_t eventId,int64_t beginTime,int64_t endTime)46 void BundleActiveEventTracker::AddToEventStats(std::vector<BundleActiveEventStats>& eventStatsList, int32_t eventId,
47     int64_t beginTime, int64_t endTime)
48 {
49     if (count_ || duration_) {
50         BundleActiveEventStats newEvent;
51         newEvent.eventId_ = eventId;
52         newEvent.count_ = count_;
53         newEvent.totalTime_ = duration_;
54         newEvent.lastEventTime_ = lastEventTime_;
55         newEvent.beginTimeStamp_ = beginTime;
56         newEvent.endTimeStamp_ = endTime;
57         eventStatsList.emplace_back(newEvent);
58     }
59 }
60 }  // namespace DeviceUsageStats
61 }  // namespace OHOS
62 
63