#!/usr/bin/env python3

import os
import sys
import math
import numpy as np

from bwparser import JansenBandwidthParser, KomloBandwidthParser

if __name__ == '__main__':

    # sensible defaults
    bandwidth_file = os.getenv('BW_FILE')

    if len(sys.argv) < 3:
        print("Usage: calcprop.py scale epoch_count")
        exit()

    scale = float(sys.argv[1])
    epoch_count = int(sys.argv[2]) + 3
    bw_parser = None

    if os.getenv('BW_ALGO') != "komlo":
        bw_parser = JansenBandwidthParser(bw_file=bandwidth_file, netscale=scale)
    else:
        bw_parser = KomloBandwidthParser(sample_size=math.ceil(6500*scale))

    dats = [
        ('vanilla_none.dat', 'Vanilla'),
        ('singlepass_merkle.dat', 'Sing(M)'),
        ('telescoping_merkle.dat', 'Tele(M)'),
        ('singlepass_threshsig.dat', 'Sing(T)'),
        ('telescoping_threshsig.dat', 'Tele(T)'),
    ]

    for file, name in dats:
        with open(file) as f:
            for line in f.readlines():
                total_bytes = float(line.split(" ")[3])
                net_size = bw_parser.get_relay_num()
                throughput = bw_parser.get_relay_throughput()
                relay_traffic = (net_size*total_bytes)/(epoch_count * 1000)
                prop = relay_traffic/throughput
                print("===================================")
                print(name)
                print("Throughput: " + np.format_float_scientific(throughput, precision=3))
                print("Total bytes: " + np.format_float_scientific(relay_traffic, precision=3))
                print("Proportion: " + np.format_float_scientific(prop, precision=3))
