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"
|
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include <QTimer>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2022-03-04 14:24:14 +08:00
|
|
|
#include "base/global.h"
|
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
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
using namespace std::chrono_literals;
|
2015-04-19 18:17:47 +03:00
|
|
|
using namespace BitTorrent;
|
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
const qint64 SAVE_INTERVAL = std::chrono::milliseconds(15min).count();
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
Statistics::Statistics(Session *session)
|
|
|
|
: QObject(session)
|
|
|
|
, m_session(session)
|
|
|
|
{
|
|
|
|
load();
|
2022-06-18 17:32:04 +08:00
|
|
|
m_lastUpdateTimer.start();
|
|
|
|
|
|
|
|
auto *timer = new QTimer(this);
|
|
|
|
connect(timer, &QTimer::timeout, this, &Statistics::gather);
|
|
|
|
timer->start(60s);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Statistics::~Statistics()
|
|
|
|
{
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2022-04-02 16:31:59 +08:00
|
|
|
qint64 Statistics::getAlltimeDL() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
return m_alltimeDL + m_sessionDL;
|
|
|
|
}
|
|
|
|
|
2022-04-02 16:31:59 +08:00
|
|
|
qint64 Statistics::getAlltimeUL() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
return m_alltimeUL + m_sessionUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::gather()
|
|
|
|
{
|
2017-04-29 14:45:30 +03:00
|
|
|
const SessionStatus &ss = m_session->status();
|
2020-11-16 10:02:11 +03:00
|
|
|
if (ss.totalDownload > m_sessionDL)
|
|
|
|
{
|
2017-04-29 14:45:30 +03:00
|
|
|
m_sessionDL = ss.totalDownload;
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
if (ss.totalUpload > m_sessionUL)
|
|
|
|
{
|
2017-04-29 14:45:30 +03:00
|
|
|
m_sessionUL = ss.totalUpload;
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
if (m_lastUpdateTimer.hasExpired(SAVE_INTERVAL))
|
|
|
|
save();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::save() const
|
|
|
|
{
|
2022-06-18 17:32:04 +08:00
|
|
|
if (!m_dirty)
|
2015-04-19 18:17:47 +03:00
|
|
|
return;
|
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
const QVariantHash stats =
|
|
|
|
{
|
|
|
|
{u"AlltimeDL"_qs, (m_alltimeDL + m_sessionDL)},
|
|
|
|
{u"AlltimeUL"_qs, (m_alltimeUL + m_sessionUL)}
|
|
|
|
};
|
|
|
|
SettingsPtr settings = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs);
|
|
|
|
settings->setValue(u"Stats/AllStats"_qs, stats);
|
|
|
|
|
|
|
|
m_lastUpdateTimer.start();
|
2015-04-19 18:17:47 +03:00
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Statistics::load()
|
|
|
|
{
|
2022-03-26 11:53:50 +08:00
|
|
|
const SettingsPtr s = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs);
|
2022-03-04 14:24:14 +08:00
|
|
|
const QVariantHash v = s->value(u"Stats/AllStats"_qs).toHash();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2022-06-18 17:32:04 +08:00
|
|
|
m_alltimeDL = v[u"AlltimeDL"_qs].toLongLong();
|
|
|
|
m_alltimeUL = v[u"AlltimeUL"_qs].toLongLong();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|