data_stat_task.py 11 KB

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