data_stat_task.py 12 KB

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