feat: implement ping and particle data size in statusbar
This commit is contained in:
+40
-26
@@ -7,6 +7,7 @@
|
|||||||
#include "authmanager.h"
|
#include "authmanager.h"
|
||||||
#include "createstreamdialog.h"
|
#include "createstreamdialog.h"
|
||||||
#include "memberpickerdialog.h"
|
#include "memberpickerdialog.h"
|
||||||
|
#include "networkmanager.h"
|
||||||
#include "settingsdialog.h"
|
#include "settingsdialog.h"
|
||||||
#include "textinputdialog.h"
|
#include "textinputdialog.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
@@ -18,7 +19,7 @@
|
|||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QRandomGenerator>
|
#include <QJsonObject>
|
||||||
#include <QToolButton>
|
#include <QToolButton>
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -81,11 +82,21 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
m_storageLabel->setStyleSheet(statusBarLabelStyle);
|
m_storageLabel->setStyleSheet(statusBarLabelStyle);
|
||||||
ui->statusbar->addPermanentWidget(m_storageLabel);
|
ui->statusbar->addPermanentWidget(m_storageLabel);
|
||||||
|
|
||||||
// Setup ping timer to oscillate every 500ms
|
// Setup ping timer for periodic server connectivity check
|
||||||
m_pingTimer = new QTimer(this);
|
m_pingTimer = new QTimer(this);
|
||||||
connect(m_pingTimer, &QTimer::timeout, this, &MainWindow::updatePingIndicator);
|
connect(m_pingTimer, &QTimer::timeout, this, []() {
|
||||||
m_pingTimer->start(500);
|
NetworkManager::instance().ping();
|
||||||
updatePingIndicator();
|
});
|
||||||
|
connect(&NetworkManager::instance(), &NetworkManager::pingResult, this, [this](int ms) {
|
||||||
|
m_pingLabel->setText("connected " + QString::number(ms) + "ms");
|
||||||
|
m_pingLabel->setStyleSheet("QLabel { color: #00AA00; }");
|
||||||
|
});
|
||||||
|
connect(&NetworkManager::instance(), &NetworkManager::pingFailed, this, [this]() {
|
||||||
|
m_pingLabel->setText("disconnected");
|
||||||
|
m_pingLabel->setStyleSheet("QLabel { color: #AA0000; }");
|
||||||
|
});
|
||||||
|
m_pingTimer->start(10000);
|
||||||
|
NetworkManager::instance().ping();
|
||||||
|
|
||||||
ui->splitter->setStretchFactor(0, 1);
|
ui->splitter->setStretchFactor(0, 1);
|
||||||
ui->splitter->setStretchFactor(1, 4);
|
ui->splitter->setStretchFactor(1, 4);
|
||||||
@@ -417,10 +428,30 @@ void MainWindow::updateStatusBar()
|
|||||||
m_positionLabel->setText("");
|
m_positionLabel->setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update storage indicator with pseudo-random size based on stream ID
|
// Update data size indicator for selected particle
|
||||||
uint hash = qHash(m_selectedStreamId);
|
if (!m_selectedParticleId.isEmpty())
|
||||||
int sizeKB = 50 + (hash % 950); // Random size between 50-1000 kB
|
{
|
||||||
m_storageLabel->setText(QString::number(sizeKB) + " kB");
|
const Particle *particle = m_store->particleById(m_selectedParticleId);
|
||||||
|
if (particle)
|
||||||
|
{
|
||||||
|
QJsonObject dataObj;
|
||||||
|
for (auto it = particle->data.constBegin(); it != particle->data.constEnd(); ++it)
|
||||||
|
dataObj.insert(it.key(), QJsonValue::fromVariant(it.value()));
|
||||||
|
int bytes = QJsonDocument(dataObj).toJson(QJsonDocument::Compact).size();
|
||||||
|
if (bytes < 1024)
|
||||||
|
m_storageLabel->setText(QString::number(bytes) + " B");
|
||||||
|
else
|
||||||
|
m_storageLabel->setText(QString::number(bytes / 1024) + " kB");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_storageLabel->setText("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_storageLabel->setText("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -429,23 +460,6 @@ void MainWindow::updateStatusBar()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::updatePingIndicator()
|
|
||||||
{
|
|
||||||
// Oscillate ping between 15-35ms
|
|
||||||
static int basePing = 23;
|
|
||||||
int variation = (QRandomGenerator::global()->bounded(21)) - 10; // -10 to +10
|
|
||||||
int currentPing = basePing + variation;
|
|
||||||
if (currentPing < 15)
|
|
||||||
{
|
|
||||||
currentPing = 15;
|
|
||||||
}
|
|
||||||
if (currentPing > 35)
|
|
||||||
{
|
|
||||||
currentPing = 35;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_pingLabel->setText("connected " + QString::number(currentPing) + "ms");
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::showTextInputDialog()
|
void MainWindow::showTextInputDialog()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ private:
|
|||||||
|
|
||||||
void showTextInputDialog();
|
void showTextInputDialog();
|
||||||
void updateStatusBar();
|
void updateStatusBar();
|
||||||
void updatePingIndicator();
|
|
||||||
QString m_selectedParticleId;
|
QString m_selectedParticleId;
|
||||||
QString m_selectedNetworkName;
|
QString m_selectedNetworkName;
|
||||||
QString m_selectedStreamId;
|
QString m_selectedStreamId;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "networkmanager.h"
|
#include "networkmanager.h"
|
||||||
|
#include <QElapsedTimer>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
@@ -107,3 +108,33 @@ void NetworkManager::setSessionToken(const QString &newToken)
|
|||||||
{
|
{
|
||||||
m_sessionToken = newToken;
|
m_sessionToken = newToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PingTimer : public QObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QElapsedTimer timer;
|
||||||
|
explicit PingTimer(QObject *parent = nullptr) : QObject(parent) { timer.start(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
void NetworkManager::ping()
|
||||||
|
{
|
||||||
|
auto *pingTimer = new PingTimer(this);
|
||||||
|
|
||||||
|
QNetworkRequest request(getFullUrl(""));
|
||||||
|
QNetworkReply *reply = m_qnam->get(request);
|
||||||
|
|
||||||
|
connect(reply, &QNetworkReply::finished, pingTimer, [this, pingTimer, reply]() {
|
||||||
|
int elapsed = static_cast<int>(pingTimer->timer.elapsed());
|
||||||
|
pingTimer->deleteLater();
|
||||||
|
|
||||||
|
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
if (statusCode > 0)
|
||||||
|
{
|
||||||
|
emit pingResult(elapsed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
emit pingFailed();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,12 +32,16 @@ public slots:
|
|||||||
QNetworkReply *del(const QString &path);
|
QNetworkReply *del(const QString &path);
|
||||||
QNetworkReply *del(const QString &path, const QJsonDocument &data);
|
QNetworkReply *del(const QString &path, const QJsonDocument &data);
|
||||||
|
|
||||||
|
void ping();
|
||||||
|
|
||||||
/// @brief sets session token which will is injected into every request's headers
|
/// @brief sets session token which will is injected into every request's headers
|
||||||
void setSessionToken(const QString &token);
|
void setSessionToken(const QString &token);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
/// @brief Emitted when a request is made and fails required authentication.
|
/// @brief Emitted when a request is made and fails required authentication.
|
||||||
void unauthorizedDetected();
|
void unauthorizedDetected();
|
||||||
|
void pingResult(int ms);
|
||||||
|
void pingFailed();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NetworkManager();
|
NetworkManager();
|
||||||
|
|||||||
Reference in New Issue
Block a user