Ejemplo n.º 1
0
def pie_graph(start_time,end_time,groupby,generate,**kwargs):
    start = time.time()
    graph_data = retrieve_date(start_time,end_time,groupby,generate,**kwargs)
    data_dict = Counter()
    for key,columns in graph_data:
        data_dict[columns[groupby]] +=columns[generate]
        #print columns[groupby],columns[generate]
    #pairs = zip(groupby_list,sum_generate_list)
    pairs = data_dict.items()
    pairs.sort(key=lambda x:x[1],reverse=True)
    groupby_list,sum_generate_list = zip(*pairs)
    groupby_list = list(groupby_list)
    sum_generate_list = list(sum_generate_list)
    #print type(groupby_list),type(sum_generate_list)
    if len(groupby_list)>10:
        groupby_list = groupby_list[:10]
        groupby_list.extend(['']*(len(sum_generate_list)-10))
    end = time.time()
    def my_display( x ):
        if x > 5:
            return '%.1f' % x + '%'
        else:
            return ""
    explode=[.0]*len(groupby_list)
    explode[0] = 0
    pylab.figure()
    pylab.pie(sum_generate_list,labels=groupby_list,autopct=my_display,shadow=False,explode=explode)
    pylab.title('%s groupby %s from %s to %s'%(generate,groupby,start_time,end_time))
    pylab.xlabel('Processing time is: %.5ss'%(end-start))
    pylab.savefig('foo.png')
    imgData = cStringIO.StringIO()
    pylab.savefig(imgData, format='png')
    imgData.seek(0)
    pylab.close()
    return imgData
Ejemplo n.º 2
0
def generate_linegraph(start_time="",end_time="",groupby="unknow",generate="unknow",cumulative=False,**kwargs):
    '''generate:CPUTime,DiskSpace,ExecTime,InputSandBoxSize,OutPutSandBoxSize,JobCount
       groupby:site,user,processingtype,country,grid
       dafaule cumulative=Falese,if you want to generate a cumulative graph,set it True
       
    '''
    #day_start_time = int(time.mktime(time.strptime(start_time,'%Y-%m-%d'))/86400)
    #day_finish_time = int(time.mktime(time.strptime(finish_time,'%Y-%m-%d'))/86400)
    
    if((generate=="unknow") or(groupby=="unknow")):
        print "error"
        return -1

    groupby_list = []

    fig = figure()
    ax = axes()
    start = time.time()
    zorder = 0.0
    y_max = 0.0
    y_max_list = []
    data_dict = {}

    graph_data = retrieve_date(start_time,end_time,groupby,generate,**kwargs)
    for key,columns in graph_data:
        data_dict.setdefault(columns[groupby],{'time':[],'value':[]})
        data_dict[columns[groupby]]['time'].append(columns["startTime"])
        data_dict[columns[groupby]]['value'].append(columns[generate])
        
    #print data_dict
    if cumulative:
        for key in data_dict.keys():
            data_dict[key]['value'] = make_cumulative_data(data_dict[key]['value'])
    #print data_dict 
    #return 0
    for key in data_dict.keys():
        groupby_list.append(key)
        color = generate_color(str(key))
        x_min = data_dict[key]['time'][0]
        x_max = data_dict[key]['time'][-1]
        y_tmp_max = max(data_dict[key]['value'])
        if y_max<y_tmp_max: y_max = y_tmp_max
        y_max_list.append(y_max)
        '''
        #set the value of Y-axis don't konw the unit
        if generate in ("CPUTime","ExecTime"):
            value_list = [int(item/86400) for item in value_list]
            ax.set_ylabel("days")
        elif generate in ("DiskSpace","InputSandBoxSize","OutputSandBoxSize"):
            value_list = [int(item/(1024*1024)) for item in value_list] 
            ax.set_ylabel("MB")
        else:
            ax.set_ylabel("Total")
        '''
        pairs = zip(data_dict[key]['value'],data_dict[key]['time'])
        pairs = [(x_min,0)]+pairs+[(x_max,0)]
        poly = Polygon(pairs,fill=True,facecolor=color,zorder=-y_max)
        ax.add_patch(poly)
        zorder -=0.1
    stamp_start_time = int(time.mktime(time.strptime(start_time,'%Y-%m-%d'))) 
    stamp_end_time = int(time.mktime(time.strptime(end_time,'%Y-%m-%d'))) 
    ax.set_xlim(stamp_start_time,stamp_end_time+1)
    ax.set_ylim(0,max(y_max_list)+1)
    ax.set_xticklabels([time.strftime('%y-%m-%d',time.localtime(x_time)) for x_time in ax.get_xticks()])
    #set the legend
    fontP = FontProperties() #set legend size
    fontP.set_size('xx-small')
    ax.legend(groupby_list,loc=0,bbox_to_anchor=(1,1.05),prop = fontP)
    
    title('%s groupby %s from %s to %s'%(generate,groupby,start_time,end_time))
    end = time.time()
    xlabel('Processing time is: %.5ss'%(end-start))
    savefig('linegraph')
    imgData = cStringIO.StringIO()
    savefig(imgData, format='png')
    imgData.seek(0)
    close()
    return imgData