Fixing alert dock refresh for both new alerts and expired alerts

Fixes #29
This commit is contained in:
Dan Paulat 2023-02-28 22:10:34 -06:00
parent 704b9e03b9
commit ed4cc033bb
2 changed files with 64 additions and 5 deletions

View file

@ -310,7 +310,8 @@ void AlertModel::HandleAlert(const types::TextEventKey& alertKey,
// Update row // Update row
if (!p->textEventKeys_.contains(alertKey)) if (!p->textEventKeys_.contains(alertKey))
{ {
beginInsertRows(QModelIndex(), 0, 0); int newIndex = p->textEventKeys_.size();
beginInsertRows(QModelIndex(), newIndex, newIndex);
p->textEventKeys_.push_back(alertKey); p->textEventKeys_.push_back(alertKey);
endInsertRows(); endInsertRows();
} }

View file

@ -2,6 +2,12 @@
#include <scwx/qt/model/alert_model.hpp> #include <scwx/qt/model/alert_model.hpp>
#include <scwx/qt/types/qt_types.hpp> #include <scwx/qt/types/qt_types.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <chrono>
#include <mutex>
#include <boost/asio/steady_timer.hpp>
namespace scwx namespace scwx
{ {
@ -16,14 +22,22 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class AlertProxyModelImpl class AlertProxyModelImpl
{ {
public: public:
explicit AlertProxyModelImpl(); explicit AlertProxyModelImpl(AlertProxyModel* self);
~AlertProxyModelImpl() = default; ~AlertProxyModelImpl();
void UpdateAlerts();
AlertProxyModel* self_;
bool alertActiveFilterEnabled_; bool alertActiveFilterEnabled_;
boost::asio::steady_timer alertUpdateTimer_;
std::mutex alertMutex_ {};
}; };
AlertProxyModel::AlertProxyModel(QObject* parent) : AlertProxyModel::AlertProxyModel(QObject* parent) :
QSortFilterProxyModel(parent), p(std::make_unique<AlertProxyModelImpl>()) QSortFilterProxyModel(parent),
p(std::make_unique<AlertProxyModelImpl>(this))
{ {
} }
AlertProxyModel::~AlertProxyModel() = default; AlertProxyModel::~AlertProxyModel() = default;
@ -63,8 +77,52 @@ bool AlertProxyModel::filterAcceptsRow(int sourceRow,
QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
} }
AlertProxyModelImpl::AlertProxyModelImpl() : alertActiveFilterEnabled_ {false} AlertProxyModelImpl::AlertProxyModelImpl(AlertProxyModel* self) :
self_ {self},
alertActiveFilterEnabled_ {false},
alertUpdateTimer_ {scwx::util::io_context()}
{ {
// Schedule alert update
UpdateAlerts();
}
AlertProxyModelImpl::~AlertProxyModelImpl()
{
std::unique_lock lock(alertMutex_);
alertUpdateTimer_.cancel();
}
void AlertProxyModelImpl::UpdateAlerts()
{
logger_->trace("UpdateAlerts");
// Take a unique lock before modifying feature lists
std::unique_lock lock(alertMutex_);
// Re-evaluate for expired alerts
if (alertActiveFilterEnabled_)
{
self_->invalidateRowsFilter();
}
using namespace std::chrono;
alertUpdateTimer_.expires_after(15s);
alertUpdateTimer_.async_wait(
[=](const boost::system::error_code& e)
{
if (e == boost::asio::error::operation_aborted)
{
logger_->debug("Alert update timer cancelled");
}
else if (e != boost::system::errc::success)
{
logger_->warn("Alert update timer error: {}", e.message());
}
else
{
UpdateAlerts();
}
});
} }
} // namespace model } // namespace model