mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-02-03 02:14:16 +00:00
extract torrent statistics from torrent speed monitor to separate file
This commit is contained in:
parent
b50d7331c7
commit
6f38616193
@ -39,6 +39,7 @@
|
||||
#include "smtp.h"
|
||||
#include "filesystemwatcher.h"
|
||||
#include "torrentspeedmonitor.h"
|
||||
#include "torrentstatistics.h"
|
||||
#include "qbtsession.h"
|
||||
#include "misc.h"
|
||||
#include "fs_utils.h"
|
||||
@ -158,6 +159,7 @@ QBtSession::QBtSession()
|
||||
// Torrent speed monitor
|
||||
m_speedMonitor = new TorrentSpeedMonitor(this);
|
||||
m_speedMonitor->start();
|
||||
m_torrentStatistics = new TorrentStatistics(this, this);
|
||||
// To download from urls
|
||||
downloader = new DownloadThread(this);
|
||||
connect(downloader, SIGNAL(downloadFinished(QString, QString)), SLOT(processDownloadedFile(QString, QString)));
|
||||
@ -190,6 +192,7 @@ QBtSession::~QBtSession() {
|
||||
if (httpServer)
|
||||
delete httpServer;
|
||||
delete m_alertDispatcher;
|
||||
delete m_torrentStatistics;
|
||||
qDebug("Deleting the session");
|
||||
delete s;
|
||||
qDebug("BTSession destructor OUT");
|
||||
@ -2815,11 +2818,11 @@ qlonglong QBtSession::getETA(const QString &hash, const libtorrent::torrent_stat
|
||||
}
|
||||
|
||||
quint64 QBtSession::getAlltimeDL() const {
|
||||
return m_speedMonitor->getAlltimeDL();
|
||||
return m_torrentStatistics->getAlltimeDL();
|
||||
}
|
||||
|
||||
quint64 QBtSession::getAlltimeUL() const {
|
||||
return m_speedMonitor->getAlltimeUL();
|
||||
return m_torrentStatistics->getAlltimeUL();
|
||||
}
|
||||
|
||||
void QBtSession::postTorrentUpdate() {
|
||||
|
@ -61,6 +61,7 @@ class HttpServer;
|
||||
class BandwidthScheduler;
|
||||
class ScanFoldersModel;
|
||||
class TorrentSpeedMonitor;
|
||||
class TorrentStatistics;
|
||||
class DNSUpdater;
|
||||
|
||||
const int MAX_LOG_MESSAGES = 1000;
|
||||
@ -291,6 +292,7 @@ private:
|
||||
// DynDNS
|
||||
DNSUpdater *m_dynDNSUpdater;
|
||||
QAlertDispatcher* m_alertDispatcher;
|
||||
TorrentStatistics* m_torrentStatistics;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -6,12 +6,14 @@ HEADERS += $$PWD/qbtsession.h \
|
||||
$$PWD/trackerinfos.h \
|
||||
$$PWD/torrentspeedmonitor.h \
|
||||
$$PWD/filterparserthread.h \
|
||||
$$PWD/alertdispatcher.h
|
||||
$$PWD/alertdispatcher.h \
|
||||
$$PWD/torrentstatistics.h
|
||||
|
||||
SOURCES += $$PWD/qbtsession.cpp \
|
||||
$$PWD/qtorrenthandle.cpp \
|
||||
$$PWD/torrentspeedmonitor.cpp \
|
||||
$$PWD/alertdispatcher.cpp
|
||||
$$PWD/alertdispatcher.cpp \
|
||||
$$PWD/torrentstatistics.cpp
|
||||
|
||||
!contains(DEFINES, DISABLE_GUI) {
|
||||
HEADERS += $$PWD/torrentmodel.h \
|
||||
|
@ -62,21 +62,16 @@ private:
|
||||
};
|
||||
|
||||
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) :
|
||||
QThread(session), m_abort(false), m_session(session),
|
||||
sessionUL(0), sessionDL(0), lastWrite(0), dirty(false)
|
||||
QThread(session), m_abort(false), m_session(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();
|
||||
if (dirty)
|
||||
lastWrite = 0;
|
||||
saveStats();
|
||||
}
|
||||
|
||||
void TorrentSpeedMonitor::run()
|
||||
@ -84,7 +79,6 @@ void TorrentSpeedMonitor::run()
|
||||
do {
|
||||
m_mutex.lock();
|
||||
getSamples();
|
||||
saveStats();
|
||||
m_abortCond.wait(&m_mutex, 1000);
|
||||
m_mutex.unlock();
|
||||
} while(!m_abort);
|
||||
@ -161,16 +155,6 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::tor
|
||||
return (status.total_wanted - status.total_wanted_done) / speed_average.download;
|
||||
}
|
||||
|
||||
quint64 TorrentSpeedMonitor::getAlltimeDL() const {
|
||||
QMutexLocker l(&m_mutex);
|
||||
return alltimeDL + sessionDL;
|
||||
}
|
||||
|
||||
quint64 TorrentSpeedMonitor::getAlltimeUL() const {
|
||||
QMutexLocker l(&m_mutex);
|
||||
return alltimeUL + sessionUL;
|
||||
}
|
||||
|
||||
void TorrentSpeedMonitor::getSamples()
|
||||
{
|
||||
const std::vector<torrent_handle> torrents = m_session->getSession()->get_torrents();
|
||||
@ -187,60 +171,4 @@ void TorrentSpeedMonitor::getSamples()
|
||||
}
|
||||
} catch(invalid_handle&) {}
|
||||
}
|
||||
libtorrent::session_status ss = m_session->getSessionStatus();
|
||||
if (ss.total_download > sessionDL) {
|
||||
sessionDL = ss.total_download;
|
||||
dirty = true;
|
||||
}
|
||||
if (ss.total_upload > sessionUL) {
|
||||
sessionUL = ss.total_upload;
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void TorrentSpeedMonitor::saveStats() const {
|
||||
if (!(dirty && (QDateTime::currentMSecsSinceEpoch() - lastWrite >= 15*60*1000) ))
|
||||
return;
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
QVariantHash v;
|
||||
v.insert("AlltimeDL", alltimeDL + sessionDL);
|
||||
v.insert("AlltimeUL", alltimeUL + sessionUL);
|
||||
s.setValue("Stats/AllStats", v);
|
||||
dirty = false;
|
||||
lastWrite = QDateTime::currentMSecsSinceEpoch();
|
||||
}
|
||||
|
||||
void TorrentSpeedMonitor::loadStats() {
|
||||
// Temp code. Versions v3.1.4 and v3.1.5 saved the data in the qbittorrent.ini file.
|
||||
// This code reads the data from there, writes it to the new file, and removes the keys
|
||||
// from the old file. This code should be removed after some time has passed.
|
||||
// e.g. When we reach v3.3.0
|
||||
QIniSettings s_old;
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
QVariantHash v;
|
||||
|
||||
// Let's test if the qbittorrent.ini holds the key
|
||||
if (s_old.contains("Stats/AllStats")) {
|
||||
v = s_old.value("Stats/AllStats").toHash();
|
||||
dirty = true;
|
||||
|
||||
// If the user has used qbt > 3.1.5 and then reinstalled/used
|
||||
// qbt < 3.1.6, there will be stats in qbittorrent-data.ini too
|
||||
// so we need to merge those 2.
|
||||
if (s.contains("Stats/AllStats")) {
|
||||
QVariantHash tmp = s.value("Stats/AllStats").toHash();
|
||||
v["AlltimeDL"] = v["AlltimeDL"].toULongLong() + tmp["AlltimeDL"].toULongLong();
|
||||
v["AlltimeUL"] = v["AlltimeUL"].toULongLong() + tmp["AlltimeUL"].toULongLong();
|
||||
}
|
||||
}
|
||||
else
|
||||
v = s.value("Stats/AllStats").toHash();
|
||||
|
||||
alltimeDL = v["AlltimeDL"].toULongLong();
|
||||
alltimeUL = v["AlltimeUL"].toULongLong();
|
||||
|
||||
if (dirty) {
|
||||
saveStats();
|
||||
s_old.remove("Stats/AllStats");
|
||||
}
|
||||
}
|
||||
|
@ -49,16 +49,12 @@ public:
|
||||
explicit TorrentSpeedMonitor(QBtSession* session);
|
||||
~TorrentSpeedMonitor();
|
||||
qlonglong getETA(const QString &hash, const libtorrent::torrent_status &status) 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);
|
||||
@ -70,13 +66,6 @@ private:
|
||||
QHash<QString, SpeedSample> m_samples;
|
||||
mutable QMutex m_mutex;
|
||||
QBtSession *m_session;
|
||||
// Will overflow at 15.9 EiB
|
||||
quint64 alltimeUL;
|
||||
quint64 alltimeDL;
|
||||
qint64 sessionUL;
|
||||
qint64 sessionDL;
|
||||
mutable qint64 lastWrite;
|
||||
mutable bool dirty;
|
||||
};
|
||||
|
||||
#endif // TORRENTSPEEDMONITOR_H
|
||||
|
95
src/qtlibtorrent/torrentstatistics.cpp
Normal file
95
src/qtlibtorrent/torrentstatistics.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
#include "torrentstatistics.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
#include <libtorrent/session.hpp>
|
||||
|
||||
#include "qbtsession.h"
|
||||
#include "qinisettings.h"
|
||||
|
||||
TorrentStatistics::TorrentStatistics(QBtSession* session, QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_session(session)
|
||||
, m_sessionUL(0)
|
||||
, m_sessionDL(0)
|
||||
, m_lastWrite(0)
|
||||
, m_dirty(false) {
|
||||
loadStats();
|
||||
connect(&m_timer, SIGNAL(timeout()), this, SLOT(gatherStats()));
|
||||
m_timer.start(60 * 1000);
|
||||
}
|
||||
|
||||
TorrentStatistics::~TorrentStatistics() {
|
||||
if (m_dirty)
|
||||
m_lastWrite = 0;
|
||||
saveStats();
|
||||
}
|
||||
|
||||
quint64 TorrentStatistics::getAlltimeDL() const {
|
||||
return m_alltimeDL + m_sessionDL;
|
||||
}
|
||||
|
||||
quint64 TorrentStatistics::getAlltimeUL() const {
|
||||
return m_alltimeUL + m_sessionUL;
|
||||
}
|
||||
|
||||
void TorrentStatistics::gatherStats() {
|
||||
libtorrent::session_status ss = m_session->getSessionStatus();
|
||||
if (ss.total_download > m_sessionDL) {
|
||||
m_sessionDL = ss.total_download;
|
||||
m_dirty = true;
|
||||
}
|
||||
if (ss.total_upload > m_sessionUL) {
|
||||
m_sessionUL = ss.total_upload;
|
||||
m_dirty = true;
|
||||
}
|
||||
|
||||
saveStats();
|
||||
}
|
||||
|
||||
void TorrentStatistics::saveStats() const {
|
||||
if (!(m_dirty && (QDateTime::currentMSecsSinceEpoch() - m_lastWrite >= 15*60*1000) ))
|
||||
return;
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
QVariantHash v;
|
||||
v.insert("AlltimeDL", m_alltimeDL + m_sessionDL);
|
||||
v.insert("AlltimeUL", m_alltimeUL + m_sessionUL);
|
||||
s.setValue("Stats/AllStats", v);
|
||||
m_dirty = false;
|
||||
m_lastWrite = QDateTime::currentMSecsSinceEpoch();
|
||||
}
|
||||
|
||||
void TorrentStatistics::loadStats() {
|
||||
// Temp code. Versions v3.1.4 and v3.1.5 saved the data in the qbittorrent.ini file.
|
||||
// This code reads the data from there, writes it to the new file, and removes the keys
|
||||
// from the old file. This code should be removed after some time has passed.
|
||||
// e.g. When we reach v3.3.0
|
||||
QIniSettings s_old;
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
QVariantHash v;
|
||||
|
||||
// Let's test if the qbittorrent.ini holds the key
|
||||
if (s_old.contains("Stats/AllStats")) {
|
||||
v = s_old.value("Stats/AllStats").toHash();
|
||||
m_dirty = true;
|
||||
|
||||
// If the user has used qbt > 3.1.5 and then reinstalled/used
|
||||
// qbt < 3.1.6, there will be stats in qbittorrent-data.ini too
|
||||
// so we need to merge those 2.
|
||||
if (s.contains("Stats/AllStats")) {
|
||||
QVariantHash tmp = s.value("Stats/AllStats").toHash();
|
||||
v["AlltimeDL"] = v["AlltimeDL"].toULongLong() + tmp["AlltimeDL"].toULongLong();
|
||||
v["AlltimeUL"] = v["AlltimeUL"].toULongLong() + tmp["AlltimeUL"].toULongLong();
|
||||
}
|
||||
}
|
||||
else
|
||||
v = s.value("Stats/AllStats").toHash();
|
||||
|
||||
m_alltimeDL = v["AlltimeDL"].toULongLong();
|
||||
m_alltimeUL = v["AlltimeUL"].toULongLong();
|
||||
|
||||
if (m_dirty) {
|
||||
saveStats();
|
||||
s_old.remove("Stats/AllStats");
|
||||
}
|
||||
}
|
41
src/qtlibtorrent/torrentstatistics.h
Normal file
41
src/qtlibtorrent/torrentstatistics.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef TORRENTSTATISTICS_H
|
||||
#define TORRENTSTATISTICS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
|
||||
class QBtSession;
|
||||
|
||||
class TorrentStatistics : QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY(TorrentStatistics)
|
||||
|
||||
public:
|
||||
TorrentStatistics(QBtSession* session, QObject* parent = 0);
|
||||
~TorrentStatistics();
|
||||
|
||||
quint64 getAlltimeDL() const;
|
||||
quint64 getAlltimeUL() const;
|
||||
|
||||
private slots:
|
||||
void gatherStats();
|
||||
|
||||
private:
|
||||
void saveStats() const;
|
||||
void loadStats();
|
||||
|
||||
private:
|
||||
QBtSession* m_session;
|
||||
// Will overflow at 15.9 EiB
|
||||
quint64 m_alltimeUL;
|
||||
quint64 m_alltimeDL;
|
||||
qint64 m_sessionUL;
|
||||
qint64 m_sessionDL;
|
||||
mutable qint64 m_lastWrite;
|
||||
mutable bool m_dirty;
|
||||
|
||||
QTimer m_timer;
|
||||
};
|
||||
|
||||
#endif // TORRENTSTATISTICS_H
|
Loading…
x
Reference in New Issue
Block a user