AppKuaishouCompanyDesignReportBusi.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppKuaishouCompanyDesignReportBusi.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 AppKuaishouCompanyDesignReportBusi:
  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_company_design_report_busi_{day_count}d
  23. select
  24. concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 ) DAY),'%Y%m%d'),'-',{date_time}) start_end_time,
  25. ifnull(company_id,'-') company_id,
  26. company_name,
  27. org_code,
  28. org_type,
  29. id,
  30. depart_name,
  31. ifnull(leader_id,'-') leader_id,
  32. leader_name,
  33. count(DISTINCT signature) num,
  34. count(distinct innovate_code) innovate_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_timesfrom
  160. (SELECT
  161. video.signature,
  162. uc.company_id,
  163. CASE
  164. WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  165. '北京汇创思拓数字科技有限公司'
  166. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  167. '广州汇创思拓数字科技有限公司'
  168. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
  169. '上海汇创思拓数字科技有限公司'
  170. ELSE
  171. uc.company_name
  172. END company_name,
  173. cma.leader_id,
  174. us.realname leader_name,
  175. ur.org_type,
  176. ur.org_code,
  177. ur.id,
  178. ur.depart_name,
  179. case when cmi.material_innovate=2 then video.signature else '' end innovate_code,
  180. report.charge ,
  181. report.`show`,
  182. report.photo_click,
  183. report.aclick ,
  184. report.bclick ,
  185. report.photo_click_ratio,
  186. report.play_3s_ratio,
  187. report.action_ratio ,
  188. report.impression_1k_cost ,
  189. report.photo_click_cost,
  190. report.action_cost,
  191. report.`share`,
  192. report.`comment`,
  193. report.`like`,
  194. report.follow ,
  195. report.cancel_follow,
  196. report.report ,
  197. report.block,
  198. report.negative,
  199. report.submit ,
  200. report.download_started,
  201. report.download_completed ,
  202. report.activation,
  203. report.event_pay_first_day,
  204. report.event_pay_purchase_amount_first_day,
  205. report.event_pay_first_day_roi,
  206. report.event_pay ,
  207. report.event_pay_purchase_amount,
  208. report.event_pay_roi,
  209. report.event_register,
  210. report.event_register_cost,
  211. report.event_register_ratio,
  212. report.event_jin_jian_app ,
  213. report.event_jin_jian_app_cost,
  214. report.event_credit_grant_app,
  215. report.event_credit_grant_app_cost ,
  216. report.event_credit_grant_app_ratio,
  217. report.event_order_paid,
  218. report.event_order_paid_purchase_amount,
  219. report.event_order_paid_cost ,
  220. report.event_next_day_stay,
  221. report.event_next_day_stay_cost ,
  222. report.event_next_day_stay_ratio,
  223. report.form_count,
  224. report.form_cost ,
  225. report.form_action_ratio,
  226. report.event_jin_jian_landing_page ,
  227. report.event_jin_jian_landing_page_cost,
  228. report.event_credit_grant_landing_page,
  229. report.event_credit_grant_landing_page_cost ,
  230. report.event_credit_grant_landing_page_ratio,
  231. report.event_valid_clues,
  232. report.event_valid_clues_cost,
  233. report.event_add_wechat,
  234. report.event_add_wechat_cost ,
  235. report.event_add_wechat_ratio,
  236. report.event_get_through,
  237. report.event_get_through_cost,
  238. report.event_get_through_ratio,
  239. report.event_app_invoked,
  240. report.event_app_invoked_cost,
  241. report.event_app_invoked_ratio,
  242. report.event_credit_grant_landing_ratio,
  243. report.play_5s_ratio,
  244. report.play_end_ratio,
  245. report.event_new_user_pay ,
  246. report.event_new_user_pay_cost,
  247. report.event_new_user_pay_ratio ,
  248. report.click_1k_cost,
  249. report.ad_product_cnt,
  250. report.event_goods_view,
  251. report.merchant_reco_fans ,
  252. report.event_order_amount_roi,
  253. report.event_goods_view_cost ,
  254. report.merchant_reco_fans_cost,
  255. report.event_button_click ,
  256. report.event_button_click_cost,
  257. report.event_button_click_ratio ,
  258. report.event_order_paid_roi,
  259. report.event_new_user_jinjian_app,
  260. report.event_new_user_jinjian_app_cost,
  261. report.event_new_user_jinjian_app_roi ,
  262. report.event_new_user_credit_grant_app,
  263. report.event_new_user_credit_grant_app_cost ,
  264. report.event_new_user_credit_grant_app_roi,
  265. report.event_new_user_jinjian_page ,
  266. report.event_new_user_jinjian_page_cost,
  267. report.event_new_user_jinjian_page_roi,
  268. report.event_new_user_credit_grant_page,
  269. report.event_new_user_credit_grant_page_cost,
  270. report.event_new_user_credit_grant_page_roi ,
  271. report.event_appoint_form ,
  272. report.event_appoint_form_cost,
  273. report.event_appoint_form_ratio ,
  274. report.event_appoint_jump_click ,
  275. report.event_appoint_jump_click_cost,
  276. report.event_appoint_jump_click_ratio ,
  277. report.union_event_pay_purchase_amount_7d,
  278. report.union_event_pay_purchase_amount_7d_roi,
  279. report.placement_type,
  280. report.ad_scene,
  281. report.event_order_successed ,
  282. report.ad_photo_played_10s_ratio,
  283. report.key_action_cost ,
  284. report.event_add_shopping_cart_cost,
  285. report.event_ad_watch_times_ratio,
  286. report.event_outbound_call_cost ,
  287. report.event_outbound_call,
  288. report.action_new_ratio,
  289. report.event_watch_app_ad ,
  290. report.event_multi_conversion_cost ,
  291. report.event_phone_card_activate,
  292. report.ad_photo_played_75percent_ratio,
  293. report.key_action_ratio,
  294. report.event_ad_watch_times_cost,
  295. report.event_add_shopping_cart,
  296. report.key_action,
  297. report.event_multi_conversion,
  298. report.event_wechat_connected,
  299. report.event_dsp_gift_form,
  300. report.event_intention_confirmed,
  301. report.event_phone_get_through,
  302. report.event_multi_conversion_ratio,
  303. report.event_measurement_house,
  304. report.ad_photo_played_2s_ratio ,
  305. report.event_outbound_call_ratio,
  306. report.event_ad_watch_times
  307. FROM
  308. (select * from media_api.kuaishou_material_video_report_daily
  309. where stat_date >= date_format(date_sub({date_time}, INTERVAL {day_count} -1 DAY),'%Y%m%d')
  310. ) report
  311. LEFT JOIN
  312. (select account_id,signature,photo_id from media_api.kuaishou_video_list group by account_id,signature,photo_id) video
  313. ON report.advertiser_id = video.account_id AND report.photo_id = video.photo_id
  314. LEFT JOIN (
  315. SELECT
  316. CODE,
  317. material_innovate,
  318. cover_url
  319. FROM
  320. `jeecg-boot`.ctop_material_info
  321. GROUP BY
  322. CODE,
  323. material_innovate,
  324. cover_url
  325. ) cmi ON video.signature = cmi. CODE
  326. LEFT JOIN (
  327. SELECT
  328. material_id,
  329. leader_id
  330. FROM
  331. (
  332. SELECT
  333. material_id,
  334. leader_id,
  335. row_number () over (
  336. PARTITION BY material_id
  337. ORDER BY
  338. create_time
  339. ) rn
  340. FROM
  341. `jeecg-boot`.ctop_material_ascription
  342. ) m
  343. WHERE
  344. rn = 1
  345. ) cma ON video.signature = cma.material_id
  346. left join `jeecg-boot`.sys_user us on us.id=cma.leader_id
  347. LEFT JOIN `jeecg-boot`.user_company uc ON cma.leader_id = uc.user_id
  348. LEFT JOIN `jeecg-boot`.sys_user_depart de ON cma.leader_id = de.user_id
  349. LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id) def
  350. group by
  351. company_id,
  352. leader_id
  353. ;
  354. """.format(date_time=self.date_time, day_count=self.day_count)
  355. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  356. self.conn.commit()
  357. TidbConn.conn_close(conn=self.conn)
  358. return 0
  359. except Exception as e:
  360. logging.error(e)
  361. return -1
  362. if __name__ == '__main__':
  363. status = AppKuaishouCompanyDesignReportBusi().taskHandler()
  364. if status == 0:
  365. print("作业执行成功")
  366. exit(0)
  367. else:
  368. print("作业执行失败")
  369. exit(-1)