1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#include "net_stats_service.h"
17b1b8bc3fSopenharmony_ci
18b1b8bc3fSopenharmony_ci#include <net/if.h>
19b1b8bc3fSopenharmony_ci#include <sys/time.h>
20b1b8bc3fSopenharmony_ci#include <unistd.h>
21b1b8bc3fSopenharmony_ci
22b1b8bc3fSopenharmony_ci#include <cinttypes>
23b1b8bc3fSopenharmony_ci#include <initializer_list>
24b1b8bc3fSopenharmony_ci
25b1b8bc3fSopenharmony_ci#include "bpf_stats.h"
26b1b8bc3fSopenharmony_ci#include "broadcast_manager.h"
27b1b8bc3fSopenharmony_ci#include "common_event_manager.h"
28b1b8bc3fSopenharmony_ci#include "common_event_support.h"
29b1b8bc3fSopenharmony_ci#include "net_manager_center.h"
30b1b8bc3fSopenharmony_ci#include "net_manager_constants.h"
31b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h"
32b1b8bc3fSopenharmony_ci#include "net_stats_constants.h"
33b1b8bc3fSopenharmony_ci#include "net_stats_database_defines.h"
34b1b8bc3fSopenharmony_ci#include "net_stats_service_common.h"
35b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h"
36b1b8bc3fSopenharmony_ci#include "netmanager_base_permission.h"
37b1b8bc3fSopenharmony_ci#include "netmanager_hitrace.h"
38b1b8bc3fSopenharmony_ci#include "netsys_controller.h"
39b1b8bc3fSopenharmony_ci#include "system_ability_definition.h"
40b1b8bc3fSopenharmony_ci
41b1b8bc3fSopenharmony_cinamespace OHOS {
42b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
43b1b8bc3fSopenharmony_ciusing namespace NetStatsDatabaseDefines;
44b1b8bc3fSopenharmony_cinamespace {
45b1b8bc3fSopenharmony_ciconstexpr std::initializer_list<NetBearType> BEAR_TYPE_LIST = {
46b1b8bc3fSopenharmony_ci    NetBearType::BEARER_CELLULAR, NetBearType::BEARER_WIFI, NetBearType::BEARER_BLUETOOTH,
47b1b8bc3fSopenharmony_ci    NetBearType::BEARER_ETHERNET, NetBearType::BEARER_VPN,  NetBearType::BEARER_WIFI_AWARE,
48b1b8bc3fSopenharmony_ci};
49b1b8bc3fSopenharmony_ciconstexpr uint32_t DAY_SECONDS = 2 * 24 * 60 * 60;
50b1b8bc3fSopenharmony_ciconstexpr const char* UID = "uid";
51b1b8bc3fSopenharmony_ci} // namespace
52b1b8bc3fSopenharmony_ciconst bool REGISTER_LOCAL_RESULT =
53b1b8bc3fSopenharmony_ci    SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetStatsService>::GetInstance().get());
54b1b8bc3fSopenharmony_ci
55b1b8bc3fSopenharmony_ciNetStatsService::NetStatsService()
56b1b8bc3fSopenharmony_ci    : SystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID, true), registerToService_(false), state_(STATE_STOPPED)
57b1b8bc3fSopenharmony_ci{
58b1b8bc3fSopenharmony_ci    netStatsCallback_ = std::make_shared<NetStatsCallback>();
59b1b8bc3fSopenharmony_ci    netStatsCached_ = std::make_unique<NetStatsCached>();
60b1b8bc3fSopenharmony_ci}
61b1b8bc3fSopenharmony_ci
62b1b8bc3fSopenharmony_ciNetStatsService::~NetStatsService() = default;
63b1b8bc3fSopenharmony_ci
64b1b8bc3fSopenharmony_civoid NetStatsService::OnStart()
65b1b8bc3fSopenharmony_ci{
66b1b8bc3fSopenharmony_ci    if (state_ == STATE_RUNNING) {
67b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("the state is already running");
68b1b8bc3fSopenharmony_ci        return;
69b1b8bc3fSopenharmony_ci    }
70b1b8bc3fSopenharmony_ci    if (!Init()) {
71b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("init failed");
72b1b8bc3fSopenharmony_ci        return;
73b1b8bc3fSopenharmony_ci    }
74b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
75b1b8bc3fSopenharmony_ci    state_ = STATE_RUNNING;
76b1b8bc3fSopenharmony_ci    sptr<NetStatsBaseService> baseService = new (std::nothrow) NetStatsServiceCommon();
77b1b8bc3fSopenharmony_ci    if (baseService == nullptr) {
78b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Net stats base service instance create failed");
79b1b8bc3fSopenharmony_ci        return;
80b1b8bc3fSopenharmony_ci    }
81b1b8bc3fSopenharmony_ci    NetManagerCenter::GetInstance().RegisterStatsService(baseService);
82b1b8bc3fSopenharmony_ci}
83b1b8bc3fSopenharmony_ci
84b1b8bc3fSopenharmony_civoid NetStatsService::OnStop()
85b1b8bc3fSopenharmony_ci{
86b1b8bc3fSopenharmony_ci    state_ = STATE_STOPPED;
87b1b8bc3fSopenharmony_ci    registerToService_ = true;
88b1b8bc3fSopenharmony_ci}
89b1b8bc3fSopenharmony_ci
90b1b8bc3fSopenharmony_ciint32_t NetStatsService::Dump(int32_t fd, const std::vector<std::u16string> &args)
91b1b8bc3fSopenharmony_ci{
92b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Start Dump, fd: %{public}d", fd);
93b1b8bc3fSopenharmony_ci    std::string result;
94b1b8bc3fSopenharmony_ci    GetDumpMessage(result);
95b1b8bc3fSopenharmony_ci    int32_t ret = dprintf(fd, "%s\n", result.c_str());
96b1b8bc3fSopenharmony_ci    return ret < 0 ? STATS_DUMP_MESSAGE_FAIL : NETMANAGER_SUCCESS;
97b1b8bc3fSopenharmony_ci}
98b1b8bc3fSopenharmony_ci
99b1b8bc3fSopenharmony_civoid NetStatsService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
100b1b8bc3fSopenharmony_ci{
101b1b8bc3fSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
102b1b8bc3fSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
103b1b8bc3fSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SHUTDOWN);
104b1b8bc3fSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
105b1b8bc3fSopenharmony_ci    subscribeInfo.SetPriority(1);
106b1b8bc3fSopenharmony_ci    subscriber_ = std::make_shared<NetStatsListener>(subscribeInfo);
107b1b8bc3fSopenharmony_ci    subscriber_->RegisterStatsCallback(EventFwk::CommonEventSupport::COMMON_EVENT_SHUTDOWN,
108b1b8bc3fSopenharmony_ci                                       [this](const EventFwk::Want &want) { return UpdateStatsData(); });
109b1b8bc3fSopenharmony_ci    subscriber_->RegisterStatsCallback(
110b1b8bc3fSopenharmony_ci        EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED,
111b1b8bc3fSopenharmony_ci        [this](const EventFwk::Want &want) {
112b1b8bc3fSopenharmony_ci            uint32_t uid = want.GetIntParam(UID, 0);
113b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("Net Manager delete uid, uid:[%{public}d]", uid);
114b1b8bc3fSopenharmony_ci            auto handler = std::make_unique<NetStatsDataHandler>();
115b1b8bc3fSopenharmony_ci            if (handler == nullptr) {
116b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("Net Manager package removed, get db handler failed. uid:[%{public}d]", uid);
117b1b8bc3fSopenharmony_ci                return static_cast<int32_t>(NETMANAGER_ERR_INTERNAL);
118b1b8bc3fSopenharmony_ci            }
119b1b8bc3fSopenharmony_ci            auto ret1 = handler->UpdateStatsFlag(uid, STATS_DATA_FLAG_UNINSTALLED);
120b1b8bc3fSopenharmony_ci            if (ret1 != NETMANAGER_SUCCESS) {
121b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("Net Manager update stats flag failed, uid:[%{public}d]", uid);
122b1b8bc3fSopenharmony_ci            }
123b1b8bc3fSopenharmony_ci            auto ret2 = handler->UpdateSimStatsFlag(uid, STATS_DATA_FLAG_UNINSTALLED);
124b1b8bc3fSopenharmony_ci            if (ret2 != NETMANAGER_SUCCESS) {
125b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("Net Manager update sim stats flag failed, uid:[%{public}d]", uid);
126b1b8bc3fSopenharmony_ci            }
127b1b8bc3fSopenharmony_ci            netStatsCached_->ForceArchiveStats(uid);
128b1b8bc3fSopenharmony_ci            return ret1 != NETMANAGER_SUCCESS ? ret1 : ret2;
129b1b8bc3fSopenharmony_ci        });
130b1b8bc3fSopenharmony_ci    EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
131b1b8bc3fSopenharmony_ci}
132b1b8bc3fSopenharmony_ci
133b1b8bc3fSopenharmony_civoid NetStatsService::GetDumpMessage(std::string &message)
134b1b8bc3fSopenharmony_ci{
135b1b8bc3fSopenharmony_ci    message.append("Net Stats Info:\n");
136b1b8bc3fSopenharmony_ci    uint64_t rxBytes = 0;
137b1b8bc3fSopenharmony_ci    uint64_t txBytes = 0;
138b1b8bc3fSopenharmony_ci    uint64_t rxPackets = 0;
139b1b8bc3fSopenharmony_ci    uint64_t txPackets = 0;
140b1b8bc3fSopenharmony_ci    NetsysController::GetInstance().GetTotalStats(rxBytes, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES));
141b1b8bc3fSopenharmony_ci    NetsysController::GetInstance().GetTotalStats(txBytes, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES));
142b1b8bc3fSopenharmony_ci    NetsysController::GetInstance().GetTotalStats(rxPackets, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_PACKETS));
143b1b8bc3fSopenharmony_ci    NetsysController::GetInstance().GetTotalStats(txPackets, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_PACKETS));
144b1b8bc3fSopenharmony_ci
145b1b8bc3fSopenharmony_ci    message.append("\tRxBytes: " + std::to_string(rxBytes) + "\n");
146b1b8bc3fSopenharmony_ci    message.append("\tTxBytes: " + std::to_string(txBytes) + "\n");
147b1b8bc3fSopenharmony_ci    message.append("\tRxPackets: " + std::to_string(rxPackets) + "\n");
148b1b8bc3fSopenharmony_ci    message.append("\tTxPackets: " + std::to_string(txPackets) + "\n");
149b1b8bc3fSopenharmony_ci    std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [&message, this](const auto &bearType) {
150b1b8bc3fSopenharmony_ci        std::list<std::string> ifaceNames;
151b1b8bc3fSopenharmony_ci        if (NetManagerCenter::GetInstance().GetIfaceNames(bearType, ifaceNames)) {
152b1b8bc3fSopenharmony_ci            return;
153b1b8bc3fSopenharmony_ci        }
154b1b8bc3fSopenharmony_ci        uint64_t rx = 0;
155b1b8bc3fSopenharmony_ci        uint64_t tx = 0;
156b1b8bc3fSopenharmony_ci        for (const auto &name : ifaceNames) {
157b1b8bc3fSopenharmony_ci            GetIfaceRxBytes(rx, name);
158b1b8bc3fSopenharmony_ci            GetIfaceTxBytes(tx, name);
159b1b8bc3fSopenharmony_ci            message.append("\t" + name + "-TxBytes: " + std::to_string(tx));
160b1b8bc3fSopenharmony_ci            message.append("\t" + name + "-RxBytes: " + std::to_string(rx));
161b1b8bc3fSopenharmony_ci        }
162b1b8bc3fSopenharmony_ci    });
163b1b8bc3fSopenharmony_ci}
164b1b8bc3fSopenharmony_ci
165b1b8bc3fSopenharmony_cibool NetStatsService::Init()
166b1b8bc3fSopenharmony_ci{
167b1b8bc3fSopenharmony_ci    if (!REGISTER_LOCAL_RESULT) {
168b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Register to local sa manager failed");
169b1b8bc3fSopenharmony_ci        registerToService_ = false;
170b1b8bc3fSopenharmony_ci        return false;
171b1b8bc3fSopenharmony_ci    }
172b1b8bc3fSopenharmony_ci    if (!registerToService_) {
173b1b8bc3fSopenharmony_ci        if (!Publish(DelayedSingleton<NetStatsService>::GetInstance().get())) {
174b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Register to sa manager failed");
175b1b8bc3fSopenharmony_ci            return false;
176b1b8bc3fSopenharmony_ci        }
177b1b8bc3fSopenharmony_ci        registerToService_ = true;
178b1b8bc3fSopenharmony_ci    }
179b1b8bc3fSopenharmony_ci    if (nullptr == netStatsCached_) {
180b1b8bc3fSopenharmony_ci        return false;
181b1b8bc3fSopenharmony_ci    }
182b1b8bc3fSopenharmony_ci    netStatsCached_->SetCallbackManager(netStatsCallback_);
183b1b8bc3fSopenharmony_ci    auto ret = netStatsCached_->StartCached();
184b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
185b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Start cached failed");
186b1b8bc3fSopenharmony_ci        return false;
187b1b8bc3fSopenharmony_ci    }
188b1b8bc3fSopenharmony_ci
189b1b8bc3fSopenharmony_ci    return true;
190b1b8bc3fSopenharmony_ci}
191b1b8bc3fSopenharmony_ci
192b1b8bc3fSopenharmony_ciint32_t NetStatsService::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
193b1b8bc3fSopenharmony_ci{
194b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter RegisterNetStatsCallback");
195b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
196b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("RegisterNetStatsCallback parameter callback is null");
197b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_PARAMETER_ERROR;
198b1b8bc3fSopenharmony_ci    }
199b1b8bc3fSopenharmony_ci    netStatsCallback_->RegisterNetStatsCallback(callback);
200b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
201b1b8bc3fSopenharmony_ci}
202b1b8bc3fSopenharmony_ci
203b1b8bc3fSopenharmony_ciint32_t NetStatsService::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
204b1b8bc3fSopenharmony_ci{
205b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter UnregisterNetStatsCallback");
206b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
207b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("UnregisterNetStatsCallback parameter callback is null");
208b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_PARAMETER_ERROR;
209b1b8bc3fSopenharmony_ci    }
210b1b8bc3fSopenharmony_ci    netStatsCallback_->UnregisterNetStatsCallback(callback);
211b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
212b1b8bc3fSopenharmony_ci}
213b1b8bc3fSopenharmony_ci
214b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName)
215b1b8bc3fSopenharmony_ci{
216b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetIfaceStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES),
217b1b8bc3fSopenharmony_ci                                                         interfaceName);
218b1b8bc3fSopenharmony_ci}
219b1b8bc3fSopenharmony_ci
220b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName)
221b1b8bc3fSopenharmony_ci{
222b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetIfaceStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES),
223b1b8bc3fSopenharmony_ci                                                         interfaceName);
224b1b8bc3fSopenharmony_ci}
225b1b8bc3fSopenharmony_ci
226b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetCellularRxBytes(uint64_t &stats)
227b1b8bc3fSopenharmony_ci{
228b1b8bc3fSopenharmony_ci    std::list<std::string> ifaceNames;
229b1b8bc3fSopenharmony_ci    if (!GetIfaceNamesFromManager(ifaceNames)) {
230b1b8bc3fSopenharmony_ci        return STATS_ERR_GET_IFACE_NAME_FAILED;
231b1b8bc3fSopenharmony_ci    }
232b1b8bc3fSopenharmony_ci
233b1b8bc3fSopenharmony_ci    for (const auto &name : ifaceNames) {
234b1b8bc3fSopenharmony_ci        uint64_t totalCellular = 0;
235b1b8bc3fSopenharmony_ci        auto ret = NetsysController::GetInstance().GetIfaceStats(
236b1b8bc3fSopenharmony_ci            totalCellular, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES), name);
237b1b8bc3fSopenharmony_ci        if (ret != NETMANAGER_SUCCESS) {
238b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Get iface stats failed result: %{public}d", ret);
239b1b8bc3fSopenharmony_ci            return ret;
240b1b8bc3fSopenharmony_ci        }
241b1b8bc3fSopenharmony_ci        stats += totalCellular;
242b1b8bc3fSopenharmony_ci    }
243b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
244b1b8bc3fSopenharmony_ci}
245b1b8bc3fSopenharmony_ci
246b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetCellularTxBytes(uint64_t &stats)
247b1b8bc3fSopenharmony_ci{
248b1b8bc3fSopenharmony_ci    std::list<std::string> ifaceNames;
249b1b8bc3fSopenharmony_ci    if (!GetIfaceNamesFromManager(ifaceNames)) {
250b1b8bc3fSopenharmony_ci        return STATS_ERR_GET_IFACE_NAME_FAILED;
251b1b8bc3fSopenharmony_ci    }
252b1b8bc3fSopenharmony_ci
253b1b8bc3fSopenharmony_ci    uint64_t totalCellular = 0;
254b1b8bc3fSopenharmony_ci    for (const auto &name : ifaceNames) {
255b1b8bc3fSopenharmony_ci        auto ret = NetsysController::GetInstance().GetIfaceStats(
256b1b8bc3fSopenharmony_ci            totalCellular, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES), name);
257b1b8bc3fSopenharmony_ci        if (ret != NETMANAGER_SUCCESS) {
258b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Get iface stats failed result: %{public}d", ret);
259b1b8bc3fSopenharmony_ci            return ret;
260b1b8bc3fSopenharmony_ci        }
261b1b8bc3fSopenharmony_ci        stats += totalCellular;
262b1b8bc3fSopenharmony_ci    }
263b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
264b1b8bc3fSopenharmony_ci}
265b1b8bc3fSopenharmony_ci
266b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetAllRxBytes(uint64_t &stats)
267b1b8bc3fSopenharmony_ci{
268b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetAllRxBytes");
269b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetTotalStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES));
270b1b8bc3fSopenharmony_ci}
271b1b8bc3fSopenharmony_ci
272b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetAllTxBytes(uint64_t &stats)
273b1b8bc3fSopenharmony_ci{
274b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetAllTxBytes");
275b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetTotalStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES));
276b1b8bc3fSopenharmony_ci}
277b1b8bc3fSopenharmony_ci
278b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetUidRxBytes(uint64_t &stats, uint32_t uid)
279b1b8bc3fSopenharmony_ci{
280b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetUidRxBytes, uid is %{public}d", uid);
281b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetUidStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES),
282b1b8bc3fSopenharmony_ci                                                       uid);
283b1b8bc3fSopenharmony_ci}
284b1b8bc3fSopenharmony_ci
285b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetUidTxBytes(uint64_t &stats, uint32_t uid)
286b1b8bc3fSopenharmony_ci{
287b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetUidTxBytes,uid is %{public}d", uid);
288b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetUidStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES),
289b1b8bc3fSopenharmony_ci                                                       uid);
290b1b8bc3fSopenharmony_ci}
291b1b8bc3fSopenharmony_ci
292b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
293b1b8bc3fSopenharmony_ci                                             NetStatsInfo &statsInfo)
294b1b8bc3fSopenharmony_ci{
295b1b8bc3fSopenharmony_ci    // Start of get traffic data by interface name.
296b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetIfaceStatsDetail, iface= %{public}s", iface.c_str());
297b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetIfaceStatsDetail start");
298b1b8bc3fSopenharmony_ci    if (start > end) {
299b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("start is after end.");
300b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
301b1b8bc3fSopenharmony_ci    }
302b1b8bc3fSopenharmony_ci    std::vector<NetStatsInfo> allInfo;
303b1b8bc3fSopenharmony_ci    auto history = std::make_unique<NetStatsHistory>();
304b1b8bc3fSopenharmony_ci    int32_t ret = history->GetHistory(allInfo, iface, start, end);
305b1b8bc3fSopenharmony_ci
306b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
307b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netStatsCached_ is fail");
308b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
309b1b8bc3fSopenharmony_ci    }
310b1b8bc3fSopenharmony_ci    netStatsCached_->GetIfaceStatsCached(allInfo);
311b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
312b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Get traffic stats data failed");
313b1b8bc3fSopenharmony_ci        return ret;
314b1b8bc3fSopenharmony_ci    }
315b1b8bc3fSopenharmony_ci    std::for_each(allInfo.begin(), allInfo.end(), [&statsInfo, &iface, &start, &end](const auto &info) {
316b1b8bc3fSopenharmony_ci        if (info.iface_ == iface && info.date_ >= start && info.date_ <= end) {
317b1b8bc3fSopenharmony_ci            statsInfo += info;
318b1b8bc3fSopenharmony_ci        }
319b1b8bc3fSopenharmony_ci    });
320b1b8bc3fSopenharmony_ci    statsInfo.iface_ = iface;
321b1b8bc3fSopenharmony_ci    statsInfo.date_ = end;
322b1b8bc3fSopenharmony_ci    // End of get traffic data by interface name.
323b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetIfaceStatsDetail end");
324b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
325b1b8bc3fSopenharmony_ci}
326b1b8bc3fSopenharmony_ci
327b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
328b1b8bc3fSopenharmony_ci                                           NetStatsInfo &statsInfo)
329b1b8bc3fSopenharmony_ci{
330b1b8bc3fSopenharmony_ci    // Start of get traffic data by usr id.
331b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetIfaceStatsDetail, iface= %{public}s uid= %{public}d", iface.c_str(), uid);
332b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetUidStatsDetail start");
333b1b8bc3fSopenharmony_ci    if (start > end) {
334b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("start is after end.");
335b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
336b1b8bc3fSopenharmony_ci    }
337b1b8bc3fSopenharmony_ci    std::vector<NetStatsInfo> allInfo;
338b1b8bc3fSopenharmony_ci    auto history = std::make_unique<NetStatsHistory>();
339b1b8bc3fSopenharmony_ci    int32_t ret = history->GetHistory(allInfo, iface, uid, start, end);
340b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
341b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netStatsCached_ is fail");
342b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
343b1b8bc3fSopenharmony_ci    }
344b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidStatsCached(allInfo);
345b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
346b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Get traffic stats data failed");
347b1b8bc3fSopenharmony_ci        return ret;
348b1b8bc3fSopenharmony_ci    }
349b1b8bc3fSopenharmony_ci    std::for_each(allInfo.begin(), allInfo.end(), [&statsInfo, &iface, &uid, &start, &end](const auto &info) {
350b1b8bc3fSopenharmony_ci        if (info.iface_ == iface && info.uid_ == uid && info.date_ >= start && info.date_ <= end) {
351b1b8bc3fSopenharmony_ci            statsInfo += info;
352b1b8bc3fSopenharmony_ci        }
353b1b8bc3fSopenharmony_ci    });
354b1b8bc3fSopenharmony_ci    statsInfo.uid_ = uid;
355b1b8bc3fSopenharmony_ci    statsInfo.iface_ = iface;
356b1b8bc3fSopenharmony_ci    statsInfo.date_ = end;
357b1b8bc3fSopenharmony_ci    // End of get traffic data by usr id.
358b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerFinishSyncTrace("NetStatsService GetUidStatsDetail end");
359b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
360b1b8bc3fSopenharmony_ci}
361b1b8bc3fSopenharmony_ci
362b1b8bc3fSopenharmony_ciint32_t NetStatsService::UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
363b1b8bc3fSopenharmony_ci                                           const NetStatsInfo &stats)
364b1b8bc3fSopenharmony_ci{
365b1b8bc3fSopenharmony_ci    // Start of update traffic data by date.
366b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateIfacesStats ifaces is %{public}s", iface.c_str());
367b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService UpdateIfacesStats start");
368b1b8bc3fSopenharmony_ci    if (start > end) {
369b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("start is after end.");
370b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
371b1b8bc3fSopenharmony_ci    }
372b1b8bc3fSopenharmony_ci    std::vector<NetStatsInfo> infos;
373b1b8bc3fSopenharmony_ci    infos.push_back(stats);
374b1b8bc3fSopenharmony_ci    auto handler = std::make_unique<NetStatsDataHandler>();
375b1b8bc3fSopenharmony_ci    auto ret = handler->DeleteByDate(IFACE_TABLE, start, end);
376b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
377b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Update ifaces stats failed");
378b1b8bc3fSopenharmony_ci    }
379b1b8bc3fSopenharmony_ci    ret = handler->WriteStatsData(infos, IFACE_TABLE);
380b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
381b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Update ifaces stats failed");
382b1b8bc3fSopenharmony_ci        return STATS_ERR_WRITE_DATA_FAIL;
383b1b8bc3fSopenharmony_ci    }
384b1b8bc3fSopenharmony_ci    // End of update traffic data by date.
385b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerFinishSyncTrace("NetStatsService UpdateIfacesStats end");
386b1b8bc3fSopenharmony_ci    return ret;
387b1b8bc3fSopenharmony_ci}
388b1b8bc3fSopenharmony_ci
389b1b8bc3fSopenharmony_ciint32_t NetStatsService::UpdateStatsData()
390b1b8bc3fSopenharmony_ci{
391b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter UpdateStatsData.");
392b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
393b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Cached is nullptr");
394b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
395b1b8bc3fSopenharmony_ci    }
396b1b8bc3fSopenharmony_ci    netStatsCached_->ForceUpdateStats();
397b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("End UpdateStatsData.");
398b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
399b1b8bc3fSopenharmony_ci}
400b1b8bc3fSopenharmony_ci
401b1b8bc3fSopenharmony_ciint32_t NetStatsService::ResetFactory()
402b1b8bc3fSopenharmony_ci{
403b1b8bc3fSopenharmony_ci    auto handler = std::make_unique<NetStatsDataHandler>();
404b1b8bc3fSopenharmony_ci    return handler->ClearData();
405b1b8bc3fSopenharmony_ci}
406b1b8bc3fSopenharmony_ci
407b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetAllStatsInfo(std::vector<NetStatsInfo> &infos)
408b1b8bc3fSopenharmony_ci{
409b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetAllStatsInfo.");
410b1b8bc3fSopenharmony_ci    if (netStatsCached_ != nullptr) {
411b1b8bc3fSopenharmony_ci        netStatsCached_->GetUidPushStatsCached(infos);
412b1b8bc3fSopenharmony_ci        netStatsCached_->GetAllPushStatsCached(infos);
413b1b8bc3fSopenharmony_ci    } else {
414b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Cached is nullptr");
415b1b8bc3fSopenharmony_ci    }
416b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetAllStatsInfo(infos);
417b1b8bc3fSopenharmony_ci}
418b1b8bc3fSopenharmony_ci
419b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetAllSimStatsInfo(std::vector<NetStatsInfo> &infos)
420b1b8bc3fSopenharmony_ci{
421b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetAllSimStatsInfo.");
422b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetAllSimStatsInfo(infos);
423b1b8bc3fSopenharmony_ci}
424b1b8bc3fSopenharmony_ci
425b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos,
426b1b8bc3fSopenharmony_ci                                                  const sptr<NetStatsNetwork> &network)
427b1b8bc3fSopenharmony_ci{
428b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetTrafficStatsByNetwork.");
429b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetTrafficStatsByNetwork start");
430b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
431b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
432b1b8bc3fSopenharmony_ci    }
433b1b8bc3fSopenharmony_ci    if (network == nullptr || network->startTime_ > network->endTime_) {
434b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("param network is invalid");
435b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
436b1b8bc3fSopenharmony_ci    }
437b1b8bc3fSopenharmony_ci    std::string ident;
438b1b8bc3fSopenharmony_ci    if (network->type_ == 0) {
439b1b8bc3fSopenharmony_ci        ident = std::to_string(network->simId_);
440b1b8bc3fSopenharmony_ci    }
441b1b8bc3fSopenharmony_ci    uint32_t start = network->startTime_;
442b1b8bc3fSopenharmony_ci    uint32_t end = network->endTime_;
443b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("param: ident=%{public}s, start=%{public}u, end=%{public}u", ident.c_str(), start, end);
444b1b8bc3fSopenharmony_ci    auto history = std::make_unique<NetStatsHistory>();
445b1b8bc3fSopenharmony_ci    if (history == nullptr) {
446b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("history is null");
447b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INTERNAL;
448b1b8bc3fSopenharmony_ci    }
449b1b8bc3fSopenharmony_ci    std::vector<NetStatsInfo> allInfo;
450b1b8bc3fSopenharmony_ci    int32_t ret = history->GetHistoryByIdent(allInfo, ident, start, end);
451b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
452b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("get history by ident failed, err code=%{public}d", ret);
453b1b8bc3fSopenharmony_ci        return ret;
454b1b8bc3fSopenharmony_ci    }
455b1b8bc3fSopenharmony_ci    netStatsCached_->GetKernelStats(allInfo);
456b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidPushStatsCached(allInfo);
457b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidStatsCached(allInfo);
458b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidSimStatsCached(allInfo);
459b1b8bc3fSopenharmony_ci    std::for_each(allInfo.begin(), allInfo.end(), [&infos, &ident, &start, &end](NetStatsInfo &info) {
460b1b8bc3fSopenharmony_ci        if (ident != info.ident_ || start > info.date_ || end < info.date_) {
461b1b8bc3fSopenharmony_ci            return;
462b1b8bc3fSopenharmony_ci        }
463b1b8bc3fSopenharmony_ci        if (info.flag_ == STATS_DATA_FLAG_UNINSTALLED) {
464b1b8bc3fSopenharmony_ci            info.uid_ = UNINSTALLED_UID;
465b1b8bc3fSopenharmony_ci        }
466b1b8bc3fSopenharmony_ci        auto item = infos.find(info.uid_);
467b1b8bc3fSopenharmony_ci        if (item == infos.end()) {
468b1b8bc3fSopenharmony_ci            infos.emplace(info.uid_, info);
469b1b8bc3fSopenharmony_ci        } else {
470b1b8bc3fSopenharmony_ci            item->second += info;
471b1b8bc3fSopenharmony_ci        }
472b1b8bc3fSopenharmony_ci    });
473b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetTrafficStatsByNetwork end");
474b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
475b1b8bc3fSopenharmony_ci}
476b1b8bc3fSopenharmony_ci
477b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid,
478b1b8bc3fSopenharmony_ci                                                     const sptr<NetStatsNetwork> &network)
479b1b8bc3fSopenharmony_ci{
480b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetTrafficStatsByUidNetwork.");
481b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetTrafficStatsByUidNetwork start");
482b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
483b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Cached is nullptr");
484b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
485b1b8bc3fSopenharmony_ci    }
486b1b8bc3fSopenharmony_ci    if (network == nullptr || network->startTime_ > network->endTime_) {
487b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("param network is invalid");
488b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
489b1b8bc3fSopenharmony_ci    }
490b1b8bc3fSopenharmony_ci    std::string ident;
491b1b8bc3fSopenharmony_ci    if (network->type_ == 0) {
492b1b8bc3fSopenharmony_ci        ident = std::to_string(network->simId_);
493b1b8bc3fSopenharmony_ci    }
494b1b8bc3fSopenharmony_ci    uint32_t start = network->startTime_;
495b1b8bc3fSopenharmony_ci    uint32_t end = network->endTime_;
496b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("GetTrafficStatsByUidNetwork param: "
497b1b8bc3fSopenharmony_ci        "uid=%{public}u, ident=%{public}s, start=%{public}u, end=%{public}u", uid, ident.c_str(), start, end);
498b1b8bc3fSopenharmony_ci    auto history = std::make_unique<NetStatsHistory>();
499b1b8bc3fSopenharmony_ci    if (history == nullptr) {
500b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("history is null");
501b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INTERNAL;
502b1b8bc3fSopenharmony_ci    }
503b1b8bc3fSopenharmony_ci    std::vector<NetStatsInfo> allInfo;
504b1b8bc3fSopenharmony_ci    int32_t ret = history->GetHistory(allInfo, uid, ident, start, end);
505b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
506b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("get history by uid and ident failed, err code=%{public}d", ret);
507b1b8bc3fSopenharmony_ci        return ret;
508b1b8bc3fSopenharmony_ci    }
509b1b8bc3fSopenharmony_ci    netStatsCached_->GetKernelStats(allInfo);
510b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidPushStatsCached(allInfo);
511b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidStatsCached(allInfo);
512b1b8bc3fSopenharmony_ci    netStatsCached_->GetUidSimStatsCached(allInfo);
513b1b8bc3fSopenharmony_ci    std::for_each(allInfo.begin(), allInfo.end(), [this, &infos, &uid, &ident, &start, &end](const NetStatsInfo &info) {
514b1b8bc3fSopenharmony_ci        if (uid != info.uid_ || ident != info.ident_ || start > info.date_ || end < info.date_) {
515b1b8bc3fSopenharmony_ci            return;
516b1b8bc3fSopenharmony_ci        }
517b1b8bc3fSopenharmony_ci        if (info.flag_ == STATS_DATA_FLAG_UNINSTALLED) {
518b1b8bc3fSopenharmony_ci            return;
519b1b8bc3fSopenharmony_ci        }
520b1b8bc3fSopenharmony_ci        MergeTrafficStats(infos, info, end);
521b1b8bc3fSopenharmony_ci    });
522b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService GetTrafficStatsByUidNetwork end");
523b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
524b1b8bc3fSopenharmony_ci}
525b1b8bc3fSopenharmony_ci
526b1b8bc3fSopenharmony_ciint32_t NetStatsService::SetAppStats(const PushStatsInfo &info)
527b1b8bc3fSopenharmony_ci{
528b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetTrafficStatsByUidNetwork.");
529b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService SetAppStats start");
530b1b8bc3fSopenharmony_ci    if (netStatsCached_ == nullptr) {
531b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Cached is nullptr");
532b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
533b1b8bc3fSopenharmony_ci    }
534b1b8bc3fSopenharmony_ci    netStatsCached_->SetAppStats(info);
535b1b8bc3fSopenharmony_ci    NetmanagerHiTrace::NetmanagerStartSyncTrace("NetStatsService SetAppStats end");
536b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
537b1b8bc3fSopenharmony_ci}
538b1b8bc3fSopenharmony_ci
539b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetCookieRxBytes(uint64_t &stats, uint64_t cookie)
540b1b8bc3fSopenharmony_ci{
541b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetCookieStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES),
542b1b8bc3fSopenharmony_ci                                                          cookie);
543b1b8bc3fSopenharmony_ci}
544b1b8bc3fSopenharmony_ci
545b1b8bc3fSopenharmony_ciint32_t NetStatsService::GetCookieTxBytes(uint64_t &stats, uint64_t cookie)
546b1b8bc3fSopenharmony_ci{
547b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().GetCookieStats(stats, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES),
548b1b8bc3fSopenharmony_ci                                                          cookie);
549b1b8bc3fSopenharmony_ci}
550b1b8bc3fSopenharmony_ci
551b1b8bc3fSopenharmony_civoid NetStatsService::MergeTrafficStats(std::vector<NetStatsInfoSequence> &statsInfoSequences, const NetStatsInfo &info,
552b1b8bc3fSopenharmony_ci                                        uint32_t currentTimestamp)
553b1b8bc3fSopenharmony_ci{
554b1b8bc3fSopenharmony_ci    NetStatsInfoSequence tmp;
555b1b8bc3fSopenharmony_ci    tmp.startTime_ = info.date_;
556b1b8bc3fSopenharmony_ci    tmp.endTime_ = info.date_;
557b1b8bc3fSopenharmony_ci    tmp.info_ = info;
558b1b8bc3fSopenharmony_ci    uint32_t previousTimestamp = currentTimestamp > DAY_SECONDS ? currentTimestamp - DAY_SECONDS : 0;
559b1b8bc3fSopenharmony_ci    if (info.date_ > previousTimestamp) {
560b1b8bc3fSopenharmony_ci        statsInfoSequences.push_back(std::move(tmp));
561b1b8bc3fSopenharmony_ci        return;
562b1b8bc3fSopenharmony_ci    }
563b1b8bc3fSopenharmony_ci    auto findRet = std::find_if(
564b1b8bc3fSopenharmony_ci        statsInfoSequences.begin(), statsInfoSequences.end(), [&info, previousTimestamp](const auto &item) {
565b1b8bc3fSopenharmony_ci            return item.endTime_ < previousTimestamp && CommonUtils::IsSameNaturalDay(info.date_, item.endTime_);
566b1b8bc3fSopenharmony_ci        });
567b1b8bc3fSopenharmony_ci    if (findRet == statsInfoSequences.end()) {
568b1b8bc3fSopenharmony_ci        statsInfoSequences.push_back(std::move(tmp));
569b1b8bc3fSopenharmony_ci        return;
570b1b8bc3fSopenharmony_ci    }
571b1b8bc3fSopenharmony_ci    (*findRet).info_ += info;
572b1b8bc3fSopenharmony_ci}
573b1b8bc3fSopenharmony_ci
574b1b8bc3fSopenharmony_cibool NetStatsService::GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
575b1b8bc3fSopenharmony_ci{
576b1b8bc3fSopenharmony_ci    int32_t ret = NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
577b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS || ifaceNames.empty()) {
578b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("Iface list is empty, ret = %{public}d", ret);
579b1b8bc3fSopenharmony_ci        return false;
580b1b8bc3fSopenharmony_ci    }
581b1b8bc3fSopenharmony_ci    return true;
582b1b8bc3fSopenharmony_ci}
583b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
584b1b8bc3fSopenharmony_ci} // namespace OHOS
585