package com.ericsson.uecontrol.core.util;

/**
 * Created by ezivkoc on 2014-07-22.
 */
public class ThroughputCalculator {
    public static final int UPDATES_PER_SEC = 2;
    public static final double NANOSEC_PER_SECOND = 1000000000.0;

    private boolean updated;
    private double throughput;
    private long previousTimeStamp;
    private long data_amount;


    public void setHandledData(long bytes){
        long currentTimeStamp = System.nanoTime();
        data_amount += bytes;
        if(currentTimeStamp - (NANOSEC_PER_SECOND/UPDATES_PER_SEC) > previousTimeStamp) {
            throughput = data_amount / ((currentTimeStamp - previousTimeStamp) / NANOSEC_PER_SECOND);
            previousTimeStamp = currentTimeStamp;
            data_amount = 0;
            updated = true;
        }
    }

    public double getByeThroughput(){
        setHandledData(0); // Update throughput
        updated = false;
        return throughput;
    }
    public double getBitThroughput(){
        return getByeThroughput()*8;
    }

    public boolean isUpdated(){
        return updated;
    }
}
