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     std::for_each(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](NetStatsInfo &info) { info.uid_ = Sim_UID; });
93     if (ret1 != NETMANAGER_SUCCESS || ret2 != NETMANAGER_SUCCESS) {
94         NETMGR_LOG_E("QueryData wrong, ret1=%{public}d, ret2=%{public}d", ret1, ret2);
95         return ret1 != NETMANAGER_SUCCESS ? ret1 : ret2;
96     }
97     infos.insert(infos.end(), uidSimTableInfos.begin(), uidSimTableInfos.end());
98     return NETMANAGER_SUCCESS;
99 }
100 
ReadStatsData(std::vector<NetStatsInfo> & infos,uint32_t uid,const std::string & ident,uint64_t start,uint64_t end)101 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, uint32_t uid, const std::string &ident,
102                                            uint64_t start, uint64_t end)
103 {
104     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
105     if (helper == nullptr) {
106         NETMGR_LOG_E("db helper instance is nullptr");
107         return NETMANAGER_ERR_INTERNAL;
108     }
109     int32_t ret1;
110     int32_t ret2;
111     std::vector<NetStatsInfo> uidSimTableInfos;
112     ret1 = helper->QueryData(UID_TABLE, uid, ident, start, end, infos);
113     if (uid == Sim_UID) {
114         ret2 = helper->QueryData(UID_SIM_TABLE, ident, start, end, uidSimTableInfos);
115         std::for_each(uidSimTableInfos.begin(), uidSimTableInfos.end(), [](NetStatsInfo &info) {
116             info.uid_ = Sim_UID;
117         });
118     } else {
119         ret2 = helper->QueryData(UID_SIM_TABLE, uid, ident, start, end, uidSimTableInfos);
120     }
121     if (ret1 != NETMANAGER_SUCCESS || ret2 != NETMANAGER_SUCCESS) {
122         NETMGR_LOG_E("QueryData wrong, ret1=%{public}d, ret2=%{public}d", ret1, ret2);
123         return ret1 != NETMANAGER_SUCCESS ? ret1 : ret2;
124     }
125     infos.insert(infos.end(), uidSimTableInfos.begin(), uidSimTableInfos.end());
126     return NETMANAGER_SUCCESS;
127 }
128 
WriteStatsData(const std::vector<NetStatsInfo> & infos,const std::string & tableName)129 int32_t NetStatsDataHandler::WriteStatsData(const std::vector<NetStatsInfo> &infos, const std::string &tableName)
130 {
131     NETMGR_LOG_I("WriteStatsData enter tableName:%{public}s", tableName.c_str());
132     if (infos.empty() || tableName.empty()) {
133         NETMGR_LOG_E("Param wrong, info: %{public}zu, tableName: %{public}zu", infos.size(), tableName.size());
134         return NETMANAGER_ERR_PARAMETER_ERROR;
135     }
136     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
137     if (helper == nullptr) {
138         NETMGR_LOG_E("db helper instance is nullptr");
139         return NETMANAGER_ERR_INTERNAL;
140     }
141     if (tableName == UID_TABLE) {
142         std::for_each(infos.begin(), infos.end(),
143                       [&helper](const auto &info) { helper->InsertData(UID_TABLE, UID_TABLE_PARAM_LIST, info); });
144         return NETMANAGER_SUCCESS;
145     }
146     if (tableName == IFACE_TABLE) {
147         std::for_each(infos.begin(), infos.end(),
148                       [&helper](const auto &info) { helper->InsertData(IFACE_TABLE, IFACE_TABLE_PARAM_LIST, info); });
149         return NETMANAGER_SUCCESS;
150     }
151     if (tableName == UID_SIM_TABLE) {
152         std::for_each(infos.begin(), infos.end(), [&helper](const auto &info) {
153             helper->InsertData(UID_SIM_TABLE, UID_SIM_TABLE_PARAM_LIST, info);
154         });
155         return NETMANAGER_SUCCESS;
156     }
157     return NETMANAGER_ERR_PARAMETER_ERROR;
158 }
159 
DeleteByUid(uint64_t uid)160 int32_t NetStatsDataHandler::DeleteByUid(uint64_t uid)
161 {
162     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
163     if (helper == nullptr) {
164         NETMGR_LOG_E("db helper instance is nullptr");
165         return NETMANAGER_ERR_INTERNAL;
166     }
167     return helper->DeleteData(UID_TABLE, uid);
168 }
169 
DeleteSimStatsByUid(uint64_t uid)170 int32_t NetStatsDataHandler::DeleteSimStatsByUid(uint64_t uid)
171 {
172     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
173     if (helper == nullptr) {
174         NETMGR_LOG_E("db helper instance is nullptr");
175         return NETMANAGER_ERR_INTERNAL;
176     }
177     return helper->DeleteData(UID_SIM_TABLE, uid);
178 }
179 
DeleteByDate(const std::string & tableName,uint64_t start,uint64_t end)180 int32_t NetStatsDataHandler::DeleteByDate(const std::string &tableName, uint64_t start, uint64_t end)
181 {
182     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
183     if (helper == nullptr) {
184         NETMGR_LOG_E("db helper instance is nullptr");
185         return NETMANAGER_ERR_INTERNAL;
186     }
187     return helper->DeleteData(tableName, start, end);
188 }
189 
UpdateStatsFlag(uint32_t uid,uint32_t flag)190 int32_t NetStatsDataHandler::UpdateStatsFlag(uint32_t uid, uint32_t flag)
191 {
192     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
193     if (helper == nullptr) {
194         NETMGR_LOG_E("db helper instance is nullptr");
195         return NETMANAGER_ERR_INTERNAL;
196     }
197     return helper->UpdateStatsFlag(UID_TABLE, uid, flag);
198 }
199 
UpdateSimStatsFlag(uint32_t uid,uint32_t flag)200 int32_t NetStatsDataHandler::UpdateSimStatsFlag(uint32_t uid, uint32_t flag)
201 {
202     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
203     if (helper == nullptr) {
204         NETMGR_LOG_E("db helper instance is nullptr");
205         return NETMANAGER_ERR_INTERNAL;
206     }
207     return helper->UpdateStatsFlag(UID_SIM_TABLE, uid, flag);
208 }
209 
ClearData()210 int32_t NetStatsDataHandler::ClearData()
211 {
212     auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
213     if (helper == nullptr) {
214         NETMGR_LOG_E("db helper instance is nullptr");
215         return NETMANAGER_ERR_INTERNAL;
216     }
217     int32_t ifaceDataRet = helper->ClearData(IFACE_TABLE);
218     int32_t uidDataRet = helper->ClearData(UID_TABLE);
219     int32_t uidSimDataRet = helper->ClearData(UID_SIM_TABLE);
220     if (ifaceDataRet != NETMANAGER_SUCCESS || uidDataRet != NETMANAGER_SUCCESS || uidSimDataRet != NETMANAGER_SUCCESS) {
221         return NETMANAGER_ERROR;
222     }
223     return NETMANAGER_SUCCESS;
224 }
225 } // namespace NetManagerStandard
226 } // namespace OHOS
227