1 /*
2 * Copyright (C) 2021-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 <iomanip>
17 #include <vector>
18 #include <string>
19 #include <sstream>
20 #include <ostream>
21 #include <ios>
22 #include "constant.h"
23 #include "download_info.h"
24 #include "download_service_manager.h"
25 #include "dump_task_info.h"
26
27 namespace OHOS::Request::Download {
Dump(int fd,const std::vector<std::string> & args)28 bool DumpTaskInfo::Dump(int fd, const std::vector<std::string> &args)
29 {
30 uint32_t argsNum = args.size();
31 if (argsNum > 1) {
32 dprintf(fd, "too many args, -t accept no arg or one arg \n");
33 return false;
34 }
35
36 if (argsNum == 0) {
37 DumpAllTask(fd);
38 } else {
39 DumpTaskDetailInfo(fd, std::stoul(args[0]));
40 }
41
42 return true;
43 }
44
DumpAllTaskTile(int fd) const45 void DumpTaskInfo::DumpAllTaskTile(int fd) const
46 {
47 std::ostringstream buffer;
48 buffer << std::left;
49 FormatSummaryTitle(buffer);
50 dprintf(fd, "%s\n", buffer.str().c_str());
51 }
52
FormatSummaryTitle(std::ostringstream & buffer) const53 void DumpTaskInfo::FormatSummaryTitle(std::ostringstream &buffer) const
54 {
55 for (const auto &it: summaryColumnTitle_) {
56 buffer << std::setw(it.first) << it.second;
57 }
58 }
59
FormatDetailTitle(std::ostringstream & buffer) const60 void DumpTaskInfo::FormatDetailTitle(std::ostringstream &buffer) const
61 {
62 for (const auto &it: detailColumnTitle_) {
63 buffer << std::setw(it.first) << it.second;
64 }
65 }
66
DumpTaskDetailInfoTile(int fd) const67 void DumpTaskInfo::DumpTaskDetailInfoTile(int fd) const
68 {
69 std::ostringstream buffer;
70 buffer << std::left;
71 FormatSummaryTitle(buffer);
72 FormatDetailTitle(buffer);
73 dprintf(fd, "%s\n", buffer.str().c_str());
74 }
75
FormatSummaryContent(const DownloadInfo & taskInfo,std::ostringstream & buffer) const76 void DumpTaskInfo::FormatSummaryContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
77 {
78 for (const auto &it: dumpSummaryCfg_) {
79 auto columnFormatFun = it.second;
80 buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
81 }
82 }
83
FormatDetailContent(const DownloadInfo & taskInfo,std::ostringstream & buffer) const84 void DumpTaskInfo::FormatDetailContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
85 {
86 for (const auto &it: dumpDetailCfg_) {
87 auto columnFormatFun = it.second;
88 buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
89 }
90 }
91
DumpAllTask(int fd) const92 bool DumpTaskInfo::DumpAllTask(int fd) const
93 {
94 std::vector<DownloadInfo> taskVector;
95 auto instance = DownloadServiceManager::GetInstance();
96 if (instance == nullptr) {
97 dprintf(fd, "not enough memory\n");
98 return false;
99 }
100 instance->QueryAllTask(taskVector);
101 dprintf(fd, "task num: %lu\n", taskVector.size());
102 if (taskVector.empty()) {
103 return true;
104 }
105
106 DumpAllTaskTile(fd);
107 for (const auto &iter: taskVector) {
108 std::ostringstream buffer;
109 buffer << std::left;
110 FormatSummaryContent(iter, buffer);
111 dprintf(fd, "%s\n", buffer.str().c_str());
112 }
113 taskVector.clear();
114 return true;
115 }
116
DumpTaskDetailInfo(int fd,uint32_t taskId) const117 bool DumpTaskInfo::DumpTaskDetailInfo(int fd, uint32_t taskId) const
118 {
119 DownloadInfo downloadInfo;
120 auto instance = DownloadServiceManager::GetInstance();
121 if (instance == nullptr) {
122 dprintf(fd, "not enough memory\n");
123 return false;
124 }
125 bool ret = instance->Query(taskId, downloadInfo);
126 if (!ret) {
127 dprintf(fd, "invalid task id %u\n", taskId);
128 return false;
129 }
130
131 DumpTaskDetailInfoTile(fd);
132 std::ostringstream buffer;
133 buffer << std::left;
134 FormatSummaryContent(downloadInfo, buffer);
135 FormatDetailContent(downloadInfo, buffer);
136 dprintf(fd, "%s\n", buffer.str().c_str());
137 return true;
138 }
139
DumpTaskID(const DownloadInfo & taskInfo) const140 std::string DumpTaskInfo::DumpTaskID(const DownloadInfo &taskInfo) const
141 {
142 return std::to_string(taskInfo.GetDownloadId());
143 }
144
DumpTaskType(const DownloadInfo & taskInfo) const145 std::string DumpTaskInfo::DumpTaskType(const DownloadInfo &taskInfo) const
146 {
147 return taskInfo.GetTaskType();
148 }
149
DumpTaskStatus(const DownloadInfo & taskInfo) const150 std::string DumpTaskInfo::DumpTaskStatus(const DownloadInfo &taskInfo) const
151 {
152 DownloadStatus status = taskInfo.GetStatus();
153 std::vector<std::pair<DownloadStatus, std::string>> mapping = {
154 {SESSION_SUCCESS, "complete"},
155 {SESSION_RUNNING, "running"},
156 {SESSION_PENDING, "pending"},
157 {SESSION_PAUSED, "pause"},
158 {SESSION_FAILED, "failed"},
159 {SESSION_UNKNOWN, "unknown"},
160 };
161
162 for (const auto &it: mapping) {
163 if (it.first == status) {
164 return it.second;
165 }
166 }
167 return "unknown";
168 }
169
DumpFileName(const DownloadInfo & taskInfo) const170 std::string DumpTaskInfo::DumpFileName(const DownloadInfo &taskInfo) const
171 {
172 return taskInfo.GetFileName();
173 }
174
DumpRoaming(const DownloadInfo & taskInfo) const175 std::string DumpTaskInfo::DumpRoaming(const DownloadInfo &taskInfo) const
176 {
177 return std::to_string(taskInfo.GetRoaming());
178 }
179
DumpNetworkType(const DownloadInfo & taskInfo) const180 std::string DumpTaskInfo::DumpNetworkType(const DownloadInfo &taskInfo) const
181 {
182 return std::to_string(taskInfo.GetNetworkType());
183 }
184
DumpMetered(const DownloadInfo & taskInfo) const185 std::string DumpTaskInfo::DumpMetered(const DownloadInfo &taskInfo) const
186 {
187 return std::to_string(taskInfo.GetMetered());
188 }
189
DumpFileSize(const DownloadInfo & taskInfo) const190 std::string DumpTaskInfo::DumpFileSize(const DownloadInfo &taskInfo) const
191 {
192 return std::to_string(taskInfo.GetDownloadTotalBytes());
193 }
194
DumpTransferredSize(const DownloadInfo & taskInfo) const195 std::string DumpTaskInfo::DumpTransferredSize(const DownloadInfo &taskInfo) const
196 {
197 return std::to_string(taskInfo.GetDownloadedBytes());
198 }
199 }