From 65f30a0df267b73d985840af30e31b44c0235dcd Mon Sep 17 00:00:00 2001 From: Pawel Olejniczak Date: Wed, 10 Aug 2022 17:50:15 +0200 Subject: [PATCH] [CP-1448] Add storage info to device info endpoint DeviceInfo endpont now contains information such as: deviceSpaceTotal - total storage space on the device systemReservedSpace - storage space on the device reserved for the OS usedUserSpace - storage space on the device used for user files on the user partition --- .../deviceInfo/DeviceInfoEndpointCommon.cpp | 36 ++++++++----------- .../include/endpoints/JsonKeyNames.hpp | 6 ++-- .../deviceInfo/DeviceInfoEndpointCommon.hpp | 2 -- .../deviceInfo/DeviceInfoEndpoint.cpp | 8 ++--- .../deviceInfo/DeviceInfoEndpoint.cpp | 8 ++--- .../service-desktop/test_device_info.py | 6 ++-- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp index 7854d5a88da4e245ab9169b23f7233dc6ecf647d..bba85eb6f6888a9154279af5753b9683d2570c38 100644 --- a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp +++ b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp @@ -139,14 +139,13 @@ namespace sdesktop::endpoints auto DeviceInfoEndpointCommon::getStorageInfo() -> std::tuple { - const std::array storagePaths{purefs::dir::getRootDiskPath(), - purefs::dir::getPreviousOSPath()}; + unsigned long totalDeviceSpaceMiB = 0; + unsigned long reservedSystemSpaceMiB = 0; + unsigned long usedUserSpaceMiB = 0; - unsigned long totalMbytes = 0; - unsigned long freeUserMbytes = 0; - unsigned long freePercent = 0; - - for (const auto &p : storagePaths) { + const std::array systemStoragePaths{purefs::dir::getRootDiskPath(), + purefs::dir::getPreviousOSPath()}; + for (const auto &p : systemStoragePaths) { auto [totalSpace, freeSpace] = getStorageStats(p); if (totalSpace < 0 || freeSpace < 0) { @@ -154,29 +153,22 @@ namespace sdesktop::endpoints continue; } - totalMbytes += totalSpace; + totalDeviceSpaceMiB += totalSpace; + reservedSystemSpaceMiB = totalDeviceSpaceMiB; } // User partition stats - const auto storagePath = purefs::dir::getUserDiskPath(); - auto [totalSpace, freeSpace] = getStorageStats(storagePath); + const auto userStoragePath = purefs::dir::getUserDiskPath(); + auto [totalSpace, freeSpace] = getStorageStats(userStoragePath); if (totalSpace < 0 || freeSpace < 0) { - LOG_ERROR("Failed to get stats for %s", storagePath.c_str()); + LOG_ERROR("Failed to get stats for %s", userStoragePath.c_str()); } else { - totalMbytes += totalSpace; - freeUserMbytes = freeSpace; - - // Deduct 1024 MB reserved for OS data on User partition - freeUserMbytes -= OS_RESERVED_SPACE_IN_MB; + usedUserSpaceMiB = totalSpace - freeSpace; + totalDeviceSpaceMiB += totalSpace; } - if (totalMbytes) { - freePercent = (freeUserMbytes * 100) / totalMbytes; - } - - return {totalMbytes, freeUserMbytes, freePercent}; + return {totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB}; } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp index 5de2556f009913bad5e422596e3c8d2b6340db9d..ee8efb01d28122fb1fb98b4d594c7e018f0da5cc 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp @@ -11,9 +11,9 @@ namespace sdesktop::endpoints::json inline constexpr auto sim = "sim"; inline constexpr auto trayState = "trayState"; inline constexpr auto signalStrength = "signalStrength"; - inline constexpr auto fsTotal = "fsTotal"; - inline constexpr auto fsFreePercent = "fsFreePercent"; - inline constexpr auto fsFree = "fsFree"; + inline constexpr auto deviceSpaceTotal = "deviceSpaceTotal"; + inline constexpr auto systemReservedSpace = "systemReservedSpace"; + inline constexpr auto usedUserSpace = "usedUserSpace"; inline constexpr auto gitRevision = "gitRevision"; inline constexpr auto gitBranch = "gitBranch"; inline constexpr auto gitTag = "gitTag"; diff --git a/module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp b/module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp index 296fdaea5ef6b4e8630f322726e306642c979789..911eba723b84dc3708029a81f64c1a5fcd9241fb 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp @@ -31,8 +31,6 @@ namespace sdesktop::endpoints auto getStorageStats(const std::string &path) -> std::tuple; auto getStorageInfo() -> std::tuple; - static constexpr auto OS_RESERVED_SPACE_IN_MB = (1024LU); - explicit DeviceInfoEndpointCommon(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) { debugName = "DeviceInfoEndpoint"; diff --git a/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp b/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp index c0ba26b84dd33eb5cdc91364e9cfe79d2829f3cb..f376ef086eda2cdbc26c14156d36c2547ad71007 100644 --- a/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +++ b/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp @@ -22,14 +22,14 @@ namespace sdesktop::endpoints auto DeviceInfoEndpoint::getDeviceInfo(Context &context) -> http::Code { - auto [totalMbytes, freeUserMbytes, freePercent] = getStorageInfo(); + auto [totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB] = getStorageInfo(); context.setResponseBody( json11::Json::object({{json::batteryLevel, std::to_string(Store::Battery::get().level)}, {json::batteryState, std::to_string(static_cast(Store::Battery::get().state))}, - {json::fsTotal, std::to_string(totalMbytes)}, - {json::fsFree, std::to_string(freeUserMbytes)}, - {json::fsFreePercent, std::to_string(freePercent)}, + {json::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, + {json::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, + {json::usedUserSpace, std::to_string(usedUserSpaceMiB)}, {json::gitRevision, (std::string)(GIT_REV)}, {json::gitBranch, (std::string)GIT_BRANCH}, {json::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, diff --git a/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp b/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp index 4e75d5c851f99b8213e24b8eb1ce0365a229e1ff..90bb298a9398e7a1d9bf3154d9a61771ce1ded3f 100644 --- a/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +++ b/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp @@ -37,7 +37,7 @@ namespace sdesktop::endpoints auto DeviceInfoEndpoint::getDeviceInfo(Context &context) -> http::Code { - auto [totalMbytes, freeUserMbytes, freePercent] = getStorageInfo(); + auto [totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB] = getStorageInfo(); context.setResponseBody(json11::Json::object( {{json::batteryLevel, std::to_string(Store::Battery::get().level)}, @@ -49,9 +49,9 @@ namespace sdesktop::endpoints std::to_string(static_cast(Store::GSM::get()->getNetwork().accessTechnology))}, {json::networkStatus, std::to_string(static_cast(Store::GSM::get()->getNetwork().status))}, {json::networkOperatorName, Store::GSM::get()->getNetworkOperatorName()}, - {json::fsTotal, std::to_string(totalMbytes)}, - {json::fsFree, std::to_string(freeUserMbytes)}, - {json::fsFreePercent, std::to_string(freePercent)}, + {json::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, + {json::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, + {json::usedUserSpace, std::to_string(usedUserSpaceMiB)}, {json::gitRevision, std::string(GIT_REV)}, {json::gitBranch, std::string(GIT_BRANCH)}, {json::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, diff --git a/test/pytest/service-desktop/test_device_info.py b/test/pytest/service-desktop/test_device_info.py index cbaa21b7154963c2ad0bb33c1f64a0d79cb9f84a..797fd5f5f326a33158d3012bf4f95065f108b64b 100644 --- a/test/pytest/service-desktop/test_device_info.py +++ b/test/pytest/service-desktop/test_device_info.py @@ -18,9 +18,9 @@ def test_get_device_information(harness): assert ret.diag_info["accessTechnology"] in ['0', '2', '3', '4', '5', '6', '7', '255'] assert 0 <= int(ret.diag_info["networkStatus"]) < 7 assert ret.diag_info["networkOperatorName"] is not None - assert 0 < int(ret.diag_info["fsTotal"]) < 16000 - assert 0 < int(ret.diag_info["fsFree"]) < 16000 - assert 0 < int(ret.diag_info["fsFreePercent"]) <= 100 + assert int(ret.diag_info["deviceSpaceTotal"]) == 14945 + assert int(ret.diag_info["systemReservedSpace"]) == 2042 + assert 0 < int(ret.diag_info["usedUserSpace"]) <= 12903 assert re.match(r"^(\d|[a-z]){8,40}$", ret.diag_info["gitRevision"]) assert ret.diag_info["gitBranch"] is not None assert int(ret.diag_info["currentRTCTime"]) > 1641991996