~aleteoryx/muditaos

ad52bab728d04de7767a4aabc8092ac7cab416f5 — Michał Kamoń 5 years ago d3ce508
[EGD-6283] Fix to slow Application Desktop start

ApplicationDesktop start was slowed down by two synchronous DB calls:
-get unread threads count
-get unread calls count
Both were replaced with asynch versions.
M module-apps/application-desktop/ApplicationDesktop.cpp => module-apps/application-desktop/ApplicationDesktop.cpp +74 -30
@@ 33,14 33,42 @@
#include <application-calllog/ApplicationCallLog.hpp>
#include <service-db/QueryMessage.hpp>
#include <module-db/queries/notifications/QueryNotificationsClear.hpp>
#include <module-db/queries/messages/threads/QueryThreadsGetCount.hpp>
#include <module-db/queries/calllog/QueryCalllogGetCount.hpp>
#include <module-services/service-db/agents/settings/SystemSettings.hpp>
#include <module-utils/magic_enum/include/magic_enum.hpp>
#include <module-apps/messages/AppMessage.hpp>
#include <SystemManager/messages/SystemManagerMessage.hpp>
#include <service-db/DBCalllogMessage.hpp>

#include <cassert>
namespace app
{

    namespace
    {
        bool requestNotSeenNotifications(app::Application *app)
        {
            const auto [succeed, _] = DBServiceAPI::GetQuery(
                app, db::Interface::Name::Notifications, std::make_unique<db::query::notifications::GetAll>());
            return succeed;
        }

        bool requestUnreadThreadsCount(app::Application *app)
        {
            const auto [succeed, _] = DBServiceAPI::GetQuery(
                app, db::Interface::Name::SMSThread, std::make_unique<db::query::ThreadGetCount>(EntryState::UNREAD));
            return succeed;
        }

        bool requestUnreadCallsCount(app::Application *app)
        {
            const auto [succeed, _] = DBServiceAPI::GetQuery(
                app, db::Interface::Name::Calllog, std::make_unique<db::query::CalllogGetCount>(EntryState::UNREAD));
            return succeed;
        }
    } // namespace

    ApplicationDesktop::ApplicationDesktop(std::string name, std::string parent, StartInBackground startInBackground)
        : Application(name, parent, startInBackground), lockHandler(this)
    {


@@ 177,6 205,12 @@ namespace app
                if (auto response = dynamic_cast<db::query::notifications::GetAllResult *>(result.get())) {
                    handled = handle(response);
                }
                if (auto response = dynamic_cast<db::query::ThreadGetCountResult *>(result.get())) {
                    handled = handle(response);
                }
                if (auto response = dynamic_cast<db::query::CalllogGetCountResult *>(result.get())) {
                    handled = handle(response);
                }
            }
        }



@@ 250,23 284,37 @@ namespace app
        assert(msg);

        if (msg->interface == db::Interface::Name::Notifications && msg->type == db::Query::Type::Update) {
            return requestNotSeenNotifications();
            return requestNotSeenNotifications(this);
        }

        if ((msg->interface == db::Interface::Name::Calllog || msg->interface == db::Interface::Name::SMSThread ||
             msg->interface == db::Interface::Name::SMS) &&
        if (msg->interface == db::Interface::Name::Calllog && msg->type != db::Query::Type::Read) {
            return requestUnreadCallsCount(this);
        }

        if ((msg->interface == db::Interface::Name::SMSThread || msg->interface == db::Interface::Name::SMS) &&
            msg->type != db::Query::Type::Read) {
            requestNotReadNotifications();
            if (auto menuWindow = dynamic_cast<gui::MenuWindow *>(getWindow(app::window::name::desktop_menu));
                menuWindow != nullptr) {
                menuWindow->refresh();
                return true;
            }
            return requestUnreadThreadsCount(this);
        }

        return false;
    }

    auto ApplicationDesktop::handle(db::query::ThreadGetCountResult *msg) -> bool
    {
        if (msg->getState() == EntryState::UNREAD) {
            notifications.notRead.SMS = msg->getCount();
        }
        return refreshMainWindow();
    }

    auto ApplicationDesktop::handle(db::query::CalllogGetCountResult *msg) -> bool
    {
        if (msg->getState() == EntryState::UNREAD) {
            notifications.notRead.Calls = msg->getCount();
        }
        return refreshMainWindow();
    }

    auto ApplicationDesktop::handle(cellular::StateChange *msg) -> bool
    {
        assert(msg);


@@ 311,20 359,6 @@ namespace app
        return true;
    }

    bool ApplicationDesktop::requestNotSeenNotifications()
    {
        const auto [succeed, _] = DBServiceAPI::GetQuery(
            this, db::Interface::Name::Notifications, std::make_unique<db::query::notifications::GetAll>());
        return succeed;
    }

    bool ApplicationDesktop::requestNotReadNotifications()
    {
        notifications.notRead.Calls = DBServiceAPI::CalllogGetCount(this, EntryState::UNREAD);
        notifications.notRead.SMS   = DBServiceAPI::ThreadGetCount(this, EntryState::UNREAD);
        return true;
    }

    // Invoked during initialization
    sys::ReturnCodes ApplicationDesktop::InitHandler()
    {


@@ 334,9 368,9 @@ namespace app
            return ret;
        }

        requestNotReadNotifications();
        requestNotSeenNotifications();

        requestNotSeenNotifications(this);
        requestUnreadThreadsCount(this);
        requestUnreadCallsCount(this);
        createUserInterface();
        setActiveWindow(gui::name::window::main_window);



@@ 370,22 404,22 @@ namespace app

        settings->registerValueChange(
            settings::SystemProperties::activeSim,
            [this](std::string value) { activeSimChanged(value); },
            [this](const std::string &value) { activeSimChanged(value); },
            settings::SettingsScope::Global);
        Store::GSM::get()->selected = Store::GSM::SIM::NONE;
        settings->registerValueChange(
            settings::SystemProperties::lockPassHash,
            [this](std::string value) { lockPassHashChanged(value); },
            [this](const std::string &value) { lockPassHashChanged(value); },
            settings::SettingsScope::Global);

        settings->registerValueChange(
            settings::SystemProperties::osCurrentVersion,
            [this](std::string value) { osCurrentVersionChanged(std::move(value)); },
            [this](const std::string &value) { osCurrentVersionChanged(value); },
            settings::SettingsScope::Global);

        settings->registerValueChange(
            settings::SystemProperties::osUpdateVersion,
            [this](std::string value) { osUpdateVersionChanged(std::move(value)); },
            [this](const std::string &value) { osUpdateVersionChanged(value); },
            settings::SettingsScope::Global);

        return sys::ReturnCodes::Success;


@@ 455,6 489,16 @@ namespace app
    void ApplicationDesktop::destroyUserInterface()
    {}

    bool ApplicationDesktop::refreshMainWindow()
    {
        if (auto menuWindow = dynamic_cast<gui::MenuWindow *>(getWindow(app::window::name::desktop_menu));
            menuWindow != nullptr) {
            menuWindow->refresh();
            return true;
        }
        return false;
    }

    void ApplicationDesktop::activeSimChanged(std::string value)
    {
        auto sim = magic_enum::enum_cast<Store::GSM::SIM>(value);

M module-apps/application-desktop/ApplicationDesktop.hpp => module-apps/application-desktop/ApplicationDesktop.hpp +9 -2
@@ 20,6 20,12 @@ namespace cellular
    class StateChange;
}

namespace db::query
{
    class ThreadGetCountResult;
    class CalllogGetCountResult;
}; // namespace db::query

namespace app
{
    inline constexpr auto name_desktop = "ApplicationDesktop";


@@ 67,6 73,8 @@ namespace app
        bool handle(db::NotificationMessage *msg);
        bool handle(cellular::StateChange *msg);
        auto handle(db::query::notifications::GetAllResult *msg) -> bool;
        auto handle(db::query::ThreadGetCountResult *msg) -> bool;
        auto handle(db::query::CalllogGetCountResult *msg) -> bool;
        auto handle(sdesktop::UpdateOsMessage *msg) -> bool;
        auto handle(sdesktop::developerMode::ScreenlockCheckEvent *event) -> bool;
        /**


@@ 77,8 85,6 @@ namespace app
        bool showCalls();
        bool clearCallsNotification();
        bool clearMessagesNotification();
        bool requestNotSeenNotifications();
        bool requestNotReadNotifications();
        unsigned int getLockPassHash() const noexcept
        {
            return lockPassHash;


@@ 94,6 100,7 @@ namespace app
        void setOsUpdateVersion(const std::string &value);

      private:
        bool refreshMainWindow();
        void activeSimChanged(std::string value);
        void lockPassHashChanged(std::string value);
        void handleLowBatteryNotification(manager::actions::ActionParamsPtr &&data);

M module-db/CMakeLists.txt => module-db/CMakeLists.txt +1 -0
@@ 84,6 84,7 @@ set(SOURCES
        queries/messages/threads/QueryThreadGetByContactID.cpp
        queries/messages/threads/QueryThreadRemove.cpp
        queries/messages/threads/QueryThreadMarkAsRead.cpp
        queries/messages/threads/QueryThreadsGetCount.cpp
        queries/notes/QueryNotesGet.cpp
        queries/notes/QueryNotesGetByText.cpp
        queries/notes/QueryNoteStore.cpp

M module-db/Interface/CalllogRecord.cpp => module-db/Interface/CalllogRecord.cpp +3 -2
@@ 300,8 300,9 @@ std::unique_ptr<db::QueryResult> CalllogRecordInterface::setAllReadQuery(std::sh

std::unique_ptr<db::QueryResult> CalllogRecordInterface::getCountQuery(std::shared_ptr<db::Query> query)
{
    auto count    = CalllogRecordInterface::GetCount();
    auto response = std::make_unique<db::query::CalllogGetCountResult>(count);
    auto localQuery = static_cast<db::query::CalllogGetCount *>(query.get());
    auto count      = GetCount(localQuery->getState());
    auto response   = std::make_unique<db::query::CalllogGetCountResult>(localQuery->getState(), count);
    response->setRequestQuery(query);
    return response;
}

M module-db/Interface/ThreadRecord.cpp => module-db/Interface/ThreadRecord.cpp +15 -1
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ThreadRecord.hpp"


@@ 12,6 12,7 @@
#include <queries/messages/threads/QueryThreadsGet.hpp>
#include <queries/messages/threads/QueryThreadsGetForList.hpp>
#include <queries/messages/threads/QueryThreadsSearchForList.hpp>
#include <queries/messages/threads/QueryThreadsGetCount.hpp>

#include <cassert>
#include <log/log.hpp>


@@ 193,6 194,9 @@ std::unique_ptr<db::QueryResult> ThreadRecordInterface::runQuery(std::shared_ptr
    else if (typeid(*query) == typeid(db::query::ThreadRemove)) {
        return threadRemoveQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::ThreadGetCount)) {
        return threadsGetCount(query);
    }

    return nullptr;
}


@@ 320,3 324,13 @@ std::unique_ptr<db::QueryResult> ThreadRecordInterface::threadRemoveQuery(const 
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> ThreadRecordInterface::threadsGetCount(const std::shared_ptr<db::Query> &query)
{
    const auto localQuery = static_cast<const db::query::ThreadGetCount *>(query.get());

    auto count    = GetCount(localQuery->getState());
    auto response = std::make_unique<db::query::ThreadGetCountResult>(localQuery->getState(), count);
    response->setRequestQuery(query);
    return response;
}

M module-db/Interface/ThreadRecord.hpp => module-db/Interface/ThreadRecord.hpp +2 -1
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 82,6 82,7 @@ class ThreadRecordInterface : public RecordInterface<ThreadRecord, ThreadRecordF
    std::unique_ptr<db::QueryResult> threadGetByNumberQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadGetByContactIDQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadRemoveQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadsGetCount(const std::shared_ptr<db::Query> &query);

    std::vector<ThreadRecord> getThreads(const std::shared_ptr<db::Query> &query);
};

M module-db/queries/calllog/QueryCalllogGetCount.cpp => module-db/queries/calllog/QueryCalllogGetCount.cpp +14 -4
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "QueryCalllogGetCount.hpp"


@@ 7,12 7,22 @@

using namespace db::query;

CalllogGetCount::CalllogGetCount() : Query(Query::Type::Read)
CalllogGetCount::CalllogGetCount(EntryState state) : Query(Query::Type::Read), state(state)
{}

CalllogGetCountResult::CalllogGetCountResult(const uint32_t count) : count(count)
auto CalllogGetCount::getState() const noexcept -> EntryState
{
    return state;
}

CalllogGetCountResult::CalllogGetCountResult(EntryState state, unsigned count) : state(state), count(count)
{}

auto CalllogGetCountResult::getState() const noexcept -> EntryState
{
    return state;
}

[[nodiscard]] auto CalllogGetCount::debugInfo() const -> std::string
{
    return "CalllogGetCount";


@@ 23,7 33,7 @@ CalllogGetCountResult::CalllogGetCountResult(const uint32_t count) : count(count
    return "CalllogGetCountResult";
}

auto CalllogGetCountResult::getCount() const -> uint32_t
auto CalllogGetCountResult::getCount() const noexcept -> unsigned
{
    return count;
}

M module-db/queries/calllog/QueryCalllogGetCount.hpp => module-db/queries/calllog/QueryCalllogGetCount.hpp +11 -6
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 6,6 6,7 @@
#include <queries/RecordQuery.hpp>
#include <queries/Filter.hpp>
#include <Interface/CalllogRecord.hpp>
#include <Common/Common.hpp>

#include <string>



@@ 13,19 14,23 @@ namespace db::query
{
    class CalllogGetCount : public Query
    {
        EntryState state;

      public:
        CalllogGetCount();
        explicit CalllogGetCount(EntryState state);
        [[nodiscard]] auto getState() const noexcept -> EntryState;
        [[nodiscard]] auto debugInfo() const -> std::string override;
    };

    class CalllogGetCountResult : public QueryResult
    {
      private:
        uint32_t count;
        EntryState state;
        unsigned count;

      public:
        CalllogGetCountResult(const uint32_t count);
        [[nodiscard]] auto getCount() const -> uint32_t;
        CalllogGetCountResult(EntryState state, unsigned count);
        [[nodiscard]] auto getState() const noexcept -> EntryState;
        [[nodiscard]] auto getCount() const noexcept -> unsigned;
        [[nodiscard]] auto debugInfo() const -> std::string override;
    };


A module-db/queries/messages/threads/QueryThreadsGetCount.cpp => module-db/queries/messages/threads/QueryThreadsGetCount.cpp +34 -0
@@ 0,0 1,34 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "QueryThreadsGetCount.hpp"

db::query::ThreadGetCount::ThreadGetCount(EntryState state) : Query(Query::Type::Read), state(state)
{}

auto db::query::ThreadGetCount::debugInfo() const -> std::string
{
    return "SMSThreadGetCount";
}

auto db::query::ThreadGetCount::getState() const noexcept -> EntryState
{
    return state;
}

db::query::ThreadGetCountResult::ThreadGetCountResult(EntryState state, unsigned count) : state(state), count(count)
{}

auto db::query::ThreadGetCountResult::getCount() const noexcept -> unsigned
{
    return count;
}
auto db::query::ThreadGetCountResult::getState() const noexcept -> EntryState
{
    return state;
}

[[nodiscard]] auto db::query::ThreadGetCountResult::debugInfo() const -> std::string
{
    return "SMSThreadGetCountResult";
}

A module-db/queries/messages/threads/QueryThreadsGetCount.hpp => module-db/queries/messages/threads/QueryThreadsGetCount.hpp +32 -0
@@ 0,0 1,32 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <Common/Query.hpp>
#include <Common/Common.hpp>

namespace db::query
{
    class ThreadGetCount : public Query
    {
        EntryState state;

      public:
        explicit ThreadGetCount(EntryState state);
        [[nodiscard]] auto debugInfo() const -> std::string override;
        [[nodiscard]] auto getState() const noexcept -> EntryState;
    };

    class ThreadGetCountResult : public QueryResult
    {
        EntryState state;
        unsigned count;

      public:
        ThreadGetCountResult(EntryState state, unsigned count);
        [[nodiscard]] auto debugInfo() const -> std::string override;
        [[nodiscard]] auto getState() const noexcept -> EntryState;
        [[nodiscard]] auto getCount() const noexcept -> unsigned;
    };
} // namespace db::query

M module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp => module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp +2 -2
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalllogHelper.hpp"


@@ 69,7 69,7 @@ auto CalllogHelper::requestDataFromDB(Context &context) -> sys::ReturnCodes

auto CalllogHelper::getCalllogCount(Context &context) -> sys::ReturnCodes
{
    auto query = std::make_unique<db::query::CalllogGetCount>();
    auto query = std::make_unique<db::query::CalllogGetCount>(EntryState::ALL);

    auto listener = std::make_unique<db::EndpointListener>(
        [](db::QueryResult *result, Context context) {