#! /usr/bin/env python3
#
# Turns spool/stats.log into a format more something suitable for R.
#
# reformat-stats <stats.log> <dst-dir>
#
# Existing data in dst-dir will be deleted.

from __future__ import print_function
import sys
import math
import os

def filterOutput(tag, host):
    os.system("cat %s/%s.dat | egrep '%s|^ *time' >%s/%s.%s.dat" % (Dest, tag, host, Dest, tag, host))

def makeOutput(file, tag):

    out = open("%s/%s.dat" % (Dest, tag), "w")

    hosts = {}
    data = {}
    keys = {}

    for line in open(file):

        f = line.split()
        if f[3] == "error": 
            continue

        try:
            (time, host, t, key, val)  = f
        except ValueError:
            try:
                (time, host, t, key)  = f
                val = "-"
            except ValueError:
                print("cannot parse '%s'" % line, file=sys.stderr)
                continue

        if t != tag:
            continue

        hosts[host] = 1

        time = math.floor(float(time))
        val = val

        if not time in data:
            data[time] = {}

        interval = data[time]

        if not host in interval:
            interval[host] = {}

        vals = interval[host]
        vals[key] = val
        keys[key] = 1

    intervals = data.keys()
    intervals.sort()

    keys = keys.keys()

    out.write("%10s %10s" % ("time", "node"))
    for k in keys:
        out.write(" %10s" % k)
    out.write("\n")

    for t in intervals:

        itv = data[t]

        idxs = itv.keys()
        idxs.sort()

        for idx in idxs:

            vals = itv[idx]

            out.write("%10.0f %10s" % (t, idx))

            for k in keys:
                if k in vals:
                    out.write(" %10s" % vals[k])
                else:
                    out.write(" %10s" % "-")

            out.write("\n")

    out.close()

    hosts = hosts.keys()
    hosts.sort()
    for host in hosts:
        filterOutput(tag, host)

    hostlist = open("%s/%s.hosts.dat" % (Dest, tag), "w")
    hostlist.write("name\n")
    for host in hosts:
        hostlist.write("%s\n" % host)


if len(sys.argv) != 3:
    print("Usage: reformat-stats <stats.log> <dst-dir>")
    print("Existing data in <dst-dir> will be deleted!")
    sys.exit(1)

Dest = sys.argv[2]

os.system("rm -rf %s" % Dest)
os.mkdir(Dest)

for tag in ["parent", "child", "interface"]:
    makeOutput(sys.argv[1], tag)

