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 #ifndef OHOS_DISTRIBUTED_CONTINUE_TIME_DUMPER_H
17 #define OHOS_DISTRIBUTED_CONTINUE_TIME_DUMPER_H
18 
19 #include <list>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "single_instance.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 
29 struct DmsDumperInfo {
30     std::string bundleName;
31     std::string netWorkId;
32     std::string abilityName;
33     std::string deviceName;
34 };
35 
36 class DmsDuration {
37 public:
38     DmsDuration() = default;
39     DmsDuration(const int64_t begin, const int64_t end, const std::string name);
40     ~DmsDuration() = default;
41 
42     void SetBeginTime(const int64_t time);
43     void SetEndTime(const int64_t time);
44     void SetDurationTime(const int64_t time);
45     void SetStrTime(const std::string name);
46     void SetDurationName(const std::string name);
47 
48     int64_t GetBeginTime();
49     int64_t GetEndTime();
50     int64_t GetDurationTime();
51     std::string GetStrTime();
52     std::string GetDurationName();
53 
54 private:
55     int64_t beginTime_;
56     int64_t endTime_;
57     int64_t duration_ = 0;
58     std::string strTime_;
59     std::string durationName_;
60 };
61 
62 class DmsContinueTime {
63 public:
64     DECLARE_SINGLE_INSTANCE_BASE(DmsContinueTime);
65 
66 public:
67     DmsContinueTime() = default;
68     ~DmsContinueTime() = default;
69 
70     void Init();
71     void SetPull(bool sign);
72     bool GetPull();
73     void SetSrcBundleName(const std::string bundleName);
74     void SetSrcAbilityName(const std::string abilityName);
75     void SetDstBundleName(const std::string bundleName);
76     void SetDstAbilityName(const std::string abilityName);
77     void SetNetWorkId(const std::string srcNetWorkId, const std::string dstNetWorkId);
78     void SetDeviceNamePull();
79     void SetDeviceNamePush();
80     DmsDumperInfo GetDstInfo();
81 
82     void SetDurationBegin(const int32_t idx, const int64_t time);
83     void SetDurationEnd(const int32_t idx, const int64_t time);
84     void SetDurationStrTime(const int32_t idx, const std::string info);
85 
86     void SetSaveDataDurationBegin(const int64_t time);
87     void SetSaveDataDurationEnd(const int64_t time);
88     DmsDuration GetSaveDataDuration();
89 
90     std::string WriteDurationInfo(DmsDuration duration);
91     void ReadDurationInfo(const char* info);
92     std::string WriteDstInfo(const std::string bundleName, const std::string abilityName);
93     void ReadDstInfo(const char* info);
94     std::string GetCurrentTime();
95     void DealDurationPull();
96     void DealDurationPush();
97     void AppendInfo();
98     void ShowInfo(std::string& result);
99 
100     int64_t GetTotalTime();
101 
102 private:
103     bool isPull_;
104     std::list<std::string> timeInfoList_;
105     std::vector<DmsDuration> durationInfo_;
106     std::string appendInfo_;
107     DmsDumperInfo srcInfo_;
108     DmsDumperInfo dstInfo_;
109     DmsDuration saveDataDuration_;
110     std::mutex infoMutex_;
111 };
112 } // namespace DistributedSchedule
113 } // namespace OHOS
114 #endif // OHOS_DISTRIBUTED_CONTINUE_TIME_DUMPER_H