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

Code before:

    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)
{
    int h = height() - YMARGIN * 2;
    int w = width() - XMARGIN * 2;

    path.moveTo(XMARGIN, YMARGIN + h);
    for (size_t i = 0; i < samples.size(); ++i) {
        int x = w * i / DESIRED_SAMPLES;
        int y = h - (int)(h * samples[i] / fMax);

        path.lineTo(XMARGIN + x, YMARGIN + y);
    }
    path.lineTo(XMARGIN + w, YMARGIN + h);
}

//MLUMIN 05_22: View updating callback
void TrafficGraphWidget::showEvent(QShowEvent *)
{

Review comment (suggestion):
Nit: Can probably save a cast by subtracting `numSamplesToRead` instead.

Code after:
    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;
        }

        maxSample = std::max(maxSample, averageRate);

        dest[destIndex] = averageRate;
        lastAverageRate = averageRate;

        currentStride -= numSamplesToRead;
        currentStride += stride;
    }

    return maxSample;
}

void TrafficGraphWidget::paintPath(QPainterPath &path, const QVector<float> &samples, int samplesHead)
{
    int h = height() - YMARGIN * 2;