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

Code before:
{
    clientModel = model;
    if(model) {
        nLastBytesIn = model->getTotalBytesRecv();
        nLastBytesOut = model->getTotalBytesSent();
		
		//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;
    }

Review comment (suggestion):
`QVector::size()` unfortunately returns an `int`, so either we have to cast calls to `.size()` to `size_t`, or we have to make `currentSampleIndex` and `destIndex` an `int`, I think I'd recommend the latter.

Code after:
    connect(baseTimer, SIGNAL(timeout()), SLOT(updateBaseRates()));
}

void TrafficGraphWidget::setClientModel(ClientModel *model)
{
    clientModel = model;
    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;