2019-02-04 13:45:22 +08:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2016-05-03 21:45:06 +02:00
|
|
|
#include "statistics.h"
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QDateTime>
|
|
|
|
|
2017-09-07 03:00:04 +03:00
|
|
|
#include "base/bittorrent/session.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/bittorrent/sessionstatus.h"
|
2016-05-03 21:45:06 +02:00
|
|
|
#include "base/profile.h"
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
static const qint64 SAVE_INTERVAL = 15 * 60 * 1000;
|
|
|
|
|
|
|
|
using namespace BitTorrent;
|
|
|
|
|
|
|
|
Statistics::Statistics(Session *session)
|
|
|
|
: QObject(session)
|
|
|
|
, m_session(session)
|
|
|
|
, m_sessionUL(0)
|
|
|
|
, m_sessionDL(0)
|
|
|
|
, m_lastWrite(0)
|
|
|
|
, m_dirty(false)
|
|
|
|
{
|
|
|
|
load();
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(&m_timer, &QTimer::timeout, this, &Statistics::gather);
|
2015-04-19 18:17:47 +03:00
|
|
|
m_timer.start(60 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
Statistics::~Statistics()
|
|
|
|
{
|
|
|
|
if (m_dirty)
|
|
|
|
m_lastWrite = 0;
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Statistics::getAlltimeDL() const
|
|
|
|
{
|
|
|
|
return m_alltimeDL + m_sessionDL;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Statistics::getAlltimeUL() const
|
|
|
|
{
|
|
|
|
return m_alltimeUL + m_sessionUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::gather()
|
|
|
|
{
|
2017-04-29 14:45:30 +03:00
|
|
|
const SessionStatus &ss = m_session->status();
|
|
|
|
if (ss.totalDownload > m_sessionDL) {
|
|
|
|
m_sessionDL = ss.totalDownload;
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
2017-04-29 14:45:30 +03:00
|
|
|
if (ss.totalUpload > m_sessionUL) {
|
|
|
|
m_sessionUL = ss.totalUpload;
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::save() const
|
|
|
|
{
|
2019-02-09 17:40:14 +02:00
|
|
|
const qint64 now = QDateTime::currentMSecsSinceEpoch();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
if (!m_dirty || ((now - m_lastWrite) < SAVE_INTERVAL))
|
|
|
|
return;
|
|
|
|
|
2016-05-03 21:45:06 +02:00
|
|
|
SettingsPtr s = Profile::instance().applicationSettings(QLatin1String("qBittorrent-data"));
|
2015-04-19 18:17:47 +03:00
|
|
|
QVariantHash v;
|
|
|
|
v.insert("AlltimeDL", m_alltimeDL + m_sessionDL);
|
|
|
|
v.insert("AlltimeUL", m_alltimeUL + m_sessionUL);
|
2016-05-03 21:45:06 +02:00
|
|
|
s->setValue("Stats/AllStats", v);
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = false;
|
|
|
|
m_lastWrite = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::load()
|
|
|
|
{
|
2019-02-09 17:40:14 +02:00
|
|
|
const SettingsPtr s = Profile::instance().applicationSettings(QLatin1String("qBittorrent-data"));
|
|
|
|
const QVariantHash v = s->value("Stats/AllStats").toHash();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
m_alltimeDL = v["AlltimeDL"].toULongLong();
|
|
|
|
m_alltimeUL = v["AlltimeUL"].toULongLong();
|
|
|
|
}
|