Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/webui/api/torrentscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,49 @@ const QString KEY_TORRENTINFO_PIECE_LENGTH = u"piece_length"_s;
const QString KEY_TORRENTINFO_TRACKERS = u"trackers"_s;
const QString KEY_TORRENTINFO_WEBSEEDS = u"webseeds"_s;

// Torrent State info
const QString KEY_TORRENTSTATE_UNKNOWN = u"unknown"_s;
const QString KEY_TORRENTSTATE_FORCED_DOWNLOADING = u"forced_downloading"_s;
const QString KEY_TORRENTSTATE_DOWNLOADING = u"downloading"_s;
const QString KEY_TORRENTSTATE_FORCED_DOWNLOADING_METADATA = u"forced_downloading_metadata"_s;
const QString KEY_TORRENTSTATE_DOWNLOADING_METADATA = u"downloading_metadata"_s;
const QString KEY_TORRENTSTATE_STALLED_DOWNLOADING = u"stalled_downloading"_s;
const QString KEY_TORRENTSTATE_FORCED_UPLOADING = u"forced_uploading"_s;
const QString KEY_TORRENTSTATE_UPLOADING = u"uploading"_s;
const QString KEY_TORRENTSTATE_STALLED_UPLOADING = u"stalled_uploading"_s;
const QString KEY_TORRENTSTATE_CHECKING_RESUME_DATA = u"checking_resume_data"_s;
const QString KEY_TORRENTSTATE_QUEUED_DOWNLOADING = u"queued_downloading"_s;
const QString KEY_TORRENTSTATE_QUEUED_UPLOADING = u"queued_uploading"_s;
const QString KEY_TORRENTSTATE_CHECKING_UPLOADING = u"checking_uploading"_s;
const QString KEY_TORRENTSTATE_CHECKING_DOWNLOADING = u"checking_downloading"_s;
const QString KEY_TORRENTSTATE_STOPPED_DOWNLOADING = u"stopped_downloading"_s;
const QString KEY_TORRENTSTATE_STOPPED_UPLOADING = u"stopped_uploading"_s;
const QString KEY_TORRENTSTATE_MOVING = u"moving"_s;
const QString KEY_TORRENTSTATE_MISSING_FILES = u"missing_files"_s;
const QString KEY_TORRENTSTATE_ERROR = u"error"_s;

const QString *TorrentStateKeyList[] = {
&KEY_TORRENTSTATE_UNKNOWN,
&KEY_TORRENTSTATE_FORCED_DOWNLOADING,
&KEY_TORRENTSTATE_DOWNLOADING,
&KEY_TORRENTSTATE_FORCED_DOWNLOADING_METADATA,
&KEY_TORRENTSTATE_DOWNLOADING_METADATA,
&KEY_TORRENTSTATE_STALLED_DOWNLOADING,
&KEY_TORRENTSTATE_FORCED_UPLOADING,
&KEY_TORRENTSTATE_UPLOADING,
&KEY_TORRENTSTATE_STALLED_UPLOADING,
&KEY_TORRENTSTATE_CHECKING_RESUME_DATA,
&KEY_TORRENTSTATE_QUEUED_DOWNLOADING,
&KEY_TORRENTSTATE_QUEUED_UPLOADING,
&KEY_TORRENTSTATE_CHECKING_UPLOADING,
&KEY_TORRENTSTATE_CHECKING_DOWNLOADING,
&KEY_TORRENTSTATE_STOPPED_DOWNLOADING,
&KEY_TORRENTSTATE_STOPPED_UPLOADING,
&KEY_TORRENTSTATE_MOVING,
&KEY_TORRENTSTATE_MISSING_FILES,
&KEY_TORRENTSTATE_ERROR
};

namespace
{
using Utils::String::parseBool;
Expand Down Expand Up @@ -514,6 +557,37 @@ TorrentsController::TorrentsController(IApplication *app, QObject *parent)
connect(BitTorrent::Session::instance(), &BitTorrent::Session::metadataDownloaded, this, &TorrentsController::onMetadataDownloaded);
}

void TorrentsController::statsAction(){
using State = BitTorrent::TorrentState;

BitTorrent::Session* const session = BitTorrent::Session::instance();
const auto torrents = session->torrents();
constexpr int minState = static_cast<int>(State::Unknown);
constexpr int maxState = static_cast<int>(State::Error);

constexpr int numStates = maxState - minState + 1;
static_assert(
sizeof(TorrentStateKeyList) / sizeof(*TorrentStateKeyList)
==
numStates,
"TorrentStateKeyList: number of keys does not match TorrentState enum count"
);

QVector<int> stateCounts(numStates, 0);
for (const BitTorrent::Torrent* const torrent : torrents) {
const int stateIndex = static_cast<int>(torrent->state()) - minState;
if (stateIndex >= 0 && stateIndex < stateCounts.size()) {
++stateCounts[stateIndex];
}
}

QJsonObject res;
for (int idx = 0; idx < stateCounts.size(); ++idx) {
res[*TorrentStateKeyList[idx]] = stateCounts[idx];
}
setResult(res);
}

void TorrentsController::countAction()
{
setResult(QString::number(BitTorrent::Session::instance()->torrentsCount()));
Expand Down
1 change: 1 addition & 0 deletions src/webui/api/torrentscontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TorrentsController : public APIController
explicit TorrentsController(IApplication *app, QObject *parent = nullptr);

private slots:
void statsAction();
void countAction();
void infoAction();
void propertiesAction();
Expand Down