# coding: utf-8
import re
import sys,os
import time
import dateutil, pylab,random
from pylab import *
import datetime


class Logstatistics(object):

    def __init__(self):
        self.log_base = r'C:\Users\Administrator\Desktop\minions'
        self.date = '11-25'
        self.log_name = 'random_danmu.log'
        self.base_log_path = r'C:\Users\Administrator\Desktop\lags'
        self.chatroom_log = os.path.join(self.log_base,self.date+self.log_name)

    def writelog(self,logname,content):

        with open(os.path.join(self.base_log_path,logname),'w') as f:
            f.write(content)


    def log_list(self):
        self.logs = []
        for root,dirs,files in os.walk(self.log_base):
            if self.date+self.log_name in files  and root != self.log_base:
                self.logs.append(os.path.join(root,self.date+self.log_name))
        return self.logs


    def compare(self):
        chatroom_result,name,count = self.build_dict(self.chatroom_log)
        for log in self.logs:
            d,point_name,logcount = self.build_dict(log)
            times = []
            lags = []
            n = 0
            #for key,values in d.items():
            for key,values in sorted(d.items(),key = lambda d:d[1][0]):
                if values[-1] > '1':
                    try:
                        times.append(datetime.datetime.strptime(values[0].split()[0],'%H:%M:%S.%f'))
                        lags.append(int(values[-1]))
                    except Exception,e:
                        print e
                        pass
                if key not in chatroom_result:
                    n+=1

            pylab.plot_date(pylab.date2num(times), lags, linestyle='-')
            text(24,120, '')
            pylab.rc('font',size = 8)
            xtext = xlabel(u'time')
            ytext = ylabel(u'lag: s')
            title_string = point_name + '\n' +  'lost package: ' + str(n) + '\n' + 'lost  percent: ' + str('%.2f' % float(float(n)/float(logcount)))
            ttext = title(title_string)
            grid(True)
            setp(ttext, size='large', color='r')
            setp(xtext, size='medium',  weight='bold', color='g')
            setp(ytext, size='medium',  weight='light', color='g')

            savefig(os.path.join(self.base_log_path , (point_name + '.png')),dpi = 120)
            #show()

            pylab.cla() #clear any prior graph
            


            print point_name,n,logcount,float(n)/float(logcount)

            #print point_name,n,'%.2f' % float(float(n)/float(logcount))
            # elif switch == 'lag':
            #     strings = ''
            #     l= []
            #     for values in d.values():
            #         mi_co = values[-1]
            #         if int(mi_co) > 0 :
            #             l.append(values)
            #     l.sort(key=lambda x:int(x[2]),reverse=True)
            #     for each in l:
            #         string = '\t'.join(each)
            #         strings += string + '\n'
            #     self.writelog(point_name,strings)



    def build_dict(self,file):
        point_name = file.split(self.log_base)[1].split('\\')[1]
        d = {}

        with open(file,'r') as f:
            n = 0
            try:
                for line in f:
                    if  line[:-1].strip():
                        n += 1
                        line = line.strip()
                        split_line =  line.split()
                        id = split_line[5]
                        ci = split_line[7].replace(',','')
                        time = split_line[1].replace(']:','')
                        mi_co =  split_line[-1]
                        d[id] = (time,ci,mi_co)
            except:
                print line
                pass

        return d,point_name,n



    def log_result(self):
        self.compare(switch='lag')


    def plot(self):
        pass



A = Logstatistics()
A.log_list()
#print A.compare(switch='lag') #write log
print A.compare()