data_stat_task.py 6.8 KB

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