~aleteoryx/muditaos

65f30a0df267b73d985840af30e31b44c0235dcd — Pawel Olejniczak 3 years ago d1726c6
[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
M module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp => module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp +14 -22
@@ 139,14 139,13 @@ namespace sdesktop::endpoints

    auto DeviceInfoEndpointCommon::getStorageInfo() -> std::tuple<long, long, long>
    {
        const std::array<std::filesystem::path, 2> 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<std::filesystem::path, 2> 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

M module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp => module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp +3 -3
@@ 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";

M module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp => module-services/service-desktop/endpoints/include/endpoints/deviceInfo/DeviceInfoEndpointCommon.hpp +0 -2
@@ 31,8 31,6 @@ namespace sdesktop::endpoints
        auto getStorageStats(const std::string &path) -> std::tuple<long, long>;
        auto getStorageInfo() -> std::tuple<long, long, long>;

        static constexpr auto OS_RESERVED_SPACE_IN_MB = (1024LU);

        explicit DeviceInfoEndpointCommon(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr)
        {
            debugName = "DeviceInfoEndpoint";

M products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp => products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +4 -4
@@ 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<int>(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<uint32_t>(std::time(nullptr)))},

M products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp => products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +4 -4
@@ 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<int>(Store::GSM::get()->getNetwork().accessTechnology))},
             {json::networkStatus, std::to_string(static_cast<int>(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<uint32_t>(std::time(nullptr)))},

M test/pytest/service-desktop/test_device_info.py => test/pytest/service-desktop/test_device_info.py +3 -3
@@ 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