#coding: utf-8
import os,sys,time,re,json
panelPath = os.getenv('BT_PANEL')

os.chdir(panelPath)
if not panelPath + "/class/" in sys.path:
    sys.path.insert(0, panelPath + "/class/")
import public

#面板日志分析统计
def logs_analysis():
    logs_path = panelPath + '/logs/request/'
    logs_tips = logs_path + 'tips/'
    admin_path = public.readFile(panelPath + '/data/admin_path.pl')
    exolode_mods = ['data','warning','message','workorder','login','public','code','wxapp','webhook','webssh']
    if admin_path:
        admin_path = admin_path.replace('/','')
        if admin_path: exolode_mods.append(admin_path)
    explode_names = ['GetNetWork','get_task_lists','get_index_list','UpdatePanel',
    'GetTaskCount','get_config','get_site_types','get_load_average','GetCpuIo',
    'GetDiskIo','GetNetWorkIo','SetControl','GetDirSize','GetSshInfo','get_host_list',
    'get_command_list','GetDataList','get_soft_list','upload','check_two_step','get_settings',
    'get_menu_list','GetSpeed','getConfigHtml','get_sync_task_find','get_buy_code','get_install_log']

    if not os.path.exists(logs_path): return
    if not os.path.exists(logs_tips): os.makedirs(logs_tips,384)
    import re
    from urllib.parse import parse_qs, urlparse


    for fname in os.listdir(logs_path):
        if fname in ['tips']:continue
        day_date = fname.split('.')[0]
        if public.format_date().find(day_date): continue

        filename = logs_path + fname.replace('.gz','')

        tip_file = logs_tips + day_date + '.pl'
        if os.path.exists(tip_file): continue
        if not os.path.exists(filename): continue
        f = open(filename,'r')
        data_list = []
        tmp_list = {}
        while True:
            try:
                tmp_line = f.readline()
                if not tmp_line: break
                log_line = json.loads(tmp_line)
                tmp = {}
                if not re.search('(iPhone|Mobile|Android|iPod|iOS)',log_line['user-agent']):
                    tmp['client_type'] = 'pc'
                else:
                    tmp['client_type'] = 'mobile'
                url_obj = urlparse(log_line['uri'])
                url_path = url_obj.path
                url_args = parse_qs(url_obj.query)
                mod_tmp = url_path.split('/')
                tmp['s_name'] = ''
                tmp['mod_name'] = ''
                if 'colony' in mod_tmp:
                    tmp['mod_name'] = mod_tmp[1] + '/' + mod_tmp[2]
                    if len(mod_tmp) > 3:
                        tmp['s_name'] = mod_tmp[3]
                else:
                    tmp['mod_name'] = mod_tmp[1]
                    if len(mod_tmp) > 2:
                        tmp['s_name'] = mod_tmp[2]

                if 'action' in url_args.keys():
                    if not url_args['action'] in [['a']]:
                        tmp['s_name'] = url_args['action'][0]
                    else:
                        tmp['s_name'] = url_args['s'][0]
                        tmp['mod_name'] = url_args['name'][0]
                if log_line['method'] == 'POST':
                    if log_line['argv'].find("\'") == -1:
                        try:
                            post = json.loads(log_line['argv'].replace("'",'"'))
                            if 'action' in post.keys():
                                if not post['action'] in ['a']:
                                    tmp['s_name'] = post['action']
                                else:
                                    tmp['s_name'] = post['name']
                                    tmp['mod_name'] = post['s']
                        except:pass
                if not tmp['mod_name'] and not tmp['s_name']: tmp['mod_name'] = 'home'
                if tmp['mod_name'] in exolode_mods: continue
                if tmp['s_name'] in explode_names: continue
                key = public.md5(tmp['mod_name'] + '_' + tmp['s_name'])
                if key in tmp_list.keys():
                    tmp_list[key]['day_count'] += 1
                else:
                    tmp['day_count'] = 1
                    tmp_list[key] = tmp
            except:
                print(public.get_error_info())
                break

        f.close()
        public.writeFile(tip_file,'')
        for k in tmp_list.keys():
            data_list.append(tmp_list[k])

        pdata = {
            'day_date': day_date,
            'data_list': json.dumps(data_list),
        }
        public.HttpPost('https://www.bt.cn/api/panel/model_total',pdata)

    logs_path = '{}/logs/click'.format(panelPath)
    logs_tips = logs_path + '/tips'
    if not os.path.exists(logs_tips): os.makedirs(logs_tips)
    for fname in os.listdir(logs_path):
        if fname in ['tips']:continue
        tip_file = '{}/tips/{}.pl'.format(logs_path,day_date)
        if os.path.exists(tip_file): continue

        day_date = fname.split('.')[0]
        if public.format_date().find(day_date) >= 0: continue

        data_list = []
        try:
            rlist = json.loads(public.readFile(logs_path + '/' + fname))
        except :
            print(public.get_error_info())
            rlist = []

        for key in rlist:
            try:
                data_list.append({ 'client_type' :'pc','os':'windows','mod_name':key,'day_count':rlist[key] })
            except :pass
        pdata = {'data_list': json.dumps(data_list),'day_date':day_date }
        

        ret = public.HttpPost('https://www.bt.cn/api/wpanel/model_click',pdata)
        public.writeFile(tip_file,'')

    #模块统计
    path = '{}/data/mod_log.json'.format(public.get_panel_path())

    con_path = '{}/data/mod_log'.format(public.get_panel_path())
    if not os.path.exists(con_path): os.makedirs(con_path)

    if os.path.exists(path):
        mdata = {}
        sday = public.format_date()
        try:
            mdata = json.loads(public.readFile(path))
        except :pass

        con_file = '/{}.pl'.format(con_path,sday)
        if not os.path.exists(con_file):

            nData = {}
            pdata = public.get_user_info()
            for key in mdata:
                if sday.find(key) >= 0:
                    nData[key] = mdata[key]
                    continue
                pdata['day_date'] = key
                pdata['data_list'] = json.dumps(mdata[key])
                try:
                    ret = json.loads(public.httpPost('https://www.bt.cn/api/v2/statistics/report_plugin_daily',pdata))
                    if not ret['success']: nData[key] = mdata[key]
                except :
                    nData[key] = mdata[key]
            public.writeFile(path,json.dumps(nData))
            public.writeFile(con_file,' ')


logs_analysis()
