AccountHourlySumToDayReport.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2022/3/24 9:53 上午
  4. # @Site :
  5. # @File : AccountHourlySumToDayReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import datetime
  10. import logging
  11. from Apietl.conn.TidbConn import TidbConn
  12. class AccountHourlySumToDayReport:
  13. def __init__(self):
  14. self.conn = TidbConn.get_connection()
  15. self.stat_date = (datetime.date.today() + datetime.timedelta(0)).strftime("%Y%m%d")
  16. def hourly_sum_report(self):
  17. try:
  18. sql = """
  19. replace into application.kuaishou_account_report_daily_dw
  20. SELECT
  21. report.account_id,
  22. report.stat_date ,
  23. bei.project_id,
  24. bei.product_id,
  25. bei.advertiser_id,
  26. bei.company_id,
  27. bei.account_name,
  28. bei.project_name,
  29. bei.advertiser_name,
  30. bei.company_name,
  31. bei.sale_id,
  32. bei.user_id,
  33. bei.org_code,
  34. report.charge,
  35. report.`show`,
  36. report.photo_click,
  37. report.aclick,
  38. report.bclick,
  39. report.photo_click_ratio,
  40. report.play_3s_ratio,
  41. report.action_ratio,
  42. report.impression_1k_cost,
  43. report.photo_click_cost,
  44. report.action_cost,
  45. report.`share`,
  46. report.`comment`,
  47. report.`like`,
  48. report.follow,
  49. report.cancel_follow,
  50. report.report,
  51. report.block,
  52. report.negative,
  53. report.submit,
  54. report.download_started,
  55. report.download_completed,
  56. report.activation,
  57. report.event_pay_first_day,
  58. report.event_pay_purchase_amount_first_day,
  59. report.event_pay_first_day_roi,
  60. report.event_pay,
  61. report.event_pay_purchase_amount,
  62. report.event_pay_roi,
  63. report.event_register,
  64. report.event_register_cost,
  65. report.event_register_ratio,
  66. report.event_jin_jian_app,
  67. report.event_jin_jian_app_cost,
  68. report.event_credit_grant_app,
  69. report.event_credit_grant_app_cost,
  70. report.event_credit_grant_app_ratio,
  71. report.event_order_paid,
  72. report.event_order_paid_purchase_amount,
  73. report.event_order_paid_cost,
  74. report.event_next_day_stay,
  75. report.event_next_day_stay_cost,
  76. report.event_next_day_stay_ratio,
  77. report.form_count,
  78. report.form_cost,
  79. report.form_action_ratio,
  80. report.event_jin_jian_landing_page,
  81. report.event_jin_jian_landing_page_cost,
  82. report.event_credit_grant_landing_page,
  83. report.event_credit_grant_landing_page_cost,
  84. report.event_credit_grant_landing_page_ratio,
  85. report.event_valid_clues,
  86. report.event_valid_clues_cost,
  87. report.event_add_wechat,
  88. report.event_add_wechat_cost,
  89. report.event_add_wechat_ratio,
  90. report.event_get_through,
  91. report.event_get_through_cost,
  92. report.event_get_through_ratio,
  93. report.event_app_invoked,
  94. report.event_app_invoked_cost,
  95. report.event_app_invoked_ratio,
  96. report.event_credit_grant_landing_ratio,
  97. report.play_5s_ratio,
  98. report.play_end_ratio,
  99. report.event_new_user_pay,
  100. report.event_new_user_pay_cost,
  101. report.event_new_user_pay_ratio,
  102. report.click_1k_cost,
  103. report.ad_product_cnt,
  104. report.event_goods_view,
  105. report.merchant_reco_fans,
  106. report.event_order_amount_roi,
  107. report.event_goods_view_cost,
  108. report.merchant_reco_fans_cost,
  109. report.event_button_click,
  110. report.event_button_click_cost,
  111. report.event_button_click_ratio,
  112. report.event_order_paid_roi,
  113. report.event_new_user_jinjian_app,
  114. report.event_new_user_jinjian_app_cost,
  115. report.event_new_user_jinjian_app_roi,
  116. report.event_new_user_credit_grant_app,
  117. report.event_new_user_credit_grant_app_cost,
  118. report.event_new_user_credit_grant_app_roi,
  119. report.event_new_user_jinjian_page,
  120. report.event_new_user_jinjian_page_cost,
  121. report.event_new_user_jinjian_page_roi,
  122. report.event_new_user_credit_grant_page,
  123. report.event_new_user_credit_grant_page_cost,
  124. report.event_new_user_credit_grant_page_roi,
  125. report.event_appoint_form,
  126. report.event_appoint_form_cost,
  127. report.event_appoint_form_ratio,
  128. report.event_appoint_jump_click,
  129. report.event_appoint_jump_click_cost,
  130. report.event_appoint_jump_click_ratio,
  131. report.union_event_pay_purchase_amount_7d,
  132. report.union_event_pay_purchase_amount_7d_roi,
  133. '' as placement_type,
  134. '' as ad_scene,
  135. report.event_order_successed,
  136. report.ad_photo_played_10s_ratio,
  137. report.key_action_cost,
  138. report.event_add_shopping_cart_cost,
  139. report.event_ad_watch_times_ratio,
  140. report.event_outbound_call_cost,
  141. report.event_outbound_call,
  142. report.action_new_ratio,
  143. report.event_watch_app_ad,
  144. report.event_multi_conversion_cost,
  145. report.event_phone_card_activate,
  146. report.ad_photo_played_75percent_ratio,
  147. report.key_action_ratio,
  148. report.event_ad_watch_times_cost,
  149. report.event_add_shopping_cart,
  150. report.key_action,
  151. report.event_multi_conversion,
  152. report.event_wechat_connected,
  153. report.event_dsp_gift_form,
  154. report.event_intention_confirmed,
  155. report.event_phone_get_through,
  156. report.event_multi_conversion_ratio,
  157. report.event_measurement_house,
  158. report.ad_photo_played_2s_ratio,
  159. report.event_outbound_call_ratio,
  160. report.event_ad_watch_times
  161. from
  162. (select
  163. stat_date,
  164. advertiser_id account_id,
  165. sum(charge) charge,
  166. sum(`show`) `show`,
  167. sum(photo_click)photo_click,
  168. sum(aclick) aclick,
  169. sum(bclick) bclick,
  170. ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
  171. ifnull(round(sum(play_3s_ratio)*100,3),0) play_3s_ratio,
  172. ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
  173. ifnull(round(sum(impression_1k_cost),3),0) impression_1k_cost,
  174. ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
  175. ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
  176. sum(`share`) `share`,
  177. sum(`comment`) `comment`,
  178. sum(`like`) `like`,
  179. sum(follow) follow,
  180. sum(cancel_follow) cancel_follow,
  181. sum(report) report,
  182. sum(block) block,
  183. sum(negative) negative,
  184. sum(submit) submit,
  185. sum(download_started) download_started,
  186. sum(download_completed) download_completed,
  187. sum(activation) activation,
  188. sum(event_pay_first_day) event_pay_first_day,
  189. sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
  190. ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
  191. sum(event_pay) event_pay,
  192. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  193. ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
  194. sum(event_register) event_register,
  195. ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
  196. ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
  197. sum(event_jin_jian_app) event_jin_jian_app,
  198. ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
  199. sum(event_credit_grant_app) event_credit_grant_app,
  200. ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
  201. ifnull(round(sum(event_credit_grant_app_ratio)*100,3),0) event_credit_grant_app_ratio,
  202. sum(event_order_paid) event_order_paid,
  203. sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
  204. ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
  205. sum(event_next_day_stay) event_next_day_stay,
  206. ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
  207. ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
  208. sum(form_count) form_count,
  209. ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
  210. ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
  211. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  212. ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
  213. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  214. ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
  215. ifnull(round(sum(event_credit_grant_landing_page)*100,3),0) event_credit_grant_landing_page_ratio,
  216. sum(event_valid_clues) event_valid_clues,
  217. ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
  218. sum(event_add_wechat) event_add_wechat,
  219. ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
  220. ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
  221. sum(event_get_through) event_get_through,
  222. ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
  223. ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
  224. sum(event_app_invoked) event_app_invoked,
  225. ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
  226. ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
  227. ifnull(round(sum(event_credit_grant_landing_ratio)*100,3),0) event_credit_grant_landing_ratio,
  228. ifnull(round(sum(play_5s_ratio)*100,3),0) play_5s_ratio,
  229. ifnull(round(sum(play_end_ratio)*100,3),0) play_end_ratio,
  230. sum(event_new_user_pay) event_new_user_pay,
  231. ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
  232. ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
  233. ifnull(round(sum(click_1k_cost)*100,3),0) click_1k_cost,
  234. sum(ad_product_cnt) ad_product_cnt,
  235. sum(event_goods_view) event_goods_view,
  236. sum(merchant_reco_fans) merchant_reco_fans,
  237. ifnull(round(sum(event_order_amount_roi)*100,3),0) event_order_amount_roi,
  238. ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
  239. ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
  240. sum(event_button_click) event_button_click,
  241. ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
  242. ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
  243. ifnull(round(sum(event_order_paid_roi)*100,3),0) event_order_paid_roi,
  244. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  245. ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
  246. ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
  247. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  248. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0) event_new_user_credit_grant_app_cost,
  249. ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
  250. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  251. ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
  252. ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
  253. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  254. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
  255. ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
  256. sum(event_appoint_form) event_appoint_form,
  257. ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
  258. ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
  259. sum(event_appoint_jump_click) event_appoint_jump_click,
  260. ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0) event_appoint_jump_click_cost,
  261. ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
  262. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  263. ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)*100,3),0) union_event_pay_purchase_amount_7d_roi,
  264. sum(event_order_successed) event_order_successed,
  265. ifnull(round(sum(ad_photo_played_10s_ratio)*100,3),0) ad_photo_played_10s_ratio,
  266. ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
  267. ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
  268. ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
  269. ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
  270. sum(event_outbound_call) event_outbound_call,
  271. ifnull(round(sum(action_new_ratio)*100,3),0) action_new_ratio,
  272. sum(event_watch_app_ad) event_watch_app_ad,
  273. ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
  274. sum(event_phone_card_activate) event_phone_card_activate,
  275. ifnull(round(sum(ad_photo_played_75percent_ratio)*100,3),0) ad_photo_played_75percent_ratio,
  276. ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
  277. ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
  278. sum(event_add_shopping_cart) event_add_shopping_cart,
  279. sum(key_action) key_action,
  280. sum(event_multi_conversion) event_multi_conversion,
  281. sum(event_wechat_connected) event_wechat_connected,
  282. sum(event_dsp_gift_form) event_dsp_gift_form,
  283. sum(event_intention_confirmed) event_intention_confirmed,
  284. sum(event_phone_get_through) event_phone_get_through,
  285. ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0) event_multi_conversion_ratio,
  286. sum(event_measurement_house) event_measurement_house,
  287. ifnull(round(sum(ad_photo_played_2s_ratio)*100,3),0) ad_photo_played_2s_ratio,
  288. ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
  289. sum(event_ad_watch_times) event_ad_watch_times
  290. from
  291. media_api.kuaishou_account_report_hourly a
  292. where stat_date={stat_date}
  293. group by advertiser_id) report
  294. LEFT JOIN (
  295. SELECT
  296. cua.account_id id,
  297. cua.account_id account_id,
  298. cp.id project_id,
  299. product.id product_id,
  300. ca.id advertiser_id,
  301. uc.company_id company_id,
  302. cua.auth_name account_name,
  303. cp.project_name project_name,
  304. ca.`name` advertiser_name,
  305. uc.company_name company_name,
  306. cp.sale_id sale_id,
  307. su.id user_id,
  308. su.leader_id,
  309. su.org_code org_code
  310. FROM
  311. `jeecg-boot`.ctop_user_allocation cua
  312. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  313. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  314. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  315. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  316. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  317. WHERE
  318. cua.account_id IS NOT NULL) bei
  319. ON report.account_id = bei.account_id;
  320. """.format(stat_date=self.stat_date)
  321. print(sql)
  322. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  323. self.conn.commit()
  324. TidbConn.conn_close(conn=self.conn)
  325. return 0
  326. except Exception as e:
  327. logging.error(e)
  328. return -1
  329. if __name__ == '__main__':
  330. AccountHourlySumToDayReport().hourly_sum_report()