AppKuaishouCompanyDesignReportBusiMonth.py 16 KB

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