#coding utf-8
from db_helper import RedisHelper
from config import set_config
from  utils import  request_post_data
import json
from log import Log
log_ = Log()


config_ = set_config()
def get_featurs(mid, data, size, top_K, flow_pool_P, rec_recall_vid_list, env_dict=None, video_static_info=None, video_hour_static_info=None):
    feature_dict = {}
    # defult value
    apptype = 4
    pagesource = ""
    versioncode = "580"
    machineinfo_brand="HUAWEI"
    recommendsource = "0"
    machineinfo_platform = 'android'
    sencetype = 100078
    machineinfo_model ='M2006C3LC'
    city_code = -1
    province_code = -1
    relevant_video_id = -1
    if env_dict and len(env_dict)>0:
        apptype = env_dict.get('app_type',4)
        pagesource = env_dict.get('pagesource', '')
        versioncode = env_dict.get('versioncode', 0)
        machineinfo_brand = env_dict.get('machineinfo_brand', '')
        machineinfo_model = env_dict.get('machineinfo_model', '')
        recommendsource = env_dict.get('recommendsource', '0')
        machineinfo_platform = env_dict.get('machineinfo_platform', '')
        sencetype = env_dict.get('sencetype', '')
        city_code = env_dict.get('city_code', -1)
        province_code = env_dict.get('province_code', -1)
        relevant_video_id = env_dict.get('relevant_video_id', -1)
        if city_code and city_code == "":
            city_code = -1
        if province_code and province_code == "":
            province_code = -1
        try:
            city_code = int(city_code)
        except:
            city_code = -1
        try:
            province_code = int(province_code)
        except:
            province_code = -1
        try:
            relevant_video_id = int(relevant_video_id)
        except:
            relevant_video_id = -1
    mid_list = []
    videoid_list = []
    apptype_list = []
    pagesource_list = []
    versioncode_list = []
    machineinfo_brand_list = []
    machineinfo_model_list = []
    machineinfo_platform_list = []
    sencetype_list = []
    day_rov_list = []
    day_share_return_score_list = []
    day_share_score_list = []
    day_return_rate_list = []
    day_ctr_score_list = []
    day_play_pv_list = []
    day_play_users_list = []
    day_return_users_list = []
    day_share_pv_list = []
    day_view_pv_list = []
    day_view_users_list = []
    day_share_users = []
    recommendsource_list = []
    relevant_video_list = []
    city_list = []
    province_list = []
    if rec_recall_vid_list and len(rec_recall_vid_list)>0:
        for i in range(len(rec_recall_vid_list)):
            mid_list.append(mid)
            videoid_list.append(rec_recall_vid_list[i])
            apptype_list.append(apptype)
            pagesource_list.append(pagesource)
            versioncode_list.append(versioncode)
            machineinfo_brand_list.append(machineinfo_brand)
            machineinfo_model_list.append(machineinfo_model)
            recommendsource_list.append(recommendsource)
            machineinfo_platform_list.append(machineinfo_platform)
            sencetype_list.append(sencetype)
            city_list.append(city_code)
            province_list.append(province_code)
            relevant_video_list.append(relevant_video_id)
    if video_static_info and len(video_static_info)>0:
        for i in range(len(video_static_info)):
            try:
                if video_static_info[i]  and len(video_static_info[i])>0:
                    per_video_staic = json.loads(video_static_info[i])
                    day_rov_list.append(float(per_video_staic[0]))
                    day_share_return_score_list.append(float(per_video_staic[1]))
                    day_share_score_list.append(float(per_video_staic[2]))
                    day_return_rate_list.append(float(per_video_staic[3]))
                    day_ctr_score_list.append(float(per_video_staic[4]))
                    day_view_users_list.append(int(per_video_staic[5]))
                    day_view_pv_list.append(int(per_video_staic[6]))
                    day_play_users_list.append(int(per_video_staic[7]))
                    day_play_pv_list.append(int(per_video_staic[8]))
                    day_share_users.append(int(per_video_staic[9]))
                    day_share_pv_list.append(int(per_video_staic[10]))
                    day_return_users_list.append(int(per_video_staic[11]))
                else:
                    insert_static_default_fea(day_ctr_score_list, day_play_pv_list, day_play_users_list,
                                              day_return_rate_list, day_return_users_list, day_rov_list,
                                              day_share_pv_list, day_share_return_score_list, day_share_score_list,
                                              day_share_users, day_view_pv_list, day_view_users_list)
            except Exception:
                insert_static_default_fea(day_ctr_score_list, day_play_pv_list, day_play_users_list,
                                          day_return_rate_list, day_return_users_list, day_rov_list,
                                          day_share_pv_list, day_share_return_score_list, day_share_score_list,
                                          day_share_users, day_view_pv_list, day_view_users_list)
    else:
        for i in range(len(rec_recall_vid_list)):
            insert_static_default_fea(day_ctr_score_list, day_play_pv_list, day_play_users_list,
                                      day_return_rate_list, day_return_users_list, day_rov_list,
                                      day_share_pv_list, day_share_return_score_list, day_share_score_list,
                                      day_share_users, day_view_pv_list, day_view_users_list)
    hour_rov_list = []
    hour_share_return_score_list =[]
    hour_share_score_list =[]
    hour_return_rate_list = []
    hour_ctr_score_list = []
    hour_play_pv_list = []
    hour_play_users_list = []
    hour_return_users_list = []
    hour_share_pv_list = []
    hour_view_pv_list = []
    hour_view_users_list = []
    hour_share_users = []

    if video_hour_static_info and len(video_hour_static_info)>0:
        for i in range(len(video_hour_static_info)):
            try:
                if video_hour_static_info[i] and len(video_hour_static_info[i])>0:
                    per_hour_video_staic = json.loads(video_hour_static_info[i])
                    hour_rov_list.append(float(per_hour_video_staic[0]))
                    hour_share_return_score_list.append(float(per_hour_video_staic[1]))
                    hour_share_score_list.append(float(per_hour_video_staic[2]))
                    hour_return_rate_list.append(float(per_hour_video_staic[3]))
                    hour_ctr_score_list.append(float(per_hour_video_staic[4]))
                    hour_view_users_list.append(int(per_hour_video_staic[5]))
                    hour_view_pv_list.append(int(per_hour_video_staic[6]))
                    hour_play_users_list.append(int(per_hour_video_staic[7]))
                    hour_play_pv_list.append(int(per_hour_video_staic[8]))
                    hour_share_users.append(int(per_hour_video_staic[9]))
                    hour_share_pv_list.append(int(per_hour_video_staic[10]))
                    hour_return_users_list.append(int(per_hour_video_staic[11]))
                else:
                    hour_rov_list.append(0.0)
                    hour_share_return_score_list.append(0.0)
                    hour_share_score_list.append(0.0)
                    hour_return_rate_list.append(0.0)
                    hour_ctr_score_list.append(0.0)
                    hour_play_pv_list.append(0)
                    hour_play_users_list.append(0)
                    hour_return_users_list.append(0)
                    hour_share_pv_list.append(0)
                    hour_view_pv_list.append(0)
                    hour_view_users_list.append(0)
                    hour_share_users.append(0)
            except Exception:
                    hour_rov_list.append(0.0)
                    hour_share_return_score_list.append(0.0)
                    hour_share_score_list.append(0.0)
                    hour_return_rate_list.append(0.0)
                    hour_ctr_score_list.append(0.0)
                    hour_play_pv_list.append(0)
                    hour_play_users_list.append(0)
                    hour_return_users_list.append(0)
                    hour_share_pv_list.append(0)
                    hour_view_pv_list.append(0)
                    hour_view_users_list.append(0)
                    hour_share_users.append(0)
    else:
        for i in range(len(rec_recall_vid_list)):
            hour_rov_list.append(0.0)
            hour_share_return_score_list.append(0.0)
            hour_share_score_list.append(0.0)
            hour_return_rate_list.append(0.0)
            hour_ctr_score_list.append(0.0)
    feature_dict["mid_list"] = mid_list
    feature_dict["videoid_list"] = videoid_list
    feature_dict["apptype_list"]  = apptype_list
    feature_dict["pagesource_list"] = pagesource_list
    feature_dict["versioncode_list"] = versioncode_list
    feature_dict["machineinfo_brand_list"] = machineinfo_brand_list
    feature_dict["machineinfo_model_list"] = machineinfo_model_list
    feature_dict["recommendsource_list"] = recommendsource_list
    feature_dict["machineinfo_model_list"] = machineinfo_model_list
    feature_dict["machineinfo_platform_list"] = machineinfo_platform_list
    feature_dict["sencetype_list"] = sencetype_list
    feature_dict["day_rov_list"] = day_rov_list
    feature_dict["day_share_return_score_list"] = day_share_return_score_list
    feature_dict["day_share_score_list"] = day_share_score_list
    feature_dict["day_return_rate_list"] = day_return_rate_list
    feature_dict["day_ctr_score_list"] = day_ctr_score_list
    feature_dict["day_play_pv_list"] = day_play_pv_list
    feature_dict["day_play_users_list"] = day_play_users_list
    feature_dict["day_return_users_list"] = day_return_users_list
    feature_dict["day_share_pv_list"] = day_share_pv_list
    feature_dict["day_view_pv_list"] = day_view_pv_list
    feature_dict["day_view_users_list"] = day_view_users_list
    feature_dict["day_share_users_list"] = day_share_users
    feature_dict["hour_rov_list"] = hour_rov_list
    feature_dict["hour_share_return_score_list"] = hour_share_return_score_list
    feature_dict["hour_share_score_list"] = hour_share_score_list
    feature_dict["hour_return_rate_list"] = hour_return_rate_list
    feature_dict["hour_ctr_score_list"] = hour_ctr_score_list
    feature_dict["hour_play_pv_list"] = hour_play_pv_list
    feature_dict["hour_play_users_list"] = hour_play_users_list
    feature_dict["hour_return_users_list"] = hour_return_users_list
    feature_dict["hour_share_pv_list"] = hour_share_pv_list
    feature_dict["hour_view_pv_list"] = hour_view_pv_list
    feature_dict["hour_view_users_list"] = hour_view_users_list
    feature_dict["hour_share_users_list"] = hour_share_users
    feature_dict["city_code"] = city_list
    feature_dict["province_code"] = province_list
    feature_dict["relevant_video_id"] = relevant_video_list
    return feature_dict


def insert_static_default_fea(day_ctr_score_list, day_play_pv_list, day_play_users_list, day_return_rate_list,
                              day_return_users_list, day_rov_list, day_share_pv_list, day_share_return_score_list,
                              day_share_score_list, day_share_users, day_view_pv_list, day_view_users_list):
    day_rov_list.append(0.0)
    day_share_return_score_list.append(0.0)
    day_share_score_list.append(0.0)
    day_return_rate_list.append(0.0)
    day_ctr_score_list.append(0.0)
    day_view_users_list.append(0)
    day_view_pv_list.append(0)
    day_play_users_list.append(0)
    day_play_pv_list.append(0)
    day_share_users.append(0)
    day_share_pv_list.append(0)
    day_return_users_list.append(0)


def get_tf_serving_sores(feature_dict):
    #for k, v in feature_dict:
    inputs_data = {
                    "mid": feature_dict["mid_list"],
                    "videoid": feature_dict["videoid_list"],
                    "apptype": feature_dict["apptype_list"],
                    "pagesource": feature_dict["pagesource_list"],
                    "versioncode": feature_dict["versioncode_list"],
                    "machineinfo_brand": feature_dict["machineinfo_brand_list"],
                    "machineinfo_model": feature_dict["machineinfo_model_list"],
                    "recommendsource": feature_dict["recommendsource_list"],
                    "machineinfo_platform": feature_dict["machineinfo_platform_list"],
                    "sencetype": feature_dict["sencetype_list"],
                    "day_rov": feature_dict["day_rov_list"],
                    "day_share_return_score": feature_dict["day_share_return_score_list"],
                    "day_share_score": feature_dict["day_share_score_list"],
                    "day_return_rate": feature_dict["day_return_rate_list"],
                    "day_ctr_score": feature_dict["day_ctr_score_list"],
                    "day_play_pv": feature_dict["day_play_pv_list"],
                    "day_play_users": feature_dict["day_play_users_list"],
                    "day_share_pv": feature_dict["day_share_pv_list"],
                    "day_return_users": feature_dict["day_return_users_list"],
                    "day_share_users": feature_dict["day_share_users_list"],
                    "day_view_pv": feature_dict["day_view_pv_list"],
                    "day_view_users":feature_dict["day_view_users_list"],
                    "hour_rov": feature_dict["hour_rov_list"],
                    "hour_share_score": feature_dict["hour_share_score_list"],
                    "hour_share_return_score": feature_dict["hour_share_return_score_list"],
                    "hour_return_rate": feature_dict["hour_return_rate_list"],
                    "hour_ctr_score": feature_dict["hour_ctr_score_list"],
                    "hour_play_pv": feature_dict["hour_play_pv_list"],
                    "hour_play_users": feature_dict["hour_play_users_list"],
                    "hour_share_pv": feature_dict["hour_share_pv_list"],
                    "hour_return_users": feature_dict["hour_return_users_list"],
                    "hour_share_users": feature_dict["hour_share_users_list"],
                    "hour_view_pv": feature_dict["hour_view_pv_list"],
                    "hour_view_users": feature_dict["hour_view_users_list"],
                    "city_code": feature_dict['city_code'],
                    "province_code": feature_dict['province_code'],
                    "relevant_video_id":feature_dict['relevant_video_id']
    }
    request_data_dict= {}
    request_data_dict["inputs"] = inputs_data
    #print(request_data_dict)
    request_data= json.dumps(request_data_dict)
    #print(request_data)
    # 调用http接口
    result = request_post_data(config_.TF_SERVING_URL,request_data, timeout=(0.1, 1))
    #print("result:", result)
    if result is None:
        #print("result is None")
        log_.info('call tf serving error，types: {}')
        return []
    #print(result)
    return result