录音和明细对比

录音和明细对比

需求说明

  • 编写python脚本用于数据对比
  • 该脚本涉及oracle、mysql、mongo数据链接和数据查询
  • 以及oracle10g、oracle11g链接解决,和不同时间字段格式化输出
  • 调接口发送支撑管理平台

代码详情

  • 脚本
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# import pymongo,time,datetime,logging.config,os,sys,time,configparser,os.path,json,datetime
#获取服务器版本信息,导入对应模块
import os
VersionShell ="  cat /etc/redhat-release |awk -F'release ' '{print $2}'|cut -d. -f1 |  cut -b 1"
OSVersion = int([ Errorinfo for Errorinfo  in os.popen(VersionShell) ][0].split("\n")[0])
if OSVersion  == 5 or  OSVersion == 4 or   OSVersion == 6:
    import time, os, ConfigParser, logging.config,sys,json,time,re,requests
    from datetime import datetime, date, timedelta
    reload(sys)
    sys.setdefaultencoding('utf8')
else:
    import time, os, configparser, logging.config,sys,json,time,re,requests
    from datetime import datetime, date, timedelta
    # import time, os, ConfigParser, logging.config,sys,json,time,re

#设置日志级别函数
def SetLogLevel(LogLevel):
    if "DEBUG" ==  LogLevel:
        logging.basicConfig(level=logging.DEBUG)
    elif "INFO" == LogLevel:
        logging.basicConfig(level=logging.INFO)
    elif "WARNING" == LogLevel:
        logging.basicConfig(level=logging.WARNING)
    elif "ERROR" == LogLevel:
        logging.basicConfig(level=logging.ERROR)

#请求推送运维支撑管理平台
def Requests_ccoddev_Api(formdata):
    headers={"Content-Type": "application/json","User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
    logger.info("Into Requests_ccoddev_Api json data: "+str(formdata))
    logger.info("Calling interface: "+ PushCcodDevApi )
    CurlShell =  " curl -H  'Content-Type:application/json' --connect-timeout " + Timeout + "  -X POST -d '" +  formdata  + "' "  + PushCcodDevApi
    logger.info(str(CurlShell))
    # Msg = os.popen(CurlShell)
    # CurlReturnMsg  = [ Errorinfo for Errorinfo  in Msg  ]
    # logger.info("Call Return: " + str(CurlReturnMsg))
    try:
        response = requests.post(PushCcodDevApi, data = formdata, headers = headers,timeout=int(Timeout))
        logger.info("Interface return: "+ str(response.json()))
    except Exception as ErrorMsg:
        logger.info("Calling interface Error Return: " +  str(ErrorMsg))

#链接Mongodb是否启动密码认证模式
def AutoMongo(MongoDBAuth):
    if "True" == MongoDBAuth:
        ConnPasswd = "mongodb://" + User + ":" + Passwd + "@" + MongoHost + ":" + Port + "/"
        try:
            clients = pymongo.MongoClient(ConnPasswd)
            logger.info("初始化连接Mongodb链接...")
            return clients
        except  Exception as e:
            logger.info("使用mongodb 账号密码链接异常:" +  str(e) + " 请检查配置异常")
            return False
    else:
        try:
            clients = pymongo.MongoClient(MongoHost, int(Port))
            logger.info("初始化连接Mongodb链接...")
            return clients
        except  Exception as Nopasswd:
            logger.info("不使用账号密码进行连接 mongodb  请检查配置异常")
            return False

#链接Mysql客户端
def ConnMysql():
    try:
        dbclients = pymysql.connect(host=MysqlHost,
                       user=MysqlUser,
                       password=MysqlPass,
                       db=MysqlDBName,
                       charset='utf8')
        logger.info("The MySQL link was initialized successfully...")
        return dbclients
    except  Exception as e:
        logger.error("Use login info: " + MysqlHost +" " + MysqlUser  +" " + MysqlPass  +" " +  MysqlDBName + " login mysql")
        logger.error("Exception in using MySQL account password link:" +  str(e) + " Please check the configuration exception")
        return False

#连接oracle客户端
def Conn_oracle():
    # 0000050330/[email protected]:1521/shpa4
    recordconn = oracle_user + "/" + oracle_passwd + "@" + oracle_host + ":1521/" + sid
    logger.info("oracle conn info: " + recordconn)
    try:
        dbclients = cx_Oracle.connect(recordconn)
        logger.info("init conn oracle ok...")
        return dbclients
    except  Exception as e:
        logger.error("use oracle conn error --> " + str(e).strip("\n") + " check config file")
        return False

#通过不同类型连接oracle或者mysql获取企业id
def GetEntDBList():
    EntId_List = []
    sql_msg = []
    if PlatformType ==  "TEST4.3_for_oracle" or PlatformType ==  "TEST4.5_for_oracle":
        Get_entid_SQL = "select ENTERPRISEID from ccod.get_entid_tables where  LICENSE_TYPE = 1 and  AGENTLICENSE  > 1"
        logger.info("Request sql: " + Get_entid_SQL)
        try:
            sql_msg = oracle_cursor.execute(Get_entid_SQL)
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_entid_SQL)
        EntId_List = [id[0] for id in sql_msg]
        logger.info("Get schema Entid list: " + str(EntId_List))
        return EntId_List
    elif PlatformType ==  "TEST4.5_for_mysql" or PlatformType ==  "TEST4.9_for_mysql":
        
        Get_entid_SQL = "select ENTERPRISEID from ucds.get_entid_tables where  LICENSE_TYPE = 1 and  AGENTLICENSE  > 1"
        logger.info("Request sql: " + Get_entid_SQL)
        try:
            mysql_cursor.execute(Get_entid_SQL)
            sql_msg = mysql_cursor.fetchall()
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_entid_SQL)
        EntId_List_old = [id for id in sql_msg]
        if len(EntId_List_old) != 0:
            EntId_List = [id[0] for id in EntId_List_old]
            logger.info("Get schema Entid list: " + str(EntId_List))
        else:
            daxie_sql_msg = ""
            daxie_Get_entid_SQL = "select ENTERPRISEID from ucds.get_entid_tables where  LICENSE_TYPE = 1 and  AGENTLICENSE  > 1"
            logger.info("Request sql: " + daxie_Get_entid_SQL)
            try:
                mysql_cursor.execute(daxie_Get_entid_SQL)
                daxie_sql_msg = mysql_cursor.fetchall()
            except  Exception as e:
                logger.error("run sql error " + str(e).strip("\n") + " ---> " + daxie_sql_msg)
            EntId_List = [id[0] for id in daxie_sql_msg]
        return EntId_List

#获取释放接通录音
def Get_Connect_record_status(entid):
    Record_status =  True
    sql_msg = ""
    SQL = "select  version_value from  ccc_111_ttt where enterprise_id = '" +  str(entid) +  "' and version_key  = 'yes'"
    logger.info("Request sql: " + SQL)
    try:
        mysql_cursor.execute(SQL)
        sql_msg = mysql_cursor.fetchall()
    except  Exception as e:
        logger.error("run sql error " + str(e).strip("\n") + " ---> " + SQL)
    Statu_num = [id for id in sql_msg]
    if len(Statu_num) != 0:
        EntId_List = Statu_num[0]
        if "no" == EntId_List[0]:
            logger.info("This entid: " +  str(entid) + " Is Connect record!!!")
            Record_status = True
        elif "yes" == EntId_List[0]:
            logger.info("This entid: " +  str(entid) + " Not  Connect record!!!")
            Record_status = False
    else:
        logger.info("This entid: " +  str(entid) + " Record status Default: " + str(Record_status) + " The field data was not queried yes")
    return Record_status

#获取通话明细数据
def GetmingxiData(ent_id):
    SessionidList = []
    sql_msg = []
    if PlatformType ==  "TEST4.5_for_mysql" or PlatformType ==  "TEST4.5_for_oracle":
        db = MongoClient[ent_id]
        session_detai_collection = db[mongodb_mingxi_table]  # 获取ent_bx_table的集合
        logger.info("Get Now mongodb info: "  + str(session_detai_collection))
        MongoSQL = 'db.mingxi.find({"start_time":{"$gte":\"' + str(Start_Timemillisecond) +  '\","$lt":\"'  + str(End_Timemillisecond) +'}\","talk_duration":{"$gte":' + str(BySecond) + '},"end_type" : { "$in" : ["254","255"] }},{ "session_id": 1, "_id":0 })'
        logging.debug("Request sql: " + MongoSQL  )
        GetDate  = session_detai_collection.find({"start_time":{"$gte":str(Start_Timemillisecond),"$lt":str(End_Timemillisecond)},"talk_duration":{"$gte":int(BySecond)},"end_type" : { "$in" : ["254","255"]}},{ "session_id": 1, "_id":0 })
        if GetDate:
            for sessionid in GetDate:
                SessionidList.append(sessionid.get("session_id"))
    elif  PlatformType ==  "TEST4.9_for_mysql":
        Get_sessionid_SQL = "select session_id from `" + str(ent_id) + "`.mingxi where talk_duration >= " + str( BySecond) + " and start_time > " + str(Start_timestamp) + " and start_time < " + str(End_timestamp)  +  " and end_type in ('254','255')"
        logger.info("Request sql: " + Get_sessionid_SQL)
        try:
            mysql_cursor.execute(Get_sessionid_SQL)
            sql_msg = mysql_cursor.fetchall()
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_sessionid_SQL)
        get_base_session_List = [id for id in sql_msg]
        if len(get_base_session_List) != 0:
            SessionidList =  [id[0] for id in sql_msg]
    elif  PlatformType ==  "TEST4.3_for_oracle":
        Get_sessionid_SQL = "select session_id from \""+ str(ent_id) +"\".R_DETAIL where TALK_DURATION  >= " + str(BySecond) + "  and  start_time >= to_date('"+ str(Start_date) +"', 'yyyy/mm/dd hh24:mi:ss') and start_time < to_date('"+ str(End_date) +"', 'yyyy/mm/dd hh24:mi:ss')" + " and end_type in ('254','255')"
        logger.info("Request sql: " + Get_sessionid_SQL)
        try:
            sql_msg = oracle_cursor.execute(Get_sessionid_SQL)
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_sessionid_SQL)
        get_base_session_List = [id for id in sql_msg]
        if len(get_base_session_List) != 0:
            SessionidList =  [id[0] for id in get_base_session_List]
    return SessionidList

#获取录音明细
def GetRecordIndexData(ent_id):
    bx_table_SessionidList = []
    bx_table_index_List = [] 
    sql_msg =  []
    sql_bx_msg = []
    if PlatformType ==  "TEST4.5_for_mysql" or PlatformType ==  "TEST4.5_for_oracle":
        db = MongoClient[ent_id]
        ent_bx_table_collection = db[mongodb_bx_table]  # 获取ent_bx_table的集合
        logger.info("Get Now mongodb info: "  + str(ent_bx_table_collection))
        MongoSQL  = 'db.RECORD_BX.find({duration:{$gte:' + str(BySecond) + '},start_time:{$gte: '+ str(Start_timestamp) +  ',"$lt":'  + str(End_timestamp) + '}},{ "session_id": 1,"record_name": 1, "_id":0 })'
        logging.debug("Request sql: " + MongoSQL  )
        GetDate  = ent_bx_table_collection.find({"start_time":{"$gte":int(Start_timestamp),"$lt":int(End_timestamp)},"duration":{"$gte":int(BySecond)}},{ "session_id": 1,"record_name": 1, "_id":0 })
        if GetDate:
            for index in GetDate:
                bx_table_SessionidList.append(index.get("session_id"))
                bx_table_index_List.append(index.get("record_name"))
    elif  PlatformType ==  "TEST4.9_for_mysql":
        Get_sessionid_SQL = "select session_id,record_name from `" + str(ent_id) + "`.RECORD_BX where duration > " + str( BySecond) + " and start_time > " + str(Start_timestamp) + " and start_time < " + str(End_timestamp) 
        logger.info("Request sql: " + Get_sessionid_SQL)
        try:
            mysql_cursor.execute(Get_sessionid_SQL)
            sql_msg = mysql_cursor.fetchall()
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_sessionid_SQL)
        get_base_session_List = [id for id in sql_msg]
        if len(get_base_session_List) != 0:
            for index in get_base_session_List:
                bx_table_SessionidList.append(index[0])
                bx_table_index_List.append(index[1])
    elif  PlatformType ==  "TEST4.3_for_oracle":
        Get_bx_sessionid_SQL = "SELECT SESSION_ID,record_name from \""+ str(ent_id) +"\".RECORD_BX  WHERE 1 = 1 AND (TO_DATE(END_TIME,'YYYY-MM-DD HH24:MI:SS')-TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS'))*86400 >= " + str(BySecond)  + " AND TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS') >= TO_DATE('" + str(Start_date) + "','YYYY-MM-DD HH24:MI:SS') AND TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS') < TO_DATE('" + str(End_date) + "','YYYY-MM-DD HH24:MI:SS')"
        logger.info("Request sql: " + Get_bx_sessionid_SQL)
        try:
            sql_bx_msg = oracle_cursor.execute(Get_bx_sessionid_SQL)
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_bx_sessionid_SQL)
        get_base_session_List = [id for id in sql_bx_msg]
        if len(get_base_session_List) != 0:
            for index in get_base_session_List:
                bx_table_SessionidList.append(index[0])
                bx_table_index_List.append(index[1])
        if len(bx_table_index_List)  == 0:
            Get_record_table_SQL = "SELECT SESSION_ID,record_name from \""+ str(ent_id) +"\".ENT_RECORD_TABLE  WHERE 1 = 1 AND (TO_DATE(END_TIME,'YYYY-MM-DD HH24:MI:SS')-TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS'))*86400 >= " + str(BySecond)  + " AND TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS') >= TO_DATE('" + str(Start_date) + "','YYYY-MM-DD HH24:MI:SS') AND TO_DATE(START_TIME,'YYYY-MM-DD HH24:MI:SS') < TO_DATE('" + str(End_date) + "','YYYY-MM-DD HH24:MI:SS')"

            logger.info("Request sql: " + Get_record_table_SQL)
            try:
                sql_msg = oracle_cursor.execute(Get_record_table_SQL)
            except  Exception as e:
                logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_record_table_SQL)
            get_base_session_List = [id for id in sql_msg]
            if len(get_base_session_List) != 0:
                for index in get_base_session_List:
                    bx_table_SessionidList.append(index[0])
                    bx_table_index_List.append(index[1])
        if len(bx_table_index_List)  == 0:
            new_sql_msg = ""
            Get_record_table_to_SQL = "SELECT SESSION_ID,record_name from  \""+ str(ent_id) +"\".RECORD_BX  WHERE 1 = 1 AND (END_TIME-START_TIME)*86400 >= " + str(BySecond)    + "  AND  to_char(start_time,'YYYY-MM-DD HH24:MI:SS') >= '" + str(Start_date) + "' AND  to_char(end_time,'YYYY-MM-DD HH24:MI:SS') < '" + str(End_date) + "'"

            logger.info("Request sql: " + Get_record_table_to_SQL)
            try:
                new_sql_msg = oracle_cursor.execute(Get_record_table_to_SQL)
            except  Exception as e:
                logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_record_table_to_SQL)
            get_base_new_session_List = [id for id in new_sql_msg]
            if len(get_base_new_session_List) != 0:
                for index2 in get_base_new_session_List:
                    bx_table_SessionidList.append(index2[0])
                    bx_table_index_List.append(index2[1])

        if len(bx_table_index_List)  == 0:
            new2_sql_msg = ""
            Get_record_table_to_SQL = "SELECT SESSION_ID,record_name from  \""+ str(ent_id) +"\".ENT_RECORD_TABLE  WHERE 1 = 1 AND (END_TIME-START_TIME)*86400 >= " + str(BySecond)    + "  AND  to_char(start_time,'YYYY-MM-DD HH24:MI:SS') >= '" + str(Start_date) + "' AND  to_char(end_time,'YYYY-MM-DD HH24:MI:SS') < '" + str(End_date) + "'"
            logger.info("Request sql: " + Get_record_table_to_SQL)
            try:
                new2_sql_msg = oracle_cursor.execute(Get_record_table_to_SQL)
            except  Exception as e:
                logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_record_table_to_SQL)
            get_base_new_session_List = [id for id in new2_sql_msg]
            if len(get_base_new_session_List) != 0:
                for index3 in get_base_new_session_List:
                    bx_table_SessionidList.append(index3[0])
                    bx_table_index_List.append(index3[1])
    logger.info("get  record index table: count session: " + str(len(bx_table_SessionidList)) + "  count record index: " + str(len(bx_table_index_List)) )
    return bx_table_SessionidList,bx_table_index_List

#获取录音文件
def Get_Record_file_Data(ent_id,record_file_index):
    File_Record_name_List = []
    sql_msg = ""
    if PlatformType ==  "TEST4.5_for_mysql" or PlatformType ==  "TEST4.5_for_oracle":
        db = MongoClient[ent_id]
        fastdfs_collection = db[mongodb_record_file_table]  # 获取fastdfs的集合
        logger.info("Get Now mongodb info: "  + str(fastdfs_collection))
        filter_mongodb_time = "/" + Mongodb_Time_fomat + "/"
        #mongodb 正则查询 db.xx_record_xx.find({"record_name":{$regex: '_201606271[4-5]'}},{record_name:1})
        MongoSQL = 'db.xx_record_xx.find({"record_name":{"$regex":\"' + str(filter_mongodb_time) +'\"}},{ "record_name": 1, "_id":0 })'
        logging.debug("Request sql: " + MongoSQL)
        
        GetDate  = fastdfs_collection.find({"record_name":{"$regex":str(filter_mongodb_time)}},{ "record_name": 1, "_id":0 })
        if GetDate:
            for Record_index in GetDate:
                File_Record_name_List.append(Record_index.get("record_name"))
    elif  PlatformType ==  "TEST4.9_for_mysql":
        # 正则查询 select record_name from xx_record_xx  where record_name regexp  '_202207281[5-6]';
        Get_RecordIndex_SQL = "select record_name from `" + str(ent_id) + "`.xx_record_xx where  start_time  like  \"%" + str(MYSQL_End_Time) + "%\""
        logger.info("Request sql: " + Get_RecordIndex_SQL)
        try:
            mysql_cursor.execute(Get_RecordIndex_SQL)
            sql_msg = mysql_cursor.fetchall()
        except  Exception as e:
            logger.error("run sql error " + str(e).strip("\n") + " ---> " + Get_RecordIndex_SQL)
        get_record_List = [id for id in sql_msg]
        if len(get_record_List) != 0:
            File_Record_name_List =  [id[0] for id in get_record_List]
    elif  PlatformType ==  "TEST4.3_for_oracle":
        File_Record_name_List = record_file_index
    return File_Record_name_List

#时间戳转换日期
def getDatetime():
    t = date.today()
    today = datetime.strptime(str(t),'%Y-%m-%d')
    yesterday = today + timedelta(days = int(Filtertime)) #减去一天
    Nowday = today + timedelta(days = -0) 
    
    yesterdaytimeArray = time.strptime(str(yesterday), "%Y-%m-%d %H:%M:%S")
    yesterdayStartTimeStamp = int(time.mktime(yesterdaytimeArray))

    timeArray = time.strptime(str(Nowday), "%Y-%m-%d %H:%M:%S")
    NowdayStartTimeStamp = int(time.mktime(timeArray))
    return yesterday,Nowday,yesterdayStartTimeStamp,NowdayStartTimeStamp

#差异输出
def DiffSessionid(DifferenceList,TablesSessionidList):
    DifferenceSessionidList = []
    for DifferenceSessionid  in DifferenceList:
        if DifferenceSessionid in TablesSessionidList:
            DifferenceSessionidList.append(DifferenceSessionid)
    return DifferenceSessionidList

#差异条数判断
def PercentageDifference(CountBxtables,CountMingxi):
    if CountBxtables != 0 and CountMingxi != 0 :
        ChayiMingxi = CountBxtables - CountMingxi
        return ChayiMingxi
    else:
        return False

# 判断文件是否存在
def IfDirexists(DirName):
    if os.path.exists(DirName):
        return True
    else:
        return False


#格式化发送json数据
def FormatSendJsonData(EntId,date,totalcallNum,totalRecordIndexNum,totalRecordFileNum,callNum,RecordIndexNum,RecordFileNum,RecordFile_RecordIndexNum,callDetails,RecordIndexDetails,RecordfileDetails,Recordfile_IndexDetails):
    JsonData = {}
    JsonData["PlatformId"] =  PlatformId
    JsonData["EntId"] =  EntId
    JsonData["date"] =  date
    JsonData["totalcallNum"] =  totalcallNum
    JsonData["totalRecordIndexNum"] =  totalRecordIndexNum
    JsonData["totalRecordFileNum"] =  totalRecordFileNum
    JsonData["callNum"] =  callNum
    JsonData["RecordIndexNum"] =  RecordIndexNum
    JsonData["RecordFileNum"] =  RecordFileNum
    JsonData["RecordFile_RecordIndexNum"] =  RecordFile_RecordIndexNum
    JsonData["callDetails"] =  callDetails
    JsonData["RecordIndexDetails"] =  RecordIndexDetails
    JsonData["RecordfileDetails"] =  RecordfileDetails
    JsonData["Recordfile_IndexDetails"] =  Recordfile_IndexDetails
    RequestsData = json.dumps(JsonData)
    return RequestsData

def Main():
    logger.info("get into main function")
    #获取企业无数据情况数
    for EntId in Get_Entid_num_list:
        Special_record_file_list = []
        Abnormal_record_file_list = []        
        GetmingxiToSessionidList = GetmingxiData(EntId)
        GetmingxiToSessionidList = list(set(GetmingxiToSessionidList))
        GetRECORD_BXToSessionidList,GetEnt_record_index_file_List = GetRecordIndexData(EntId)
        GetRECORD_BXToSessionidList = list(set(GetRECORD_BXToSessionidList))
        Get_Record_fastdfs_files_List = Get_Record_file_Data(EntId,GetEnt_record_index_file_List)
        for record_file_to in Get_Record_fastdfs_files_List:
            #去掉留言录音情况
            if "Customer" not in  record_file_to  and "custom" not in record_file_to and  "_sr_" not in record_file_to:
                Special_record_file_list.append(record_file_to)
        Get_Record_fastdfs_files_List = list(set(Special_record_file_list))

        guolv_index_file = []
        for Index_record_file_to in GetEnt_record_index_file_List:
            if "Customer" not in  Index_record_file_to  and "custom" not in Index_record_file_to   and  "_sr_" not in Index_record_file_to  :
                guolv_index_file.append(Index_record_file_to)
        GetEnt_record_index_file_List = list(set(guolv_index_file))

        #获取各自的数据的总数
        call_nums =  len(GetmingxiToSessionidList)
        record_index_nums =  len(GetRECORD_BXToSessionidList)
        record_Index_file_index_nums = len(GetEnt_record_index_file_List)

        record_file_nums = len(Get_Record_fastdfs_files_List)
        GetGetmingxiToSessionidListNums = len(GetmingxiToSessionidList)
        GetGetRECORD_BXToSessionidListNums = len(GetRECORD_BXToSessionidList)
        logger.info("Current enterprise: " + str(EntId) +" Query time range: " + str(Start_date) + " To " + str(End_date ) + " get  call details nums: " + str(call_nums) + " get  record details nums: " + str(record_index_nums)  + " get  record index file  details nums: " + str(record_Index_file_index_nums)+ " get  record  file  details nums: " + str(record_file_nums))
        
        #判断录音和明细是否有数据
        if call_nums !=0 and record_index_nums != 0:
            #获取录音接通状态
            if Get_Connect_record_status(EntId):
                    record_index_to_recordfile_diff_json = FormatSendJsonData(EntId,str(Start_date),call_nums,record_index_nums,record_Index_file_index_nums,"1",\
                    "1","1","1",["振铃录音&不进行对比"],["振铃录音&不进行对比"],["振铃录音&不进行对比"],["振铃录音&不进行对比"])
                    Requests_ccoddev_Api(record_index_to_recordfile_diff_json)
            else:
                Ret_Mingxi_To_BX_List = list(set(GetmingxiToSessionidList).difference(set(GetRECORD_BXToSessionidList)))
                Ret_BX_To_Mingxi_List = list(set(GetRECORD_BXToSessionidList).difference(set(GetmingxiToSessionidList)))

                Getmingxi_To_DifferenceNum =  len(Ret_Mingxi_To_BX_List)
                GetRECORD_BX_To_DifferenceNum =  len(Ret_BX_To_Mingxi_List)

                if Get_Record_fastdfs_files_List != 0:
                    if  PlatformType ==  "TEST4.3_for_oracle":
                        for record_file in Get_Record_fastdfs_files_List:
                            record_file_path = Storage_record_path + record_file
                            if IfDirexists(record_file_path)  == False:
                                Abnormal_record_file_list.append(record_file)
                    else:
                        Abnormal_record_file_list = list(set(GetEnt_record_index_file_List).difference(set(Get_Record_fastdfs_files_List)))
                    GetEnt_recordIndex_To_RecordFile_DifferenceNum =  len(Abnormal_record_file_list)

                diff_recordfile_to_recordIndex_list = list(set(Get_Record_fastdfs_files_List).difference(set(GetEnt_record_index_file_List)))
                diff_recordfile_to_recordIndex_list_Num =  len(diff_recordfile_to_recordIndex_list)

                #发送数据当运维支撑管理平台
                record_index_to_recordfile_diff_json = FormatSendJsonData(EntId,str(Start_date),GetGetmingxiToSessionidListNums,GetGetRECORD_BXToSessionidListNums,record_file_nums,Getmingxi_To_DifferenceNum,\
                GetRECORD_BX_To_DifferenceNum,GetEnt_recordIndex_To_RecordFile_DifferenceNum,diff_recordfile_to_recordIndex_list_Num,Ret_Mingxi_To_BX_List,Ret_BX_To_Mingxi_List,Abnormal_record_file_list,diff_recordfile_to_recordIndex_list)
                Requests_ccoddev_Api(record_index_to_recordfile_diff_json)

                if Get_Record_fastdfs_files_List == 0 and Getmingxi_To_DifferenceNum == 0 and GetRECORD_BX_To_DifferenceNum == 0 and diff_recordfile_to_recordIndex_list_Num == 0:
                    logger.inof("This entid " + str(EntId) +  "date: " + str(Start_date) + "Contrast normal")
                else:
                    logger.error("Get entid: " + str(EntId) + " Report Detail number: " + str(call_nums) +  " Record Index Detail number:" + str(record_index_nums) +  " Record file Detail number:" + str(record_index_nums)) 
        else:
            logger.info("This entid " + str(EntId) +  " date: " + str(Start_date) + " record or Detail 0")

        time.sleep(1)

if __name__ ==  '__main__': 
    for dirpath in os.popen("pwd"):
        dirpath = dirpath.strip('\n')
    cfgpath = os.path.join(dirpath, "cfg/config.ini")
    if OSVersion  == 5 or  OSVersion == 4 or   OSVersion == 6:
        conf = ConfigParser.ConfigParser()
    else:
        conf = configparser.ConfigParser()
        # conf = ConfigParser.ConfigParser()
    print("config file ---> ",cfgpath)
    # conf.read(cfgpath, encoding='UTF-8')
    conf.read(cfgpath)
    GetLogDir = conf.get("Base","LogDir")
    LogLevel = conf.get("Base","LogLevel")
    CheckintervalDate = conf.get("Base","CheckintervalDate")
    BySecond = conf.get("Base","BySecond")
    PlatformType = conf.get("Base","PlatformType")
    differenceTime = conf.get("Base","differenceTime")
    PushCcodDevApi = conf.get("Base","PushCcodDevApi")
    PlatformId = conf.get("Base","PlatformId")
    Storage_record_path = conf.get("Base","Storage_record_path")
    Timeout = conf.get("Base","Timeout")
    Filtertime = conf.get("Base","Filtertime")
    LogRunSentence = " [ ! -d " + GetLogDir  + "  ] && mkdir -p  " + GetLogDir
    os.system(LogRunSentence)

    SetLogLevel(LogLevel)
    logging.config.fileConfig("./cfg/logger.conf")
    logger = logging.getLogger("rotatfile")
    MongoDBAuth = conf.get("mongodb", "Auth")
    MongoHost = conf.get("mongodb", "MongoHost")
    User = conf.get("mongodb", "User")
    Passwd = conf.get("mongodb", "Passwd")
    Port = conf.get("mongodb", "Port")
    mongodb_bx_table = conf.get("mongodb", "mongodb_bx_table")
    mongodb_mingxi_table = conf.get("mongodb", "mongodb_mingxi_table")
    mongodb_record_file_table = conf.get("mongodb", "mongodb_record_file_table")

    oracle_host = conf.get("oracle", "oracle_host")
    sid = conf.get("oracle", "sid")
    oracle_user = conf.get("oracle", "oracle_user")
    oracle_passwd = conf.get("oracle", "oracle_passwd")
    
    MysqlHost = conf.get("mysql", "MysqlHost")
    MysqlUser = conf.get("mysql", "MysqlUser")
    MysqlPass = conf.get("mysql", "MysqlPass")
    MysqlDBName = conf.get("mysql", "MysqlDBName")

    logger.info("This program is applicable to ccod platform and is used for comparison of call details and recording details && recording files and recording details !!!")

    #根据平台类型导入加载对应数据库模块
    if PlatformType ==  "TEST4.3_for_oracle":
        import cx_Oracle,pymysql
    elif PlatformType ==  "TEST4.5_for_oracle":
        import cx_Oracle,pymongo,pymysql
    elif PlatformType ==  "TEST4.5_for_mysql":
        import pymysql,pymongo
    elif PlatformType ==  "TEST4.9_for_mysql":
        import pymysql

    #根据平台类型初始化不同数据库客户端
    Oracle_conn = ""
    oracle_cursor = ""
    mysql_cursor = ""
    mysql_conn = ""
    mysql_conn = ""
    logger.info("Now Use Platform Type: " + PlatformType )
    if PlatformType ==  "TEST4.3_for_oracle":
        #链接oracle数据库
        Oracle_conn = Conn_oracle()
        if Oracle_conn == False:
            logger.error("conn db error exit...")
            sys.exit()
        oracle_cursor = Oracle_conn.cursor()
        
        #链接mysql数据库
        mysql_conn = ConnMysql()
        if mysql_conn == False:
            logger.info("Mysql conn error exit....")
            sys.exit()
        logger.info("connn mysql db ok...")
        mysql_cursor = mysql_conn.cursor()

    elif PlatformType ==  "TEST4.5_for_mysql":
        #链接mysql数据库
        mysql_conn = ConnMysql()
        if mysql_conn == False:
            logger.info("Mysql conn error exit....")
            sys.exit()
        logger.info("connn mysql db ok...")
        mysql_cursor = mysql_conn.cursor()

        GetMongoClients = AutoMongo(MongoDBAuth)
        if  False !=  GetMongoClients:
            logger.info("connn mongodb db ok...")
            MongoClient = GetMongoClients
    elif PlatformType ==  "TEST4.5_for_oracle":
        #链接oracle数据库
        Oracle_conn = Conn_oracle()
        if Oracle_conn == False:
            logger.error("conn oracle db error exit...")
            sys.exit()
        oracle_cursor = Oracle_conn.cursor()
        logger.info("connn oracle db ok...")

        #链接Mongodb数据库
        GetMongoClients = AutoMongo(MongoDBAuth)
        if  False !=  GetMongoClients:
            MongoClient = GetMongoClients
            logger.info("conn Mongodb db ok...")

        #链接mysql数据库
        mysql_conn = ConnMysql()
        if mysql_conn == False:
            logger.info("Mysql conn error exit....")
            sys.exit()
        logger.info("connn mysql db ok...")
        mysql_cursor = mysql_conn.cursor()
    elif PlatformType ==  "TEST4.9_for_mysql":
        logger.info("Now Platform Type: " + PlatformType )
        #链接mysql数据库
        mysql_conn = ConnMysql()
        mysql_cursor = mysql_conn.cursor()
        logger.info("connn mysql db ok...")

    #获取对应企业schema
    Get_Entid_num_list = GetEntDBList()
    if Get_Entid_num_list == None:
        logger.error("get entid num is null exit script run")
        sys.exit()
    
    #测试企业验证
    # Get_Entid_num_list = ["2103020001","2107140002"]
    

    #获取需要查询过滤条件的时间或者时间戳
    Get_Date = getDatetime()
    Start_date = Get_Date[0]
    End_date = Get_Date[1]
    MYSQL_Time_fomat = str(Start_date).split("-")
    Mongodb_Time_fomat = str(Start_date).split(" ")[0].replace('-','')

    NewHours = ""
    Newminutes = ""
    Hours  = MYSQL_Time_fomat[1][0:1]
    if Hours == "0":
        NewHours = MYSQL_Time_fomat[1].replace('0','')
    minutes = MYSQL_Time_fomat[2][0:1]
    if minutes == "0":
        Newminutes = MYSQL_Time_fomat[2].split()[0].replace('0','')
    else:
        Newminutes = MYSQL_Time_fomat[2].split()[0].replace('0','')
    # MYSQL_End_Time = MYSQL_Time_fomat[0] + "-" + str(NewHours) + "-" + str(Newminutes) + " 00:00:00"
    MYSQL_End_Time = MYSQL_Time_fomat[0] + "-" + str(NewHours) + "-" + str(Newminutes)
    Start_timestamp = Get_Date[2]
    End_timestamp = Get_Date[3]
    Start_Timemillisecond = str(Start_timestamp) + "000"
    End_Timemillisecond = str(End_timestamp) + "000"
    logger.info("Current filtering time condition: " + str(Start_date) + " " + str(End_date) + " " + str(Start_timestamp) + " " +  str(End_timestamp) \
    + " " + str(Start_Timemillisecond) + " " +  str(End_Timemillisecond))

    # #主函数
    Main()

    logger.info("release DB conn!!!")
    if PlatformType ==  "TEST4.3_for_oracle":
        oracle_cursor.close()
    elif PlatformType ==  "TEST4.5_for_mysql":
        GetMongoClients.close()
        mysql_cursor.close()
        mysql_conn.close()
    elif  PlatformType ==  "TEST4.5_for_oracle":
        GetMongoClients.close()
        oracle_cursor.close()
    elif  PlatformType ==  "TEST4.9_for_mysql":
        mysql_cursor.close()
        mysql_conn.close()

  • 配置文件
[Base]
#执行运行间隔休眠时间单位是s
CheckintervalDate=900
#日志目录
LogDir=./log
#设置日志级别: DEBUG、INFO、WARNING、ERROR
LogLevel=DEBUG
#查询通话大于通话多少秒的数据
BySecond=10
#对比异常调运维支撑管理平台接口
PushCcodDevApi=http://xxx,xxx.com/Contrast
#接口超时时间
Timeout = 10
#平台类型: TEST4.3_for_oracle、TEST4.5_for_mysql、TEST4.5_for_oracle、TEST4.9_for_mysql
PlatformType=TEST4.5_for_oracle
#录音存储路径
Storage_record_path=/Data/record/
#平台id
PlatformId=1003
#过滤时间类型: Day、Hour
TimeType = Day
#查询几天前的数据.-1则昨天,-2则前天依次类推
Filtertime = -1
#按照当前时间截止前多少秒,为结束时间,
Earlyendtime = -3600
#需要检查间隔时间是秒为单位
differenceTime= -7200

[mysql]
MysqlHost=xx.xxx.xx.xx
MysqlUser=ucds
MysqlPass=ucds
MysqlDBName=ucds

[oracle]
oracle_host=xx.xxx.xx.xx
sid=ccod
oracle_user=ccod
oracle_passwd=ccod

[mongodb]
#mongodb IP地址
MongoHost=xx.xxx.xx.xx
#mongodb Port
Port=30000
#Mongodb 用户名
User=admin
#Mongodb 密码
Passwd=admin
#登陆库名
AdminDB=admin
#Mongodb集合
mongodb_bx_table=test_table1
mongodb_mingxi_table=test_table2
mongodb_record_file_table=test_table3
#是否启动密码加密: True启用, False不启用
Auth=False
  • 日志配置文件
###############################################
[loggers]
keys=root,rotatfile
[logger_root]
#level=INFO
handlers=hand01,hand02
[logger_rotatfile]
handlers=hand01,hand02
qualname=rotatfile
propagate=0
###############################################
[handlers]
keys=hand01,hand02
[handler_hand01]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=form01
args=('./log/DetailedContrast.log', 'a', 50*1024*1024, 100)
[handler_hand02]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stderr,)

###############################################
[formatters]
keys=form01
[formatter_form01]
format=%(asctime)s %(thread)d %(filename)s:%(lineno)d %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

你可能感兴趣的:(录音和明细对比)