• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "net_stats_data_handler.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_stats_database_defines.h"
20 #include "net_stats_database_helper.h"
21 #include "net_stats_constants.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_base_common_utils.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 using namespace NetStatsDatabaseDefines;
28 
ReadStatsData(std::vector<NetStatsInfo> & infos,uint64_t start,uint64_t end)29 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, uint64_t start, uint64_t end)
30 {
31     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
32     if (helper == nullptr) {
33         NETMGR_LOG_E("db helper instance is nullptr");
34         return NETMANAGER_ERR_INTERNAL;
35     }
36     return helper->SelectData(infos, UID_TABLE, start, end);
37 }
38 
ReadStatsData(std::vector<NetStatsInfo> & infos,uint64_t uid,uint64_t start,uint64_t end)39 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, uint64_t uid, uint64_t start, uint64_t end)
40 {
41     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
42     if (helper == nullptr) {
43         NETMGR_LOG_E("db helper instance is nullptr");
44         return NETMANAGER_ERR_INTERNAL;
45     }
46     return helper->SelectData(uid, start, end, infos);
47 }
48 
ReadStatsData(std::vector<NetStatsInfo> & infos,const std::string & iface,uint64_t start,uint64_t end)49 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, const std::string &iface, uint64_t start,
50                                            uint64_t end)
51 {
52     if (iface.empty()) {
53         NETMGR_LOG_E("Param is invalid");
54         return NETMANAGER_ERR_PARAMETER_ERROR;
55     }
56     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
57     if (helper == nullptr) {
58         NETMGR_LOG_E("db helper instance is nullptr");
59         return NETMANAGER_ERR_INTERNAL;
60     }
61     return helper->SelectData(iface, start, end, infos);
62 }
63 
ReadStatsData(std::vector<NetStatsInfo> & infos,const std::string & iface,const uint32_t uid,uint64_t start,uint64_t end)64 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, const std::string &iface,
65                                            const uint32_t uid, uint64_t start, uint64_t end)
66 {
67     if (iface.empty()) {
68         NETMGR_LOG_E("Param is invalid");
69         return NETMANAGER_ERR_PARAMETER_ERROR;
70     }
71     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
72     if (helper == nullptr) {
73         NETMGR_LOG_E("db helper instance is nullptr");
74         return NETMANAGER_ERR_INTERNAL;
75     }
76     return helper->SelectData(iface, uid, start, end, infos);
77 }
78 
ReadStatsDataByIdent(std::vector<NetStatsInfo> & infos,const std::string & ident,uint64_t start,uint64_t end)79 int32_t NetStatsDataHandler::ReadStatsDataByIdent(std::vector<NetStatsInfo> &infos, const std::string &ident,
80                                                   uint64_t start, uint64_t end)
81 {
82     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
83     if (helper == nullptr) {
84         NETMGR_LOG_E("db helper instance is nullptr");
85         return NETMANAGER_ERR_INTERNAL;
86     }
87     int32_t ret1;
88     int32_t ret2;
89     std::vector<NetStatsInfo> uidSimTableInfos;
90     ret1 = helper->QueryData(UID_TABLE, ident, start, end, infos);
91     ret2 = helper->QueryData(UID_SIM_TABLE, ident, start, end, uidSimTableInfos);
92     uidSimTableInfos.erase(std::remove_if(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](const auto &item) {
93                                return item.flag_ <= STATS_DATA_FLAG_DEFAULT || item.flag_ >= STATS_DATA_FLAG_LIMIT;
94                            }),
95                            uidSimTableInfos.end());
96     std::for_each(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](NetStatsInfo &info) {
97         if (info.flag_ == STATS_DATA_FLAG_SIM_BASIC) {
98             info.uid_ = Sim_UID;
99         } else if (info.flag_ == STATS_DATA_FLAG_SIM2_BASIC) {
100             info.uid_ = SIM2_UID;
101         }
102     });
103     if (ret1 != NETMANAGER_SUCCESS || ret2 != NETMANAGER_SUCCESS) {
104         NETMGR_LOG_E("QueryData wrong, ret1=%{public}d, ret2=%{public}d", ret1, ret2);
105         return ret1 != NETMANAGER_SUCCESS ? ret1 : ret2;
106     }
107     infos.insert(infos.end(), uidSimTableInfos.begin(), uidSimTableInfos.end());
108     return NETMANAGER_SUCCESS;
109 }
110 
ReadStatsData(std::vector<NetStatsInfo> & infos,uint32_t uid,const std::string & ident,uint64_t start,uint64_t end)111 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, uint32_t uid, const std::string &ident,
112                                            uint64_t start, uint64_t end)
113 {
114     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
115     if (helper == nullptr) {
116         NETMGR_LOG_E("db helper instance is nullptr");
117         return NETMANAGER_ERR_INTERNAL;
118     }
119     int32_t ret1;
120     int32_t ret2;
121     std::vector<NetStatsInfo> uidSimTableInfos;
122     ret1 = helper->QueryData(UID_TABLE, uid, ident, start, end, infos);
123     if (uid == Sim_UID || uid == SIM2_UID) {
124         ret2 = helper->QueryData(UID_SIM_TABLE, ident, start, end, uidSimTableInfos);
125         uidSimTableInfos.erase(std::remove_if(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](const auto &item) {
126                                    return item.flag_ <= STATS_DATA_FLAG_DEFAULT || item.flag_ >= STATS_DATA_FLAG_LIMIT;
127                                }),
128                                uidSimTableInfos.end());
129         std::for_each(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](NetStatsInfo &info) {
130             if (info.flag_ == STATS_DATA_FLAG_SIM_BASIC) {
131                 info.uid_ = Sim_UID;
132             } else if (info.flag_ == STATS_DATA_FLAG_SIM2_BASIC) {
133                 info.uid_ = SIM2_UID;
134             }
135         });
136     } else {
137         ret2 = helper->QueryData(UID_SIM_TABLE, uid, ident, start, end, uidSimTableInfos);
138     }
139     if (ret1 != NETMANAGER_SUCCESS || ret2 != NETMANAGER_SUCCESS) {
140         NETMGR_LOG_E("QueryData wrong, ret1=%{public}d, ret2=%{public}d", ret1, ret2);
141         return ret1 != NETMANAGER_SUCCESS ? ret1 : ret2;
142     }
143     infos.insert(infos.end(), uidSimTableInfos.begin(), uidSimTableInfos.end());
144     return NETMANAGER_SUCCESS;
145 }
146 
WriteStatsData(const std::vector<NetStatsInfo> & infos,const std::string & tableName)147 int32_t NetStatsDataHandler::WriteStatsData(const std::vector<NetStatsInfo> &infos, const std::string &tableName)
148 {
149     NETMGR_LOG_I("WriteStatsData enter tableName:%{public}s", tableName.c_str());
150     if (infos.empty() || tableName.empty()) {
151         NETMGR_LOG_E("Param wrong, info: %{public}zu, tableName: %{public}zu", infos.size(), tableName.size());
152         return NETMANAGER_ERR_PARAMETER_ERROR;
153     }
154     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
155     if (helper == nullptr) {
156         NETMGR_LOG_E("db helper instance is nullptr");
157         return NETMANAGER_ERR_INTERNAL;
158     }
159     if (tableName == UID_TABLE) {
160         std::for_each(infos.begin(), infos.end(),
161                       [&helper](const auto &info) { helper->InsertData(UID_TABLE, UID_TABLE_PARAM_LIST, info); });
162         return NETMANAGER_SUCCESS;
163     }
164     if (tableName == IFACE_TABLE) {
165         std::for_each(infos.begin(), infos.end(),
166                       [&helper](const auto &info) { helper->InsertData(IFACE_TABLE, IFACE_TABLE_PARAM_LIST, info); });
167         return NETMANAGER_SUCCESS;
168     }
169     if (tableName == UID_SIM_TABLE) {
170         std::for_each(infos.begin(), infos.end(), [&helper](const auto &info) {
171             helper->InsertData(UID_SIM_TABLE, UID_SIM_TABLE_PARAM_LIST, info);
172         });
173         return NETMANAGER_SUCCESS;
174     }
175     return NETMANAGER_ERR_PARAMETER_ERROR;
176 }
177 
DeleteByUid(uint64_t uid)178 int32_t NetStatsDataHandler::DeleteByUid(uint64_t uid)
179 {
180     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
181     if (helper == nullptr) {
182         NETMGR_LOG_E("db helper instance is nullptr");
183         return NETMANAGER_ERR_INTERNAL;
184     }
185     return helper->DeleteData(UID_TABLE, uid);
186 }
187 
DeleteSimStatsByUid(uint64_t uid)188 int32_t NetStatsDataHandler::DeleteSimStatsByUid(uint64_t uid)
189 {
190     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
191     if (helper == nullptr) {
192         NETMGR_LOG_E("db helper instance is nullptr");
193         return NETMANAGER_ERR_INTERNAL;
194     }
195     return helper->DeleteData(UID_SIM_TABLE, uid);
196 }
197 
DeleteByDate(const std::string & tableName,uint64_t start,uint64_t end)198 int32_t NetStatsDataHandler::DeleteByDate(const std::string &tableName, uint64_t start, uint64_t end)
199 {
200     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
201     if (helper == nullptr) {
202         NETMGR_LOG_E("db helper instance is nullptr");
203         return NETMANAGER_ERR_INTERNAL;
204     }
205     return helper->DeleteData(tableName, start, end);
206 }
207 
UpdateStatsFlag(uint32_t uid,uint32_t flag)208 int32_t NetStatsDataHandler::UpdateStatsFlag(uint32_t uid, uint32_t flag)
209 {
210     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
211     if (helper == nullptr) {
212         NETMGR_LOG_E("db helper instance is nullptr");
213         return NETMANAGER_ERR_INTERNAL;
214     }
215     return helper->UpdateStatsFlag(UID_TABLE, uid, flag);
216 }
217 
UpdateSimStatsFlag(uint32_t uid,uint32_t flag)218 int32_t NetStatsDataHandler::UpdateSimStatsFlag(uint32_t uid, uint32_t flag)
219 {
220     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
221     if (helper == nullptr) {
222         NETMGR_LOG_E("db helper instance is nullptr");
223         return NETMANAGER_ERR_INTERNAL;
224     }
225     return helper->UpdateStatsFlag(UID_SIM_TABLE, uid, flag);
226 }
227 
UpdateSimDataFlag(uint32_t oldFlag,uint32_t newFlag)228 int32_t NetStatsDataHandler::UpdateSimDataFlag(uint32_t oldFlag, uint32_t newFlag)
229 {
230     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
231     if (helper == nullptr) {
232         NETMGR_LOG_E("db helper instance is nullptr");
233         return NETMANAGER_ERR_INTERNAL;
234     }
235     return helper->UpdateDataFlag(UID_SIM_TABLE, oldFlag, newFlag);
236 }
237 
ClearData()238 int32_t NetStatsDataHandler::ClearData()
239 {
240     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
241     if (helper == nullptr) {
242         NETMGR_LOG_E("db helper instance is nullptr");
243         return NETMANAGER_ERR_INTERNAL;
244     }
245     int32_t ifaceDataRet = helper->ClearData(IFACE_TABLE);
246     int32_t uidDataRet = helper->ClearData(UID_TABLE);
247     int32_t uidSimDataRet = helper->ClearData(UID_SIM_TABLE);
248     if (ifaceDataRet != NETMANAGER_SUCCESS || uidDataRet != NETMANAGER_SUCCESS || uidSimDataRet != NETMANAGER_SUCCESS) {
249         return NETMANAGER_ERROR;
250     }
251     return NETMANAGER_SUCCESS;
252 }
253 } // namespace NetManagerStandard
254 } // namespace OHOS
255