Collect and save alltime UL/DL samples

This commit is contained in:
Nick Tiskov 2013-11-14 23:56:13 +04:00
parent d1921933f4
commit 687e7a1343
4 changed files with 51 additions and 4 deletions

View file

@ -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));

View file

@ -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);

View file

@ -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<torrent_handle> 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();
}

View file

@ -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<QString, SpeedSample> m_samples;
mutable QMutex m_mutex;
QBtSession *m_session;
// Will overflow at 15.9 EiB
quint64 alltimeUL;
quint64 alltimeDL;
};
#endif // TORRENTSPEEDMONITOR_H