AppBytedanceProductReportBusiMonth.py 18 KB

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