# -*- coding: utf-8 -*-
# author：Li Mingjie time:2019/2/28
# Brief:Response time analysis

import matplotlib.pyplot as plt
import bottom_function.data_read as dr
import numpy as np


def cost_time_plot(datatype, starttime, endtime):
    cost_time_dict = dr.read_cost_time_data(datatype=datatype, starttime=starttime, endtime=endtime)
    y_max = 0
    xl_list = list(cost_time_dict.keys())
    plt.figure(figsize=(10, 8))

    for x in range(6):
        lab_list = []
        y_list = []
        x_list = []
        t_list = list(cost_time_dict[xl_list[x]])[0:3]
        for m in t_list:
            lab_list.append(m[0])
            y_list.append(m[1])
            x += 0.2
            x_list.append(x)
            y_max = max(y_list) if max(y_list) > y_max else y_max

        plt.bar(x_list, y_list, width=0.2, color=['r', 'g', 'b'])
        for i, j, lab in zip(x_list, y_list, lab_list):
            if j == 0:
                continue
            plt.text(i, j, lab, ha='center', va='bottom', fontsize=9, rotation=70)
    # plt.rcParams['font.family'] = ['sans-serif']
    # plt.rcParams['font.sans-serif'] = ['SimHei']
    # plt.xticks(np.arange(0.5,6,1),xl_list,position=(0.06,-0.07))
    plt.ylim(0, y_max * 1.2)
    plt.xticks(np.arange(0.5, 6, 1), xl_list)
    if datatype == 'application':
        plt.title(str(starttime) + ' to ' + str(endtime) + ' cost time analysis of application resources', fontsize=15)
    elif datatype == 'control':
        plt.title(str(starttime) + ' to ' + str(endtime) + ' cost time analysis of control resources', fontsize=15)
    else:
        plt.title(str(starttime) + ' to ' + str(endtime) + ' cost time analysis of all resources', fontsize=15)
    plt.tight_layout(5)
    path = '/roobo/soft/phpmyadmin/response_time.jpg'
    plt.savefig(path)
    return 'http://120.79.171.145:8000/response_time.jpg'

