AppKuaishouCompanyUserReportBusiMonth.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppKuaishouCompanyUserReportBusiMonth.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 AppKuaishouCompanyUserReportBusiMonth:
  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. 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_user_report_busi_m
  23. SELECT
  24. date_format({date_time},'%Y%m') start_end_time,
  25. ifnull(company_id,'-') company_id,
  26. company_name,
  27. org_code,
  28. org_type,
  29. dep_id,
  30. depart_name,
  31. ifnull(leader_id,'-') leader_id,
  32. leader_name,
  33. count(DISTINCT account_id) account_num,
  34. sum(charge) charge,
  35. sum(`show`) `show`,
  36. sum(photo_click)photo_click,
  37. sum(aclick) aclick,
  38. sum(bclick) bclick,
  39. ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
  40. ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0) play_3s_ratio,
  41. ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
  42. ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
  43. ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
  44. ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
  45. sum(`share`) `share`,
  46. sum(`comment`) `comment`,
  47. sum(`like`) `like`,
  48. sum(follow) follow,
  49. sum(cancel_follow) cancel_follow,
  50. sum(report) report,
  51. sum(block) block,
  52. sum(negative) negative,
  53. sum(submit) submit,
  54. sum(download_started) download_started,
  55. sum(download_completed) download_completed,
  56. sum(activation) activation,
  57. sum(event_pay_first_day) event_pay_first_day,
  58. sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
  59. ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
  60. sum(event_pay) event_pay,
  61. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  62. ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
  63. sum(event_register) event_register,
  64. ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
  65. ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
  66. sum(event_jin_jian_app) event_jin_jian_app,
  67. ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
  68. sum(event_credit_grant_app) event_credit_grant_app,
  69. ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
  70. ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
  71. sum(event_order_paid) event_order_paid,
  72. sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
  73. ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
  74. sum(event_next_day_stay) event_next_day_stay,
  75. ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
  76. ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
  77. sum(form_count) form_count,
  78. ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
  79. ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
  80. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  81. ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
  82. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  83. ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
  84. ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
  85. sum(event_valid_clues) event_valid_clues,
  86. ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
  87. sum(event_add_wechat) event_add_wechat,
  88. ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
  89. ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
  90. sum(event_get_through) event_get_through,
  91. ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
  92. ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
  93. sum(event_app_invoked) event_app_invoked,
  94. ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
  95. ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
  96. ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
  97. ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
  98. ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
  99. sum(event_new_user_pay) event_new_user_pay,
  100. ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
  101. ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
  102. ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
  103. sum(ad_product_cnt) ad_product_cnt,
  104. sum(event_goods_view) event_goods_view,
  105. sum(merchant_reco_fans) merchant_reco_fans,
  106. ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
  107. ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
  108. ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
  109. sum(event_button_click) event_button_click,
  110. ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
  111. ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
  112. ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
  113. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  114. ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
  115. ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
  116. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  117. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0) event_new_user_credit_grant_app_cost,
  118. ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
  119. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  120. ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
  121. ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
  122. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  123. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
  124. ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
  125. sum(event_appoint_form) event_appoint_form,
  126. ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
  127. ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
  128. sum(event_appoint_jump_click) event_appoint_jump_click,
  129. ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0) event_appoint_jump_click_cost,
  130. ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
  131. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  132. ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
  133. sum(event_order_successed) event_order_successed,
  134. ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
  135. ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
  136. ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
  137. ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
  138. ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
  139. sum(event_outbound_call) event_outbound_call,
  140. ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
  141. sum(event_watch_app_ad) event_watch_app_ad,
  142. ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
  143. sum(event_phone_card_activate) event_phone_card_activate,
  144. ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
  145. ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
  146. ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
  147. sum(event_add_shopping_cart) event_add_shopping_cart,
  148. sum(key_action) key_action,
  149. sum(event_multi_conversion) event_multi_conversion,
  150. sum(event_wechat_connected) event_wechat_connected,
  151. sum(event_dsp_gift_form) event_dsp_gift_form,
  152. sum(event_intention_confirmed) event_intention_confirmed,
  153. sum(event_phone_get_through) event_phone_get_through,
  154. ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0) event_multi_conversion_ratio,
  155. sum(event_measurement_house) event_measurement_house,
  156. ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
  157. ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
  158. sum(event_ad_watch_times) event_ad_watch_times
  159. from (
  160. SELECT
  161. report.advertiser_id account_id,
  162. dep.company_id,
  163. CASE
  164. WHEN dep.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  165. '北京汇创思拓数字科技有限公司'
  166. WHEN dep.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  167. '广州汇创思拓数字科技有限公司'
  168. WHEN dep.company_id = '4b10089775c040119e139087517aed88' THEN
  169. '上海汇创思拓数字科技有限公司'
  170. ELSE
  171. dep.company_name
  172. END company_name,
  173. dep.org_code,
  174. dep.org_type,
  175. dep.dep_id,
  176. dep.depart_name,
  177. dep.user_id,
  178. dep.realname,
  179. dep.leader_id,
  180. dep.leader_name,
  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_account_report_daily
  310. where stat_date between date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') and {date_time}
  311. ) report
  312. left join
  313. (
  314. SELECT
  315. cua.account_id,
  316. uc.company_id,
  317. uc.company_name,
  318. pa.org_code,
  319. pa.org_type,
  320. pa.id dep_id,
  321. pa.depart_name,
  322. cua.user_id,
  323. su.realname,
  324. cp.sale_id,
  325. su.leader_id,
  326. se.realname leader_name
  327. FROM
  328. `jeecg-boot`.ctop_user_allocation cua
  329. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  330. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  331. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  332. LEFT JOIN `jeecg-boot`.sys_user se ON su.leader_id=se.id
  333. LEFT JOIN `jeecg-boot`.sys_user_depart de ON su.leader_id = de.user_id
  334. LEFT JOIN `jeecg-boot`.sys_depart pa ON pa.id = de.dep_id) dep
  335. ON report.advertiser_id = dep.account_id) com
  336. where com.company_id is not null
  337. group by
  338. company_id,
  339. company_name,
  340. org_code,
  341. org_type,
  342. dep_id,
  343. depart_name,
  344. leader_id,
  345. leader_name;
  346. """.format(date_time=self.date_time, day_count=self.day_count)
  347. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  348. self.conn.commit()
  349. TidbConn.conn_close(conn=self.conn)
  350. return 0
  351. except Exception as e:
  352. logging.error(e)
  353. return -1
  354. if __name__ == '__main__':
  355. status = AppKuaishouCompanyUserReportBusiMonth().taskHandler()
  356. if status == 0:
  357. print("作业执行成功")
  358. exit(0)
  359. else:
  360. print("作业执行失败")
  361. exit(-1)