Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

trafficgraphdata.cpp
Go to the documentation of this file.
1 #include <qt/trafficgraphdata.h>
2 
3 const int TrafficGraphData::RangeMinutes[] = {5,10,15,30,60,120,180,360,720,1440};
16  } ;
17 
20 
22  :currentGraphRange(range),
23  currentSampleCounter(0),
24  nLastBytesIn(0),
25  nLastBytesOut(0)
26 {
27 }
28 
30 {
31  SampleQueue& queue = sampleMap[range];
32  if (queue.size() > DesiredQueueSizes[range]) {
33  sampleStash[range].push_front(queue.at(DesiredQueueSizes[range]));
34  }
35 }
36 
38 {
39  SampleQueue& queue = sampleMap[range];
40  if (queue.size() == DesiredQueueSizes[range] + 2) {
41  update(nextRange, queue.takeLast() + queue.takeLast());
42  }
43 }
44 
46 {
47  SampleQueue& stashQueue = sampleStash[range];
48  if (stashQueue.size() == 3) {
49  update(nextRange, stashQueue.takeLast() + stashQueue.takeLast() + stashQueue.takeLast());
50  }
51 }
52 
53 void TrafficGraphData::setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut)
54 {
55  this->nLastBytesIn = nLastBytesIn;
56  this->nLastBytesOut = nLastBytesOut;
57 }
58 
59 bool TrafficGraphData::update(quint64 totalBytesRecv, quint64 totalBytesSent)
60 {
61  float inRate = totalBytesRecv - nLastBytesIn;
62  float outRate = totalBytesSent - nLastBytesOut;
63  nLastBytesIn = totalBytesRecv;
64  nLastBytesOut = totalBytesSent;
65  return update(TrafficSample(inRate, outRate));
66 }
67 
68 bool TrafficGraphData::update(const TrafficSample& trafficSample)
69 {
70  update(Range_5m, trafficSample);
71 
73 
76  return true;
77  }
78  return false;
79 }
80 
81 void TrafficGraphData::update(GraphRange range, const TrafficSample& trafficSample)
82 {
83  SampleQueue& queue = sampleMap[range];
84  queue.push_front(trafficSample);
85 
86  switch(range) {
87  case Range_5m: {
91  return;
92  }
93  case Range_15m: {
95  return;
96  }
97  case Range_30m: {
99  return;
100  }
101  case Range_1h: {
105  return;
106  }
107  case Range_3h: {
109  return;
110  }
111  case Range_6h: {
113  return;
114  }
115  case Range_12h: {
117  return;
118  }
119  default: {
120  if (queue.size() > DesiredQueueSizes[range]) {
121  queue.removeLast();
122  }
123  return;
124  }
125  }
126 }
127 
129 {
130  currentGraphRange = newRange;
132 }
133 
135 {
136  SampleQueue result;
137  int i = rangeQueue.size() - 1;
138 
139  while (i - 1 >= 0) {
140  result.push_front(rangeQueue.at(i) + rangeQueue.at(i - 1));
141  i -= 2;
142  }
143  return result;
144 }
145 
147 {
148  SampleQueue result;
149  int lastUnusedSample = std::min(rangeQueue.size() - 1, DESIRED_DATA_SAMPLES - 1);
150 
151  // use stash first
152  SampleQueue& stashQueue = sampleStash[range];
153  TrafficSample sum(0, 0);
154  for (int i = 0; i < stashQueue.size(); ++i) {
155  sum += stashQueue.at(i);
156  }
157  int toFullSample = 3 - stashQueue.size();
158  if (toFullSample > rangeQueue.size())
159  return result;
160 
161  // append to stash data to create whole sample
162  for (int i = 0; i < toFullSample; ++i) {
163  sum += rangeQueue.at(lastUnusedSample);
164  --lastUnusedSample;
165  }
166  result.push_front(sum);
167 
168  while (lastUnusedSample - 2 >= 0) {
169  result.push_front(rangeQueue.at(lastUnusedSample) + rangeQueue.at(lastUnusedSample - 1) + rangeQueue.at(lastUnusedSample - 2));
170  lastUnusedSample -= 3;
171  }
172  return result;
173 }
174 
176 {
177  switch(range) {
178  case Range_5m: {
179  return sampleMap[Range_5m];
180  }
181  case Range_10m: {
183  queue.append(sampleMap[Range_10m]);
184  return queue;
185  }
186  case Range_15m: {
188  queue.append(sampleMap[Range_15m]);
189  return queue;
190  }
191  case Range_30m: {
193  queue.append(sampleMap[Range_30m]);
194  return queue;
195  }
196  case Range_1h: {
198  queue.append(sampleMap[Range_1h]);
199  return queue;
200  }
201  case Range_2h: {
203  queue.append(sampleMap[Range_2h]);
204  return queue;
205  }
206  case Range_3h: {
208  queue.append(sampleMap[Range_3h]);
209  return queue;
210  }
211  case Range_6h: {
213  queue.append(sampleMap[Range_6h]);
214  return queue;
215  }
216  case Range_12h: {
218  queue.append(sampleMap[Range_12h]);
219  return queue;
220  }
221  case Range_24h: {
223  queue.append(sampleMap[Range_24h]);
224  return queue;
225  }
226  default: {
227  return SampleQueue();
228  }
229  }
230 }
231 
233 {
234  SampleQueue newQueue;
235  getRangeQueue(currentGraphRange).mid(0, DESIRED_DATA_SAMPLES).swap(newQueue);
236  return newQueue;
237 }
238 
240 {
241  // to base range
243  // to B/s
244  result = result * 1000 / SMALLEST_SAMPLE_PERIOD;
245  // to KB/s
246  result = result / 1024;
247 
248  return result;
249 
250 }
251 
253 {
254  SampleQueue newQueue;
255  getRangeQueue(currentGraphRange).mid(0, DESIRED_DATA_SAMPLES).swap(newQueue);
256  for (auto& sample : newQueue){
257  sample.in = convertSampleToBandwidth(sample.in);
258  sample.out = convertSampleToBandwidth(sample.out);
259  }
260  return newQueue;
261 }
262 
264 {
265  sampleMap.clear();
266  sampleMap.clear();
268  nLastBytesIn = 0;
269  nLastBytesOut = 0;
270 }
TrafficGraphData(GraphRange range)
static const int DesiredQueueSizes[]
SampleQueue sumEach2Samples(const SampleQueue &rangeQueue)
void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut)
void tryUpdateNextWithLast3Samples(GraphRange range, GraphRange nextRange)
SampleQueue getRangeQueue(GraphRange range)
SampleQueueMap sampleStash
QQueue< TrafficSample > SampleQueue
GraphRange currentGraphRange
static const int SMALLEST_SAMPLE_PERIOD
static const int DESIRED_DATA_SAMPLES
void tryUpdateNextWithLast2Samples(GraphRange range, GraphRange nextRange)
volatile double sum
Definition: Examples.cpp:23
SampleQueue getCurrentRangeQueue()
float convertSampleToBandwidth(float dataAmount)
static const int RangeMinutes[]
void tryAddingSampleToStash(GraphRange range)
bool update(const TrafficSample &trafficSample)
void switchRange(GraphRange newRange)
SampleQueue getCurrentRangeQueueWithAverageBandwidth()
SampleQueue sumEach3Samples(const SampleQueue &rangeQueue, GraphRange range)
SampleQueueMap sampleMap
Released under the MIT license