Repository: dogecoin/dogecoin
File: src/qt/trafficgraphwidget.cpp (C++)

Code before:
		//MLUMIN 05_22: Timer to take time-based regular samples based on a period established as SAMPLE_RATE_MS globally
        timer->start(SAMPLE_RATE_MS);
    }
}

int TrafficGraphWidget::getGraphRangeMins() const
{
    return nMins;
}

//MLUMIN 05_22: Resampling function neccessary for re-fitting all acquired data samples into displayed visual sample canvas/view.
float TrafficGraphWidget::resampleSamples(const QVector<float> &source, QVector<float> &dest) const
{
    float stride = (float)nSamplesWindowSize / dest.size();
    float numSamplesToCombine = stride;
    float maxSample = 0.0f;
    float lastSample = 0.0f;

    size_t currentSampleIndex = nSamplesTailIndex;
    for (size_t destIndex = 0; destIndex < dest.size(); destIndex++) {
        int numSamplesToRead = (int)numSamplesToCombine;

        float currentSample = 0.0f;

        for (size_t i = 0; i < numSamplesToRead; ++i) {
            currentSample = std::max(currentSample, source[currentSampleIndex]);
            maxSample = std::max(maxSample, currentSample);

            currentSampleIndex++;
            if (currentSampleIndex >= source.size()) {
                currentSampleIndex = 0;
            }
        }

        // We seem to be trying to upsample. just use the previous sample.
        if (numSamplesToRead == 0) {
            currentSample = lastSample;
        }

        dest[destIndex] = currentSample;
        lastSample = currentSample;

        numSamplesToCombine -= (int)numSamplesToCombine;
        numSamplesToCombine += stride;
    }

    return maxSample;
}

//MLUMIN 05_22: Decouple desired drawn samples from total acquired samples
void TrafficGraphWidget::paintPath(QPainterPath &path, const QVector<float> &samples)

Review comment (suggestion):
I'm not sure that `max` is the correct aggregation for what we're displaying in the graph. This calculates the maximum rate observed for a single sample within the series that we're aggregating, but I think if we're showing traffic, we want to show the average rate of the series, not its peak. This makes a huge difference when aggregating larger sets. 

Perhaps it's an idea to just add all `source[currentSampleIndex]` inside the loop and then divide that by `numSamplesToCombine`?

What do we want to show in the graph when there is a short peak in usage within the samples we're aggregating?

```
max( 1, 1, 101, 1, 1) = 101
avg( 1, 1, 101, 1, 1) = 21
```

Code after:
    if(model) {
        nTimespanLastBytesIn = model->getTotalBytesRecv();
        nTimespanLastBytesOut = model->getTotalBytesSent();
        nBaseLastBytesIn = nTimespanLastBytesIn;
        nBaseLastBytesOut = nTimespanLastBytesOut;

        baseTimer->start(BASE_SAMPLE_PERIOD_MS);
    }
}

int TrafficGraphWidget::getGraphRangeMins() const
{
    return nMins;
}

float TrafficGraphWidget::resampleSamples(const QVector<float> &source, QVector<float> &dest) const
{
    int numSourceSamples = nMins * 60;
    float stride = (float)numSourceSamples / dest.size();
    float currentStride = stride;
    float maxSample = 0.0f;
    float lastAverageRate = 0.0f;

    int sourceTailIndex = nBaseSamplesHeadIndex - numSourceSamples;
    if (sourceTailIndex < 0) {
        sourceTailIndex += BASE_SAMPLES_BUF_SIZE;
    }

    int currentSampleIndex = sourceTailIndex;
    for (int destIndex = 0; destIndex < dest.size(); destIndex++) {
        int numSamplesToRead = (int)currentStride;

        float aggregate = 0.0f;
        for (int i = 0; i < numSamplesToRead; ++i) {
            float currentSample = source[currentSampleIndex];

            aggregate += currentSample;

            currentSampleIndex++;
            if (currentSampleIndex >= source.size()) {
                currentSampleIndex = 0;
            }
        }

        float averageRate = 0.0f;
        if (numSamplesToRead == 0) {
            // We seem to be trying to upsample. just use the previous sample.
            averageRate = lastAverageRate;
        }
        else {
            averageRate = aggregate / numSamplesToRead;