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 return helper->SelectData(infos, UID_TABLE, start, end);
33 }
34
ReadStatsData(std::vector<NetStatsInfo> & infos,uint64_t uid,uint64_t start,uint64_t end)35 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, uint64_t uid, uint64_t start, uint64_t end)
36 {
37 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
38 return helper->SelectData(uid, start, end, infos);
39 }
40
ReadStatsData(std::vector<NetStatsInfo> & infos,const std::string & iface,uint64_t start,uint64_t end)41 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, const std::string &iface, uint64_t start,
42 uint64_t end)
43 {
44 if (iface.empty()) {
45 NETMGR_LOG_E("Param is invalid");
46 return NETMANAGER_ERR_PARAMETER_ERROR;
47 }
48 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
49 return helper->SelectData(iface, start, end, infos);
50 }
51
ReadStatsData(std::vector<NetStatsInfo> & infos,const std::string & iface,const uint32_t uid,uint64_t start,uint64_t end)52 int32_t NetStatsDataHandler::ReadStatsData(std::vector<NetStatsInfo> &infos, const std::string &iface,
53 const uint32_t uid, uint64_t start, uint64_t end)
54 {
55 if (iface.empty()) {
56 NETMGR_LOG_E("Param is invalid");
57 return NETMANAGER_ERR_PARAMETER_ERROR;
58 }
59 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
60 return helper->SelectData(iface, uid, start, end, infos);
61 }
62
WriteStatsData(const std::vector<NetStatsInfo> & infos,const std::string & tableName)63 int32_t NetStatsDataHandler::WriteStatsData(const std::vector<NetStatsInfo> &infos, const std::string &tableName)
64 {
65 NETMGR_LOG_I("WriteStatsData enter tableName:%{public}s", tableName.c_str());
66 if (infos.empty() || tableName.empty()) {
67 NETMGR_LOG_E("Param wrong, info: %{public}zu, tableName: %{public}zu", infos.size(), tableName.size());
68 return NETMANAGER_ERR_PARAMETER_ERROR;
69 }
70 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
71 if (tableName == UID_TABLE) {
72 std::for_each(infos.begin(), infos.end(),
73 [&helper](const auto &info) { helper->InsertData(UID_TABLE, UID_TABLE_PARAM_LIST, info); });
74 return NETMANAGER_SUCCESS;
75 }
76 if (tableName == IFACE_TABLE) {
77 std::for_each(infos.begin(), infos.end(),
78 [&helper](const auto &info) { helper->InsertData(IFACE_TABLE, IFACE_TABLE_PARAM_LIST, info); });
79 return NETMANAGER_SUCCESS;
80 }
81 return NETMANAGER_ERR_PARAMETER_ERROR;
82 }
83
DeleteByUid(uint64_t uid)84 int32_t NetStatsDataHandler::DeleteByUid(uint64_t uid)
85 {
86 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
87 return helper->DeleteData(UID_TABLE, uid);
88 }
89
DeleteByDate(const std::string & tableName,uint64_t start,uint64_t end)90 int32_t NetStatsDataHandler::DeleteByDate(const std::string &tableName, uint64_t start, uint64_t end)
91 {
92 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
93 return helper->DeleteData(tableName, start, end);
94 }
95
ClearData()96 int32_t NetStatsDataHandler::ClearData()
97 {
98 auto helper = std::make_unique<NetStatsDatabaseHelper>(NET_STATS_DATABASE_PATH);
99 int32_t ifaceDataRet = helper->ClearData(IFACE_TABLE);
100 int32_t uidDataRet = helper->ClearData(UID_TABLE);
101 if (ifaceDataRet != NETMANAGER_SUCCESS || uidDataRet != NETMANAGER_SUCCESS) {
102 return NETMANAGER_ERROR;
103 }
104 return NETMANAGER_SUCCESS;
105 }
106 } // namespace NetManagerStandard
107 } // namespace OHOS
108