data_stat_task.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from model.DataBaseUtils import MysqlUtils,CkUtils
  2. from model.DateUtils import DateUtils
  3. from model.log import logger
  4. log=logger()
  5. db = MysqlUtils()
  6. ck = CkUtils()
  7. dt = DateUtils()
  8. from datetime import datetime
  9. from sync_to_ck_task import dw_order_channel_cost_sync_ck
  10. def dw_daily_channel_cost(ymd):
  11. sql="""replace into dw_daily_channel_cost
  12. select x.dt,x.channel,pitcher,stage,x.platform,x.book,
  13. ifnull(view_count,0),ifnull(click_count,0),ifnull(follow_user,0),ifnull(cost,0)/100 as cost,
  14. ifnull(web_view_count,0) web_view_count,
  15. ifnull(platform_view_count,0) platform_view_count,
  16. ifnull(web_order_count,0) web_order_count,
  17. ifnull(type,''),ifnull(require_roi,0),ifnull(require_mult,0)
  18. from
  19. (select dt,channel,stage,pitcher,platform,book from channel_info_daily where dt='{0}' and channel!='') x
  20. left join
  21. (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,
  22. sum(web_view_count) as web_view_count,sum(platform_view_count) as platform_view_count,sum(web_order_count) as web_order_count
  23. from
  24. (select account_id,cost,view_count,valid_click_count,round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  25. 0 as web_view_count,
  26. 0 as platform_view_count,
  27. 0 as web_order_count
  28. from daily_vx where date='{0} 00:00:00'
  29. union
  30. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  31. ifnull(web_commodity_page_view_count,0) as web_view_count,
  32. ifnull(platform_page_view_count,0) as platform_view_count,
  33. ifnull(web_order_count,0) as web_order_count
  34. from daily_qq where date='{0} 00:00:00') a
  35. left join
  36. (select account_id,channel from channel_by_account_daily where dt='{0}') b on a.account_id=b.account_id group by channel)
  37. z on x.channel=z.channel
  38. left join (SELECT channel,type from channel_by_account_daily GROUP By channel,type) k on x.channel=k.channel
  39. left join (
  40. select dt,book,platform,require_roi,require_mult from ods_book_info_daily
  41. )m on x.book=m.book and x.platform=m.platform and x.dt=m.dt
  42. """.format(ymd)
  43. print(sql)
  44. db.quchen_text.execute(sql)
  45. def channel_by_account_daily(ymd):
  46. """返回当天消耗账户对应的公众号表"""
  47. sql="""replace into channel_by_account_daily
  48. select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel,type from
  49. (select account_id,name,'qq' as type from advertiser_qq
  50. union
  51. select account_id,name,'vx' as type from advertiser_vx
  52. ) a
  53. left join
  54. (select b.account_id,b.name from
  55. (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
  56. 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(ymd)
  57. print(sql)
  58. db.quchen_text.execute(sql)
  59. def channel_info_daily(ymd):
  60. """获取公众号某天的期数,投手,平台,书籍
  61. @ return [[]]
  62. """
  63. # 获取现在的全量公众号信息
  64. sql="""select '{}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
  65. select name from advertiser_vx where name is not null group by name-- 公众号全量表
  66. union
  67. select name from advertiser_qq where name is not null group by name
  68. union
  69. select name from account_change group by name
  70. union
  71. select channel as name from pitcher_change group by channel
  72. union
  73. select name from platform_change group by name
  74. union
  75. select name from book_change group by name) a left join (
  76. select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_qq where name is not null group by name,stage,pitcher,platform,book
  77. union
  78. select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_vx where name is not null and name !=''
  79. ) b on a.name=b.name
  80. """.format(ymd)
  81. data=db.quchen_text.get_data_list(sql)
  82. pitcher_change=db.quchen_text.getData(
  83. "select b.channel as channel,pitcher from "
  84. "(select max(start_time) as start_time,channel from pitcher_change "
  85. " where start_time<='{}' GROUP BY channel) a"
  86. " left join pitcher_change b on a.start_time=b.start_time and a.channel=b.channel".format(ymd))
  87. platform_change=db.quchen_text.getData("select b.name as channel,current_platform as platform from (select max(change_date) as change_date,name from platform_change "
  88. "where change_date<='{}' GROUP BY name) a "
  89. "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
  90. book_change=db.quchen_text.getData("select b.name as channel,book from (select max(start_time) as start_time,name from book_change "
  91. "where start_time<='{}' GROUP BY name) a "
  92. "left join book_change b on a.start_time=b.start_time and a.name=b.name".format(ymd))
  93. for i in data:
  94. for j in pitcher_change:
  95. if i[1]==j[0]:
  96. i[3]=j[1]
  97. for k in platform_change:
  98. if i[1]==k[0]:
  99. i[4]=k[1]
  100. for h in book_change:
  101. if i[1]==h[0]:
  102. i[5]=h[1]
  103. print(data)
  104. insert_sql="replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
  105. db.quchen_text.executeMany(insert_sql,data)
  106. def ods_order(dt):
  107. sql=""" replace into ods_order
  108. select
  109. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  110. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  111. ELSE order_time end date,
  112. stage,platform,channel,channel_id,user_id,
  113. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  114. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  115. ELSE order_time end order_time,
  116. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  117. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  118. ELSE reg_time end reg_time,
  119. amount,from_novel,order_id from `order` where date=UNIX_TIMESTAMP('{}')
  120. """.format(dt)
  121. db.quchen_text.execute(sql)
  122. def order_account_text():
  123. db.quchen_text.execute("truncate order_account_text")
  124. with open('./wending_account_config.csv',encoding='utf-8') as f:
  125. for i in f.readlines():
  126. db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
  127. def dw_channel_daily_total(ymd):
  128. sql=f"""insert into dw_channel_daily_total
  129. select '{ymd}' dt,channel,total_cost,total_amount,total_first_amount from
  130. (select channel,sum(cost) total_cost from dw_daily_channel_cost where dt<='{ymd}' group by channel)a
  131. left outer join
  132. (select channel,sum(amount) total_amount,
  133. sum(if(toDate(formatDateTime(reg_time,'%Y-%m-%d'))=date,amount,0)) total_first_amount
  134. from order where date<='{ymd}' group by channel) b
  135. on a.channel=b.channel
  136. """
  137. ck.execute(f"alter table dw_channel_daily_total drop partition '{ymd}'")
  138. print(sql)
  139. ck.execute(sql)
  140. if __name__ == '__main__':
  141. for i in dt.getDateLists('2019-03-18','2021-01-27'):
  142. channel_by_account_daily(i)
  143. channel_info_daily(i)
  144. # dw_daily_pitcher('2021-01-14')
  145. # dw_daily_channel_cost('2021-01-28')
  146. exit(0)
  147. # dw_channel_daily_total('2020-07-20')
  148. # channel_by_account_daily('2020-12-17')
  149. # dw_daily_channel_cost('2020-12-17')
  150. # dw_order_channel_cost_sync_ck('2020-12-17')
  151. # exit(0)
  152. # ods_order('2020-12-20')
  153. # dw_daily_channel_plus()
  154. # exit()
  155. # dw_daily_channel()
  156. # exit(0)
  157. # dm_pitcher_daily_page_total()
  158. # dm_pitcher_daily_page_total()
  159. # exit(0)
  160. # dw_channel_daily_total('2021-01-11')
  161. # dw_daily_channel_cost('2021-01-12')
  162. # dw_channel_daily_total('2021-01-13')
  163. # exit(0)
  164. # dw_daily_channel()
  165. # # exit(0)
  166. # for i in dt.getDateLists('2019-03-18','2021-01-14'):
  167. # print(i)
  168. # dw_channel_daily_total(i)
  169. # dw_daily_pitcher(i)
  170. # channel_by_account_daily(i)
  171. # dw_daily_channel_cost(i)
  172. # dw_order_channel_cost_sync_ck(i)
  173. # dw_daily_channel()
  174. # order_sync_ck(today)
  175. # # ods_order(i)
  176. # channel_info_daily(i)
  177. # dw_daily_channel_cost(i)
  178. # dw_order_channel_cost_sync_ck(i)
  179. # dw_channel_daily_total(i)