From 4d3672f8944e461f06720f4df89fa9bd34c8a57d Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Tue, 21 Jan 2014 00:10:45 +0200 Subject: [PATCH] Save statistics every 15min and only if there was new traffic. Fixes #1288 and partially #1272. --- src/qtlibtorrent/torrentspeedmonitor.cpp | 46 ++++++++++++++++++++---- src/qtlibtorrent/torrentspeedmonitor.h | 6 ++-- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/qtlibtorrent/torrentspeedmonitor.cpp b/src/qtlibtorrent/torrentspeedmonitor.cpp index e92e65b94..41f6e7c9b 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.cpp +++ b/src/qtlibtorrent/torrentspeedmonitor.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include "qbtsession.h" @@ -61,7 +62,8 @@ private: }; TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : - QThread(session), m_abort(false), m_session(session) + QThread(session), m_abort(false), m_session(session), + sessionUL(0), sessionDL(0), lastWrite(0), dirty(false) { connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString))); connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle))); @@ -72,6 +74,8 @@ TorrentSpeedMonitor::~TorrentSpeedMonitor() { m_abort = true; m_abortCond.wakeOne(); wait(); + dirty = true; + lastWrite = 0; saveStats(); } @@ -184,22 +188,50 @@ void TorrentSpeedMonitor::getSamples() } catch(invalid_handle&) {} } libtorrent::session_status ss = m_session->getSessionStatus(); - sessionDL = ss.total_download; - sessionUL = ss.total_upload; + 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 { - QIniSettings s; + 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() { - QIniSettings s; - QVariantHash v(s.value("Stats/AllStats", QVariantHash()).toHash()); + // 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; + } + else + v = s.value("Stats/AllStats").toHash(); + alltimeDL = v["AlltimeDL"].toULongLong(); alltimeUL = v["AlltimeUL"].toULongLong(); - sessionDL = sessionUL = 0; + + if (dirty) { + saveStats(); + s_old.remove("Stats/AllStats"); + } } diff --git a/src/qtlibtorrent/torrentspeedmonitor.h b/src/qtlibtorrent/torrentspeedmonitor.h index 134d18e88..0d71d64f6 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.h +++ b/src/qtlibtorrent/torrentspeedmonitor.h @@ -73,8 +73,10 @@ private: // Will overflow at 15.9 EiB quint64 alltimeUL; quint64 alltimeDL; - quint64 sessionUL; - quint64 sessionDL; + qint64 sessionUL; + qint64 sessionDL; + mutable qint64 lastWrite; + mutable bool dirty; }; #endif // TORRENTSPEEDMONITOR_H