AppKuaishouVideoReportBusiMonth.py 16 KB

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