#!env python
#coding: utf-8

import sys, os, re
from collections import defaultdict, namedtuple
import matplotlib.pyplot as plt

RecordRaw=namedtuple("RecordRaw", "type durtation data_size throughput_total throughput_per_node")
Record=namedtuple("Record", "type language durtation data_size throughput_total throughput_per_node")

def group_by_type(datas):
    groups = defaultdict(dict)
    for i in datas:
        words = re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', i.type).split()
        prefix = words[0].lower()
        suffix = "_".join([x.lower() for x in words[1:]])
        groups[suffix][prefix] = Record(type = "".join(words[1:]),
                                        language = prefix,
                                        durtation = i.durtation,
                                        data_size = i.data_size,
                                        throughput_total = i.throughput_total,
                                        throughput_per_node = i.throughput_per_node
                                        )
    return dict(groups)

def report_plot(fn):
    if not os.path.isfile(fn):
        print "Failed to find `sparkbench.report`"
        sys.exit(1)

    with open(fn) as f:
        data = [x.split() for x in f.readlines()[1:]]

    groups = group_by_type([RecordRaw(type = x[0],
                                      data_size = int(x[3]),
                                      durtation = float(x[4]),
                                      throughput_total = int(x[5]) / 1024 / 1024,
                                      throughput_per_node = int(x[6]) / 1024 /1024
                                      ) for x in data])

    #print groups
    base_dir = os.path.dirname(fn)
    plot(groups, "Seconds of durtations", "Seconds", "durtation", os.path.join(base_dir, "durtation.png"))
    plot(groups, "Throughput in total", "MB/s", "throughput_total", os.path.join(base_dir, "throughput_total.png"))
    plot(groups, "Throughput per node", "MB/s", "throughput_per_node", os.path.join(base_dir, "throughput_per_node.png"))

def plot(groups, title="Seconds of durtations", ylabel="Seconds", value_field="durtation", fig_fn = "foo.png"):
    # plot it
    keys = groups.keys()
    languages = reduce(lambda x,y: x.union(y), [set([groups[x][y].language for y in groups[x]]) for x in groups])
    width = 0.2
    rects = []


    fig, ax = plt.subplots()
    colors='rgb'
    for idx, lang in enumerate(languages):
        rects.append(ax.bar([x + width * idx for x in range(len(keys))], # x index
                            [getattr(groups[x][lang], value_field) if x in groups else 0 for x in keys], # value
                            width,
                            color = colors[idx]
                            )  # width
        )
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    ax.set_xticks([(x + width) for x in range(len(keys))])
    ax.set_xticklabels(keys)

    ax.legend([x[0] for x in rects],
              languages)

    def autolabel(rects):
        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d' % int(height),
                    ha='center', va='bottom')
#    [autolabel(x) for x in rects]

    fig = plt.gcf()
    fig.set_size_inches(18.5,10.5)
    plt.savefig(fig_fn, dpi=100)



if __name__ == "__main__":
    try:
        default_report_fn = sys.argv[1]
    except:
        default_report_fn = "sparkbench.report"
        
    report_plot(os.path.join(os.path.dirname(__file__), "..", default_report_fn))

@@ -324,16 +324,316 @@
 node%22)%0A%0A
+def human_readable_size(n):%0A    %22convert number into human readable string%22%0A    if n%3C1000: return str(n)%0A    if n%3C800000: return %22%25.3fK%22 %25 (n/1000.0)%0A    if n%3C800000000: return %22%25.3fM%22 %25 (n/1000000.0)%0A    if n%3C800000000000: return %22%25.3fG%22 %25 (n/1000000000.0)%0A    return %22%25.3fT%22 %25 (n/1000000000000.0)%0A%0A
 def grou
@@ -646,24 +646,24 @@
 ype(datas):%0A
-
     groups =
@@ -2062,16 +2062,33 @@
 rtations
+ (Less is better)
 %22, %22Seco
@@ -2184,16 +2184,35 @@
 in total
+ (Higher is better)
 %22, %22MB/s
@@ -2319,16 +2319,35 @@
 per node
+ (Higher is better)
 %22, %22MB/s
@@ -3271,20 +3271,120 @@
 klabels(
-keys
+%5B%22%25s %5Cn@%25s%22 %25 (x, human_readable_size(groups%5Bx%5D.values()%5B0%5D.data_size)) for x in keys%5D)%0A    ax.grid(True
 )%0A%0A    a
@@ -3722,16 +3722,16 @@
 t.gcf()%0A
-
     fig.
@@ -3757,16 +3757,21 @@
 5,10.5)%0A
+    %0A
     plt.

improve report_gen_plot