AppKuaishouVideoReportBusiMonth7.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppKuaishouVideoReportBusiMonth.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 AppKuaishouVideoReportBusiMonth:
  13. def __init__(self):
  14. self.conn = TidbConn.get_connection()
  15. self.day_count = 31
  16. def taskHandler(self):
  17. try:
  18. sql_porject = """select advertiser_id from media_api.kuaishou_material_video_report_daily where stat_date between 20210701 and 20210731
  19. group by advertiser_id """
  20. account_list = TidbConn.quary(self.conn, sql_porject)
  21. for account_id in account_list:
  22. try:
  23. sql = """SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  24. REPLACE INTO application.app_kuaishou_video_report_busi_m
  25. SELECT
  26. date_format(20210701,'%Y%m') stat_mon,
  27. report.account_id,
  28. report.photo_id,
  29. report.image_token,
  30. report.photo_caption,
  31. report.photo_url,
  32. report.cover_url,
  33. cua.project_id, #项目id
  34. product.id product_id, #项目中产品id
  35. cua.advertiser_id,#广告主id
  36. uc.company_id,#运营人的公司id
  37. cua.auth_name account_name,#账户名称
  38. cp.project_name,#项目名称
  39. ca.`name` advertiser_name,#广告主名称
  40. uc.company_name, #运营人的公司名称
  41. cp.sale_id, #项目里的销售id
  42. cua.user_id, #运营id
  43. cma.clip_id, #剪辑id
  44. cma.shot_id, #拍摄id
  45. cma.plan_id, #编导id
  46. cma.leader_id, #负责人id
  47. su.org_code, #机构编码
  48. video.signature, #md5
  49. video.channel_type chl_type, #渠道类型
  50. clip.realname clip_name, #剪辑名称
  51. shot.realname shot_name, #拍摄名称
  52. plan.realname plan_name, #编导名称
  53. leader.realname lead_name,#负责人名称
  54. CASE WHEN cmi.material_name IS NOT NULL THEN cmi.material_name
  55. ELSE video.photo_name END AS video_name,##视频名称
  56. case when video.channel_type = 1 then '素造' else '内部' end as chl_name, #渠道名称
  57. up.company_id clip_company_id, #剪辑公司
  58. report.charge ,
  59. report.`show`,
  60. report.photo_click,
  61. report.aclick ,
  62. report.bclick ,
  63. report.photo_click_ratio,
  64. report.play_3s_ratio,
  65. report.action_ratio ,
  66. report.impression_1k_cost ,
  67. report.photo_click_cost,
  68. report.action_cost,
  69. report.`share`,
  70. report.`comment`,
  71. report.`like`,
  72. report.follow ,
  73. report.cancel_follow,
  74. report.report ,
  75. report.block,
  76. report.negative,
  77. report.submit ,
  78. report.download_started,
  79. report.download_completed ,
  80. report.activation,
  81. report.event_pay_first_day,
  82. report.event_pay_purchase_amount_first_day,
  83. report.event_pay_first_day_roi,
  84. report.event_pay ,
  85. report.event_pay_purchase_amount,
  86. report.event_pay_roi,
  87. report.event_register,
  88. report.event_register_cost,
  89. report.event_register_ratio,
  90. report.event_jin_jian_app ,
  91. report.event_jin_jian_app_cost,
  92. report.event_credit_grant_app,
  93. report.event_credit_grant_app_cost ,
  94. report.event_credit_grant_app_ratio,
  95. report.event_order_paid,
  96. report.event_order_paid_purchase_amount,
  97. report.event_order_paid_cost ,
  98. report.event_next_day_stay,
  99. report.event_next_day_stay_cost ,
  100. report.event_next_day_stay_ratio,
  101. report.form_count,
  102. report.form_cost ,
  103. report.form_action_ratio,
  104. report.event_jin_jian_landing_page ,
  105. report.event_jin_jian_landing_page_cost,
  106. report.event_credit_grant_landing_page,
  107. report.event_credit_grant_landing_page_cost ,
  108. report.event_credit_grant_landing_page_ratio,
  109. report.event_valid_clues,
  110. report.event_valid_clues_cost,
  111. report.event_add_wechat,
  112. report.event_add_wechat_cost ,
  113. report.event_add_wechat_ratio,
  114. report.event_get_through,
  115. report.event_get_through_cost,
  116. report.event_get_through_ratio,
  117. report.event_app_invoked,
  118. report.event_app_invoked_cost,
  119. report.event_app_invoked_ratio,
  120. report.event_credit_grant_landing_ratio,
  121. report.play_5s_ratio,
  122. report.play_end_ratio,
  123. report.event_new_user_pay ,
  124. report.event_new_user_pay_cost,
  125. report.event_new_user_pay_ratio ,
  126. report.click_1k_cost,
  127. report.ad_product_cnt,
  128. report.event_goods_view,
  129. report.merchant_reco_fans ,
  130. report.event_order_amount_roi,
  131. report.event_goods_view_cost ,
  132. report.merchant_reco_fans_cost,
  133. report.event_button_click ,
  134. report.event_button_click_cost,
  135. report.event_button_click_ratio ,
  136. report.event_order_paid_roi,
  137. report.event_new_user_jinjian_app,
  138. report.event_new_user_jinjian_app_cost,
  139. report.event_new_user_jinjian_app_roi ,
  140. report.event_new_user_credit_grant_app,
  141. report.event_new_user_credit_grant_app_cost ,
  142. report.event_new_user_credit_grant_app_roi,
  143. report.event_new_user_jinjian_page ,
  144. report.event_new_user_jinjian_page_cost,
  145. report.event_new_user_jinjian_page_roi,
  146. report.event_new_user_credit_grant_page,
  147. report.event_new_user_credit_grant_page_cost,
  148. report.event_new_user_credit_grant_page_roi ,
  149. report.event_appoint_form ,
  150. report.event_appoint_form_cost,
  151. report.event_appoint_form_ratio ,
  152. report.event_appoint_jump_click ,
  153. report.event_appoint_jump_click_cost,
  154. report.event_appoint_jump_click_ratio ,
  155. report.union_event_pay_purchase_amount_7d,
  156. report.union_event_pay_purchase_amount_7d_roi,
  157. report.event_order_successed ,
  158. report.ad_photo_played_10s_ratio,
  159. report.key_action_cost ,
  160. report.event_add_shopping_cart_cost,
  161. report.event_ad_watch_times_ratio,
  162. report.event_outbound_call_cost ,
  163. report.event_outbound_call,
  164. report.action_new_ratio,
  165. report.event_watch_app_ad ,
  166. report.event_multi_conversion_cost ,
  167. report.event_phone_card_activate,
  168. report.ad_photo_played_75percent_ratio,
  169. report.key_action_ratio,
  170. report.event_ad_watch_times_cost,
  171. report.event_add_shopping_cart,
  172. report.key_action,
  173. report.event_multi_conversion,
  174. report.event_wechat_connected,
  175. report.event_dsp_gift_form,
  176. report.event_intention_confirmed,
  177. report.event_phone_get_through,
  178. report.event_multi_conversion_ratio,
  179. report.event_measurement_house,
  180. report.ad_photo_played_2s_ratio ,
  181. report.event_outbound_call_ratio,
  182. report.event_ad_watch_times
  183. from
  184. (select
  185. advertiser_id account_id,
  186. photo_id,
  187. image_token,
  188. photo_caption,
  189. photo_url,
  190. cover_url,
  191. sum(charge) charge,
  192. sum(`show`) `show`,
  193. sum(photo_click)photo_click,
  194. sum(aclick) aclick,
  195. sum(bclick) bclick,
  196. ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
  197. ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0) play_3s_ratio,
  198. ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
  199. ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
  200. ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
  201. ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
  202. sum(`share`) `share`,
  203. sum(`comment`) `comment`,
  204. sum(`like`) `like`,
  205. sum(follow) follow,
  206. sum(cancel_follow) cancel_follow,
  207. sum(report) report,
  208. sum(block) block,
  209. sum(negative) negative,
  210. sum(submit) submit,
  211. sum(download_started) download_started,
  212. sum(download_completed) download_completed,
  213. sum(activation) activation,
  214. sum(event_pay_first_day) event_pay_first_day,
  215. sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
  216. ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
  217. sum(event_pay) event_pay,
  218. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  219. ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
  220. sum(event_register) event_register,
  221. ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
  222. ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
  223. sum(event_jin_jian_app) event_jin_jian_app,
  224. ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
  225. sum(event_credit_grant_app) event_credit_grant_app,
  226. ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
  227. ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
  228. sum(event_order_paid) event_order_paid,
  229. sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
  230. ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
  231. sum(event_next_day_stay) event_next_day_stay,
  232. ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
  233. ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
  234. sum(form_count) form_count,
  235. ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
  236. ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
  237. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  238. ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
  239. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  240. ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
  241. ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
  242. sum(event_valid_clues) event_valid_clues,
  243. ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
  244. sum(event_add_wechat) event_add_wechat,
  245. ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
  246. ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
  247. sum(event_get_through) event_get_through,
  248. ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
  249. ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
  250. sum(event_app_invoked) event_app_invoked,
  251. ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
  252. ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
  253. ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
  254. ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
  255. ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
  256. sum(event_new_user_pay) event_new_user_pay,
  257. ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
  258. ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
  259. ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
  260. sum(ad_product_cnt) ad_product_cnt,
  261. sum(event_goods_view) event_goods_view,
  262. sum(merchant_reco_fans) merchant_reco_fans,
  263. ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
  264. ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
  265. ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
  266. sum(event_button_click) event_button_click,
  267. ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
  268. ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
  269. ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
  270. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  271. ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
  272. ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
  273. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  274. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0) event_new_user_credit_grant_app_cost,
  275. ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
  276. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  277. ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
  278. ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
  279. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  280. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
  281. ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
  282. sum(event_appoint_form) event_appoint_form,
  283. ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
  284. ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
  285. sum(event_appoint_jump_click) event_appoint_jump_click,
  286. ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0) event_appoint_jump_click_cost,
  287. ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
  288. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  289. ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
  290. sum(event_order_successed) event_order_successed,
  291. ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
  292. ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
  293. ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
  294. ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
  295. ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
  296. sum(event_outbound_call) event_outbound_call,
  297. ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
  298. sum(event_watch_app_ad) event_watch_app_ad,
  299. ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
  300. sum(event_phone_card_activate) event_phone_card_activate,
  301. ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
  302. ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
  303. ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
  304. sum(event_add_shopping_cart) event_add_shopping_cart,
  305. sum(key_action) key_action,
  306. sum(event_multi_conversion) event_multi_conversion,
  307. sum(event_wechat_connected) event_wechat_connected,
  308. sum(event_dsp_gift_form) event_dsp_gift_form,
  309. sum(event_intention_confirmed) event_intention_confirmed,
  310. sum(event_phone_get_through) event_phone_get_through,
  311. ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0) event_multi_conversion_ratio,
  312. sum(event_measurement_house) event_measurement_house,
  313. ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
  314. ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
  315. sum(event_ad_watch_times) event_ad_watch_times
  316. from media_api.kuaishou_material_video_report_daily
  317. where stat_date between 20210701 and 20210731
  318. group by
  319. advertiser_id,
  320. photo_id)report
  321. left join media_api.kuaishou_video_list video
  322. on report.account_id = video.account_id and report.photo_id = video.photo_id
  323. LEFT JOIN (select code,material_name from `jeecg-boot`.ctop_material_info where status=1) cmi ON video.signature = cmi.CODE
  324. LEFT JOIN (select material_id,clip_id,shot_id,plan_id,leader_id from (
  325. select material_id,clip_id,shot_id,plan_id,leader_id,row_number()over(partition by material_id order by create_time)rn
  326. from `jeecg-boot`.ctop_material_ascription )m where rn=1) cma
  327. ON video.signature = cma.material_id
  328. LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.account_id = cua.account_id
  329. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON cua.advertiser_id = ca.id
  330. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  331. LEFT JOIN `jeecg-boot`.user_company uc ON cua.user_id = uc.user_id
  332. LEFT JOIN `jeecg-boot`.ctop_product product ON cp.product_id = product.id
  333. LEFT JOIN `jeecg-boot`.sys_user su ON cua.user_id = su.id
  334. LEFT JOIN `jeecg-boot`.sys_user clip ON clip.id = cma.clip_id
  335. LEFT JOIN `jeecg-boot`.sys_user shot ON shot.id = cma.shot_id
  336. LEFT JOIN `jeecg-boot`.sys_user plan ON plan.id = cma.plan_id
  337. LEFT JOIN `jeecg-boot`.sys_user leader ON leader.id = cma.leader_id
  338. LEFT JOIN `jeecg-boot`.user_company up ON up.user_id = cma.clip_id
  339. where report.account_id={account_id};
  340. """.format(day_count=self.day_count, account_id=account_id[0])
  341. print(sql)
  342. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  343. self.conn.commit()
  344. except Exception as e:
  345. logging.error(e)
  346. return -1
  347. TidbConn.conn_close(conn=self.conn)
  348. return 0
  349. except Exception as e:
  350. logging.error(e)
  351. return -1
  352. if __name__ == '__main__':
  353. status = AppKuaishouVideoReportBusiMonth().taskHandler()
  354. if status == 0:
  355. print("作业执行成功")
  356. exit(0)
  357. else:
  358. print("作业执行失败")
  359. exit(-1)