AppBytedanceProjectReportBusiMonth.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppBytedanceProjectReportBusiMonth.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 AppBytedanceProjectReportBusiMonth:
  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_bytedance_project_report_busi_m
  23. SELECT
  24. date_format({date_time},'%Y%m') stat_mon,
  25. uc.company_id,
  26. CASE
  27. WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  28. '北京汇创思拓数字科技有限公司'
  29. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  30. '广州汇创思拓数字科技有限公司'
  31. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
  32. '上海汇创思拓数字科技有限公司'
  33. ELSE
  34. uc.company_name
  35. END company_name,
  36. cp.product_id,
  37. cn.product_name,
  38. cua.project_id,
  39. cp.project_name,
  40. count(report.advertiser_id) account_num,
  41. sum(cost) cost,
  42. sum(`show`) `show`,
  43. round(sum(cost) / sum(`show`), 3) avg_show_cost,
  44. sum(click) click,
  45. round(sum(cost) / sum(`click`), 3) avg_click_cost,
  46. round(
  47. sum(click) / sum(`show`) * 100,
  48. 3
  49. ) ctr,
  50. sum(`convert`) `convert`,
  51. round(sum(cost) / sum(`convert`), 3) convert_cost,
  52. round(
  53. sum(`convert`) / sum(click) * 100,
  54. 3
  55. ) convert_rate,
  56. sum(deep_convert) deep_convert,
  57. round(
  58. sum(cost) / sum(deep_convert),
  59. 3
  60. ) deep_convert_cost,
  61. round(
  62. sum(deep_convert) / sum(`convert`) * 100,
  63. 3
  64. ) deep_convert_rate,
  65. sum(attribution_convert) attribution_convert,
  66. round(
  67. sum(cost) / sum(attribution_convert),
  68. 3
  69. ) attribution_convert_cost,
  70. sum(attribution_deep_convert) attribution_deep_convert,
  71. round(
  72. sum(cost) / sum(attribution_deep_convert),
  73. 3
  74. ) attribution_deep_convert_cost,
  75. sum(download_start) download_start,
  76. round(
  77. sum(cost) / sum(download_start),
  78. 3
  79. ) download_start_cost,
  80. round(
  81. sum(download_start) / sum(click),
  82. 3
  83. ) download_start_rate,
  84. sum(download_finish) download_finish,
  85. round(
  86. sum(cost) / sum(download_finish),
  87. 3
  88. ) download_finish_cost,
  89. round(
  90. sum(download_finish) / sum(download_start) * 100,
  91. 3
  92. ) download_finish_rate,
  93. sum(click_install) click_install,
  94. sum(install_finish) install_finish,
  95. round(
  96. sum(cost) / sum(install_finish),
  97. 3
  98. ) install_finish_cost,
  99. round(
  100. sum(install_finish) / sum(download_finish) * 100,
  101. 3
  102. ) install_finish_rate,
  103. sum(active) active,
  104. round(sum(cost) / sum(active), 3) active_cost,
  105. round(
  106. sum(active) / sum(click) * 100,
  107. 3
  108. ) active_rate,
  109. sum(register) register,
  110. round(sum(cost) / sum(register), 3) active_register_cost,
  111. round(
  112. sum(active_register_rate) / {day_count} * 100,
  113. 3
  114. ) active_register_rate,
  115. sum(next_day_open) next_day_open,
  116. round(
  117. sum(cost) / sum(next_day_open),
  118. 3
  119. ) next_day_open_cost,
  120. round(
  121. sum(next_day_open) / sum(active) * 100,
  122. 3
  123. ) next_day_open_rate,
  124. sum(
  125. attribution_next_day_open_cnt
  126. ) attribution_next_day_open_cnt,
  127. round(
  128. sum(cost) / sum(
  129. attribution_next_day_open_cnt
  130. ),
  131. 3
  132. ) attribution_next_day_open_cost,
  133. round(
  134. sum(
  135. attribution_next_day_open_cnt
  136. ) / sum(active) * 100,
  137. 3
  138. ) attribution_next_day_open_rate,
  139. sum(game_addiction) game_addiction,
  140. round(
  141. sum(cost) / sum(game_addiction),
  142. 3
  143. ) game_addiction_cost,
  144. round(
  145. sum(game_addiction_rate) / {day_count} * 100,
  146. 3
  147. ) game_addiction_rate,
  148. sum(pay_count) pay_count,
  149. round(sum(cost) / sum(pay_count), 3) active_pay_cost,
  150. round(
  151. sum(pay_count) / sum(active) * 100,
  152. 3
  153. ) active_pay_rate,
  154. sum(loan_completion) loan_completion,
  155. round(
  156. sum(cost) / sum(loan_completion),
  157. 3
  158. ) loan_completion_cost,
  159. round(
  160. sum(loan_completion) / sum(register) * 100,
  161. 3
  162. ) loan_completion_rate,
  163. round(sum(pre_loan_credit), 3) pre_loan_credit,
  164. round(
  165. sum(cost) / sum(pre_loan_credit),
  166. 3
  167. ) pre_loan_credit_cost,
  168. sum(loan_credit) loan_credit,
  169. round(
  170. sum(cost) / sum(loan_credit),
  171. 3
  172. ) loan_credit_cost,
  173. round(
  174. sum(loan_credit) / sum(loan_completion) * 100,
  175. 3
  176. ) loan_credit_rate,
  177. sum(in_app_uv) in_app_uv,
  178. sum(in_app_detail_uv) in_app_detail_uv,
  179. sum(in_app_cart) in_app_cart,
  180. sum(in_app_pay) in_app_pay,
  181. sum(in_app_order) in_app_order,
  182. round(
  183. sum(
  184. attribution_game_pay_7d_count
  185. ) / {day_count},
  186. 3
  187. ) attribution_game_pay_7d_count,
  188. round(
  189. sum(
  190. attribution_game_pay_7d_cost
  191. ) / {day_count},
  192. 3
  193. ) attribution_game_pay_7d_cost,
  194. round(
  195. sum(
  196. attribution_active_pay_7d_per_count
  197. ) / {day_count},
  198. 3
  199. ) attribution_active_pay_7d_per_count,
  200. sum(game_pay_cost) game_pay_cost,
  201. sum(game_pay_count) game_pay_count,
  202. sum(phone) phone,
  203. sum(form) form,
  204. sum(map_search) map_search,
  205. sum(button) button,
  206. sum(`view`) `view`,
  207. sum(download) download,
  208. sum(qq) qq,
  209. sum(lottery) lottery,
  210. sum(vote) vote,
  211. sum(message) message,
  212. sum(redirect) redirect,
  213. sum(shopping) shopping,
  214. sum(consult) consult,
  215. sum(wechat) wechat,
  216. sum(phone_confirm) phone_confirm,
  217. sum(phone_connect) phone_connect,
  218. sum(consult_effective) consult_effective,
  219. sum(coupon) coupon,
  220. sum(coupon_single_page) coupon_single_page,
  221. sum(redirect_to_shop) redirect_to_shop,
  222. sum(poi_collect) poi_collect,
  223. sum(poi_address_click) poi_address_click,
  224. sum(luban_order_cnt) luban_order_cnt,
  225. sum(luban_order_stat_amount) luban_order_stat_amount,
  226. sum(luban_order_roi) luban_order_roi,
  227. sum(luban_live_enter_cnt) luban_live_enter_cnt,
  228. sum(
  229. live_watch_one_minute_count
  230. ) live_watch_one_minute_count,
  231. sum(luban_live_follow_cnt) luban_live_follow_cnt,
  232. sum(live_fans_club_join_cnt) live_fans_club_join_cnt,
  233. sum(luban_live_comment_cnt) luban_live_comment_cnt,
  234. sum(luban_live_share_cnt) luban_live_share_cnt,
  235. sum(luban_live_gift_cnt) luban_live_gift_cnt,
  236. sum(luban_live_gift_amount) luban_live_gift_amount,
  237. sum(
  238. luban_live_slidecart_click_cnt
  239. ) luban_live_slidecart_click_cnt,
  240. sum(
  241. luban_live_click_product_cnt
  242. ) luban_live_click_product_cnt,
  243. sum(luban_live_pay_order_count) luban_live_pay_order_count,
  244. sum(
  245. luban_live_pay_order_stat_cost
  246. ) luban_live_pay_order_stat_cost,
  247. sum(live_component_click_count) live_component_click_count,
  248. round(
  249. sum(cost) / sum(live_component_click_count),
  250. 3
  251. ) live_component_click_cost,
  252. round(
  253. sum(live_component_click_count) / {day_count} * 100,
  254. 3
  255. ) live_component_click_rate,
  256. sum(wechat_login_count) wechat_login_count,
  257. sum(
  258. attribution_wechat_login_30d_count
  259. ) attribution_wechat_login_30d_count,
  260. round(
  261. sum(cost) / sum(wechat_login_count),
  262. 3
  263. ) wechat_login_cost,
  264. round(
  265. sum(cost) / sum(
  266. attribution_wechat_login_30d_count
  267. ),
  268. 3
  269. ) attribution_wechat_login_30d_cost,
  270. sum(wechat_first_pay_count) wechat_first_pay_count,
  271. sum(
  272. attribution_wechat_first_pay_30d_count
  273. ) attribution_wechat_first_pay_30d_count,
  274. round(
  275. sum(cost) / sum(wechat_first_pay_count),
  276. 3
  277. ) wechat_first_pay_cost,
  278. round(
  279. sum(cost) / sum(
  280. attribution_wechat_first_pay_30d_count
  281. ),
  282. 3
  283. ) attribution_wechat_first_pay_30d_cost,
  284. round(
  285. sum(wechat_first_pay_count) / sum(wechat_login_count) * 100,
  286. 3
  287. ) wechat_first_pay_rate,
  288. round(
  289. sum(
  290. attribution_wechat_first_pay_30d_count
  291. ) / sum(
  292. attribution_wechat_login_30d_count
  293. ) * 100,
  294. 3
  295. ) attribution_wechat_first_pay_30d_rate,
  296. sum(wechat_pay_amount) wechat_pay_amount,
  297. sum(
  298. attribution_wechat_pay_30d_amount
  299. ) attribution_wechat_pay_30d_amount,
  300. sum(
  301. attribution_wechat_pay_30d_roi
  302. ) attribution_wechat_pay_30d_roi,
  303. sum(phone_effective) phone_effective,
  304. sum(total_play) total_play,
  305. sum(valid_play) valid_play,
  306. round(
  307. sum(cost) / sum(valid_play),
  308. 3
  309. ) valid_play_cost,
  310. round(
  311. sum(valid_play) / sum(`show`) * 100,
  312. 3
  313. ) valid_play_rate,
  314. sum(play_25_feed_break) play_25_feed_break,
  315. sum(play_50_feed_break) play_50_feed_break,
  316. sum(play_75_feed_break) play_75_feed_break,
  317. sum(play_100_feed_break) play_100_feed_break,
  318. round(
  319. sum(average_play_time_per_play) / {day_count},
  320. 3
  321. ) average_play_time_per_play,
  322. round(
  323. sum(play_over_rate) / {day_count} * 100,
  324. 3
  325. ) play_over_rate,
  326. round(
  327. sum(wifi_play) / sum(total_play) * 100,
  328. 3
  329. ) wifi_play_rate,
  330. sum(wifi_play) wifi_play,
  331. round(sum(play_duration_sum) / {day_count}, 3) play_duration_sum,
  332. sum(
  333. advanced_creative_phone_click
  334. ) advanced_creative_phone_click,
  335. sum(
  336. advanced_creative_counsel_click
  337. ) advanced_creative_counsel_click,
  338. sum(
  339. advanced_creative_form_click
  340. ) advanced_creative_form_click,
  341. sum(
  342. advanced_creative_coupon_addition
  343. ) advanced_creative_coupon_addition,
  344. sum(
  345. advanced_creative_form_submit
  346. ) advanced_creative_form_submit,
  347. sum(card_show) card_show,
  348. sum(`share`) `share`,
  349. sum(`comment`) `comment`,
  350. sum(`like`) `like`,
  351. sum(follow) follow,
  352. sum(home_visited) home_visited,
  353. sum(ies_challenge_click) ies_challenge_click,
  354. sum(ies_music_click) ies_music_click,
  355. sum(location_click) location_click,
  356. sum(message_action) message_action,
  357. sum(click_landing_page) click_landing_page,
  358. sum(click_shopwindow) click_shopwindow,
  359. sum(click_website) click_website,
  360. sum(click_download) click_download,
  361. sum(click_call_dy) click_call_dy,
  362. sum(submit_certification_count) submit_certification_count,
  363. sum(approval_count) approval_count,
  364. sum(first_order_count) first_rental_order_count,
  365. sum(first_rental_order_count) first_order_count,
  366. sum(commute_first_pay_count) commute_first_pay_count
  367. FROM
  368. (
  369. SELECT
  370. *
  371. FROM
  372. media_api.bytedance_advertiser_report_daily
  373. WHERE
  374. stat_datetime >= date_format(
  375. date_sub({date_time}, INTERVAL {day_count} DAY),
  376. '%Y%m%d'
  377. )
  378. ) report
  379. LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.advertiser_id = cua.account_id
  380. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  381. LEFT JOIN `jeecg-boot`.ctop_product cn ON cp.product_id = cn.id
  382. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  383. GROUP BY
  384. uc.company_id,
  385. CASE
  386. WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  387. '北京汇创思拓数字科技有限公司'
  388. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  389. '广州汇创思拓数字科技有限公司'
  390. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
  391. '上海汇创思拓数字科技有限公司'
  392. ELSE
  393. uc.company_name
  394. END,cua.project_id,
  395. cp.project_name
  396. having sum(cost)>0""".format(date_time=self.date_time, day_count=self.day_count)
  397. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  398. self.conn.commit()
  399. TidbConn.conn_close(conn=self.conn)
  400. return 0
  401. except Exception as e:
  402. logging.error(e)
  403. return -1
  404. if __name__ == '__main__':
  405. status = AppBytedanceProjectReportBusiMonth().taskHandler()
  406. if status == 0:
  407. print("作业执行成功")
  408. exit(0)
  409. else:
  410. print("作业执行失败")
  411. exit(-1)