data_stat_task.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(ymd):
  10. sql="""replace into dw_daily_channel
  11. select dt,x.channel,pitcher,stage,platform,book,ifnull(order_count,0),ifnull(order_user,0),ifnull(order_amount,0),ifnull(first_order_count,0),ifnull(first_order_user,0),
  12. ifnull(first_order_amount,0),ifnull(view_count,0),ifnull(click_count,0),ifnull(follow_user,0),ifnull(cost,0) from
  13. (select dt,channel,stage,pitcher,platform,book from channel_info_daily where dt='{0}' and channel!='') x
  14. left join
  15. (select channel,count(1) as order_count,count(distinct user_id) as order_user,sum(amount) as order_amount,
  16. sum(if(date_format(reg_time,'%Y-%m-%d')=date_format(date,'%Y-%m-%d'),1,0)) as first_order_count,
  17. count(distinct if(date_format(reg_time,'%Y-%m-%d')=date_format(date,'%Y-%m-%d'),user_id,''))-1 as first_order_user,
  18. sum(if(date_format(reg_time,'%Y-%m-%d')=date_format(date,'%Y-%m-%d'),amount,0)) as first_order_amount
  19. from ods_order where date='{0}' group by channel) y on x.channel=y.channel
  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 from
  22. (select account_id,cost,view_count,valid_click_count,round(valid_click_count*official_account_follow_rate,0) as from_follow_uv from daily_vx where date='{0} 00:00:00'
  23. union
  24. select account_id,cost,view_count,valid_click_count,from_follow_uv from daily_qq where date='{0} 00:00:00') a
  25. left join
  26. (select account_id,channel from channel_by_account_daily where dt='{0}') b on a.account_id=b.account_id group by channel) z on x.channel=z.channel""".format(ymd)
  27. db.quchen_text.execute(sql)
  28. def channel_by_account_daily(ymd):
  29. """返回当天消耗账户对应的公众号表"""
  30. sql="""replace into channel_by_account_daily
  31. select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel from
  32. (select account_id,name from advertiser_qq
  33. union
  34. select account_id,name from advertiser_vx
  35. ) a
  36. left join
  37. (select b.account_id,b.name from
  38. (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
  39. 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)
  40. db.quchen_text.execute(sql)
  41. def channel_info_daily(ymd):
  42. """获取公众号某天的期数,投手,平台,书籍
  43. @ return [[]]
  44. """
  45. # 获取现在的全量公众号信息
  46. sql="""select '{}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
  47. select name from advertiser_vx where name is not null group by name-- 公众号全量表
  48. union
  49. select name from advertiser_qq where name is not null group by name
  50. union
  51. select name from account_change group by name
  52. union
  53. select channel as name from pitcher_change group by channel
  54. union
  55. select name from platform_change group by name
  56. union
  57. select name from book_change group by name) a left join (
  58. 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
  59. union
  60. select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_vx where name is not null and name !=''
  61. ) b on a.name=b.name""".format(ymd)
  62. data=db.quchen_text.get_data_list(sql)
  63. pitcher_change=db.quchen_text.getData(
  64. "select b.channel as channel,pitcher from "
  65. "(select min(end_time) as end_time,channel from pitcher_change "
  66. " where end_time>'{}' GROUP BY channel) a"
  67. " left join pitcher_change b on a.end_time=b.end_time and a.channel=b.channel".format(ymd))
  68. platform_change=db.quchen_text.getData("select b.name as channel,primary_platform as platform from (select min(change_date) as change_date,name from platform_change "
  69. "where change_date>'{}' GROUP BY name) a "
  70. "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
  71. book_change=db.quchen_text.getData("select b.name as channel,book from (select min(end_time) as end_time,name from book_change "
  72. "where end_time>'{}' GROUP BY name) a "
  73. "left join book_change b on a.end_time=b.end_time and a.name=b.name".format(ymd))
  74. for i in data:
  75. for j in pitcher_change:
  76. if i[1]==j[0]:
  77. i[3]=j[1]
  78. for k in platform_change:
  79. if i[1]==k[0]:
  80. i[4]=k[1]
  81. for h in book_change:
  82. if i[1]==h[0]:
  83. i[5]=h[1]
  84. print(data)
  85. insert_sql="replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
  86. db.quchen_text.executeMany(insert_sql,data)
  87. def ods_order(dt):
  88. sql=""" replace into ods_order
  89. select
  90. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  91. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  92. ELSE left(order_time,10) end date,
  93. stage,platform,channel,channel_id,user_id,
  94. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  95. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  96. ELSE order_time end order_time,
  97. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  98. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  99. ELSE reg_time end reg_time,
  100. amount,from_novel,order_id from `order` where date=UNIX_TIMESTAMP('{}')
  101. """.format(dt)
  102. db.quchen_text.execute(sql)
  103. def order_account_text():
  104. db.quchen_text.execute("truncate order_account_text")
  105. with open('./wending_account_config.csv',encoding='utf-8') as f:
  106. for i in f.readlines():
  107. db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
  108. if __name__ == '__main__':
  109. # dw_daily_channel('2020-12-14')
  110. # channel_info_daily('2020-12-14')
  111. for i in dt.getDateLists('2019-05-20','2020-12-16'):
  112. print(i)
  113. # channel_by_account_daily(i)
  114. # channel_info_daily(i)
  115. # dw_daily_channel(i)
  116. clean_order(i)