AppKuaishouProjectReportBusiMonth.py 10 KB

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