# -*- coding: utf-8 -*-
import flask
import os
import sys
import ast
import json
import argparse

app = flask.Flask(__name__)
filename = ''

@app.route('/metrics')
def get_metrics():
    """ Retrieves the last saved avg latency and throughput
        Args:
            -
        Returns:
            dict: A JSON object containing the avg latency and throughput
    """
    status = 200
    return flask.Response(get_stats_json(),
                          status=status,
                          mimetype='application/json')

def get_stats_json():
    try:
        f = open(filename, 'r')
        line = f.readline()
        res = line.split(" ")
        res_dict = {"avg_lat": float(res[1])}
        line = f.readline()         
        res = line.split(" ")
        res_dict["throughput"] = float(res[1])
        js = json.dumps(res_dict, indent=2)
        f.close()
        return js
    except Exception as e:
        print("An exception occurred")
        print(str(e))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Give configuration options')
    parser.add_argument('--filename', metavar='filename', type=str,
                       help='the filename from which will retrieve the metrics')
    parser.add_argument('--port', metavar='port', type=int, default=5000,
                    help='Server port (default 5000)')
    args = parser.parse_args()
    filename = args.filename
    app.run(host='0.0.0.0', port=args.port, debug=True)

# -*- coding: utf-8 -*-
import flask
import os
import sys
import ast
import json
import argparse

app = flask.Flask(__name__)
filename = ''

@app.route('/metrics')
def get_metrics():
    """ Retrieves the last saved avg latency and throughput
        Args:
            -
        Returns:
            dict: A JSON object containing the avg latency and throughput
    """
    status = 200
    return flask.Response(get_stats_json(),
                          status=status,
                          mimetype='application/json')

def get_stats_json():
    try:
        f = open(filename, 'r')
        line = f.readline()
        res = line.split(" ")
        res_dict = {"avg_lat": float(res[1])}
        line = f.readline()         
        res = line.split(" ")
        res_dict["throughput"] = float(res[1])
        js = json.dumps(res_dict, indent=2)
        f.close()
        return js
    except Exception as e:
        print("An exception occurred")
        print(str(e))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Give configuration options')
    parser.add_argument('--filename', metavar='filename', type=str,
                       help='the filename from which will retrieve the metrics')
    parser.add_argument('--port', metavar='port', type=int, default=5000,
                    help='Server port (default 5000)')
    args = parser.parse_args()
    filename = args.filename
    app.run(host='0.0.0.0', port=args.port, debug=True)

# -*- coding: utf-8 -*- Retrieves the last saved avg latency and throughput         Args:             -         Returns:             dict: A JSON object containing the avg latency and throughput