Skip to content

Commit

Permalink
Add more session reset information to JSON API
Browse files Browse the repository at this point in the history
It now adds information about effective autoreset size, the autoreset
request state and some timer information and info about the reset
stream, if one is going.
  • Loading branch information
askmeaboutlo0m committed Sep 7, 2024
1 parent 4672d3c commit 4a57f1f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Unreleased Version 2.2.2-pre
* Fix: Work around Huion tablets emitting mouse clicks every few pen presses and causing full-pressure strokes to be started. Thanks Blozzom, DT and and Dumb Dog Disease for reporting.
* Feature: Toggle layer visibility action, available in the Layer menu. Mostly useful to let assign a keyboard shortcut to it. Thanks incoheart for suggesting.
* Feature: Remember ranges set for MyPaint brushes in the settings editor, rather than implicitly fitting them to the curve. Thanks Verdrusk for suggesting.
* Server Feature: Provide more session status information for administrators, such as the autoreset state.

2024-08-09 Version 2.2.2-beta.3
* Fix: Use more accurate timers for performance profiles if the platform supports it.
Expand Down
31 changes: 28 additions & 3 deletions src/libserver/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,14 +1556,14 @@ QJsonObject Session::getDescription(bool full) const
// Full descriptions includes detailed info for server admins.
o["maxSize"] = int(m_history->sizeLimit());
o["resetThreshold"] = int(m_history->autoResetThreshold());
o[QStringLiteral("effectiveResetThreshold")] =
int(m_history->effectiveAutoResetThreshold());
o["deputies"] = m_history->hasFlag(SessionHistory::Deputies);
o["hasOpword"] = !m_history->opwordHash().isEmpty();

QJsonArray users;
for(const Client *user : m_clients) {
QJsonObject u = user->description(false);
u["online"] = true;
users << u;
users.append(getUserDescription(user));
}
for(auto u = m_pastClients.constBegin(); u != m_pastClients.constEnd();
++u) {
Expand All @@ -1590,6 +1590,31 @@ QJsonObject Session::getDescription(bool full) const
return o;
}

QJsonObject Session::getUserDescription(const Client *user) const
{
QJsonObject u = user->description(false);
u[QStringLiteral("online")] = true;
u[QStringLiteral("holdLocked")] = user->isHoldLocked();

Client::ResetFlags resetFlags = user->resetFlags();
QJsonArray f;
if(resetFlags.testFlag(Client::ResetFlag::Awaiting)) {
f.append(QStringLiteral("awaiting"));
}
if(resetFlags.testFlag(Client::ResetFlag::Queried)) {
f.append(QStringLiteral("queried"));
}
if(resetFlags.testFlag(Client::ResetFlag::Responded)) {
f.append(QStringLiteral("responded"));
}
if(resetFlags.testFlag(Client::ResetFlag::Streaming)) {
f.append(QStringLiteral("streaming"));
}
u[QStringLiteral("resetFlags")] = f;

return u;
}

QJsonObject Session::getExportBanList() const
{
return m_history->banlist().toExportJson();
Expand Down
4 changes: 3 additions & 1 deletion src/libserver/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class Session : public QObject, public sessionlisting::Announcable {
* @param full - include detailed information (for admin use)
* @return
*/
QJsonObject getDescription(bool full = false) const;
virtual QJsonObject getDescription(bool full = false) const;

QJsonObject getExportBanList() const;

Expand Down Expand Up @@ -491,6 +491,8 @@ private slots:
JsonApiMethod method, const QStringList &path,
const QJsonObject &request);

QJsonObject getUserDescription(const Client *user) const;

SessionHistory *m_history;
ServerConfig *m_config;
sessionlisting::Announcements *m_announcements;
Expand Down
24 changes: 24 additions & 0 deletions src/libserver/sessionhistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern "C" {
#include "libserver/client.h"
#include "libserver/sessionhistory.h"
#include "libshared/net/servercmd.h"
#include <QJsonObject>

namespace server {

Expand Down Expand Up @@ -388,6 +389,29 @@ const QString *SessionHistory::authenticatedUsernameFor(const QString &authId)
return it == m_authUsernames.constEnd() ? nullptr : &it.value();
}

QJsonValue SessionHistory::getStreamedResetDescription() const
{
QString state;
switch(m_resetStreamState) {
case ResetStreamState::None:
return QJsonValue();
case ResetStreamState::Streaming:
state = QStringLiteral("streaming");
break;
case ResetStreamState::Prepared:
state = QStringLiteral("prepared");
break;
}
return QJsonObject({
{QStringLiteral("state"), state},
{QStringLiteral("ctxId"), m_resetStreamCtxId},
{QStringLiteral("size"), double(m_resetStreamSize)},
{QStringLiteral("startIndex"), double(m_resetStreamStartIndex)},
{QStringLiteral("messageCount"), m_resetStreamMessageCount},
{QStringLiteral("haveConsumer"), m_resetStreamConsumer != nullptr},
});
}

int SessionHistory::incrementNextCatchupKey(int &nextCatchupKey)
{
int result = nextCatchupKey;
Expand Down
3 changes: 3 additions & 0 deletions src/libserver/sessionhistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "libshared/net/message.h"
#include "libshared/util/passwordhash.h"
#include <QDateTime>
#include <QJsonValue>
#include <QObject>
#include <tuple>

Expand Down Expand Up @@ -446,6 +447,8 @@ class SessionHistory : public QObject {
return m_authUsernames;
}

QJsonValue getStreamedResetDescription() const;

signals:
/**
* @brief This signal is emited when new messages are added to the history
Expand Down
55 changes: 55 additions & 0 deletions src/libserver/thinsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,61 @@ void ThinSession::cleanupHistoryCache()
history()->cleanupBatches(minIdx);
}

QJsonObject ThinSession::getDescription(bool full) const
{
QJsonObject o = Session::getDescription(full);
if(full) {
QString sessionState;
switch(state()) {
case State::Initialization:
sessionState = QStringLiteral("initialization");
break;
case State::Running:
sessionState = QStringLiteral("running");
break;
case State::Reset:
sessionState = QStringLiteral("reset");
break;
case State::Shutdown:
sessionState = QStringLiteral("shutdown");
break;
}

QString autoresetRequestStatus;
switch(m_autoResetRequestStatus) {
case AutoResetState::NotSent:
autoresetRequestStatus = QStringLiteral("not sent");
break;
case AutoResetState::Queried:
autoresetRequestStatus = QStringLiteral("queried");
break;
case AutoResetState::QueriedWaiting:
autoresetRequestStatus = QStringLiteral("queried waiting");
break;
case AutoResetState::Requested:
autoresetRequestStatus = QStringLiteral("requested");
break;
}

const SessionHistory *hist = history();
QJsonObject a = QJsonObject({
{QStringLiteral("delay"), m_autoResetDelay.remainingTime()},
{QStringLiteral("historyFirstIndex"), double(hist->firstIndex())},
{QStringLiteral("historyLastIndex"), double(hist->lastIndex())},
{QStringLiteral("requestStatus"), autoresetRequestStatus},
{QStringLiteral("sessionState"), sessionState},
{QStringLiteral("stream"), hist->getStreamedResetDescription()},
});

if(m_autoResetTimer->isActive()) {
a[QStringLiteral("timer")] = m_autoResetTimer->remainingTime();
}

o[QStringLiteral("autoreset")] = a;
}
return o;
}

void ThinSession::readyToAutoReset(
const AutoResetResponseParams &params, const QString &payload)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libserver/thinsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ThinSession final : public Session {

bool supportsAutoReset() const override { return true; }

QJsonObject getDescription(bool full = false) const override;

protected:
void addToHistory(const net::Message &msg) override;
void onSessionInitialized() override;
Expand Down

0 comments on commit 4a57f1f

Please sign in to comment.