sync_to_ck_task.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 order(ymd):
  10. log.debug("sync order")
  11. col = "date,stage,platform,channel,channel_id,user_id,order_time,reg_time,amount,from_novel,order_id"
  12. sql ="select * from ods_order where date='{}'".format(ymd)
  13. data = db.quchen_text.getData(sql)
  14. # 掌中云时间格式转换
  15. data1 = []
  16. for x in data:
  17. li = list(x)
  18. li[0]=str(li[0])
  19. li[6]=str(li[6])
  20. li[7]=str(li[7])
  21. #
  22. # li[6] = li[6].replace('T',' ').replace('+08:00','').replace('/','-')
  23. #
  24. # if li[6] == None or li[6] == '':
  25. # li[6] = '0000-00-00 00:00:00'
  26. #
  27. # if li[7]==None or li[7]=='':
  28. # li[7]='0000-00-00 00:00:00'
  29. # else:
  30. # li[7] = li[7].replace('T',' ').replace('+08:00','').replace('/','-')
  31. #
  32. # try:
  33. # li[6]=str(datetime.strptime(li[6],'%Y-%m-%d %H:%M'))
  34. # li[7]=str(datetime.strptime(li[7],'%Y-%m-%d %H:%M'))
  35. # # li[7]=str(datetime.strptime(li[7],'%Y/%m/%d %H:%M:%S'))
  36. # except:
  37. # pass
  38. # print("======================")
  39. # try:
  40. # li[6] = str(datetime.strptime(li[6][:17], '%Y-%m-%d %H:%M'))
  41. # li[7] = str(datetime.strptime(li[7][:17], '%Y/%m/%d %H:%M'))
  42. # except:
  43. # pass
  44. # li[6]='2020-11-01 00:00:00'
  45. # li[7]='2020-11-01 00:00:00'
  46. # li[6] =li[6][:19]
  47. # li[7] =li[7][:19]
  48. #
  49. # li[9] = '' if li[9] is None else li[9]
  50. # li[5] = '' if li[5] is None else li[5]
  51. # li[1] = '' if li[1] is None else li[1]
  52. #
  53. data1.append(tuple(li))
  54. # 删除分区
  55. ck.execute("alter table order drop partition '{}' ".format(ymd))
  56. ck.insertMany("order", col, tuple(data1))
  57. def daily_qq(mydt):
  58. log.debug("sync daily_qq")
  59. table='daily_qq'
  60. col=db.quchen_text.getColumn("daily_qq")
  61. sql = "select * from {} where date='{} 00:00:00'".format(table,mydt)
  62. data = db.quchen_text.getData(sql)
  63. li1 = []
  64. for i in data:
  65. li2 = list(i)
  66. li2[1] = str(li2[1])
  67. li2[12]=round(i[12],4)
  68. li1.append(tuple(li2))
  69. ck.execute("alter table {} drop partition '{}' ".format(table, mydt))
  70. ck.insertMany(table, col, li1)
  71. def daily_vx(mydt):
  72. log.debug("sync daily_vx")
  73. table='daily_vx'
  74. col=db.quchen_text.getColumn(table)
  75. sql = "select * from {} where date='{} 00:00:00'".format(table,mydt)
  76. data = db.quchen_text.getData(sql)
  77. # print(len(data))
  78. li1 = []
  79. for i in data:
  80. li2 = list(i)
  81. li2[1] = str(li2[1])
  82. li2[5] = round(i[5],4)
  83. li2[6] = round(i[6],4)
  84. li2[7] = round(i[7],4)
  85. li2[8] = round(i[8],4)
  86. li2[10] = round(i[10],4)
  87. li1.append(tuple(li2))
  88. ck.execute("alter table {} drop partition '{}' ".format(table, mydt))
  89. ck.insertMany(table, col, li1)
  90. # 广告计划
  91. def daily_vx_campaign(ymd):
  92. table = 'daily_vx_campaign'
  93. sql = 'select * from ' + table + ' where date="{} 00:00:00" '.format(ymd)
  94. data = db.quchen_text.getData(sql)
  95. data1 = []
  96. for i in data:
  97. li = list(i)
  98. li[1] = str(li[1])
  99. li[5] = round(li[5], 4)
  100. li[6] = round(li[6], 4)
  101. li[7] = round(li[7], 4)
  102. li[8] = round(li[8], 4)
  103. li[12] = round(li[12], 4)
  104. li[13] = round(li[13], 4)
  105. li[15] = round(li[15], 4)
  106. data1.append(tuple(li))
  107. col = db.quchen_text.getColumn("daily_vx_campaign")
  108. ck.execute("alter table order drop partition '{}' ".format(ymd))
  109. ck.insertMany(table, col, tuple(data1))
  110. def advertiser_vx_qq():
  111. sql = "select account_id,name,stage,pitcher,platform,book,'vx' as tag from advertiser_vx " \
  112. " union " \
  113. "select account_id,name,stage,pitcher,platform,book,'qq' as tag from advertiser_qq"
  114. data = db.quchen_text.getData(sql)
  115. data1 = []
  116. for i in data:
  117. li = list(i)
  118. for idx, val in enumerate(li):
  119. if val == None:
  120. li[idx] = ''
  121. data1.append(tuple(li))
  122. ck.execute("truncate table advertiser_vx_qq")
  123. ck.execute("insert into advertiser_vx_qq (account_id,name,stage,pitcher,platform,book,tag) values " + str(data1)[1:-1])
  124. def pitcher_change():
  125. sql="select channel,pitcher,ifnull(start_time,'') as start_time," \
  126. " ifnull(end_time,'') as end_time from pitcher_change"
  127. data=db.quchen_text.getData(sql)
  128. ck.execute("truncate table pitcher")
  129. ck.execute("insert into pitcher_change values {}".format(str(data)[1:-1]))
  130. def book_change():
  131. table="book_change"
  132. col="name,book,start_time,end_time"
  133. sql="select name,book,ifnull(start_time,'') as start_time,ifnull(end_time,'') as end_time from {}".format(table)
  134. data=db.quchen_text.getData(sql)
  135. ck.execute("truncate table {}".format(table))
  136. ck.insertMany(table,col,data)
  137. def account_change():
  138. table = "account_change"
  139. col = "account_id,name,pitcher,start_time,end_time"
  140. sql = "select account_id,name,pitcher,ifnull(start_time,'') as start_time,ifnull(end_time,'') as end_time from {}".format(table)
  141. data = db.quchen_text.getData(sql)
  142. ck.execute("truncate table {}".format(table))
  143. ck.insertMany(table, col, data)
  144. def platform_change():
  145. table = "platform_change"
  146. col = "name,primary_platform,current_platform,change_date"
  147. sql = "select name,primary_platform,current_platform,date_format(change_date,'%Y-%m-%d') as change_date from {}".format(table)
  148. data = db.quchen_text.getData(sql)
  149. ck.execute("truncate table {}".format(table))
  150. ck.insertMany(table, col, data)
  151. def adcreative():
  152. """todo:表需要重新设计 无法分区"""
  153. pass
  154. def campaign_vx():
  155. """todo:表需要重新设计 无法分区"""
  156. # table="campaign_vx"
  157. # sql="select * from {}".format(table)
  158. # data=db.quchen_text.getData(sql)
  159. pass
  160. if __name__ == '__main__':
  161. order('2020-12-09')