AppKuaishouProjectReportBusiMonth.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppKuaishouProjectReportBusiMonth.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import logging
  10. from Apietl.conn.TidbConn import TidbConn
  11. from Apietl.utlis.Utils import Utils
  12. class AppKuaishouProjectReportBusiMonth:
  13. def __init__(self):
  14. self.conn = TidbConn.get_connection()
  15. self.parm = Utils.get_task_args()
  16. self.date_time = self.parm[0]
  17. self.day_count = Utils.get_days()
  18. def taskHandler(self):
  19. try:
  20. sql = """
  21. SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  22. REPLACE INTO application.app_kuaishou_project_report_busi_m
  23. SELECT
  24. date_format({date_time},'%Y%m') as stat_mon,
  25. ifnull(uc.company_id,'-') company_id,
  26. CASE WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司'
  27. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司'
  28. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司' ELSE uc.company_name END
  29. company_name,
  30. ifnull(cp.product_id,'-') product_id,
  31. cn.product_name,
  32. ifnull(cua.project_id,'-') project_id,
  33. cp.project_name,
  34. count(report.advertiser_id) account_num,
  35. sum(charge) charge,
  36. sum(`show`) `show`,
  37. sum(photo_click)photo_click,
  38. sum(aclick) aclick,
  39. sum(bclick) bclick,
  40. ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
  41. ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0) play_3s_ratio,
  42. ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
  43. ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
  44. ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
  45. ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
  46. sum(`share`) `share`,
  47. sum(`comment`) `comment`,
  48. sum(`like`) `like`,
  49. sum(follow) follow,
  50. sum(cancel_follow) cancel_follow,
  51. sum(report) report,
  52. sum(block) block,
  53. sum(negative) negative,
  54. sum(submit) submit,
  55. sum(download_started) download_started,
  56. sum(download_completed) download_completed,
  57. sum(activation) activation,
  58. sum(event_pay_first_day) event_pay_first_day,
  59. sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
  60. ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
  61. sum(event_pay) event_pay,
  62. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  63. ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
  64. sum(event_register) event_register,
  65. ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
  66. ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
  67. sum(event_jin_jian_app) event_jin_jian_app,
  68. ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
  69. sum(event_credit_grant_app) event_credit_grant_app,
  70. ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
  71. ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
  72. sum(event_order_paid) event_order_paid,
  73. sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
  74. ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
  75. sum(event_next_day_stay) event_next_day_stay,
  76. ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
  77. ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
  78. sum(form_count) form_count,
  79. ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
  80. ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
  81. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  82. ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
  83. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  84. ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
  85. ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
  86. sum(event_valid_clues) event_valid_clues,
  87. ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
  88. sum(event_add_wechat) event_add_wechat,
  89. ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
  90. ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
  91. sum(event_get_through) event_get_through,
  92. ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
  93. ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
  94. sum(event_app_invoked) event_app_invoked,
  95. ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
  96. ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
  97. ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
  98. ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
  99. ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
  100. sum(event_new_user_pay) event_new_user_pay,
  101. ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
  102. ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
  103. ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
  104. sum(ad_product_cnt) ad_product_cnt,
  105. sum(event_goods_view) event_goods_view,
  106. sum(merchant_reco_fans) merchant_reco_fans,
  107. ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
  108. ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
  109. ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
  110. sum(event_button_click) event_button_click,
  111. ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
  112. ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
  113. ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
  114. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  115. ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
  116. ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
  117. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  118. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0) event_new_user_credit_grant_app_cost,
  119. ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
  120. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  121. ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
  122. ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
  123. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  124. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
  125. ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
  126. sum(event_appoint_form) event_appoint_form,
  127. ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
  128. ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
  129. sum(event_appoint_jump_click) event_appoint_jump_click,
  130. ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0) event_appoint_jump_click_cost,
  131. ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
  132. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  133. ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
  134. sum(event_order_successed) event_order_successed,
  135. ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
  136. ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
  137. ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
  138. ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
  139. ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
  140. sum(event_outbound_call) event_outbound_call,
  141. ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
  142. sum(event_watch_app_ad) event_watch_app_ad,
  143. ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
  144. sum(event_phone_card_activate) event_phone_card_activate,
  145. ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
  146. ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
  147. ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
  148. sum(event_add_shopping_cart) event_add_shopping_cart,
  149. sum(key_action) key_action,
  150. sum(event_multi_conversion) event_multi_conversion,
  151. sum(event_wechat_connected) event_wechat_connected,
  152. sum(event_dsp_gift_form) event_dsp_gift_form,
  153. sum(event_intention_confirmed) event_intention_confirmed,
  154. sum(event_phone_get_through) event_phone_get_through,
  155. ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0) event_multi_conversion_ratio,
  156. sum(event_measurement_house) event_measurement_house,
  157. ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
  158. ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
  159. sum(event_ad_watch_times) event_ad_watch_times
  160. from
  161. (select * from media_api.kuaishou_account_report_daily
  162. where stat_date between date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') and {date_time}
  163. ) report
  164. LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
  165. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  166. LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
  167. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  168. GROUP BY
  169. cua.project_id;
  170. """.format(date_time=self.date_time, day_count=self.day_count)
  171. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  172. self.conn.commit()
  173. TidbConn.conn_close(conn=self.conn)
  174. return 0
  175. except Exception as e:
  176. logging.error(e)
  177. return -1
  178. if __name__ == '__main__':
  179. status = AppKuaishouProjectReportBusiMonth().taskHandler()
  180. if status == 0:
  181. print("作业执行成功")
  182. exit(0)
  183. else:
  184. print("作业执行失败")
  185. exit(-1)