#!/usr/bin/env python3
import json
import os
import sys

# paths
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
RESULTS_DIR = os.path.join(SCRIPT_DIR, "results")
MEASUREMENTS_PATH = os.path.join(SCRIPT_DIR, ".apollo_measurements")

LDBC_TIME_FACTORS = {
    "SECONDS": 1.0,
    "MILLISECONDS": 1000.0,
    "MICROSECONDS": 1000000.0,
    "NANOSECONDS": 1000000000.0
}

def generate_measurements(path):
    ret = ""
    action, db, scale, ldbc, results = os.path.basename(path).split("-")
    test_path = "{}.{}.{}".format(action, scale, db)
    with open(path) as f:
        results = json.load(f)
    metrics = ["total_duration", "total_count", "throughput"]
    divs = [LDBC_TIME_FACTORS[results["unit"]], 1, 1]
    for metric, div in zip(metrics, divs):
        ret += "{}.{} {}\n".format(test_path, metric, results[metric] / div)
    for result in results["all_metrics"]:
        name = result["name"]
        run_time = dict(result["run_time"])
        unit = run_time.pop("unit")
        run_time.pop("name")
        for key, value in run_time.items():
            scale = LDBC_TIME_FACTORS[unit] if key != "count" else 1
            ret += "{}.queries.{}.{} {}\n".format(test_path, name, key, value / scale)
    return ret

measurements = ""
for fname in sorted(os.listdir(RESULTS_DIR)):
    path = os.path.join(RESULTS_DIR, fname)
    if not os.path.isfile(path): continue
    if not path.endswith(".json"): continue
    measurements += generate_measurements(path)

with open(MEASUREMENTS_PATH, "w") as f:
    f.write(measurements)
