Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
trafficgraphwidget.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <interfaces/node.h>
7#include <qt/clientmodel.h>
8
9#include <QPainter>
10#include <QPainterPath>
11#include <QColor>
12#include <QTimer>
13
14#include <chrono>
15#include <cmath>
16
17#define DESIRED_SAMPLES 800
18
19#define XMARGIN 10
20#define YMARGIN 10
21
23 : QWidget(parent),
24 vSamplesIn(),
25 vSamplesOut()
26{
27 timer = new QTimer(this);
28 connect(timer, &QTimer::timeout, this, &TrafficGraphWidget::updateRates);
29}
30
32{
33 clientModel = model;
34 if(model) {
37 }
38}
39
41{
42 int sampleCount = samples.size();
43 if(sampleCount > 0) {
44 int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
45 int x = XMARGIN + w;
46 path.moveTo(x, YMARGIN + h);
47 for(int i = 0; i < sampleCount; ++i) {
48 x = XMARGIN + w - w * i / DESIRED_SAMPLES;
49 int y = YMARGIN + h - (int)(h * samples.at(i) / fMax);
50 path.lineTo(x, y);
51 }
52 path.lineTo(x, YMARGIN + h);
53 }
54}
55
57{
58 QPainter painter(this);
59 painter.fillRect(rect(), Qt::black);
60
61 if(fMax <= 0.0f) return;
62
63 QColor axisCol(Qt::gray);
64 int h = height() - YMARGIN * 2;
65 painter.setPen(axisCol);
66 painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
67
68 // decide what order of magnitude we are
69 int base = std::floor(std::log10(fMax));
70 float val = std::pow(10.0f, base);
71
72 const QString units = tr("kB/s");
73 const float yMarginText = 2.0;
74
75 // draw lines
76 painter.setPen(axisCol);
77 painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
78 for(float y = val; y < fMax; y += val) {
79 int yy = YMARGIN + h - h * y / fMax;
80 painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
81 }
82 // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
83 if(fMax / val <= 3.0f) {
84 axisCol = axisCol.darker();
85 val = pow(10.0f, base - 1);
86 painter.setPen(axisCol);
87 painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
88 int count = 1;
89 for(float y = val; y < fMax; y += val, count++) {
90 // don't overwrite lines drawn above
91 if(count % 10 == 0)
92 continue;
93 int yy = YMARGIN + h - h * y / fMax;
94 painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
95 }
96 }
97
98 painter.setRenderHint(QPainter::Antialiasing);
99 if(!vSamplesIn.empty()) {
102 painter.fillPath(p, QColor(0, 255, 0, 128));
103 painter.setPen(Qt::green);
104 painter.drawPath(p);
105 }
106 if(!vSamplesOut.empty()) {
109 painter.fillPath(p, QColor(255, 0, 0, 128));
110 painter.setPen(Qt::red);
111 painter.drawPath(p);
112 }
113}
114
116{
117 if(!clientModel) return;
118
121 float in_rate_kilobytes_per_sec = static_cast<float>(bytesIn - nLastBytesIn) / timer->interval();
122 float out_rate_kilobytes_per_sec = static_cast<float>(bytesOut - nLastBytesOut) / timer->interval();
127
128 while(vSamplesIn.size() > DESIRED_SAMPLES) {
129 vSamplesIn.pop_back();
130 }
131 while(vSamplesOut.size() > DESIRED_SAMPLES) {
132 vSamplesOut.pop_back();
133 }
134
135 float tmax = 0.0f;
136 for (const float f : vSamplesIn) {
137 if(f > tmax) tmax = f;
138 }
139 for (const float f : vSamplesOut) {
140 if(f > tmax) tmax = f;
141 }
142 fMax = tmax;
143 update();
144}
145
147{
149 const auto msecs_per_sample{std::chrono::duration_cast<std::chrono::milliseconds>(m_range) / DESIRED_SAMPLES};
150 timer->stop();
151 timer->setInterval(msecs_per_sample);
152
153 clear();
154}
155
157{
158 timer->stop();
159
160 vSamplesOut.clear();
161 vSamplesIn.clear();
162 fMax = 0.0f;
163
164 if(clientModel) {
167 }
168 timer->start();
169}
Model for Bitcoin network client.
Definition clientmodel.h:57
interfaces::Node & node() const
Definition clientmodel.h:66
std::chrono::minutes m_range
void paintEvent(QPaintEvent *) override
ClientModel * clientModel
TrafficGraphWidget(QWidget *parent=nullptr)
void setClientModel(ClientModel *model)
QQueue< float > vSamplesOut
void paintPath(QPainterPath &path, QQueue< float > &samples)
void setGraphRange(std::chrono::minutes new_range)
QQueue< float > vSamplesIn
virtual int64_t getTotalBytesRecv()=0
Get total bytes recv.
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
static int count
#define YMARGIN
#define XMARGIN
#define DESIRED_SAMPLES
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82