channel_success_rate.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #-*- coding: utf-8 -*-
  2. #__author__ = "dukun"
  3. import time
  4. import pymysql
  5. import logging
  6. import datetime
  7. import numpy as np
  8. import pandas as pd
  9. from celery_tasks import cel
  10. #from DBUtils.PooledDB import PooledDB
  11. from dbutils.pooled_db import PooledDB
  12. #配置输出日志格式
  13. LOG_FORMAT = "%(asctime)s %(filename)s[line:%(lineno)d] %(name)s %(levelname)s %(pathname)s %(message)s "
  14. #配置输出时间的格式,注意月份和天数不要搞乱了
  15. DATE_FORMAT = '%Y-%m-%d %H:%M:%S %a '
  16. logging.basicConfig(level=logging.INFO,
  17. format=LOG_FORMAT,
  18. datefmt = DATE_FORMAT ,
  19. filename=r"./logs/channel.log" #有了filename参数就不会直接输出显示到控制台,而是直接写入文件
  20. )
  21. #数据库连接信息
  22. channel_db_config = {
  23. 'host' : '47.95.217.180',
  24. 'port' : 3306,
  25. 'user' : 'root',
  26. 'password' : '93DkChZMgZRyCbWh',
  27. 'db' : 'fmp',
  28. 'charset' : 'utf8',
  29. 'autocommit' : 1
  30. }
  31. stat_db_config = {
  32. 'host' : '127.0.0.1',
  33. 'port' : 3306,
  34. 'user' : 'root',
  35. 'password' : 'nibuzhidaowozhidao',
  36. 'db' : 'statistical',
  37. 'charset' : 'utf8',
  38. 'autocommit' : 1
  39. }
  40. #创建数据库连接池
  41. def createPool(db_config):
  42. spool = PooledDB(pymysql, 5, **db_config)
  43. return spool
  44. @cel.task
  45. def get_channel_total_rate():
  46. conn = createPool(channel_db_config).connection()
  47. stat_conn = createPool(stat_db_config).connection()
  48. #print(conn)
  49. channel_sql = """ SELECT
  50. t1.order_id,trans_id,channel_name,status,t1.cnt
  51. FROM
  52. (SELECT
  53. order_id,trans_id,send_status AS status,COUNT(*) AS cnt
  54. FROM
  55. mobile_flow_dispatch_rec
  56. WHERE
  57. UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(create_date) <= 1800
  58. AND trans_id NOT IN(92,93,94,96,97,98,101,102,103,107,108,109,110,111,112,113,114,115)
  59. GROUP BY
  60. order_id,status )t1 LEFT JOIN access_channel_info
  61. ON t1.trans_id = access_channel_info.channel_seq_id"""
  62. ins_sql = 'INSERT INTO channel_success_rate VALUES(0,%s,%s,%s,%s,%s,%s)'
  63. df = pd.read_sql(channel_sql,con = conn)
  64. channel_ids = ['83','84','85','89','90','91','95','104','105','116','117','118','119','120',
  65. '121','122','123','124','125','126','127','128']
  66. rate_list = []
  67. date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
  68. for channel_id in channel_ids:
  69. channel_df = df[df['trans_id'] == channel_id]
  70. if channel_df.empty is True:
  71. continue
  72. total_count = channel_df.groupby('order_id').size().shape[0]
  73. succ_count = channel_df[channel_df['status'] == '2'].shape[0]
  74. if total_count > 0 :
  75. rate = succ_count / total_count
  76. else:
  77. rate = 0
  78. channel_name = channel_df['channel_name'].iloc[0]
  79. rate_list.append((date,channel_id,channel_name,-100,100,rate))
  80. try:
  81. stat_cursor = stat_conn.cursor()
  82. stat_cursor.executemany(ins_sql,rate_list)
  83. except:
  84. stat_conn.rollback()
  85. print('数据回滚')
  86. print(222222222222222222)
  87. conn.close()
  88. stat_conn.close()
  89. @cel.task
  90. def get_channel_rate():
  91. conn = createPool(channel_db_config).connection()
  92. stat_conn = createPool(stat_db_config).connection()
  93. #print(conn)
  94. channel_sql = """ SELECT
  95. t1.order_id,trans_id,channel_name,flow_amount,status,t1.cnt
  96. FROM
  97. (SELECT
  98. order_id,trans_id,flow_amount,send_status AS status,COUNT(*) AS cnt
  99. FROM
  100. mobile_flow_dispatch_rec
  101. WHERE
  102. UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(create_date) <= (3 * 3600)
  103. AND trans_id NOT IN(92,93,94,96,97,98,101,102,103,107,108,109,110,111,112,113,114,115)
  104. GROUP BY
  105. order_id,status )t1 LEFT JOIN access_channel_info
  106. ON t1.trans_id = access_channel_info.channel_seq_id"""
  107. ins_sql = 'INSERT INTO channel_success_rate VALUES(0,%s,%s,%s,%s,%s,%s)'
  108. channel_ids = ['83','84','85','89','90','91','95','104','105','116','117','118','119','120',
  109. '121','122','123','124','125','126','127','128']
  110. faces = [30,50,100,200,300,500]
  111. df = pd.read_sql(channel_sql,con = conn)
  112. df['flow_amount'] = df['flow_amount'].astype(int)
  113. #df['cnt'] = df['cnt'].astype(int)
  114. #print(df)
  115. sucess_rate = []
  116. for channel_id in channel_ids:
  117. channel_df = df[df['trans_id'] == channel_id]
  118. if channel_df.empty is True:
  119. continue
  120. channel_name = channel_df['channel_name'].iloc[0]
  121. #print(channel_name)
  122. for face in faces:
  123. face_df = channel_df[channel_df['flow_amount'] == face]
  124. if face_df.empty is True:
  125. continue
  126. #print(face_df)
  127. total_count = face_df.groupby('order_id').size().shape[0]
  128. #print(total_count)
  129. #print(channel_df)
  130. order_ids = face_df.groupby('order_id').count().index
  131. #print(order_ids)
  132. suc_count1 = 0
  133. suc_count2 = 0
  134. suc_count3 = 0
  135. for order_id in order_ids:
  136. #print(order_id)
  137. la_df = face_df[face_df['order_id'] == order_id]
  138. if la_df[la_df['status'] == '2'].empty is False:
  139. #print(la_df)
  140. if la_df[la_df['status'] == '4'].empty is True:
  141. suc_count1 += 1
  142. elif la_df[la_df['status'] == '4']['cnt'].iloc[0] == 1:
  143. suc_count2 += 1
  144. elif la_df[la_df['status'] == '4']['cnt'].iloc[0] == 2:
  145. suc_count3 += 1
  146. date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
  147. #print(date)
  148. suc_rate1 = suc_count1 / total_count
  149. total_count2 = total_count - suc_count1
  150. if total_count2 > 0 :
  151. suc_rate2 = suc_count2 / total_count2
  152. else:
  153. suc_rate2 = 0
  154. total_count3 = total_count2 - suc_count3
  155. if total_count3 > 0 :
  156. suc_rate3 = suc_count3 / total_count3
  157. else:
  158. suc_rate3 = 0
  159. sucess_rate.append((date,channel_id,channel_name,face,1,suc_rate1))
  160. sucess_rate.append((date,channel_id,channel_name,face,2,suc_rate2 ))
  161. sucess_rate.append((date,channel_id,channel_name,face,3,suc_rate3 ))
  162. try:
  163. stat_cursor = stat_conn.cursor()
  164. stat_cursor.executemany(ins_sql,sucess_rate)
  165. except:
  166. stat_conn.rollback()
  167. print('数据回滚')
  168. print(11111111111111111)
  169. conn.close()
  170. stat_conn.close()
  171. #get_channel_total_rate()
  172. #if __name__ == '__main__':