neobytes/src/qt/trafficgraphdata.h
krychlicki 18c83f58e3 Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click

* set default sample height

set default sample height so after clearing traffic graph have some
scale

* reduce available traffic graph ranges, add optimized graph data storage

reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class

* code formatting corrections
2017-05-28 16:49:34 +03:00

91 lines
2.2 KiB
C++

#ifndef TRAFFICGRAPHDATA_H
#define TRAFFICGRAPHDATA_H
#include <QHash>
#include <QQueue>
struct TrafficSample
{
float in;
float out;
TrafficSample(float in, float out)
{
this->in = in;
this->out = out;
}
TrafficSample operator+(const TrafficSample& other) const
{
return TrafficSample(this->in + other.in, this->out + other.out);
}
TrafficSample& operator+=(const TrafficSample& rhs)
{
this->in += rhs.in;
this->out += rhs.out;
return *this;
}
};
class TrafficGraphData
{
public:
enum GraphRange
{
Range_5m,
Range_10m,
Range_15m,
Range_30m,
Range_1h,
Range_2h,
Range_3h,
Range_6h,
Range_12h,
Range_24h,
};
static const int RangeMinutes[];
static const int DESIRED_DATA_SAMPLES;
static const int SMALLEST_SAMPLE_PERIOD;
typedef QQueue<TrafficSample> SampleQueue;
typedef QHash<GraphRange,SampleQueue> SampleQueueMap;
TrafficGraphData(GraphRange range);
bool update(const TrafficSample& trafficSample);
bool update(quint64 totalBytesRecv, quint64 totalBytesSent);
void switchRange(GraphRange newRange);
SampleQueue getRangeQueue(GraphRange range);
SampleQueue getCurrentRangeQueue();
void clear();
void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut);
private:
static const int DesiredQueueSizes[];
SampleQueueMap sampleMap;
SampleQueueMap sampleStash;
GraphRange currentGraphRange;
int currentSampleCounter;
quint64 nLastBytesIn;
quint64 nLastBytesOut;
void update(GraphRange range, const TrafficSample &trafficSample);
void tryAddingSampleToStash(GraphRange range);
void tryUpdateNextWithLast2Samples(GraphRange range, GraphRange nextRange);
void tryUpdateNextWithLast3Samples(GraphRange range, GraphRange nextRange);
SampleQueue sumEach2Samples(const SampleQueue &rangeQueue);
SampleQueue sumEach3Samples(const SampleQueue &rangeQueue, GraphRange range);
TrafficGraphData(const TrafficGraphData& that);
TrafficGraphData& operator=(TrafficGraphData const&);
};
#endif // TRAFFICGRAPHDATA_H