From 687e7a1343083336f443fb4ca550ec81fb93d63c Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Thu, 14 Nov 2013 23:56:13 +0400 Subject: [PATCH] Collect and save alltime UL/DL samples --- src/qtlibtorrent/qbtsession.cpp | 8 ++++++ src/qtlibtorrent/qbtsession.h | 2 ++ src/qtlibtorrent/torrentspeedmonitor.cpp | 35 +++++++++++++++++++++++- src/qtlibtorrent/torrentspeedmonitor.h | 10 +++++-- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index b42398fbb..808f54837 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -2778,6 +2778,14 @@ qlonglong QBtSession::getETA(const QString &hash) const return m_speedMonitor->getETA(hash); } +quint64 QBtSession::getAlltimeDL() const { + return m_speedMonitor->getAlltimeDL(); +} + +quint64 QBtSession::getAlltimeUL() const { + return m_speedMonitor->getAlltimeUL(); +} + void QBtSession::handleIPFilterParsed(int ruleCount) { addConsoleMessage(tr("Successfully parsed the provided IP filter: %1 rules were applied.", "%1 is a number").arg(ruleCount)); diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index e2b1fd30c..749b836b3 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -107,6 +107,8 @@ public: inline bool isLSDEnabled() const { return LSDEnabled; } inline bool isPexEnabled() const { return PeXEnabled; } inline bool isQueueingEnabled() const { return queueingEnabled; } + quint64 getAlltimeDL() const; + quint64 getAlltimeUL() const; public slots: QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); diff --git a/src/qtlibtorrent/torrentspeedmonitor.cpp b/src/qtlibtorrent/torrentspeedmonitor.cpp index 5b9d55cab..549949253 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.cpp +++ b/src/qtlibtorrent/torrentspeedmonitor.cpp @@ -35,6 +35,7 @@ #include "qbtsession.h" #include "misc.h" #include "torrentspeedmonitor.h" +#include "qinisettings.h" using namespace libtorrent; @@ -64,12 +65,14 @@ TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : { connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString))); connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle))); + loadStats(); } TorrentSpeedMonitor::~TorrentSpeedMonitor() { m_abort = true; m_abortCond.wakeOne(); wait(); + saveStats(); } void TorrentSpeedMonitor::run() @@ -77,6 +80,7 @@ void TorrentSpeedMonitor::run() do { m_mutex.lock(); getSamples(); + saveStats(); m_abortCond.wait(&m_mutex, 1000); m_mutex.unlock(); } while(!m_abort); @@ -153,6 +157,16 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const return (h.total_wanted() - h.total_wanted_done()) / speed_average.download; } +quint64 TorrentSpeedMonitor::getAlltimeDL() const { + QMutexLocker l(&m_mutex); + return alltimeDL; +} + +quint64 TorrentSpeedMonitor::getAlltimeUL() const { + QMutexLocker l(&m_mutex); + return alltimeUL; +} + void TorrentSpeedMonitor::getSamples() { const std::vector torrents = m_session->getSession()->get_torrents(); @@ -163,8 +177,27 @@ void TorrentSpeedMonitor::getSamples() try { torrent_status st = it->status(0x0); if (!st.paused) { - m_samples[misc::toQString(it->info_hash())].addSample(st.download_payload_rate, st.upload_payload_rate); + int up = st.upload_payload_rate; + int down = st.download_payload_rate; + m_samples[misc::toQString(it->info_hash())].addSample(down, up); + alltimeDL += down; + alltimeUL += up; } } catch(invalid_handle&) {} } } + +void TorrentSpeedMonitor::saveStats() const { + QIniSettings s; + QVariantHash v; + v.insert("AlltimeDL", alltimeDL); + v.insert("AlltimeUL", alltimeUL); + s.setValue("Stats/AllStats", v); +} + +void TorrentSpeedMonitor::loadStats() { + QIniSettings s; + QVariantHash v(s.value("Stats/AllStats", QVariantHash()).toHash()); + alltimeDL = v["AlltimeDL"].toULongLong(); + alltimeUL = v["AlltimeUL"].toULongLong(); +} diff --git a/src/qtlibtorrent/torrentspeedmonitor.h b/src/qtlibtorrent/torrentspeedmonitor.h index 4a29ac4e4..3a310a719 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.h +++ b/src/qtlibtorrent/torrentspeedmonitor.h @@ -49,26 +49,30 @@ public: explicit TorrentSpeedMonitor(QBtSession* session); ~TorrentSpeedMonitor(); qlonglong getETA(const QString &hash) const; + quint64 getAlltimeDL() const; + quint64 getAlltimeUL() const; protected: void run(); private: void getSamples(); + void saveStats() const; + void loadStats(); private slots: void removeSamples(const QString& hash); void removeSamples(const QTorrentHandle& h); -private: - static const int sampling_interval = 1000; // 1s - private: bool m_abort; QWaitCondition m_abortCond; QHash m_samples; mutable QMutex m_mutex; QBtSession *m_session; + // Will overflow at 15.9 EiB + quint64 alltimeUL; + quint64 alltimeDL; }; #endif // TORRENTSPEEDMONITOR_H