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 #ifndef NET_STATS_CSV_H 17 #define NET_STATS_CSV_H 18 19 #include <iostream> 20 #include <fstream> 21 #include <sstream> 22 #include <vector> 23 #include <string> 24 #include <map> 25 26 #include "net_stats_info.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 // iface.csv column definition 31 enum class IfaceCsvColumn { 32 IFACE_NAME, 33 TYPE 34 }; 35 36 // uid.csv column definition 37 enum class UidCsvColumn { 38 UID, 39 UID_NAME 40 }; 41 42 // iface_stats.csv column definition 43 enum class IfaceStatsCsvColumn { 44 IFACE_NAME, 45 TIME, 46 RXBYTES, 47 TXBYTES 48 } ; 49 50 // uid_stats.csv column definition 51 enum class UidStatCsvColumn { 52 UID, 53 IFACE_NAME, 54 TIME, 55 RXBYTES, 56 TXBYTES 57 } ; 58 59 class CSVRow { 60 public: 61 const std::string& operator[](uint32_t index) const 62 { 63 return data_[index]; 64 } 65 66 void ReadNextRow(std::istream& str); 67 68 private: 69 std::vector<std::string> data_; 70 }; 71 72 class NetStatsCsv : public virtual RefBase { 73 public: 74 NetStatsCsv(); 75 ~NetStatsCsv(); 76 77 bool ExistsIface(const std::string& iface); 78 bool ExistsUid(const uint32_t uid); 79 bool CorrectedIfacesStats(const std::string &iface, uint32_t start, uint32_t end, const NetStatsInfo &stats); 80 bool UpdateIfaceCsvInfo(); 81 bool UpdateUidCsvInfo(); 82 bool UpdateIfaceStats(); 83 bool UpdateUidStats(); 84 bool UpdateIfaceStatsCsv(const std::string &iface); 85 bool UpdateUidStatsCsv(uint32_t uid, const std::string &iface); 86 bool DeleteUidStatsCsv(uint32_t uid); 87 NetStatsResultCode GetIfaceBytes(const std::string &iface, uint32_t start, uint32_t end, 88 NetStatsInfo &statsInfo); 89 NetStatsResultCode GetUidBytes(const std::string &iface, uint32_t uid, uint32_t start, 90 uint32_t end, NetStatsInfo &statsInfo); 91 NetStatsResultCode ResetFactory(); 92 93 private: 94 std::string GetCurrentTime(); 95 uint32_t GetIfaceCalculateTime(const std::string &iface, uint32_t time); 96 uint32_t GetUidCalculateTime(uint32_t uid, const std::string &iface, uint32_t time); 97 void GetCalculateStatsInfo(const CSVRow &row, uint32_t calStartTime, uint32_t calEndTime, 98 uint32_t timeCloumn, std::vector<NetStatsInfo> &vecRow); 99 void GetSumStats(const std::vector<NetStatsInfo> &vecRow, NetStatsInfo &sumStats); 100 void GetPeriodStats(const NetStatsInfo &startStats, const NetStatsInfo &endStats, NetStatsInfo &totalStats); 101 bool RenameStatsCsv(const std::string &fromFileName, const std::string &bakFile, const std::string &toFile); 102 void GenerateNewIfaceStats(uint32_t start, uint32_t end, const NetStatsInfo &stats, 103 std::map<uint32_t, NetStatsInfo> &newIfaceStats); 104 105 private: 106 std::mutex mutex_; 107 }; 108 } // namespace NetManagerStandard 109 } // namespace OHOS 110 #endif // NET_STATS_CSV_H 111