#!/usr/bin/env python
"""
Format the varnishstat JSON output as CSV.

This file is in the public domain.

Author: Lasse Karstensen <lkarsten@varnish-software.com>, August 2015.
"""
import sys
import json
from pprint import pprint

def jsonchunksplit(fp):
    lines = []
    while True:
        buf = fp.readline()
        if buf == "":
            if len(lines) > 0:
                yield "\n".join(lines)
            break
        elif buf == "\n":
            continue
        elif buf == "}\n":
            lines += [buf]
            yield "\n".join(lines)
            lines = []
        else:
            lines += [buf[:-1]]


def keyfilter(sample):
    keys = []
    for k, values in sample.items():
        if k == "timestamp":
            continue
        elif k.startswith("VBE."):
            continue
        elif k.startswith("MEMPOOL."):
            continue
        elif k.startswith("LCK."):
            continue
        elif k.endswith(".happy"):
            continue
        # For now only consider counters, no gauges.
        elif values["flag"] != "c":
            continue

        keys.append(k)
    keys.sort()
    keys.insert(0, "timestamp")
    return keys


def main():
    keys = None
    fp = open(sys.argv[1])

    for chunk in jsonchunksplit(fp):
        sample = json.loads(chunk)

        if keys is None:
            keys = keyfilter(sample)
            print ",".join(keys)

        for i, key in enumerate(keys):
            if i > 0:
                sys.stdout.write(",")
            if key == "timestamp":
                sys.stdout.write(sample[key])
            else:
                sys.stdout.write("%s" % sample[key]["value"])
        sys.stdout.write("\n")

if __name__ == "__main__":
    main()
