data_stat_task.py 8.4 KB

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