AppKuaishouCompanyDesignReportBusi.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. company_id,
  26. company_name,
  27. org_code,
  28. org_type,
  29. id,
  30. depart_name,
  31. 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. round(sum(photo_click)/sum(`show`),3) photo_click_ratio,
  41. round(sum(play_3s_ratio)/{day_count}*100,3) play_3s_ratio,
  42. round(sum(bclick)/sum(aclick)*100,3) action_ratio,
  43. round(sum(impression_1k_cost)/{day_count},3) impression_1k_cost,
  44. round(sum(charge)/sum(photo_click),3) photo_click_cost,
  45. round(sum(charge)/sum(bclick),3) 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. round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3) event_pay_first_day_roi ,
  61. sum(event_pay) event_pay,
  62. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  63. round(sum(event_pay_purchase_amount)/sum(charge)*100,3) event_pay_roi,
  64. sum(event_register) event_register,
  65. round(sum(charge)/sum(event_register),3) event_register_cost,
  66. round(sum(event_register)/sum(activation)*100,3) event_register_ratio,
  67. sum(event_jin_jian_app) event_jin_jian_app,
  68. round(sum(charge)/sum(event_jin_jian_app),3) event_jin_jian_app_cost,
  69. sum(event_credit_grant_app) event_credit_grant_app,
  70. round(sum(charge)/sum(event_credit_grant_app),3) event_credit_grant_app_cost ,
  71. round(sum(event_credit_grant_app_ratio)/{day_count}*100,3) 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. round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3) event_order_paid_cost,
  75. sum(event_next_day_stay) event_next_day_stay,
  76. round(sum(charge)/sum(event_next_day_stay),3) event_next_day_stay_cost,
  77. round(sum(event_next_day_stay)/sum(activation)*100,3) event_next_day_stay_ratio,
  78. sum(form_count) form_count,
  79. round(sum(charge)/sum(form_count),3) form_cost,
  80. round(sum(form_count)/sum(`bclick`)*100,3) form_action_ratio,
  81. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  82. round(sum(charge)/sum(event_jin_jian_landing_page),3) event_jin_jian_landing_page_cost,
  83. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  84. round(sum(charge)/sum(event_credit_grant_landing_page),3) event_credit_grant_landing_page_cost,
  85. round(sum(event_credit_grant_landing_page)/{day_count}*100,3) event_credit_grant_landing_page_ratio,
  86. sum(event_valid_clues) event_valid_clues,
  87. round(sum(charge)/sum(event_valid_clues),3) event_valid_clues_cost,
  88. sum(event_add_wechat) event_add_wechat,
  89. round(sum(charge)/sum(event_add_wechat),3) event_add_wechat_cost,
  90. round(sum(event_add_wechat)/sum(bclick)*100,3) event_add_wechat_ratio,
  91. sum(event_get_through) event_get_through,
  92. round(sum(charge)/sum(event_get_through),3) event_get_through_cost,
  93. round(sum(event_get_through)/sum(bclick)*100,3) event_get_through_ratio,
  94. sum(event_app_invoked) event_app_invoked,
  95. round(sum(charge)/sum(event_app_invoked),3) event_app_invoked_cost ,
  96. round(sum(event_app_invoked)/sum(bclick)*100,3) event_app_invoked_ratio,
  97. round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3) event_credit_grant_landing_ratio,
  98. round(sum(play_5s_ratio)/{day_count}*100,3) play_5s_ratio,
  99. round(sum(play_end_ratio)/{day_count}*100,3) play_end_ratio,
  100. sum(event_new_user_pay) event_new_user_pay,
  101. round(sum(charge)/sum(event_new_user_pay),3) event_new_user_pay_cost,
  102. round(sum(event_new_user_pay)/sum(activation)*100,3) event_new_user_pay_ratio,
  103. round(sum(click_1k_cost)/{day_count}*100) 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. round(sum(event_order_amount_roi)/{day_count}*100,3) event_order_amount_roi,
  108. round(sum(charge)/sum(event_goods_view) ,3) event_goods_view_cost,
  109. round(sum(charge)/sum(merchant_reco_fans),3) merchant_reco_fans_cost,
  110. sum(event_button_click) event_button_click,
  111. round(sum(charge)/sum(event_button_click),3) event_button_click_cost,
  112. round(sum(event_button_click)/sum(bclick)*100,3) event_button_click_ratio ,
  113. round(sum(event_order_paid_roi)/{day_count}*100,3) event_order_paid_roi,
  114. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  115. round(sum(charge)/sum(event_new_user_jinjian_app),3) event_new_user_jinjian_app_cost,
  116. round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3) event_new_user_jinjian_app_roi ,
  117. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  118. round(sum(charge)/sum(event_new_user_credit_grant_app),3) event_new_user_credit_grant_app_cost,
  119. round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3) event_new_user_credit_grant_app_roi,
  120. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  121. round(sum(charge)/sum(event_new_user_jinjian_page),3) event_new_user_jinjian_page_cost,
  122. round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3) event_new_user_jinjian_page_roi ,
  123. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  124. round(sum(charge)/sum(event_new_user_credit_grant_page) ,3) event_new_user_credit_grant_page_cost,
  125. round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3) event_new_user_credit_grant_page_roi,
  126. sum(event_appoint_form) event_appoint_form,
  127. round(sum(charge)/sum(event_appoint_form),3) event_appoint_form_cost,
  128. round(sum(event_appoint_form)/sum(form_count)*100,3) event_appoint_form_ratio,
  129. sum(event_appoint_jump_click) event_appoint_jump_click,
  130. round(sum(charge)/sum(event_appoint_jump_click),3) event_appoint_jump_click_cost,
  131. round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3) event_appoint_jump_click_ratio,
  132. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  133. round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3) union_event_pay_purchase_amount_7d_roi,
  134. sum(event_order_successed) event_order_successed,
  135. round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3) ad_photo_played_10s_ratio,
  136. round(sum(charge)/sum(key_action),3) key_action_cost,
  137. round(sum(charge)/sum(event_add_shopping_cart) ,3) event_add_shopping_cart_cost ,
  138. round(sum(event_ad_watch_times)/sum(bclick)*100,3) event_ad_watch_times_ratio,
  139. round(sum(charge)/sum(event_outbound_call),3) event_outbound_call_cost,
  140. sum(event_outbound_call) event_outbound_call,
  141. round(sum(action_new_ratio)/{day_count}*100,3) action_new_ratio,
  142. sum(event_watch_app_ad) event_watch_app_ad,
  143. round(sum(charge)/sum(event_multi_conversion) ,3) event_multi_conversion_cost,
  144. sum(event_phone_card_activate) event_phone_card_activate,
  145. round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3) ad_photo_played_75percent_ratio,
  146. round(sum(key_action)/sum(activation)*100,3) key_action_ratio ,
  147. round(sum(charge)/sum(event_ad_watch_times),3) 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. round(sum(event_multi_conversion)/sum(activation)*100,3) event_multi_conversion_ratio,
  156. sum(event_measurement_house) event_measurement_house,
  157. round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3) ad_photo_played_2s_ratio,
  158. round(sum(event_outbound_call_ratio)/sum(activation)*100,3) event_outbound_call_ratio,
  159. sum(event_ad_watch_times) event_ad_watch_times
  160. from
  161. (SELECT
  162. video.signature,
  163. uc.company_id,
  164. CASE
  165. WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  166. '北京汇创思拓数字科技有限公司'
  167. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  168. '广州汇创思拓数字科技有限公司'
  169. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
  170. '上海汇创思拓数字科技有限公司'
  171. ELSE
  172. uc.company_name
  173. END company_name,
  174. cma.leader_id,
  175. us.realname leader_name,
  176. ur.org_type,
  177. ur.org_code,
  178. ur.id,
  179. ur.depart_name,
  180. case when cmi.material_innovate=2 then video.signature else '' end innovate_code,
  181. report.charge ,
  182. report.`show`,
  183. report.photo_click,
  184. report.aclick ,
  185. report.bclick ,
  186. report.photo_click_ratio,
  187. report.play_3s_ratio,
  188. report.action_ratio ,
  189. report.impression_1k_cost ,
  190. report.photo_click_cost,
  191. report.action_cost,
  192. report.`share`,
  193. report.`comment`,
  194. report.`like`,
  195. report.follow ,
  196. report.cancel_follow,
  197. report.report ,
  198. report.block,
  199. report.negative,
  200. report.submit ,
  201. report.download_started,
  202. report.download_completed ,
  203. report.activation,
  204. report.event_pay_first_day,
  205. report.event_pay_purchase_amount_first_day,
  206. report.event_pay_first_day_roi,
  207. report.event_pay ,
  208. report.event_pay_purchase_amount,
  209. report.event_pay_roi,
  210. report.event_register,
  211. report.event_register_cost,
  212. report.event_register_ratio,
  213. report.event_jin_jian_app ,
  214. report.event_jin_jian_app_cost,
  215. report.event_credit_grant_app,
  216. report.event_credit_grant_app_cost ,
  217. report.event_credit_grant_app_ratio,
  218. report.event_order_paid,
  219. report.event_order_paid_purchase_amount,
  220. report.event_order_paid_cost ,
  221. report.event_next_day_stay,
  222. report.event_next_day_stay_cost ,
  223. report.event_next_day_stay_ratio,
  224. report.form_count,
  225. report.form_cost ,
  226. report.form_action_ratio,
  227. report.event_jin_jian_landing_page ,
  228. report.event_jin_jian_landing_page_cost,
  229. report.event_credit_grant_landing_page,
  230. report.event_credit_grant_landing_page_cost ,
  231. report.event_credit_grant_landing_page_ratio,
  232. report.event_valid_clues,
  233. report.event_valid_clues_cost,
  234. report.event_add_wechat,
  235. report.event_add_wechat_cost ,
  236. report.event_add_wechat_ratio,
  237. report.event_get_through,
  238. report.event_get_through_cost,
  239. report.event_get_through_ratio,
  240. report.event_app_invoked,
  241. report.event_app_invoked_cost,
  242. report.event_app_invoked_ratio,
  243. report.event_credit_grant_landing_ratio,
  244. report.play_5s_ratio,
  245. report.play_end_ratio,
  246. report.event_new_user_pay ,
  247. report.event_new_user_pay_cost,
  248. report.event_new_user_pay_ratio ,
  249. report.click_1k_cost,
  250. report.ad_product_cnt,
  251. report.event_goods_view,
  252. report.merchant_reco_fans ,
  253. report.event_order_amount_roi,
  254. report.event_goods_view_cost ,
  255. report.merchant_reco_fans_cost,
  256. report.event_button_click ,
  257. report.event_button_click_cost,
  258. report.event_button_click_ratio ,
  259. report.event_order_paid_roi,
  260. report.event_new_user_jinjian_app,
  261. report.event_new_user_jinjian_app_cost,
  262. report.event_new_user_jinjian_app_roi ,
  263. report.event_new_user_credit_grant_app,
  264. report.event_new_user_credit_grant_app_cost ,
  265. report.event_new_user_credit_grant_app_roi,
  266. report.event_new_user_jinjian_page ,
  267. report.event_new_user_jinjian_page_cost,
  268. report.event_new_user_jinjian_page_roi,
  269. report.event_new_user_credit_grant_page,
  270. report.event_new_user_credit_grant_page_cost,
  271. report.event_new_user_credit_grant_page_roi ,
  272. report.event_appoint_form ,
  273. report.event_appoint_form_cost,
  274. report.event_appoint_form_ratio ,
  275. report.event_appoint_jump_click ,
  276. report.event_appoint_jump_click_cost,
  277. report.event_appoint_jump_click_ratio ,
  278. report.union_event_pay_purchase_amount_7d,
  279. report.union_event_pay_purchase_amount_7d_roi,
  280. report.placement_type,
  281. report.ad_scene,
  282. report.event_order_successed ,
  283. report.ad_photo_played_10s_ratio,
  284. report.key_action_cost ,
  285. report.event_add_shopping_cart_cost,
  286. report.event_ad_watch_times_ratio,
  287. report.event_outbound_call_cost ,
  288. report.event_outbound_call,
  289. report.action_new_ratio,
  290. report.event_watch_app_ad ,
  291. report.event_multi_conversion_cost ,
  292. report.event_phone_card_activate,
  293. report.ad_photo_played_75percent_ratio,
  294. report.key_action_ratio,
  295. report.event_ad_watch_times_cost,
  296. report.event_add_shopping_cart,
  297. report.key_action,
  298. report.event_multi_conversion,
  299. report.event_wechat_connected,
  300. report.event_dsp_gift_form,
  301. report.event_intention_confirmed,
  302. report.event_phone_get_through,
  303. report.event_multi_conversion_ratio,
  304. report.event_measurement_house,
  305. report.ad_photo_played_2s_ratio ,
  306. report.event_outbound_call_ratio,
  307. report.event_ad_watch_times
  308. FROM
  309. (select * from media_api.kuaishou_material_video_report_daily where stat_date >= date_format(date_sub({date_time}, INTERVAL ({day_count} -1 ) DAY),'%Y%m%d')) report
  310. LEFT JOIN
  311. (select account_id,signature,photo_id from media_api.kuaishou_video_list group by account_id,signature,photo_id) video
  312. ON report.advertiser_id = video.account_id AND report.photo_id = video.photo_id
  313. LEFT JOIN (
  314. SELECT
  315. CODE,
  316. material_innovate,
  317. cover_url
  318. FROM
  319. `jeecg-boot`.ctop_material_info
  320. GROUP BY
  321. CODE,
  322. material_innovate,
  323. cover_url
  324. ) cmi ON video.signature = cmi. CODE
  325. LEFT JOIN (
  326. SELECT
  327. material_id,
  328. leader_id
  329. FROM
  330. (
  331. SELECT
  332. material_id,
  333. leader_id,
  334. row_number () over (
  335. PARTITION BY material_id
  336. ORDER BY
  337. create_time
  338. ) rn
  339. FROM
  340. `jeecg-boot`.ctop_material_ascription
  341. ) m
  342. WHERE
  343. rn = 1
  344. ) cma ON video.signature = cma.material_id
  345. left join `jeecg-boot`.sys_user us on us.id=cma.leader_id
  346. LEFT JOIN `jeecg-boot`.user_company uc ON cma.leader_id = uc.user_id
  347. LEFT JOIN `jeecg-boot`.sys_user_depart de ON cma.leader_id = de.user_id
  348. LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id) def
  349. group by
  350. company_id,
  351. company_name,
  352. org_code,
  353. org_type,
  354. id,
  355. depart_name,
  356. leader_id,
  357. leader_name;
  358. """.format(date_time=self.date_time, day_count=self.day_count)
  359. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  360. self.conn.commit()
  361. TidbConn.conn_close(conn=self.conn)
  362. return 0
  363. except Exception as e:
  364. logging.error(e)
  365. return -1
  366. if __name__ == '__main__':
  367. status = AppKuaishouCompanyDesignReportBusi().taskHandler()
  368. if status == 0:
  369. print("作业执行成功")
  370. exit(0)
  371. else:
  372. print("作业执行失败")
  373. exit(-1)