data_stat_task.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 clean_order(dt):
  44. sql=""" replace into ods_order
  45. select
  46. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  47. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  48. ELSE left(order_time,10) end date,
  49. stage,platform,channel,channel_id,user_id,
  50. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  51. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  52. ELSE order_time end order_time,
  53. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  54. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  55. ELSE reg_time end reg_time,
  56. amount,from_novel,order_id from `order` where date=UNIX_TIMESTAMP('{}')
  57. """.format(dt)
  58. db.quchen_text.execute(sql)
  59. if __name__ == '__main__':
  60. li=dt.getDateLists('2019-03-01','2020-01-01')
  61. for i in li:
  62. print(i)
  63. clean_order(i)