from flask import Flask
import pycurl
from io import StringIO
import time
from DBSession import session_maker
from model import InstanceList
app = Flask(__name__)
from utils import *

def  test():

    return ipaddr


def  h5_curl():

        c = pycurl.Curl()
        c.setopt(pycurl.URL, "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=/")
        c.setopt(pycurl.HTTPHEADER, ["Accept:"])
        c.setopt(pycurl.WRITEFUNCTION, StringIO.write)
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.setopt(pycurl.MAXREDIRS, 5)
        c.perform()


        info = c.getinfo(pycurl.INFO_FILETIME)

        print(info)
def  requesl_url_list(apptype):
    endpoint = 'cn-hangzhou.log.aliyuncs.com'
    accessKeyId = 'LTAIWYUujJAm7CbH'
    accessKey = 'RfSjdiWwED1sGFlsjXv0DlfTnZTG1P'
    logstore = 'request-log'
    client = LogClient(endpoint, accessKeyId, accessKey)
    project = 'wqsd-longvideoapi'
    topic = ""
    start_time = int(time.time())
    end_time = start_time - 86400
    # url = '/longvideoapi/user/idolsUpdated'
    req = GetLogsRequest(project, logstore, end_time, start_time, topic,
                         "params.appType=%s | select requestUri  group by requestUri " %(apptype), 30, 0, False)
    res = client.get_logs(req)


    return  res


def produce_video_data():
    start_time = (int(time.strftime("%Y%m%d%H%M", time.localtime())) - 30) * 100000000000
    end_time = (int(time.strftime("%Y%m%d%H%M", time.localtime()))) * 100000000000
    sum_sql = ("select count(*) as totalCount from"              
              "(select t1.project_id, t1.produce_status "
              "from produce_video_project t1 "
              "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
              " where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1) ss1"%(start_time,end_time)
               )


def all_metric():
    """视频合成类metrics"""

    """视频合成成功率"""
    time_stamp = int(time.time())
    end_time = int(datetime.datetime.fromtimestamp(time_stamp).strftime('%Y%m%d%H%M%S')) * 1000000000
    start_time = int(datetime.datetime.fromtimestamp(time_stamp-300).strftime('%Y%m%d%H%M%S')) * 1000000000
    print(start_time, end_time)
    query_sql = ("select totalCount , (successCount+processingCount1), round((successCount + processingCount1)/totalCount * 100,2) from  "
                "(select count(*) as totalCount,"
                 "sum(case when produce_status in (5,6,7,8) then 1 else 0 end) as successCount,"
                 "sum(case when produce_status = 99 then 1 else 0 end) as failCount , "
                 "sum(case when produce_status in(0,1,2,3,4) then 1 else 0 end) as processingCount,"
                 "sum(case when produce_status in(0,1,2,3,4) and (rate < 0.7 or rate is null) then 1 else 0 end) processingCount1,"
                 "sum(case when produce_status in(0,1,2,3,4) and rate >= 0.7 then 1 else 0 end) processingCount2  from  "
                 "(select t1.project_id, t1.produce_status, round((t2.last_connect_timestamp - t1.submit_timestamp) / (t1.video_duration/1000), 1) as rate  from produce_video_project t1  "
                 "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
                 "where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1) ss1" %(start_time ,end_time)

                )
    res = db_query(query_sql)
    if res[0] is not None:
        total_cnt = res[0]
    else:
        total_cnt = 0
    if res[1] is not None:
        success_cnt = res[1]
    else:
        success_cnt = 0

    if total_cnt == 0:
        rate = 100
    else:
        rate = round((success_cnt / total_cnt) ,2)* 100

    # produce_video_task_rate.labels("produce_video_task_rate").set(rate)
    # produce_video_task_total.labels('total_cnt').set(total_cnt)
    # produce_video_task_sucess.labels('success_cnt').set(success_cnt)
    print(total_cnt,success_cnt,rate)
if __name__ == '__main__':
    # with session_maker() as session:
    #     instance_info = session.query(InstanceList).filter(InstanceList.status==0).all()
    #     for index in range(len(instance_info)):
    #         print( instance_info[index].ipadd)
    HOST = 'rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com'
    PORT = '3306'
    DATABASE = 'devops'
    USERNAME = 'devops'
    PASSWORD = 'devops@123456'
    conn = pymysql.connect(host=HOST, user=USERNAME, password=PASSWORD, database=DATABASE, charset="utf8")
    cursor = conn.cursor()
    requestUri_list = [
        '/longvideoapi/sys/config',
        '/longvideoapi/video/played',
        '/longvideoapi/video/shared/weixin/friend',
        '/longvideoapi/user/login/v2',
        '/longvideoapi/user/info/getUserInfoExt',
        '/longvideoapi/user/token/check',
        '/longvideoapi/user/token/refresh',
        '/longvideoapi/user/info/homepageHead',
        '/longvideoapi/user/share/homepage/circle',
        '/longvideoapi/user/idols',
        '/longvideoapi/user/fans',
        '/longvideoapi/user/recommend/list/v4',
        '/longvideoapi/user/outside/subscribeBatch',
        '/longvideoapi/user/info/getBaseUserInfo',
        '/longvideoapi/user/info/getTargetBaseUserInfo',
        '/longvideoapi/user/getWxUserInfoByCode',
        '/longvideoapi/user/follow',
        '/longvideoapi/user/unfollow',
        '/longvideoapi/video/favorite',
        '/longvideoapi/video/unfavorite',
        '/longvideoapi/video/shared/weixin/circle',
        '/longvideoapi/videoCollection/loadUserVideoCollections',
        '/longvideoapi/video/v2/sharePage/queryVideosByCollectionId',
        '/longvideoapi/video/v2/detailPage/queryVideosByCollectionId',
        '/longvideoapi/videoCollection/loadUserVideoCollectionsForShareOrDetailPage',
        '/longvideoapi/video/distribute/getAppInfo',
        '/longvideoapi/video/distribute/category/videoList/v2',
        '/longvideoapi/video/v2/followed',
        '/longvideoapi/video/v2/loadHotVideosByCollectionId',
        '/longvideoapi/video/v2/loadLatelyVideosByCollectionId',
        '/longvideoapi/video/v2/detail',
        '/longvideoapi/video/v2/sharePageDetail',
        '/longvideoapi/video/send',
        '/longvideoapi/video/getCoverImagePaths',
        '/longvideoapi/video/updateVideo',
        '/longvideoapi/video/deleteVideo',
        '/longvideoapi/video/barrage/send',
        '/longvideoapi/video/barrage/switch',
        '/longvideoapi/video/videoActionReport',
        '/longvideoapi/video/barrage/v2/list',
        '/longvideoapi/video/shared/h5"',
        '/longvideoapi/user/share/report',
        '/longvideoapi/oss/signature',
        '/longvideoapi/oss/completeUpload',
        '/longvideoapi/oss/producevideo/getStsToken',
        '/longvideoapi/operate/message/list',
        '/longvideoapi/video/message/list',
        '/longvideoapi/sys/message/list',
        '/longvideoapi/user/station/message/statistics/info',
        '/longvideoapi/user/station/message/statistics/read',
        '/longvideoapi/user/station/message/totalCount/v2',
        '/longvideoapi/user/station/message/totalRead',
        '/longvideoapi/user/station/notice/list',
        '/longvideoapi/user/station/message/share/list',
        '/longvideoapi/user/station/message/prise/list',
        '/longvideoapi/user/station/message/favorite/list',
        '/longvideoapi/user/station/message/comment/list',
        '/longvideoapi/user/station/message/barrage/list',
        '/longvideoapi/operation/message/list',
        '/longvideoapi/search/userandvideo/list',
        '/longvideoapi/search/tips',
        '/longvideoapi/search/user/list',
        '/longvideoapi/search/video/list',
        '/longvideoapi/search/hot/words',
        '/longvideoapi/search/hot/videos',
        '/longvideoapi/video/distribute/search/recommendVideoes/v2',
        '/longvideoapi/comment/top/save',
        '/longvideoapi/comment/top/page',
        '/longvideoapi/comment/praise',
        '/longvideoapi/comment/second/save',
        '/longvideoapi/comment/second/page',
        '/longvideoapi/comment/delete',
        '/longvideoapi/comment/updelete',
        '/longvideoapi/comment/hots',
        '/longvideoapi/bank/withdraw/getBankWithdrawConfig',
        '/longvideoapi/bank/withdraw/getBankWithdrawRecordList',
        '/longvideoapi/bank/withdraw/wxSamllapp/requestWithdraw',
        '/longvideoapi/purchase/video/list',
        '/longvideoapi/profits/myself/head',
        '/longvideoapi/profits/myself/list',
        '/longvideoapi/profits/myself/recordList/v2',
        '/longvideoapi/profits/reward/recordListAccordingToUser',
        '/longvideoapi/profits/reward/recordListAccordingToVideo',
        '/longvideoapi/video/reward/getVideoRewardHeadInfo',
        '/longvideoapi/video/reward/getVideoRewardRecordList',
        '/longvideoapi/video/reward/getVideoRewardConfig',
        '/longvideoapi/video/reward/getVideoRewardConfig/v2',
        '/longvideoapi/order/updateClientPayStatus',
        '/longvideoapi/order/purchase',
        '/longvideoapi/profits/videoPay/head',
        '/longvideoapi/profits/videoPay/list',
        '/longvideoapi/video/getPayedVideoPath',
        '/longvideoapi/order/reward',
        '/longvideoapi/video/reward/getVideoDetailRewardInfo',
        '/longvideoapi/video/recommend/detailPage/list',
        '/longvideoapi/video/recommend/sharePage/list',
        '/longvideoapi/measure/report/view',
        '/longvideoapi/video/validatePwd"',
        '/longvideoapi/video/validatePwdAuth"',
        '/longvideoapi/video/v2/getCutVideo',
        '/longvideoapi/frontConfig/getWxFrontConfig',
        '/longvideoapi/frontConfig/getVideoPlayConfig',
        '/longvideoapi/frontConfig/getUserFrontConfig',
        '/longvideoapi/rhythmapp/homepage/getAllVideos',
        '/longvideoapi/ad/position/info'
        ]

    # for i in range(len(instance_info["Instances"]["Instance"])):
    #     instance_id = instance_info["Instances"]["Instance"][i]["InstanceId"]
    #     ipaddr = instance_info["Instances"]["Instance"][i]["VpcAttributes"]["PrivateIpAddress"]["IpAddress"][0]
    #     server_name = instance_info["Instances"]["Instance"][i]["Tags"]["Tag"][0]["TagValue"]
    #     status = instance_info["Instances"]["Instance"][i]["Status"]
    #     instance_name = instance_info["Instances"]["Instance"][i]["HostName"]
    #     if status == "running":
    # #         status = 1
    # for url in requestUri_list:
    #     sql = "UPDATE `reuqest_url_list`  set  `level`=0 WHERE `request_url` ='%s' and `apptype` in (1,13)"%(url)
    #     cursor.execute(sql)
    #     conn.commit()
    # #
    # c = pycurl.Curl()
    # c.setopt(pycurl.URL, "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=/")
    # c.setopt(pycurl.HTTPHEADER, ["Accept:"])
    # b = StringIO.StringIO()
    # c.setopt(pycurl.WRITEFUNCTION, b.write)
    # c.setopt(pycurl.FOLLOWLOCATION, 1)
    # c.setopt(pycurl.MAXREDIRS, 5)
    # c.perform()
    #
    #
    # info = c.getinfo(pycurl.INFO_FILETIME)
    #
    # print(info)

    # with open("/t3.txt", "r") as f:  # 打开文件
    #     data = f.read()  # 读取文件
    #    #     print(data)
    # 20210716000000000111111
    # 20210726200800000000000
    # start_time = int(time.strftime("%Y%m%d%H%M", time.localtime())) * 100000000000
    # end_time = (int(time.strftime("%Y%m%d%H%M", time.localtime())) + 5) * 100000000000
    # print(start_time , end_time)



    #
    # appType = ['0', '4', '5', '6', '12' ,'15','1','13']
    #
    # #
    # for index in range(len(appType)):
    #     type = appType[index]
    # #     res = requesl_url_list(type)
    # #     for i in range(len(res.body)):
    # #         url = res.body[i]["requestUri"]
    # #         sql = "insert into  `reuqest_url_list`  (`apptype`, `request_url`) VALUES (%s, '%s')"%(type, url)
    # #         print(sql)
    # #         cursor.execute(sql)
    # #         conn.commit()
    #
    #
    #
    #     res = count_avg_max(type)
    #     for i in range(len(res.body)):
    #         url = res.body[i]["requestUri"]
    #         cnt = res.body[i]["cnt"]
    #         max_time = res.body[i]["max_time"]
    #         avg_time = res.body[i]["avg_time"]
    #         sql = "UPDATE `reuqest_url_list`  set `qps`=%s, `max_time`=%s, `avg_time`=%s WHERE `request_url` ='%s' and `apptype`=%s"%(cnt, max_time, avg_time, url, type)
    #         cursor.execute(sql)
    #         conn.commit()
    #     for i in range(len(res.body)):
    #         url = res.body[i]["requestUri"]
    #         cnt = res.body[i]["cnt"]
    #         sql = "UPDATE `reuqest_url_list`  set `rt_1`=%s WHERE `request_url` ='%s' and `apptype`=%s"%(cnt, url, type)
    #         cursor.execute(sql)
    #         conn.commit()
    #
    #     res = count_rt_less_time_count(type, 200, 500)
    #     for i in range(len(res.body)):
    #         url = res.body[i]["requestUri"]
    #         cnt = res.body[i]["cnt"]
    #         sql = "UPDATE `reuqest_url_list`  set `rt_2`=%s WHERE `request_url` ='%s' and `apptype`=%s" % (cnt, url, type)
    #         cursor.execute(sql)
    #         conn.commit()
    #
    #     res = count_rt_less_time_count(type, 500, 1000)
    #     for i in range(len(res.body)):
    #         url = res.body[i]["requestUri"]
    #         cnt = res.body[i]["cnt"]
    #         sql = "UPDATE `reuqest_url_list`  set `rt_3`=%s WHERE `request_url` ='%s' and `apptype`=%s" % (cnt, url, type)
    #         cursor.execute(sql)
    #         conn.commit()
    #
    #     res = count_rt_less_time_count(type, 1000, 10000)
    #     for i in range(len(res.body)):
    #         url = res.body[i]["requestUri"]
    #         cnt = res.body[i]["cnt"]
    #         sql = "UPDATE `reuqest_url_list`  set `rt_4`=%s WHERE `request_url` ='%s' and `apptype`=%s" % (cnt, url, type)
    #         cursor.execute(sql)
    #         conn.commit()
    #
    #

    # start_time = int(time.strftime("%Y%m%d%H%M", time.localtime())) * 100000000000
    # end_time = (int(time.strftime("%Y%m%d%H%M", time.localtime())) + 5) * 100000000000
    # query_sql = ("select round((successCount + processingCount1)/totalCount * 100,2) from  "
    #             "(select count(*) as totalCount,"
    #              "sum(case when produce_status in (5,6,7,8) then 1 else 0 end) as successCount,"
    #              "sum(case when produce_status = 99 then 1 else 0 end) as failCount , "
    #              "sum(case when produce_status in(0,1,2,3,4) then 1 else 0 end) as processingCount,"
    #              "sum(case when produce_status in(0,1,2,3,4) and (rate < 1 or rate is null) then 1 else 0 end) processingCount1,"
    #              "sum(case when produce_status in(0,1,2,3,4) and rate >= 1 then 1 else 0 end) processingCount2  from  "
    #              "(select t1.project_id, t1.produce_status, round((t2.last_connect_timestamp - t1.submit_timestamp) / (t1.video_duration/1000), 1) as rate  from produce_video_project t1  "
    #              "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
    #              "where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1) ss1" %(start_time ,end_time)
    #
    #             )
    # res = db_query(query_sql)
    #
    # if res[0] is not None:
    #     print(res[0])
    time_stamp = int(time.time())
    end_time = int(datetime.datetime.fromtimestamp(time_stamp).strftime('%Y%m%d%H%M%S')) * 1000000000
    start_time = int(datetime.datetime.fromtimestamp(time_stamp-300).strftime('%Y%m%d%H%M%S')) * 1000000000
    # rt - 300
    print(start_time, end_time)
    all_metric()

