data_stat_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. from model.DataBaseUtils import MysqlUtils, CkUtils
  2. from datetime import datetime, timedelta, timezone
  3. from model.DateUtils import DateUtils
  4. import logging
  5. import time
  6. db = MysqlUtils()
  7. ck = CkUtils()
  8. dt = DateUtils()
  9. def platform_data_sum(ymd):
  10. logging.info('dw_daily_platform_cost开始数据更新')
  11. ck.execute("alter table game_data.dw_daily_platform_cost drop partition '{}' ".format(ymd))
  12. sql = f'''
  13. insert into game_data.dw_daily_platform_cost
  14. select * from game_data.dw_daily_channel_cost b where dt='{ymd}'
  15. '''
  16. ck.execute(sql)
  17. logging.info('dw_daily_platform_cost数据更新,结束')
  18. def dw_daily_channel_cost(ymd):
  19. logging.info("run> dw_daily_channel_cost")
  20. datatime_ymd = datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))).timetuple()
  21. timestamp_ymd = time.mktime(datatime_ymd)
  22. tomorrow_ymd = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  23. days=1)).timetuple()
  24. timestamp_tom = time.mktime(tomorrow_ymd)
  25. sql = f"""
  26. select x.dt,x.channel,pitcher,stage,x.platform,x.book,
  27. ifnull(view_count,0),ifnull(click_count,0),
  28. ifnull(follow_user,0),ifnull(cost,0)/100 as cost,
  29. ifnull(web_view_count,0) web_view_count,
  30. ifnull(platform_view_count,0) platform_view_count,
  31. ifnull(web_order_count,0) web_order_count,
  32. if(stage ='趣程15期' or stage ='趣程26期' or stage ='趣程30期','GDT','MP') type
  33. ,0 require_roi,0 require_mult,ifnull(y.reg_num,0),ifnull(w.create_user_num,0)
  34. from
  35. ( select dt, channel,stage,pitcher,platform,book from channel_info_daily cid
  36. where dt='{ymd}' and channel !=''
  37. and channel in
  38. (select distinct(channel) from channel_by_account_daily cbad
  39. where dt='{ymd}'
  40. and (type ='GDT' or type='MP')
  41. )
  42. ) x -- 只允许渠道MP、GDT
  43. left join
  44. (select channel,sum(cost) as cost,sum(view_count) as view_count,sum(valid_click_count) as click_count,
  45. sum(from_follow_uv) as follow_user,
  46. sum(web_view_count) as web_view_count,
  47. sum(platform_view_count) as platform_view_count,
  48. sum(web_order_count) as web_order_count
  49. from
  50. (select account_id,cost,view_count,valid_click_count,
  51. round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  52. 0 as web_view_count,
  53. 0 as platform_view_count,
  54. 0 as web_order_count
  55. from daily_vx where date='{ymd} 00:00:00'
  56. union
  57. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  58. ifnull(web_commodity_page_view_count,0) as web_view_count,
  59. ifnull(platform_page_view_count,0) as platform_view_count,
  60. ifnull(web_order_count,0) as web_order_count
  61. from daily_qq where date='{ymd} 00:00:00' ) a
  62. left join
  63. (select account_id,channel from channel_by_account_daily where dt='{ymd}') b
  64. on a.account_id=b.account_id group by channel)
  65. z on x.channel=z.channel
  66. left join
  67. (
  68. select c.name as channel ,DATE(FROM_UNIXTIME(origin.create_time)) as wx_date,
  69. count(*) as reg_num
  70. from
  71. db_mp.h_member origin left join
  72. db_mp.mp_conf_agent a on origin.app_id =a.app_id and origin.agent_id = a.agent_id
  73. left join
  74. db_mp.mp_mp_conf b on a.advertiser_conf_id =b.id
  75. left join
  76. quchen_text.advertiser_vx c on b.mp_id =c.wechat_account_id
  77. where c.wechat_account_id is not null
  78. and origin.create_time > {timestamp_ymd} and origin.create_time < {timestamp_tom}
  79. group by name,wx_date
  80. ) y on x.channel= y.channel
  81. left join
  82. (
  83. select f.name as channel,DATE(FROM_UNIXTIME(a.create_time)) as wx_date,
  84. count(*) as create_user_num
  85. from db_mp.h_mg_role a
  86. left join db_mp.h_mem_game b on a.mg_mem_id = b.id
  87. left join db_mp.h_member c on b.mem_id = c.id
  88. left join db_mp.mp_conf_agent d on c.app_id = d.app_id and c.agent_id = d.agent_id
  89. left join db_mp.mp_mp_conf e on d.advertiser_conf_id =e.id
  90. left join quchen_text.advertiser_vx f on e.mp_id = f.wechat_account_id
  91. where f.name is not null
  92. and c.create_time > {timestamp_ymd} and c.create_time < {timestamp_tom}
  93. group by f.name,wx_date
  94. order by wx_date desc
  95. ) w on x.channel= w.channel
  96. """
  97. data = db.quchen_text.get_data_list(sql)
  98. data1 = []
  99. col = "dt,channel,pitcher,stage,platform,book,view_count,click_count,follow_user,cost,web_view_count,platform_view_count,web_order_count,type,require_roi,require_mult,reg_num,create_user_num"
  100. for i in data:
  101. i[0] = str(i[0])
  102. i[9] = str(i[9])
  103. i[6] = float(i[6])
  104. i[7] = float(i[7])
  105. i[8] = float(i[8])
  106. i[9] = float(i[9])
  107. i[10] = float(i[10])
  108. i[11] = float(i[11])
  109. i[12] = float(i[12])
  110. data1.append(tuple(i))
  111. ck.execute(f"alter table game_data.dw_daily_channel_cost drop partition '{ymd}' ")
  112. logging.info(len(data1))
  113. ck.insertMany("game_data.dw_daily_channel_cost", col, tuple(data1))
  114. def channel_by_account_daily(ymd):
  115. """返回当天消耗账户对应的公众号表"""
  116. logging.info("run> channel_by_account_daily")
  117. sql = """replace into channel_by_account_daily
  118. select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel,type from
  119. (select account_id,name,'GDT' type from advertiser_qq
  120. union
  121. select account_id,name,'MP' type from advertiser_vx
  122. union
  123. select advertiser_id,channel,'BYTEDANCE' type from advertiser_bytedance
  124. ) a
  125. left join
  126. (select b.account_id,b.name from
  127. (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
  128. left join account_change b on a.end_time=b.end_time and a.account_id=b.account_id) b on a.account_id=b.account_id""".format(
  129. ymd)
  130. db.quchen_text.execute(sql)
  131. def channel_info_daily(ymd):
  132. """获取公众号某天的期数,投手,平台,书籍
  133. @ return [[]]
  134. """
  135. # 获取现在的全量公众号信息
  136. logging.info("run> channel_info_daily")
  137. sql = f"""select '{ymd}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
  138. select name from advertiser_vx where name is not null group by name-- 公众号全量表
  139. union
  140. select name from account_change group by name
  141. union
  142. select channel as name from pitcher_change group by channel
  143. union
  144. select name from platform_change group by name
  145. union
  146. select name from book_change group by name) a
  147. left join
  148. ( select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,
  149. ifnull(platform,'') platform,ifnull(book,'') book
  150. from advertiser_vx
  151. where name is not null
  152. and start_date <= '{ymd}'
  153. group by name,stage,pitcher,platform,book
  154. ) b on a.name=b.name
  155. """
  156. data = db.quchen_text.get_data_list(sql)
  157. pitcher_change = db.quchen_text.getData(
  158. "select b.channel as channel,pitcher from "
  159. "(select max(start_time) as start_time,channel from pitcher_change "
  160. " where start_time<='{}' GROUP BY channel) a"
  161. " left join pitcher_change b on a.start_time=b.start_time and a.channel=b.channel".format(ymd))
  162. platform_change = db.quchen_text.getData(
  163. "select b.name as channel,current_platform as platform from (select max(change_date) as change_date,name from platform_change "
  164. "where change_date<='{}' GROUP BY name) a "
  165. "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
  166. book_change = db.quchen_text.getData(
  167. "select b.name as channel,book from (select max(start_time) as start_time,name from book_change "
  168. "where start_time<='{}' GROUP BY name) a "
  169. "left join book_change b on a.start_time=b.start_time and a.name=b.name".format(ymd))
  170. stage_change = db.quchen_text.getData(
  171. "select channel,stage from (select max(start_date) as start_date,channel from stage_change "
  172. "where start_date<='{}' GROUP BY channel) a "
  173. "left join stage_change using(start_date,channel)".format(ymd))
  174. for i in data:
  175. for j in pitcher_change:
  176. if i[1] == j[0]:
  177. i[3] = j[1]
  178. for k in platform_change:
  179. if i[1] == k[0]:
  180. i[4] = k[1]
  181. for h in book_change:
  182. if i[1] == h[0]:
  183. i[5] = h[1]
  184. for m in stage_change:
  185. if i[1] == m[0]:
  186. i[2] = m[1]
  187. insert_sql = "replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
  188. db.quchen_text.executeMany(insert_sql, data)
  189. def ods_order(dt):
  190. sql = """ replace into ods_order
  191. select
  192. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  193. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  194. ELSE order_time end date,
  195. stage,platform,channel,channel_id,user_id,
  196. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  197. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  198. ELSE order_time end order_time,
  199. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  200. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  201. ELSE reg_time end reg_time,
  202. amount,from_novel,order_id,2 from `order` where date=UNIX_TIMESTAMP('{}')
  203. """.format(dt)
  204. db.quchen_text.execute(sql)
  205. def order_account_text():
  206. db.quchen_text.execute("truncate order_account_text")
  207. with open('./wending_account_config.csv', encoding='utf-8') as f:
  208. for i in f.readlines():
  209. db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
  210. if __name__ == '__main__':
  211. # channel_info_daily('2021-02-06')
  212. # channel_by_account_daily('2021-02-05')
  213. for i in dt.getDateLists('2021-04-18', '2021-10-28'):
  214. print(i)
  215. channel_info_daily(i)
  216. dw_daily_channel_cost(i)
  217. # ods_order('2021-05-06')
  218. platform_data_sum(i)