data_stat_task.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 dt,x.channel,pitcher,stage,platform,book,
  13. ifnull(view_count,0),ifnull(click_count,0),ifnull(follow_user,0),ifnull(cost,0) 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. from
  18. (select dt,channel,stage,pitcher,platform,book from channel_info_daily where dt='{0}' and channel!='') x
  19. left join
  20. (select channel,type,sum(cost) as cost,sum(view_count) as view_count,sum(valid_click_count) as click_count,sum(from_follow_uv) as follow_user,
  21. sum(web_view_count) as web_view_count,sum(platform_view_count) as platform_view_count,sum(web_order_count) as web_order_count
  22. from
  23. (select account_id,cost,view_count,valid_click_count,round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  24. 0 as web_view_count,
  25. 0 as platform_view_count,
  26. 0 as web_order_count
  27. from daily_vx where date='{0} 00:00:00'
  28. union
  29. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  30. ifnull(web_commodity_page_view_count,0) as web_view_count,
  31. ifnull(platform_page_view_count,0) as platform_view_count,
  32. ifnull(web_order_count,0) as web_order_count
  33. from daily_qq where date='{0} 00:00:00') a
  34. left join
  35. (select account_id,channel,type from channel_by_account_daily where dt='{0}') b on a.account_id=b.account_id group by channel,type) z on x.channel=z.channel
  36. """.format(ymd)
  37. db.quchen_text.execute(sql)
  38. def channel_by_account_daily(ymd):
  39. """返回当天消耗账户对应的公众号表"""
  40. sql="""replace into channel_by_account_daily
  41. select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel,type from
  42. (select account_id,name,'qq' as type from advertiser_qq
  43. union
  44. select account_id,name,'vx' as type from advertiser_vx
  45. ) a
  46. left join
  47. (select b.account_id,b.name from
  48. (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
  49. 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)
  50. db.quchen_text.execute(sql)
  51. def channel_info_daily(ymd):
  52. """获取公众号某天的期数,投手,平台,书籍
  53. @ return [[]]
  54. """
  55. # 获取现在的全量公众号信息
  56. sql="""select '{}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
  57. select name from advertiser_vx where name is not null group by name-- 公众号全量表
  58. union
  59. select name from advertiser_qq where name is not null group by name
  60. union
  61. select name from account_change group by name
  62. union
  63. select channel as name from pitcher_change group by channel
  64. union
  65. select name from platform_change group by name
  66. union
  67. select name from book_change group by name) a left join (
  68. 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
  69. union
  70. select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_vx where name is not null and name !=''
  71. ) b on a.name=b.name""".format(ymd)
  72. data=db.quchen_text.get_data_list(sql)
  73. pitcher_change=db.quchen_text.getData(
  74. "select b.channel as channel,pitcher from "
  75. "(select max(start_time) as start_time,channel from pitcher_change "
  76. " where start_time<='{}' GROUP BY channel) a"
  77. " left join pitcher_change b on a.start_time=b.start_time and a.channel=b.channel".format(ymd))
  78. 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 "
  79. "where change_date<='{}' GROUP BY name) a "
  80. "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
  81. book_change=db.quchen_text.getData("select b.name as channel,book from (select max(start_time) as start_time,name from book_change "
  82. "where start_time<='{}' GROUP BY name) a "
  83. "left join book_change b on a.start_time=b.start_time and a.name=b.name".format(ymd))
  84. for i in data:
  85. for j in pitcher_change:
  86. if i[1]==j[0]:
  87. i[3]=j[1]
  88. for k in platform_change:
  89. if i[1]==k[0]:
  90. i[4]=k[1]
  91. for h in book_change:
  92. if i[1]==h[0]:
  93. i[5]=h[1]
  94. print(data)
  95. insert_sql="replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
  96. db.quchen_text.executeMany(insert_sql,data)
  97. def ods_order(dt):
  98. sql=""" replace into ods_order
  99. select
  100. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  101. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  102. ELSE order_time end date,
  103. stage,platform,channel,channel_id,user_id,
  104. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  105. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  106. ELSE order_time end order_time,
  107. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  108. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  109. ELSE reg_time end reg_time,
  110. amount,from_novel,order_id from `order` where date=UNIX_TIMESTAMP('{}')
  111. """.format(dt)
  112. db.quchen_text.execute(sql)
  113. def order_account_text():
  114. db.quchen_text.execute("truncate order_account_text")
  115. with open('./wending_account_config.csv',encoding='utf-8') as f:
  116. for i in f.readlines():
  117. db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
  118. def dw_daily_channel_plus():
  119. """快照表 每日一更新 t-1"""
  120. sql="""insert into dw_daily_channel_plus
  121. select
  122. dt,channel,pitcher,stage,platform,book,order_count,order_user,order_amount,first_order_count,first_order_user,first_order_amount,
  123. view_count,click_count,follow_user,cost,reg_order_count,reg_order_user,reg_order_amount,reg_order_amount30
  124. from
  125. (select dt,channel,picher as pitcher,stage,platform,book,cost,view_count,click_count,follow_user,order_count,order_user,order_amount from dw_daily_channel) a
  126. left outer join
  127. (select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt2,channel as channel2,
  128. sum(amount) as reg_order_amount,
  129. count(distinct user_id) as reg_order_user,
  130. count(1) as reg_order_count,
  131. sum(if(subtractDays(date, 30)>reg_time,toDecimal32(0,2),amount)) as reg_order_amount30
  132. from order where reg_time>'2019-03-18 00:00:00' group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel) b
  133. on dt=dt2 and channel=channel2 left outer join
  134. (select date as dt3,channel as channel3,
  135. count(1) as first_order_count,
  136. count(distinct user_id) as first_order_user,
  137. sum(amount) as first_order_amount
  138. from order where toDate(reg_time)=date group by date,channel) c
  139. on dt=dt3 and channel=channel3"""
  140. ck.execute("truncate table dw_daily_channel_plus")
  141. ck.execute(sql)
  142. print("ok")
  143. if __name__ == '__main__':
  144. # channel_by_account_daily('2020-12-17')
  145. # dw_daily_channel_cost('2020-12-17')
  146. # dw_order_channel_cost_sync_ck('2020-12-17')
  147. # exit(0)
  148. # ods_order('2020-12-20')
  149. # dw_daily_channel_plus()
  150. # exit()
  151. for i in dt.getDateLists('2019-03-18','2020-12-20'):
  152. print(i)
  153. # ods_order(i)
  154. # channel_by_account_daily(i)
  155. # channel_info_daily(i)
  156. # dw_daily_channel_cost(i)
  157. dw_order_channel_cost_sync_ck(i)