image_day.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import logging
  2. from model.DataBaseUtils import MysqlUtils,CkUtils
  3. from model.DateUtils import DateUtils
  4. # logging.getLogger().setLevel(logging.WARNING)
  5. import pandas as pd
  6. db = MysqlUtils()
  7. ck = CkUtils()
  8. du=DateUtils()
  9. def run(dt):
  10. # 单图片的消耗
  11. sql=f"""replace into dm_image_cost_day
  12. select a.dt,type,book,signature,sum(cost),sum(view_count),sum(click_count),sum(follow_count),min(preview_url) from
  13. (SELECT a.dt,a.ad_id,a.cost,c.image_id,preview_url,signature,view_count,click_count,follow_count,ad_name,b.type,title,description,e.channel,stage,pitcher,platform,book
  14. from
  15. ad_cost_day a
  16. left join ad_info b on a.ad_id=b.ad_id
  17. left join adcreative_info c on b.adcreative_id=c.adcreative_id
  18. left join channel_by_account_daily e on b.account_id=e.account_id and a.dt=e.dt
  19. left join channel_info_daily f on e.channel=f.channel and e.dt=f.dt
  20. left join image_info g on c.image_id=g.image_id
  21. where a.dt='{dt}' and INSTR(c.image_id,',')=0 and preview_url is not null ) a
  22. GROUP BY signature,book,type,a.dt
  23. """
  24. db.quchen_text.execute(sql)
  25. # 多图的
  26. def run2(dt):
  27. sql2 = f"""
  28. SELECT dt,image_id,sum(cost),sum(view_count),sum(click_count),sum(follow_count),type,book
  29. FROM
  30. (SELECT a.dt,a.ad_id,a.cost,c.image_id,view_count,click_count,follow_count,ad_name,b.type,title,description,e.channel,stage,pitcher,platform,book
  31. from
  32. ad_cost_day a
  33. left join ad_info b on a.ad_id=b.ad_id
  34. left join adcreative_info c on b.adcreative_id=c.adcreative_id
  35. left join channel_by_account_daily e on b.account_id=e.account_id and a.dt=e.dt
  36. left join channel_info_daily f on e.channel=f.channel and e.dt=f.dt
  37. where a.dt='{dt}' and INSTR(c.image_id,',')>0 ) a
  38. group by image_id,dt,type,book
  39. """
  40. data =db.quchen_text.get_data_list(sql2)
  41. # print(data)
  42. li=[]
  43. for i in data:
  44. li.extend(i[1].split(','))
  45. # print(set(li))
  46. sql3 =f"select image_id,preview_url,signature from image_info where image_id in ({str(set(li))[1:-1]})"
  47. image_di={}
  48. di2 = {}
  49. image_data =db.quchen_text.getData(sql3)
  50. for x in image_data:
  51. image_di[x[0]]=x[1]
  52. di2[x[0]]=x[2]
  53. # print(image_di)
  54. for i in data:
  55. y = ''
  56. p =''
  57. for j in i[1].split(','):
  58. if image_di.get(j):
  59. y = y+','+image_di.get(j)
  60. p = p+','+di2.get(j)
  61. i.append(y[1:])
  62. i.append(p[1:])
  63. df = pd.DataFrame(data)
  64. # print(df)
  65. df2 =df.groupby([0,6,7,9],as_index=False).agg({2:'sum',3:'sum',4:'sum',5:'sum',8:'min'})
  66. # print(df2)
  67. # print()
  68. db.quchen_text.executeMany('replace into dm_image_cost_day values(%s,%s,%s,%s,%s,%s,%s,%s,%s)',df2.values.tolist())
  69. def day():
  70. for i in du.getDateLists('2021-03-26','2021-04-08'):
  71. run(i)
  72. run2(i)
  73. if __name__ == '__main__':
  74. day()