Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / statsdialog.cpp
blob483e38499a97845e461ffc96faab50b1c3d1b101
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2013 Nick Tiskov <daymansmail@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "statsdialog.h"
31 #include <algorithm>
33 #include "base/bittorrent/cachestatus.h"
34 #include "base/bittorrent/session.h"
35 #include "base/bittorrent/sessionstatus.h"
36 #include "base/bittorrent/torrent.h"
37 #include "base/global.h"
38 #include "base/utils/misc.h"
39 #include "base/utils/string.h"
40 #include "ui_statsdialog.h"
41 #include "utils.h"
43 #define SETTINGS_KEY(name) u"StatisticsDialog/" name
45 StatsDialog::StatsDialog(QWidget *parent)
46 : QDialog(parent)
47 , m_ui(new Ui::StatsDialog)
48 , m_storeDialogSize(SETTINGS_KEY(u"Size"_s))
50 m_ui->setupUi(this);
52 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &StatsDialog::close);
54 update();
55 connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated
56 , this, &StatsDialog::update);
58 #ifdef QBT_USES_LIBTORRENT2
59 m_ui->labelCacheHitsText->hide();
60 m_ui->labelCacheHits->hide();
61 #endif
63 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
64 resize(dialogSize);
67 StatsDialog::~StatsDialog()
69 m_storeDialogSize = size();
70 delete m_ui;
73 void StatsDialog::update()
75 const BitTorrent::SessionStatus &ss = BitTorrent::Session::instance()->status();
76 const BitTorrent::CacheStatus &cs = BitTorrent::Session::instance()->cacheStatus();
78 // All-time DL/UL
79 const qint64 atd = ss.allTimeDownload;
80 const qint64 atu = ss.allTimeUpload;
81 m_ui->labelAlltimeDL->setText(Utils::Misc::friendlyUnit(atd));
82 m_ui->labelAlltimeUL->setText(Utils::Misc::friendlyUnit(atu));
83 // Total waste (this session)
84 m_ui->labelWaste->setText(Utils::Misc::friendlyUnit(ss.totalWasted));
85 // Global ratio
86 m_ui->labelGlobalRatio->setText(
87 ((atd > 0) && (atu > 0))
88 ? Utils::String::fromDouble(static_cast<qreal>(atu) / atd, 2)
89 : u"-"_s);
90 #ifndef QBT_USES_LIBTORRENT2
91 // Cache hits
92 const qreal readRatio = cs.readRatio;
93 m_ui->labelCacheHits->setText(u"%1%"_s.arg((readRatio > 0)
94 ? Utils::String::fromDouble((100 * readRatio), 2)
95 : u"0"_s));
96 #endif
97 // Buffers size
98 m_ui->labelTotalBuf->setText(Utils::Misc::friendlyUnit(cs.totalUsedBuffers * 16 * 1024));
99 // Disk overload (100%) equivalent
100 // From lt manual: disk_write_queue and disk_read_queue are the number of peers currently waiting on a disk write or disk read
101 // to complete before it receives or sends any more data on the socket. It's a metric of how disk bound you are.
103 m_ui->labelWriteStarve->setText(u"%1%"_s.arg(((ss.diskWriteQueue > 0) && (ss.peersCount > 0))
104 ? Utils::String::fromDouble((100. * ss.diskWriteQueue / ss.peersCount), 2)
105 : u"0"_s));
106 m_ui->labelReadStarve->setText(u"%1%"_s.arg(((ss.diskReadQueue > 0) && (ss.peersCount > 0))
107 ? Utils::String::fromDouble((100. * ss.diskReadQueue / ss.peersCount), 2)
108 : u"0"_s));
110 // Disk queues
111 m_ui->labelQueuedJobs->setText(QString::number(cs.jobQueueLength));
112 m_ui->labelJobsTime->setText(tr("%1 ms", "18 milliseconds").arg(cs.averageJobTime));
113 m_ui->labelQueuedBytes->setText(Utils::Misc::friendlyUnit(cs.queuedBytes));
115 // Total connected peers
116 m_ui->labelPeers->setText(QString::number(ss.peersCount));