AppKuaishouProjectReportBusi.py 10 KB

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