1 /*
2 * Copyright (c) 2021 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 <fstream>
17 #include <filesystem>
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 #include <ctime>
22 #include <vector>
23
24 #include "net_stats_csv.h"
25
26 #include "net_mgr_log_wrapper.h"
27
28 #include "netsys_controller.h"
29
30 namespace OHOS {
31 namespace NetManagerStandard {
32 const std::string CSV_DIR = "/data/data/";
33 const std::string UID_LIST_DIR = "/data/data/uid/";
34 const std::string IFACE_CSV_FILE_NAME = "iface.csv";
35 const std::string UID_CSV_FILE_NAME = "uid.csv";
36 const std::string IFACE_STATS_CSV_FILE_NAME = "iface_stats.csv";
37 const std::string IFACE_STATS_CSV_BAK = "iface_stats_bak";
38 const std::string IFACE_STATS_CSV_NEW = "iface_stats_new";
39 const std::string UID_STATS_CSV_FILE_NAME = "uid_stats.csv";
40 const std::string UID_STATS_CSV_BAK = "uid_stats_bak";
41 const std::string UID_STATS_CSV_NEW = "uid_stats_new";
42 constexpr uint32_t START_END_NOT_EXIST = 0;
43 constexpr uint32_t START_NOT_EXIST_BUT_END_EXIST = 1;
44 constexpr uint32_t START_EXIST_BUT_END_NOT_EXIST = 10;
45 constexpr uint32_t START_END_EXIST = 11;
46 constexpr uint32_t DIFF_START_END = 10;
47
operator >>(std::istream & str,CSVRow & data)48 static std::istream& operator>>(std::istream& str, CSVRow& data)
49 {
50 data.ReadNextRow(str);
51 return str;
52 }
53
ReadNextRow(std::istream & str)54 void CSVRow::ReadNextRow(std::istream& str)
55 {
56 std::string line;
57 std::getline(str, line);
58
59 std::stringstream lineStream(line);
60 std::string cell;
61
62 data_.clear();
63 while (std::getline(lineStream, cell, ',')) {
64 data_.push_back(cell);
65 }
66
67 if (!lineStream && cell.empty()) {
68 data_.push_back("");
69 }
70 }
71
NetStatsCsv()72 NetStatsCsv::NetStatsCsv()
73 {}
74
~NetStatsCsv()75 NetStatsCsv::~NetStatsCsv() {}
76
ExistsIface(const std::string & iface)77 bool NetStatsCsv::ExistsIface(const std::string& iface)
78 {
79 if (iface.empty())
80 return false;
81
82 std::ifstream ifaceCsvFile(CSV_DIR + IFACE_CSV_FILE_NAME);
83 if (!ifaceCsvFile) {
84 NETMGR_LOG_E("ifstream failed");
85 return false;
86 }
87
88 CSVRow row;
89 while (ifaceCsvFile >> row) {
90 if (iface.compare(row[static_cast<uint32_t>(IfaceCsvColumn::IFACE_NAME)]) == 0)
91 return true;
92 }
93 return false;
94 }
95
ExistsUid(const uint32_t uid)96 bool NetStatsCsv::ExistsUid(const uint32_t uid)
97 {
98 std::ifstream uidCsvFile(CSV_DIR + UID_CSV_FILE_NAME);
99 if (!uidCsvFile) {
100 NETMGR_LOG_E("ifstream failed");
101 return false;
102 }
103
104 CSVRow row;
105 while (uidCsvFile >> row) {
106 if (std::to_string(uid).compare(row[static_cast<uint32_t>(UidCsvColumn::UID)]) == 0)
107 return true;
108 }
109 return false;
110 }
111
RenameStatsCsv(const std::string & fromFileName,const std::string & bakFile,const std::string & toFile)112 bool NetStatsCsv::RenameStatsCsv(const std::string &fromFileName,
113 const std::string &bakFile, const std::string &toFile)
114 {
115 if (fromFileName.empty()) {
116 NETMGR_LOG_E("fromFileName is empty");
117 return false;
118 }
119 std::string bakIfaceStatsFileName = CSV_DIR + bakFile + "_" + GetCurrentTime();
120 std::string toFileName = CSV_DIR + toFile;
121 std::lock_guard lock(mutex_);
122 if (std::rename(toFileName.c_str(), bakIfaceStatsFileName.c_str())) {
123 NETMGR_LOG_E("ofstream open failed");
124 return false;
125 }
126
127 if (std::rename(fromFileName.c_str(), toFileName.c_str())) {
128 NETMGR_LOG_E("ofstream open failed");
129 return false;
130 }
131 return true;
132 }
133
GenerateNewIfaceStats(uint32_t start,uint32_t end,const NetStatsInfo & stats,std::map<uint32_t,NetStatsInfo> & newIfaceStats)134 void NetStatsCsv::GenerateNewIfaceStats(uint32_t start, uint32_t end, const NetStatsInfo &stats,
135 std::map<uint32_t, NetStatsInfo> &newIfaceStats)
136 {
137 uint32_t startExist = 0;
138 uint32_t endExist = 0;
139 auto searchStart = newIfaceStats.find(start);
140 if (searchStart != newIfaceStats.end()) {
141 startExist = 1;
142 } else {
143 startExist = 0;
144 }
145 auto searchEnd = newIfaceStats.find(end);
146 if (searchEnd != newIfaceStats.end()) {
147 endExist = 1;
148 } else {
149 endExist = 0;
150 }
151
152 NetStatsInfo calStats;
153 uint32_t exist = startExist * DIFF_START_END + endExist;
154 switch (exist) {
155 case START_END_NOT_EXIST:
156 newIfaceStats.insert( {start, calStats} );
157 newIfaceStats.insert( {end, stats} );
158 break;
159 case START_NOT_EXIST_BUT_END_EXIST:
160 newIfaceStats.insert( {start, calStats} );
161 calStats.rxBytes_ = (searchStart->second).rxBytes_ + stats.rxBytes_;
162 calStats.txBytes_ = (searchStart->second).txBytes_ + stats.txBytes_;
163 newIfaceStats[end].rxBytes_ = calStats.rxBytes_;
164 newIfaceStats[end].txBytes_ = calStats.txBytes_;
165 break;
166 case START_EXIST_BUT_END_NOT_EXIST:
167 calStats.rxBytes_ = (searchStart->second).rxBytes_ + stats.rxBytes_;
168 calStats.txBytes_ = (searchStart->second).txBytes_ + stats.txBytes_;
169 newIfaceStats.insert( {end, calStats} );
170 break;
171 case START_END_EXIST:
172 calStats.rxBytes_ = (searchStart->second).rxBytes_ + stats.rxBytes_;
173 calStats.txBytes_ = (searchStart->second).txBytes_ + stats.txBytes_;
174 newIfaceStats[end].rxBytes_ = calStats.rxBytes_;
175 newIfaceStats[end].txBytes_ = calStats.txBytes_;
176 break;
177 default:
178 break;
179 }
180 for (auto it = newIfaceStats.cbegin(); it != newIfaceStats.cend();) {
181 if (it->first > start && it->first < end) {
182 newIfaceStats.erase(it++);
183 } else {
184 ++it;
185 }
186 }
187 }
188
CorrectedIfacesStats(const std::string & iface,uint32_t start,uint32_t end,const NetStatsInfo & stats)189 bool NetStatsCsv::CorrectedIfacesStats(const std::string &iface,
190 uint32_t start, uint32_t end, const NetStatsInfo &stats)
191 {
192 std::ofstream newIfaceStatsCsvFile;
193 std::string newIfaceStatsFileName = CSV_DIR + IFACE_STATS_CSV_NEW + "_" + GetCurrentTime();
194 newIfaceStatsCsvFile.open(newIfaceStatsFileName, std::fstream::app);
195 if (!newIfaceStatsCsvFile.is_open()) {
196 NETMGR_LOG_E("ofstream open failed");
197 return false;
198 }
199
200 CSVRow row;
201 uint32_t colTime = 0;
202 uint32_t timeCloumn = 0;
203 NetStatsInfo statsRow;
204 std::map<uint32_t, NetStatsInfo> newIfaceStats;
205 std::ifstream iface_stats_file(CSV_DIR + IFACE_STATS_CSV_FILE_NAME, std::fstream::in);
206 while (iface_stats_file >> row) {
207 if (iface.compare(row[static_cast<uint32_t>(IfaceStatsCsvColumn::IFACE_NAME)]) == 0) {
208 timeCloumn = static_cast<uint32_t>(IfaceStatsCsvColumn::TIME);
209 colTime = static_cast<uint32_t>(std::stoi(row[(timeCloumn)]));
210 statsRow.rxBytes_ =
211 static_cast<int64_t>(std::stol(row[static_cast<uint32_t>(IfaceStatsCsvColumn::RXBYTES)]));
212 statsRow.txBytes_ =
213 static_cast<int64_t>(std::stol(row[static_cast<uint32_t>(IfaceStatsCsvColumn::TXBYTES)]));
214 newIfaceStats.insert( {colTime, statsRow} );
215 continue;
216 }
217 newIfaceStatsCsvFile << row[static_cast<uint32_t>(IfaceStatsCsvColumn::IFACE_NAME)] << ","
218 << row[static_cast<uint32_t>(IfaceStatsCsvColumn::TIME)] << ","
219 << row[static_cast<uint32_t>(IfaceStatsCsvColumn::RXBYTES)] << ","
220 << row[static_cast<uint32_t>(IfaceStatsCsvColumn::TXBYTES)] << std::endl;
221 }
222 iface_stats_file.close();
223 GenerateNewIfaceStats(start, end, stats, newIfaceStats);
224 for (auto const& [key, val] : newIfaceStats) {
225 newIfaceStatsCsvFile << iface << "," << key << "," << val.rxBytes_ << "," << val.txBytes_ << std::endl;
226 }
227 newIfaceStatsCsvFile.close();
228 if (!RenameStatsCsv(newIfaceStatsFileName, IFACE_STATS_CSV_BAK, IFACE_STATS_CSV_FILE_NAME)) {
229 return false;
230 }
231 return true;
232 }
233
UpdateIfaceCsvInfo()234 bool NetStatsCsv::UpdateIfaceCsvInfo()
235 {
236 std::fstream ifaceCsvFile;
237 ifaceCsvFile.open(CSV_DIR + IFACE_CSV_FILE_NAME, std::fstream::out | std::fstream::trunc);
238 if (!ifaceCsvFile.is_open()) {
239 NETMGR_LOG_E("fstream open failed");
240 ifaceCsvFile.close();
241 return false;
242 }
243 std::vector<std::string> ifNameList = NetsysController::GetInstance().InterfaceGetList();
244 for (std::vector<std::string>::iterator iter = ifNameList.begin(); iter != ifNameList.end(); ++iter) {
245 if (*iter != "lo") {
246 ifaceCsvFile << *iter << std::endl;
247 }
248 }
249 ifaceCsvFile.close();
250 return true;
251 }
252
UpdateUidCsvInfo()253 bool NetStatsCsv::UpdateUidCsvInfo()
254 {
255 std::fstream uidCsvFile;
256 uidCsvFile.open(CSV_DIR + UID_CSV_FILE_NAME, std::fstream::out | std::fstream::trunc);
257 if (!uidCsvFile.is_open()) {
258 NETMGR_LOG_E("fstream open failed");
259 uidCsvFile.close();
260 return false;
261 }
262 std::vector<std::string> uidList = NetsysController::GetInstance().UidGetList();
263 for (std::vector<std::string>::iterator iter = uidList.begin(); iter != uidList.end(); ++iter) {
264 uidCsvFile << *iter << std::endl;
265 }
266 uidCsvFile.close();
267 return true;
268 }
269
UpdateIfaceStats()270 bool NetStatsCsv::UpdateIfaceStats()
271 {
272 std::ifstream ifaceCsvFile(CSV_DIR + IFACE_CSV_FILE_NAME);
273 if (!ifaceCsvFile) {
274 NETMGR_LOG_E("ifstream failed");
275 return false;
276 }
277
278 CSVRow row;
279 std::lock_guard lock(mutex_);
280 while (ifaceCsvFile >> row) {
281 if (!UpdateIfaceStatsCsv(row[static_cast<uint32_t>(IfaceCsvColumn::IFACE_NAME)])) {
282 NETMGR_LOG_E("UpdateIfaceStatsCsv failed, ifaceName[%{public}s]",
283 row[static_cast<uint32_t>(IfaceCsvColumn::IFACE_NAME)].c_str());
284 return false;
285 }
286 }
287 return true;
288 }
289
UpdateUidStats()290 bool NetStatsCsv::UpdateUidStats()
291 {
292 std::ifstream uidCsvFile(CSV_DIR + UID_CSV_FILE_NAME);
293 if (!uidCsvFile) {
294 NETMGR_LOG_E("ifstream failed");
295 return false;
296 }
297
298 // get current iface name, todo
299 std::string iface = "eth0";
300 CSVRow row;
301 while (uidCsvFile >> row) {
302 uint32_t uid = static_cast<std::uint32_t>(std::stoi(row[static_cast<uint32_t>(UidCsvColumn::UID)]));
303 if (!UpdateUidStatsCsv(uid, iface)) {
304 NETMGR_LOG_E("UpdateUidStatsCsv failed, uid[%{public}d], ifaceName[%{public}s]", uid, iface.c_str());
305 return false;
306 }
307 }
308 return true;
309 }
310
UpdateIfaceStatsCsv(const std::string & iface)311 bool NetStatsCsv::UpdateIfaceStatsCsv(const std::string &iface)
312 {
313 std::ofstream ifaceStatsCsvFile;
314 ifaceStatsCsvFile.open(CSV_DIR + IFACE_STATS_CSV_FILE_NAME, std::fstream::app);
315 if (!ifaceStatsCsvFile.is_open()) {
316 NETMGR_LOG_E("ofstream open failed");
317 ifaceStatsCsvFile.close();
318 return false;
319 }
320 ifaceStatsCsvFile << iface << "," << GetCurrentTime() << ","
321 << NetsysController::GetInstance().GetIfaceRxBytes(iface) << "," <<
322 NetsysController::GetInstance().GetIfaceTxBytes(iface) << std::endl;
323
324 ifaceStatsCsvFile.close();
325 return true;
326 }
327
UpdateUidStatsCsv(uint32_t uid,const std::string & iface)328 bool NetStatsCsv::UpdateUidStatsCsv(uint32_t uid, const std::string &iface)
329 {
330 std::ofstream uidStatsCsvFile;
331 uidStatsCsvFile.open(CSV_DIR + UID_STATS_CSV_FILE_NAME, std::fstream::app);
332 if (!uidStatsCsvFile.is_open()) {
333 NETMGR_LOG_E("ofstream open failed");
334 uidStatsCsvFile.close();
335 return false;
336 }
337
338 uidStatsCsvFile << uid<< "," << iface << "," << GetCurrentTime() << ","
339 << NetsysController::GetInstance().GetUidOnIfaceRxBytes(uid, iface) << "," <<
340 NetsysController::GetInstance().GetUidOnIfaceTxBytes(uid, iface) << std::endl;
341 uidStatsCsvFile.close();
342 return true;
343 }
344
DeleteUidStatsCsv(uint32_t uid)345 bool NetStatsCsv::DeleteUidStatsCsv(uint32_t uid)
346 {
347 std::string strUid = std::to_string(uid);
348 // std::filesystem::remove_all(UID_LIST_DIR + strUid.c_str());
349 NETMGR_LOG_I("Delete mock uid directory: /data/data/uid/[%{public}d]", uid);
350 UpdateUidCsvInfo();
351
352 std::ofstream newUidStatsCsvFile;
353 std::string newUidStatsFileName = CSV_DIR + UID_STATS_CSV_NEW + "_" + GetCurrentTime();
354 newUidStatsCsvFile.open(newUidStatsFileName, std::fstream::app);
355 if (!newUidStatsCsvFile.is_open()) {
356 NETMGR_LOG_E("ofstream open failed");
357 return false;
358 }
359
360 std::ifstream uid_file(CSV_DIR + UID_STATS_CSV_FILE_NAME, std::fstream::in);
361 CSVRow row;
362 while (uid_file >> row) {
363 if ((std::to_string(uid).compare(row[static_cast<uint32_t>(UidStatCsvColumn::UID)]) == 0)) {
364 continue;
365 }
366 newUidStatsCsvFile << row[static_cast<uint32_t>(UidStatCsvColumn::UID)] << ","
367 << row[static_cast<uint32_t>(UidStatCsvColumn::IFACE_NAME)] << ","
368 << row[static_cast<uint32_t>(UidStatCsvColumn::TIME)] << ","
369 << row[static_cast<uint32_t>(UidStatCsvColumn::RXBYTES)] << ","
370 << row[static_cast<uint32_t>(UidStatCsvColumn::TXBYTES)] << std::endl;
371 }
372 newUidStatsCsvFile.close();
373 if (!RenameStatsCsv(newUidStatsFileName, UID_STATS_CSV_BAK, UID_STATS_CSV_FILE_NAME)) {
374 return false;
375 }
376 return true;
377 }
378
GetIfaceCalculateTime(const std::string & iface,uint32_t time)379 uint32_t NetStatsCsv::GetIfaceCalculateTime(const std::string &iface, uint32_t time)
380 {
381 std::ifstream iface_file(CSV_DIR + IFACE_STATS_CSV_FILE_NAME, std::fstream::in);
382 CSVRow row;
383 uint32_t columnTime = 0;
384 while (iface_file >> row) {
385 if (iface.compare(row[static_cast<uint32_t>(IfaceStatsCsvColumn::IFACE_NAME)]) == 0) {
386 columnTime = static_cast<uint32_t>(std::stoi(row[static_cast<uint32_t>(IfaceStatsCsvColumn::TIME)]));
387 if (time <= columnTime) {
388 return columnTime;
389 }
390 }
391 }
392 return columnTime;
393 }
394
GetUidCalculateTime(uint32_t uid,const std::string & iface,uint32_t time)395 uint32_t NetStatsCsv::GetUidCalculateTime(uint32_t uid, const std::string &iface, uint32_t time)
396 {
397 std::ifstream uid_file(CSV_DIR + UID_STATS_CSV_FILE_NAME, std::fstream::in);
398 CSVRow row;
399 uint32_t columnTime = 0;
400 while (uid_file >> row) {
401 if ((std::to_string(uid).compare(row[static_cast<uint32_t>(UidStatCsvColumn::UID)]) == 0) &&
402 (iface.compare(row[static_cast<uint32_t>(UidStatCsvColumn::IFACE_NAME)]) == 0)) {
403 columnTime = static_cast<uint32_t>(std::stoi(row[static_cast<uint32_t>(UidStatCsvColumn::TIME)]));
404 if (time <= columnTime) {
405 return columnTime;
406 }
407 }
408 }
409 return columnTime;
410 }
411
GetSumStats(const std::vector<NetStatsInfo> & vecRow,NetStatsInfo & sumStats)412 void NetStatsCsv::GetSumStats(const std::vector<NetStatsInfo> &vecRow, NetStatsInfo &sumStats)
413 {
414 uint32_t restartTimes = 0;
415 NetStatsInfo startStats = vecRow[0];
416 for (uint32_t i = 1; i < vecRow.size(); i++) {
417 if (vecRow[i].rxBytes_ < vecRow[i - 1].rxBytes_) {
418 restartTimes++;
419 NetStatsInfo endStats = vecRow[i - 1];
420 GetPeriodStats(startStats, endStats, sumStats);
421 startStats = vecRow[i];
422 }
423 }
424 if (restartTimes >= 1) {
425 GetPeriodStats(startStats, vecRow[vecRow.size() - 1], sumStats);
426 }
427 if (restartTimes == 0) {
428 GetPeriodStats(vecRow[0], vecRow[vecRow.size() - 1], sumStats);
429 }
430 }
431
GetPeriodStats(const NetStatsInfo & startStats,const NetStatsInfo & endStats,NetStatsInfo & totalStats)432 void NetStatsCsv::GetPeriodStats(const NetStatsInfo &startStats, const NetStatsInfo &endStats, NetStatsInfo &totalStats)
433 {
434 totalStats.rxBytes_ += endStats.rxBytes_ - startStats.rxBytes_;
435 totalStats.txBytes_ += endStats.txBytes_ - startStats.txBytes_;
436 }
437
GetCalculateStatsInfo(const CSVRow & row,uint32_t calStartTime,uint32_t calEndTime,uint32_t timeCloumn,std::vector<NetStatsInfo> & vecRow)438 void NetStatsCsv::GetCalculateStatsInfo(const CSVRow &row, uint32_t calStartTime, uint32_t calEndTime,
439 uint32_t timeCloumn, std::vector<NetStatsInfo> &vecRow)
440 {
441 uint32_t rowTime = static_cast<uint32_t>(std::stoi(row[(timeCloumn)]));
442 if (calStartTime <= rowTime && rowTime <= calEndTime) {
443 uint32_t rxColumn = static_cast<uint32_t>(++timeCloumn);
444 uint32_t txCloumn = static_cast<uint32_t>(++timeCloumn);
445 NetStatsInfo statsRow;
446 statsRow.rxBytes_ = static_cast<int64_t>(std::stoi(row[rxColumn]));
447 statsRow.txBytes_ = static_cast<int64_t>(std::stoi(row[txCloumn]));
448 vecRow.push_back(statsRow);
449 }
450 }
451
GetIfaceBytes(const std::string & iface,uint32_t start,uint32_t end,NetStatsInfo & statsInfo)452 NetStatsResultCode NetStatsCsv::GetIfaceBytes(const std::string &iface, uint32_t start, uint32_t end,
453 NetStatsInfo &statsInfo)
454 {
455 uint32_t calStartTime = GetIfaceCalculateTime(iface, start);
456 uint32_t calEndTime = GetIfaceCalculateTime(iface, end);
457 if (calStartTime == calEndTime) {
458 statsInfo.rxBytes_ = 0;
459 statsInfo.txBytes_ = 0;
460 NETMGR_LOG_E("there is no data in this time period, please enter a valid time period.");
461 return NetStatsResultCode::ERR_INVALID_TIME_PERIOD;
462 }
463
464 CSVRow row;
465 std::vector<NetStatsInfo> vecRow;
466 std::ifstream iface_file(CSV_DIR + IFACE_STATS_CSV_FILE_NAME, std::fstream::in);
467 while (iface_file >> row) {
468 if (iface.compare(row[static_cast<uint32_t>(IfaceStatsCsvColumn::IFACE_NAME)]) == 0) {
469 uint32_t timeCloumn = static_cast<uint32_t>(IfaceStatsCsvColumn::TIME);
470 GetCalculateStatsInfo(row, calStartTime, calEndTime, timeCloumn, vecRow);
471 }
472 }
473 GetSumStats(vecRow, statsInfo);
474 return NetStatsResultCode::ERR_NONE;
475 }
476
GetUidBytes(const std::string & iface,uint32_t uid,uint32_t start,uint32_t end,NetStatsInfo & statsInfo)477 NetStatsResultCode NetStatsCsv::GetUidBytes(const std::string &iface, uint32_t uid, uint32_t start,
478 uint32_t end, NetStatsInfo &statsInfo)
479 {
480 uint32_t calStartTime = GetUidCalculateTime(uid, iface, start);
481 uint32_t calEndTime = GetUidCalculateTime(uid, iface, end);
482 if (calStartTime == calEndTime) {
483 statsInfo.rxBytes_ = 0;
484 statsInfo.txBytes_ = 0;
485 NETMGR_LOG_E("there is no data in this time period, please enter a valid time period.");
486 return NetStatsResultCode::ERR_INVALID_TIME_PERIOD;
487 }
488
489 std::ifstream uid_file(CSV_DIR + UID_STATS_CSV_FILE_NAME, std::fstream::in);
490 CSVRow row;
491 std::vector<NetStatsInfo> vecRow;
492 while (uid_file >> row) {
493 if ((std::to_string(uid).compare(row[static_cast<uint32_t>(UidStatCsvColumn::UID)]) == 0) &&
494 (iface.compare(row[static_cast<uint32_t>(UidStatCsvColumn::IFACE_NAME)]) == 0)) {
495 uint32_t timeCloumn = static_cast<uint32_t>(UidStatCsvColumn::TIME);
496 GetCalculateStatsInfo(row, calStartTime, calEndTime, timeCloumn, vecRow);
497 }
498 }
499 GetSumStats(vecRow, statsInfo);
500 return NetStatsResultCode::ERR_NONE;
501 }
502
GetCurrentTime()503 std::string NetStatsCsv::GetCurrentTime()
504 {
505 std::time_t now = std::time(nullptr);
506 if (now < 0) {
507 NETMGR_LOG_E("NetStatsCsv GetCurrentTime failed");
508 }
509 std::stringstream ss;
510 ss << now;
511 return ss.str();
512 }
513
ResetFactory()514 NetStatsResultCode NetStatsCsv::ResetFactory()
515 {
516 if (remove((CSV_DIR + IFACE_CSV_FILE_NAME).c_str()) == -1 ||
517 remove((CSV_DIR + UID_CSV_FILE_NAME).c_str()) == -1 ||
518 remove((CSV_DIR + IFACE_STATS_CSV_FILE_NAME).c_str()) == -1 ||
519 remove((CSV_DIR + UID_STATS_CSV_FILE_NAME).c_str()) == -1) {
520 NETMGR_LOG_I("ResetFactory is failed");
521 return NetStatsResultCode::ERR_INTERNAL_ERROR;
522 }
523 NETMGR_LOG_I("Reset Factory Stats, delete files /data/data/iface.csv");
524 NETMGR_LOG_I("Reset Factory Stats, delete files /data/data/uid.csv");
525 NETMGR_LOG_I("Reset Factory Stats, delete files /data/data/iface_stats.csv");
526 NETMGR_LOG_I("Reset Factory Stats, delete files /data/data/uid_stats.csv");
527 return NetStatsResultCode::ERR_NONE;
528 }
529 } // namespace NetManagerStandard
530 } // namespace OHOS
531