From f74c65fcc0b1da75e91b212b97ddf079361bf448 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 11 Apr 2020 20:52:35 +0200 Subject: [PATCH] created a specific GxsEvent for Group Statistics changes and handle it in the GUI for all friends at once every 2 min at most --- libretroshare/src/gxs/rsgenexchange.cc | 2 +- libretroshare/src/retroshare/rsgxschannels.h | 1 + libretroshare/src/retroshare/rsgxsforums.h | 1 + libretroshare/src/retroshare/rsgxsservice.h | 3 +- libretroshare/src/retroshare/rsposted.h | 1 + libretroshare/src/services/p3gxschannels.cc | 22 +++++++++- libretroshare/src/services/p3gxsforums.cc | 23 +++++++++-- libretroshare/src/services/p3postbase.cc | 40 ++++++++++++++----- .../src/gui/Posted/PostedDialog.cpp | 5 +++ .../src/gui/gxs/GxsGroupFrameDialog.cpp | 25 ++++++++++++ .../src/gui/gxs/GxsGroupFrameDialog.h | 13 +++++- .../src/gui/gxschannels/GxsChannelDialog.cpp | 4 ++ .../src/gui/gxsforums/GxsForumsDialog.cpp | 5 +++ 13 files changed, 126 insertions(+), 19 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index d95def1de..af4a02fcd 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -1692,7 +1692,7 @@ void RsGenExchange::notifyChangedGroupStats(const RsGxsGroupId &grpId) { RS_STACK_MUTEX(mGenMtx); - RsGxsGroupChange* gc = new RsGxsGroupChange(RsGxsNotify::TYPE_PROCESSED, false); + RsGxsGroupChange* gc = new RsGxsGroupChange(RsGxsNotify::TYPE_STATISTICS_CHANGED, false); gc->mGrpIdList.push_back(grpId); mNotifications.push_back(gc); } diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index 83f21a5e9..9cc4bd33e 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -111,6 +111,7 @@ enum class RsChannelEventCode: uint8_t SUBSCRIBE_STATUS_CHANGED = 0x06, // subscription for channel mChannelGroupId changed. READ_STATUS_CHANGED = 0x07, // existing message has been read or set to unread RECEIVED_DISTANT_SEARCH_RESULT = 0x08, // result for the given group id available for the given turtle request id + STATISTICS_CHANGED = 0x09, // stats (nb of supplier friends, how many msgs they have etc) has changed }; struct RsGxsChannelEvent: RsEvent diff --git a/libretroshare/src/retroshare/rsgxsforums.h b/libretroshare/src/retroshare/rsgxsforums.h index 5bff8e267..11a47cf7b 100644 --- a/libretroshare/src/retroshare/rsgxsforums.h +++ b/libretroshare/src/retroshare/rsgxsforums.h @@ -111,6 +111,7 @@ enum class RsForumEventCode: uint8_t UPDATED_MESSAGE = 0x04, /// existing message has been updated in a particular forum SUBSCRIBE_STATUS_CHANGED = 0x05, /// forum was subscribed or unsubscribed READ_STATUS_CHANGED = 0x06, /// msg was read or marked unread + STATISTICS_CHANGED = 0x07, /// suppliers and how many messages they have changed }; struct RsGxsForumEvent: RsEvent diff --git a/libretroshare/src/retroshare/rsgxsservice.h b/libretroshare/src/retroshare/rsgxsservice.h index cb4fcf7eb..4739b0ec2 100644 --- a/libretroshare/src/retroshare/rsgxsservice.h +++ b/libretroshare/src/retroshare/rsgxsservice.h @@ -46,7 +46,8 @@ struct RsGxsNotify TYPE_RECEIVED_NEW = 0x02, TYPE_PROCESSED = 0x03, TYPE_RECEIVED_PUBLISHKEY = 0x04, - TYPE_RECEIVED_DISTANT_SEARCH_RESULTS = 0x05 + TYPE_RECEIVED_DISTANT_SEARCH_RESULTS = 0x05, + TYPE_STATISTICS_CHANGED = 0x06 }; virtual ~RsGxsNotify() {} diff --git a/libretroshare/src/retroshare/rsposted.h b/libretroshare/src/retroshare/rsposted.h index 066232d5b..1a089de8e 100644 --- a/libretroshare/src/retroshare/rsposted.h +++ b/libretroshare/src/retroshare/rsposted.h @@ -114,6 +114,7 @@ enum class RsPostedEventCode: uint8_t UPDATED_POSTED_GROUP = 0x04, UPDATED_MESSAGE = 0x05, READ_STATUS_CHANGED = 0x06, + STATISTICS_CHANGED = 0x07, }; diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 1e7538c57..9e0072d5b 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -302,7 +302,6 @@ void p3GxsChannels::notifyChanges(std::vector &changes) { switch (grpChange->getType()) { - default: case RsGxsNotify::TYPE_PROCESSED: // happens when the group is subscribed { std::list &grpList = grpChange->mGrpIdList; @@ -318,6 +317,20 @@ void p3GxsChannels::notifyChanges(std::vector &changes) } break; + case RsGxsNotify::TYPE_STATISTICS_CHANGED: + { + std::list &grpList = grpChange->mGrpIdList; + std::list::iterator git; + for (git = grpList.begin(); git != grpList.end(); ++git) + { + auto ev = std::make_shared(); + ev->mChannelGroupId = *git; + ev->mChannelEventCode = RsChannelEventCode::STATISTICS_CHANGED; + rsEvents->postEvent(ev); + } + } + break; + case RsGxsNotify::TYPE_PUBLISHED: case RsGxsNotify::TYPE_RECEIVED_NEW: { @@ -356,9 +369,14 @@ void p3GxsChannels::notifyChanges(std::vector &changes) rsEvents->postEvent(ev); } + } + break; + + default: + RsErr() << " Got a GXS event of type " << grpChange->getType() << " Currently not handled." << std::endl; break; } - } + } RsGxsDistantSearchResultChange *dsrChange = dynamic_cast(*it); diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index a37a62098..42b60fad2 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -246,7 +246,6 @@ void p3GxsForums::notifyChanges(std::vector &changes) { switch (grpChange->getType()) { - default: case RsGxsNotify::TYPE_PROCESSED: // happens when the group is subscribed { std::list &grpList = grpChange->mGrpIdList; @@ -260,7 +259,7 @@ void p3GxsForums::notifyChanges(std::vector &changes) } } - break; + break; case RsGxsNotify::TYPE_PUBLISHED: case RsGxsNotify::TYPE_RECEIVED_NEW: @@ -288,8 +287,26 @@ void p3GxsForums::notifyChanges(std::vector &changes) << " Not notifying already known forum " << *git << std::endl; } - break; } + break; + + case RsGxsNotify::TYPE_STATISTICS_CHANGED: + { + std::list &grpList = grpChange->mGrpIdList; + std::list::iterator git; + for (git = grpList.begin(); git != grpList.end(); ++git) + { + auto ev = std::make_shared(); + ev->mForumGroupId = *git; + ev->mForumEventCode = RsForumEventCode::STATISTICS_CHANGED; + rsEvents->postEvent(ev); + } + } + break; + default: + RsErr() << " Got a GXS event of type " << grpChange->getType() << " Currently not handled." << std::endl; + break; + #ifdef NOT_USED_YET case RsGxsNotify::TYPE_RECEIVED_PUBLISHKEY: diff --git a/libretroshare/src/services/p3postbase.cc b/libretroshare/src/services/p3postbase.cc index 8dd48c174..771103eb0 100644 --- a/libretroshare/src/services/p3postbase.cc +++ b/libretroshare/src/services/p3postbase.cc @@ -133,8 +133,7 @@ void p3PostBase::notifyChanges(std::vector &changes) #endif switch(grpChange->getType()) - { - default: + { case RsGxsNotify::TYPE_PROCESSED: // happens when the group is subscribed { std::list &grpList = grpChange->mGrpIdList; @@ -148,15 +147,30 @@ void p3PostBase::notifyChanges(std::vector &changes) } } - break; + break; - case RsGxsNotify::TYPE_PUBLISHED: - case RsGxsNotify::TYPE_RECEIVED_NEW: - { - /* group received */ - const std::list& grpList = grpChange->mGrpIdList; + case RsGxsNotify::TYPE_STATISTICS_CHANGED: + { + std::list &grpList = grpChange->mGrpIdList; + std::list::iterator git; - for (auto git = grpList.begin(); git != grpList.end(); ++git) + for (git = grpList.begin(); git != grpList.end(); ++git) + { + auto ev = std::make_shared(); + ev->mPostedGroupId = *git; + ev->mPostedEventCode = RsPostedEventCode::STATISTICS_CHANGED; + rsEvents->postEvent(ev); + } + } + break; + + case RsGxsNotify::TYPE_PUBLISHED: + case RsGxsNotify::TYPE_RECEIVED_NEW: + { + /* group received */ + const std::list& grpList = grpChange->mGrpIdList; + + for (auto git = grpList.begin(); git != grpList.end(); ++git) { if(mKnownPosted.find(*git) == mKnownPosted.end()) { @@ -178,9 +192,13 @@ void p3PostBase::notifyChanges(std::vector &changes) << " Not notifying already known forum " << *git << std::endl; } - } + } break; - } + + default: + RsErr() << " Got a GXS event of type " << grpChange->getType() << " Currently not handled." << std::endl; + break; + } } delete *it; diff --git a/retroshare-gui/src/gui/Posted/PostedDialog.cpp b/retroshare-gui/src/gui/Posted/PostedDialog.cpp index b6da6024e..1be39110f 100644 --- a/retroshare-gui/src/gui/Posted/PostedDialog.cpp +++ b/retroshare-gui/src/gui/Posted/PostedDialog.cpp @@ -69,6 +69,11 @@ void PostedDialog::handleEvent_main_thread(std::shared_ptr event) case RsPostedEventCode::SUBSCRIBE_STATUS_CHANGED: // [[fallthrough]]; updateDisplay(true); break; + + case RsPostedEventCode::STATISTICS_CHANGED: + updateGroupStatistics(e->mPostedGroupId); + break; + default: break; } } diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp index d506ae944..1245c80b2 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp @@ -59,6 +59,8 @@ #define MAX_COMMENT_TITLE 32 +static const uint32_t DELAY_BETWEEN_GROUP_STATISTICS_UPDATE = 120; // do not update group statistics more often than once every 2 mins + /* * Transformation Notes: * there are still a couple of things that the new groups differ from Old version. @@ -77,6 +79,8 @@ GxsGroupFrameDialog::GxsGroupFrameDialog(RsGxsIfaceHelper *ifaceImpl, QWidget *p ui->setupUi(this); mShouldUpdateMessageSummaryList = true; + mShouldUpdateGroupStatistics = false; + mLastGroupStatisticsUpdateTs=0; mInitialized = false; mDistSyncAllowed = allow_dist_sync; mInFill = false; @@ -204,6 +208,21 @@ void GxsGroupFrameDialog::paintEvent(QPaintEvent *pe) mGroupIdsSummaryToUpdate.clear(); } + rstime_t now = time(nullptr); + + if(mShouldUpdateGroupStatistics && now > DELAY_BETWEEN_GROUP_STATISTICS_UPDATE + mLastGroupStatisticsUpdateTs) + { + // This mechanism allows to gather multiple updateGroupStatistics events at once and not send too many of them at the same time. + // it avoids re-loadign all the group everytime a friend sends new statistics. + + for(auto& groupId: mGroupStatisticsToUpdate) + updateGroupStatisticsReal(groupId); + + mShouldUpdateGroupStatistics = false; + mLastGroupStatisticsUpdateTs = time(nullptr); + mGroupStatisticsToUpdate.clear(); + } + MainPage::paintEvent(pe); } @@ -1106,6 +1125,12 @@ void GxsGroupFrameDialog::updateGroupSummary() /*********************** **** **** **** ***********************/ void GxsGroupFrameDialog::updateGroupStatistics(const RsGxsGroupId &groupId) +{ + mGroupStatisticsToUpdate.insert(groupId); + mShouldUpdateGroupStatistics = true; +} + +void GxsGroupFrameDialog::updateGroupStatisticsReal(const RsGxsGroupId &groupId) { RsThread::async([this,groupId]() { diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h index ba7581542..669324324 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.h @@ -104,6 +104,11 @@ protected: virtual bool getGroupData(std::list& groupInfo) =0; virtual bool getGroupStatistics(const RsGxsGroupId& groupId,GxsGroupStatistic& stat) =0; + +private: + void updateGroupStatisticsReal(const RsGxsGroupId &groupId); + void updateMessageSummaryListReal(RsGxsGroupId groupId); + private slots: void todo(); @@ -161,7 +166,6 @@ private: void initUi(); - void updateMessageSummaryListReal(RsGxsGroupId groupId); void openGroupInNewTab(const RsGxsGroupId &groupId); void groupSubscribe(bool subscribe); @@ -205,9 +209,16 @@ private: RsGxsGroupId mNavigatePendingGroupId; RsGxsMessageId mNavigatePendingMsgId; + // Message summary list update + bool mShouldUpdateMessageSummaryList ; // whether we should update the counting for groups. This takes some CPU so we only do it when needed. std::set mGroupIdsSummaryToUpdate; + // GroupStatistics update + bool mShouldUpdateGroupStatistics; + rstime_t mLastGroupStatisticsUpdateTs; + std::set mGroupStatisticsToUpdate; + UIStateHelper *mStateHelper; /** Qt Designer generated object */ diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp index 9c3afc945..961c55709 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp @@ -80,6 +80,10 @@ void GxsChannelDialog::handleEvent_main_thread(std::shared_ptr ev updateDisplay(true); break; + case RsChannelEventCode::STATISTICS_CHANGED: + updateGroupStatistics(e->mChannelGroupId); + break; + default: break; } diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp index e8b7f7d3d..20f0a99ae 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp @@ -69,6 +69,11 @@ void GxsForumsDialog::handleEvent_main_thread(std::shared_ptr eve case RsForumEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true); break; + + case RsForumEventCode::STATISTICS_CHANGED: + updateGroupStatistics(e->mForumGroupId); + break; + default: break; }