data_stat_task.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. from model.DataBaseUtils import MysqlUtils, CkUtils
  2. from datetime import datetime, timedelta, timezone
  3. from model.DateUtils import DateUtils
  4. import logging
  5. import time
  6. db = MysqlUtils()
  7. ck = CkUtils()
  8. dt = DateUtils()
  9. def platform_data_sum(ymd):
  10. logging.info('dw_daily_platform_cost开始数据更新')
  11. ck.execute("alter table game_data.dw_daily_platform_cost drop partition '{}' ".format(ymd))
  12. sql = f'''
  13. insert into game_data.dw_daily_platform_cost
  14. select * from game_data.dw_daily_channel_cost b where dt='{ymd}'
  15. '''
  16. ck.execute(sql)
  17. logging.info('dw_daily_platform_cost数据更新,结束')
  18. def dw_daily_channel_cost_ck_gamedatasum(ymd):
  19. logging.info("run> dw_daily_channel_cost")
  20. datatime_ymd = datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))).timetuple()
  21. datatime_ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  22. days=1)).timetuple()
  23. # datatime_str
  24. ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  25. days=1)).strftime('%Y-%m-%d')
  26. ymd_tom_after = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  27. days=2)).strftime('%Y-%m-%d')
  28. ymd_seven_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  29. days=7)).strftime('%Y-%m-%d')
  30. ymd_fifteen_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  31. days=15)).strftime('%Y-%m-%d')
  32. ymd_thirty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  33. days=30)).strftime('%Y-%m-%d')
  34. ymd_fortyfive_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  35. days=45)).strftime('%Y-%m-%d')
  36. ymd_sixty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  37. days=60)).strftime('%Y-%m-%d')
  38. # timestamp
  39. timestamp_ymd = time.mktime(datatime_ymd)
  40. timestamp_tom = time.mktime(datatime_ymd_tom)
  41. ck.execute(f"alter table game_data.dw_daily_channel_cost drop partition '{ymd}' ")
  42. # TODO:创建角色量有问题
  43. ck_sql = f'''
  44. insert into game_data.dw_daily_channel_cost
  45. (dt,channel,pitcher,stage,platform,book,view_count,click_count,follow_user,cost,
  46. web_view_count,platform_view_count,web_order_count,type,require_roi,require_mult,
  47. reg_num,create_user_num,today_active_user_rate,second_stay_rate,third_stay_rate,
  48. seven_stay_rate,fifteen_stay_rate,thirty_stay_rate,fortyfive_stay_rate,
  49. sixty_stay_rate,game_user_sum)
  50. select x.dt,x.channel,pitcher,stage,x.platform,x.book,
  51. ifnull(view_count,0),ifnull(click_count,0),
  52. ifnull(follow_user,0),ifnull(cost,0)/100 as cost,
  53. ifnull(web_view_count,0) web_view_count,
  54. ifnull(platform_view_count,0) platform_view_count,
  55. ifnull(web_order_count,0) web_order_count,
  56. if(stage ='趣程15期' or stage ='趣程26期' or stage ='趣程30期','GDT','MP') type
  57. ,0 require_roi,0 require_mult,
  58. ifnull(y.reg_num,0),ifnull(w.create_user_num,0),
  59. v.today_active_user_rate,
  60. v.second_stay_rate,
  61. v.third_stay_rate,
  62. v.seven_stay_rate,
  63. v.fifteen_stay_rate,
  64. v.thirty_stay_rate,
  65. v.fortyfive_stay_rate,
  66. v.sixty_stay_rate,
  67. v.game_user_sum
  68. from
  69. ( select dt, channel,stage,pitcher,platform,book from CostSourceData.channel_info_daily final
  70. where dt='{ymd}' and channel !=''
  71. and channel in
  72. (select distinct(channel) from CostSourceData.channel_by_account_daily final
  73. where dt='{ymd}'
  74. and (type ='GDT' or type='MP')
  75. )
  76. ) x -- 只允许渠道MP、GDT
  77. left join
  78. ( select channel,
  79. sum(cost) as cost,
  80. sum(view_count) as view_count,
  81. sum(valid_click_count) as click_count,
  82. sum(from_follow_uv) as follow_user,
  83. sum(web_view_count) as web_view_count,
  84. sum(platform_view_count) as platform_view_count,
  85. sum(web_order_count) as web_order_count
  86. from
  87. (select account_id,cost,view_count,valid_click_count,
  88. round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  89. 0 as web_view_count,
  90. 0 as platform_view_count,
  91. 0 as web_order_count
  92. from CostSourceData.daily_vx final
  93. where date=toDateTime('{ymd} 00:00:00')
  94. union all
  95. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  96. ifnull(web_commodity_page_view_count,0) as web_view_count,
  97. ifnull(platform_page_view_count,0) as platform_view_count,
  98. ifnull(web_order_count,0) as web_order_count
  99. from CostSourceData.daily_qq final
  100. where date=toDateTime('{ymd} 00:00:00')
  101. ) a
  102. left join
  103. (select toString(account_id) account_id,channel
  104. from CostSourceData.channel_by_account_daily final
  105. where dt=toDate('{ymd}')) b
  106. on a.account_id=b.account_id group by channel)
  107. z on x.channel=z.channel -- 只允许渠道cost 消耗
  108. left join
  109. (
  110. select channel,wx_date,count(*) reg_num
  111. from (
  112. select h.name as channel ,toDate(create_time) wx_date
  113. from
  114. GameDataSum.h_member_sum origin left join
  115. (select a.name name,d.app_id app_id ,d.agent_id agent_id
  116. from ( select * from CostSourceData.advertiser_vx final) a
  117. left join GameDataSum.h_game_sum b on a.book = b.name
  118. left join GameDataSum.mp_mp_conf_sum c on a.name= c.wx_name
  119. left join GameDataSum.mp_conf_agent_sum d on c.id=d.advertiser_conf_id
  120. where d.app_id =b.id
  121. group by a.name,d.app_id ,d.agent_id ) h
  122. on origin.app_id = h.app_id and origin.agent_id = h.agent_id
  123. where h.name is not null
  124. and origin.create_time > {timestamp_ymd} and origin.create_time < {timestamp_tom}
  125. ) tmp
  126. group by channel, wx_date
  127. ) y on x.channel= y.channel
  128. --- 注册用户数
  129. left join
  130. (
  131. select channel, wx_date,
  132. count(*) as create_user_num from
  133. (select h.name as channel,DATE(FROM_UNIXTIME(c.create_time)) as wx_date
  134. from ( select * from GameDataSum.h_mg_role_sum where create_time >= {timestamp_ymd} ) a
  135. left join GameDataSum.h_mem_game_sum b on a.mg_mem_id = b.id
  136. left join (select * from GameDataSum.h_member_sum
  137. where create_time >= {timestamp_ymd} and create_time <= {timestamp_tom} ) c
  138. on b.mem_id = c.id
  139. left join
  140. (select a.name name ,d.app_id app_id ,d.agent_id agent_id
  141. from ( select * from CostSourceData.advertiser_vx final) a
  142. left join GameDataSum.h_game_sum b on a.book = b.name
  143. left join GameDataSum.mp_mp_conf_sum c on a.name= c.wx_name
  144. left join GameDataSum.mp_conf_agent_sum d on c.id=d.advertiser_conf_id
  145. where d.app_id =b.id
  146. group by a.name,d.app_id ,d.agent_id ) h
  147. on c.app_id = h.app_id and c.agent_id = h.agent_id
  148. where h.name is not null
  149. ) xx
  150. group by channel,wx_date
  151. ) w on x.channel= w.channel
  152. -----创建角色数
  153. left join
  154. ( select channel ,
  155. if(max(d_ct)=0,0,ifnull(max(e_ct),0)/max(d_ct)) as today_active_user_rate,
  156. if(sum(a_ct)=0,0,ifnull(sum(b_ct),0)/sum(a_ct)) as second_stay_rate,
  157. if(sum(a_ct)=0,0,ifnull(sum(c_ct),0)/sum(a_ct)) as third_stay_rate,
  158. if(sum(a_ct)=0,0,ifnull(sum(seven_ct),0)/sum(a_ct)) as seven_stay_rate,
  159. if(sum(a_ct)=0,0,ifnull(sum(fifteen_ct),0)/sum(a_ct)) as fifteen_stay_rate,
  160. if(sum(a_ct)=0,0,ifnull(sum(thirty_ct),0)/sum(a_ct)) as thirty_stay_rate,
  161. if(sum(a_ct)=0,0,ifnull(sum(fortyfive_ct),0)/sum(a_ct)) as fortyfive_stay_rate,
  162. if(sum(a_ct)=0,0,ifnull(sum(sixty_ct),0)/sum(a_ct)) as sixty_stay_rate,
  163. max(d_ct) game_user_sum from
  164. (select h.name as channel ,
  165. a.ct as a_ct,b.ct as b_ct,c.ct as c_ct,d.ct as d_ct,e.ct as e_ct,
  166. seven.ct as seven_ct,fifteen.ct as fifteen_ct,thirty.ct as thirty_ct,
  167. fortyfive.ct as fortyfive_ct,sixty.ct as sixty_ct
  168. from
  169. (select a.name as name,d.app_id as app_id ,d.agent_id as agent_id
  170. from ( select * from CostSourceData.advertiser_vx final) a
  171. left join GameDataSum.h_game_sum b on a.book = b.name
  172. left join GameDataSum.mp_mp_conf_sum c on a.name= c.wx_name
  173. left join GameDataSum.mp_conf_agent_sum d on c.id=d.advertiser_conf_id
  174. where d.app_id =b.id
  175. group by a.name ,d.app_id ,d.agent_id) h
  176. left join
  177. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  178. ( select * from GameDataSum.h_member_sum
  179. where create_time >={timestamp_ymd}
  180. and create_time <={timestamp_tom}
  181. ) a
  182. left join
  183. (select DISTINCT mem_id mem_id
  184. from GameDataSum.h_log_mem_login_sum
  185. where date = '{ymd}'
  186. ) b on a.id=b.mem_id
  187. where b.mem_id !=''
  188. group by a.app_id ,a.agent_id
  189. ) a on a.app_id = h.app_id and a.agent_id = h.agent_id
  190. ----第一天
  191. left join
  192. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  193. ( select * from GameDataSum.h_member_sum
  194. where create_time >={timestamp_ymd}
  195. and create_time <={timestamp_tom}
  196. ) a
  197. left join
  198. (select DISTINCT mem_id mem_id
  199. from GameDataSum.h_log_mem_login_sum
  200. where date = '{ymd_tom}'
  201. ) b on a.id=b.mem_id
  202. where b.mem_id !=''
  203. group by a.app_id ,a.agent_id
  204. ) b on h.app_id =b.app_id and h.agent_id =b.agent_id
  205. ----第二天
  206. left join
  207. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  208. ( select * from GameDataSum.h_member_sum
  209. where create_time >={timestamp_ymd}
  210. and create_time <={timestamp_tom}
  211. ) a
  212. left join
  213. (select DISTINCT mem_id mem_id
  214. from GameDataSum.h_log_mem_login_sum
  215. where date = '{ymd_tom_after}'
  216. ) b on a.id=b.mem_id
  217. where b.mem_id !=''
  218. group by a.app_id ,a.agent_id
  219. ) c on h.app_id =c.app_id and h.agent_id =c.agent_id
  220. --第三天
  221. left join
  222. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  223. ( select * from GameDataSum.h_member_sum
  224. where create_time >={timestamp_ymd}
  225. and create_time <={timestamp_tom}
  226. ) a
  227. left join
  228. (select DISTINCT mem_id mem_id
  229. from GameDataSum.h_log_mem_login_sum
  230. where date = '{ymd_seven_day}'
  231. ) b on a.id=b.mem_id
  232. where b.mem_id !=''
  233. group by a.app_id ,a.agent_id
  234. ) seven on h.app_id = seven.app_id and h.agent_id = seven.agent_id
  235. --第七天
  236. left join
  237. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  238. ( select * from GameDataSum.h_member_sum
  239. where create_time >={timestamp_ymd}
  240. and create_time <={timestamp_tom}
  241. ) a
  242. left join
  243. (select DISTINCT mem_id mem_id
  244. from GameDataSum.h_log_mem_login_sum
  245. where date = '{ymd_fifteen_day}'
  246. ) b on a.id=b.mem_id
  247. where b.mem_id !=''
  248. group by a.app_id ,a.agent_id
  249. ) fifteen on h.app_id =fifteen.app_id and h.agent_id = fifteen.agent_id
  250. --第15天
  251. left join
  252. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  253. ( select * from GameDataSum.h_member_sum
  254. where create_time >={timestamp_ymd}
  255. and create_time <={timestamp_tom}
  256. ) a
  257. left join
  258. (select DISTINCT mem_id mem_id
  259. from GameDataSum.h_log_mem_login_sum
  260. where date = '{ymd_thirty_day}'
  261. ) b on a.id=b.mem_id
  262. where b.mem_id !=''
  263. group by a.app_id ,a.agent_id
  264. ) thirty on h.app_id =thirty.app_id and h.agent_id = thirty.agent_id
  265. --第30天
  266. left join
  267. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  268. ( select * from GameDataSum.h_member_sum
  269. where create_time >={timestamp_ymd}
  270. and create_time <={timestamp_tom}
  271. ) a
  272. left join
  273. (select DISTINCT mem_id mem_id
  274. from GameDataSum.h_log_mem_login_sum
  275. where date = '{ymd_fortyfive_day}'
  276. ) b on a.id=b.mem_id
  277. where b.mem_id !=''
  278. group by a.app_id ,a.agent_id
  279. ) fortyfive on h.app_id =fortyfive.app_id and h.agent_id = fortyfive.agent_id
  280. --第45天
  281. left join
  282. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  283. ( select * from GameDataSum.h_member_sum
  284. where create_time >={timestamp_ymd}
  285. and create_time <={timestamp_tom}
  286. ) a
  287. left join
  288. (select DISTINCT mem_id mem_id
  289. from GameDataSum.h_log_mem_login_sum
  290. where date = '{ymd_sixty_day}'
  291. ) b on a.id=b.mem_id
  292. where b.mem_id !=''
  293. group by a.app_id ,a.agent_id
  294. ) sixty on h.app_id =sixty.app_id and h.agent_id = sixty.agent_id
  295. --第60天
  296. left join
  297. (select app_id ,count(*) as ct from GameDataSum.h_member_sum hm
  298. where create_time <={timestamp_tom}
  299. group by app_id ) d on h.app_id =d.app_id
  300. --游戏用户数量
  301. left join
  302. (select count(distinct(mem_id)) as ct,app_id from GameDataSum.h_log_mem_login_sum
  303. where date = '{ymd}'
  304. group by app_id ) e on h.app_id =e.app_id
  305. --今日活跃用户数量
  306. )
  307. as keep_data
  308. group by channel) v on x.channel= v.channel
  309. '''
  310. print(ck_sql)
  311. ck.execute(ck_sql)
  312. def dw_daily_channel_cost_ck(ymd):
  313. def table_name(datatime_tmp, datatime_realtime):
  314. datatime_use = min(datatime_tmp, datatime_realtime)
  315. str_year = datatime_use.tm_year
  316. str_mon = datatime_use.tm_mon
  317. str_mon = str_mon if str_mon > 9 else '0' + str(str_mon)
  318. res = 'h_log_mem_login_{}{}'.format(str_year, str_mon)
  319. return res
  320. logging.info("run> dw_daily_channel_cost")
  321. datatime_ymd = datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))).timetuple()
  322. datatime_ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  323. days=1)).timetuple()
  324. datatime_ymd_tom_after = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  325. days=2)).timetuple()
  326. datatime_ymd_seven_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  327. days=7)).timetuple()
  328. datatime_ymd_fifteen_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  329. days=15)).timetuple()
  330. datatime_ymd_thirty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  331. days=30)).timetuple()
  332. datatime_ymd_fortyfive_day = (
  333. datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  334. days=45)).timetuple()
  335. datatime_ymd_sixty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  336. days=60)).timetuple()
  337. datatime_realtime = datetime.now().timetuple()
  338. # datatime_str
  339. ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  340. days=1)).strftime('%Y-%m-%d')
  341. ymd_tom_after = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  342. days=2)).strftime('%Y-%m-%d')
  343. ymd_seven_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  344. days=7)).strftime('%Y-%m-%d')
  345. ymd_fifteen_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  346. days=15)).strftime('%Y-%m-%d')
  347. ymd_thirty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  348. days=30)).strftime('%Y-%m-%d')
  349. ymd_fortyfive_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  350. days=45)).strftime('%Y-%m-%d')
  351. ymd_sixty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  352. days=60)).strftime('%Y-%m-%d')
  353. # timestamp
  354. timestamp_ymd = time.mktime(datatime_ymd)
  355. timestamp_tom = time.mktime(datatime_ymd_tom)
  356. # table_name
  357. table_name_login_today = table_name(datatime_ymd, datatime_realtime)
  358. table_name_login_tom = table_name(datatime_ymd_tom, datatime_realtime)
  359. table_name_login_tom_after = table_name(datatime_ymd_tom_after, datatime_realtime)
  360. table_name_login_seven_day = table_name(datatime_ymd_seven_day, datatime_realtime)
  361. table_name_login_fifteen_day = table_name(datatime_ymd_fifteen_day, datatime_realtime)
  362. table_name_login_thirty_day = table_name(datatime_ymd_thirty_day, datatime_realtime)
  363. table_name_login_fortyfive_day = table_name(datatime_ymd_fortyfive_day, datatime_realtime)
  364. table_name_login_sixty_day = table_name(datatime_ymd_sixty_day, datatime_realtime)
  365. database_names = {'GameDataTwoDbMpPart', 'GameDataOneDbMpPart'}
  366. for database_name in database_names:
  367. ck_sql = f'''
  368. insert into game_data.dw_daily_channel_cost
  369. (dt,channel,pitcher,stage,platform,book,view_count,click_count,follow_user,cost,
  370. web_view_count,platform_view_count,web_order_count,type,require_roi,require_mult,
  371. reg_num,create_user_num,today_active_user_rate,second_stay_rate,third_stay_rate,
  372. seven_stay_rate,fifteen_stay_rate,thirty_stay_rate,fortyfive_stay_rate,
  373. sixty_stay_rate,game_user_sum)
  374. select x.dt,x.channel,pitcher,stage,x.platform,x.book,
  375. ifnull(view_count,0),ifnull(click_count,0),
  376. ifnull(follow_user,0),ifnull(cost,0)/100 as cost,
  377. ifnull(web_view_count,0) web_view_count,
  378. ifnull(platform_view_count,0) platform_view_count,
  379. ifnull(web_order_count,0) web_order_count,
  380. if(stage ='趣程15期' or stage ='趣程26期' or stage ='趣程30期','GDT','MP') type
  381. ,0 require_roi,0 require_mult,
  382. ifnull(y.reg_num,0),ifnull(w.create_user_num,0),
  383. v.today_active_user_rate,
  384. v.second_stay_rate,
  385. v.third_stay_rate,
  386. v.seven_stay_rate,
  387. v.fifteen_stay_rate,
  388. v.thirty_stay_rate,
  389. v.fortyfive_stay_rate,
  390. v.sixty_stay_rate,
  391. v.game_user_sum
  392. from
  393. ( select dt, channel,stage,pitcher,platform,book from CostSourceData.channel_info_daily cid
  394. where dt='{ymd}' and channel !=''
  395. and channel in
  396. (select distinct(channel) from CostSourceData.channel_by_account_daily cbad
  397. where dt='{ymd}'
  398. and (type ='GDT' or type='MP')
  399. )
  400. ) x -- 只允许渠道MP、GDT
  401. left join
  402. ( select channel,
  403. sum(cost) as cost,
  404. sum(view_count) as view_count,
  405. sum(valid_click_count) as click_count,
  406. sum(from_follow_uv) as follow_user,
  407. sum(web_view_count) as web_view_count,
  408. sum(platform_view_count) as platform_view_count,
  409. sum(web_order_count) as web_order_count
  410. from
  411. (select account_id,cost,view_count,valid_click_count,
  412. round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  413. 0 as web_view_count,
  414. 0 as platform_view_count,
  415. 0 as web_order_count
  416. from CostSourceData.daily_vx
  417. where date=toDateTime('{ymd} 00:00:00')
  418. union all
  419. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  420. ifnull(web_commodity_page_view_count,0) as web_view_count,
  421. ifnull(platform_page_view_count,0) as platform_view_count,
  422. ifnull(web_order_count,0) as web_order_count
  423. from CostSourceData.daily_qq
  424. where date=toDateTime('{ymd} 00:00:00')
  425. ) a
  426. left join
  427. (select toString(account_id) account_id,channel
  428. from CostSourceData.channel_by_account_daily
  429. where dt=toDate('{ymd}')) b
  430. on a.account_id=b.account_id group by channel)
  431. z on x.channel=z.channel -- 只允许渠道cost 消耗
  432. left join
  433. (
  434. select channel,wx_date,count(*) reg_num
  435. from (
  436. select h.name as channel ,toDate(create_time) wx_date
  437. from
  438. {database_name}.h_member origin left join
  439. (select a.name name,d.app_id app_id ,d.agent_id agent_id from CostSourceData.advertiser_vx a
  440. left join {database_name}.h_game b on a.book = b.name
  441. left join {database_name}.mp_mp_conf c on a.name= c.wx_name
  442. left join {database_name}.mp_conf_agent d on c.id=d.advertiser_conf_id
  443. where d.app_id =b.id
  444. group by a.name,d.app_id ,d.agent_id ) h
  445. on origin.app_id = h.app_id and origin.agent_id = h.agent_id
  446. where h.name is not null
  447. and origin.create_time > {timestamp_ymd} and origin.create_time < {timestamp_tom}
  448. ) tmp
  449. group by channel, wx_date
  450. ) y on x.channel= y.channel
  451. --- 注册用户数
  452. left join
  453. (
  454. select channel, wx_date,
  455. count(*) as create_user_num from
  456. (select h.name as channel,DATE(FROM_UNIXTIME(c.create_time)) as wx_date
  457. from {database_name}.h_mg_role a
  458. left join {database_name}.h_mem_game b on a.mg_mem_id = b.id
  459. left join {database_name}.h_member c on b.mem_id = c.id
  460. left join
  461. (select a.name name ,d.app_id app_id ,d.agent_id agent_id from CostSourceData.advertiser_vx a
  462. left join {database_name}.h_game b on a.book = b.name
  463. left join {database_name}.mp_mp_conf c on a.name= c.wx_name
  464. left join {database_name}.mp_conf_agent d on c.id=d.advertiser_conf_id
  465. where d.app_id =b.id
  466. group by a.name,d.app_id ,d.agent_id ) h
  467. on c.app_id = h.app_id and c.agent_id = h.agent_id
  468. where h.name is not null
  469. and c.create_time >= {timestamp_ymd} and c.create_time <= {timestamp_tom}
  470. ) xx
  471. group by channel,wx_date
  472. ) w on x.channel= w.channel
  473. -----创建角色数
  474. left join
  475. ( select channel ,
  476. if(max(d_ct)=0,0,ifnull(max(e_ct),0)/max(d_ct)) as today_active_user_rate,
  477. if(sum(a_ct)=0,0,ifnull(sum(b_ct),0)/sum(a_ct)) as second_stay_rate,
  478. if(sum(a_ct)=0,0,ifnull(sum(c_ct),0)/sum(a_ct)) as third_stay_rate,
  479. if(sum(a_ct)=0,0,ifnull(sum(seven_ct),0)/sum(a_ct)) as seven_stay_rate,
  480. if(sum(a_ct)=0,0,ifnull(sum(fifteen_ct),0)/sum(a_ct)) as fifteen_stay_rate,
  481. if(sum(a_ct)=0,0,ifnull(sum(thirty_ct),0)/sum(a_ct)) as thirty_stay_rate,
  482. if(sum(a_ct)=0,0,ifnull(sum(fortyfive_ct),0)/sum(a_ct)) as fortyfive_stay_rate,
  483. if(sum(a_ct)=0,0,ifnull(sum(sixty_ct),0)/sum(a_ct)) as sixty_stay_rate,
  484. max(d_ct) game_user_sum from
  485. (select h.name as channel ,
  486. a.ct as a_ct,b.ct as b_ct,c.ct as c_ct,d.ct as d_ct,e.ct as e_ct,
  487. seven.ct as seven_ct,fifteen.ct as fifteen_ct,thirty.ct as thirty_ct,
  488. fortyfive.ct as fortyfive_ct,sixty.ct as sixty_ct
  489. from
  490. (select a.name as name,d.app_id as app_id ,d.agent_id as agent_id
  491. from CostSourceData.advertiser_vx a
  492. left join {database_name}.h_game b on a.book = b.name
  493. left join {database_name}.mp_mp_conf c on a.name= c.wx_name
  494. left join {database_name}.mp_conf_agent d on c.id=d.advertiser_conf_id
  495. where d.app_id =b.id
  496. group by a.name ,d.app_id ,d.agent_id) h
  497. left join
  498. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  499. {database_name}.h_member a
  500. left join
  501. (select toUInt64(mem_id) mem_id from
  502. (select Distinct(mem_id) mem_id,date
  503. from {database_name}.{table_name_login_today}
  504. where date = '{ymd}'
  505. ) ) b on a.id=b.mem_id
  506. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  507. and b.mem_id !=0
  508. group by a.app_id ,a.agent_id
  509. ) a on a.app_id = h.app_id and a.agent_id = h.agent_id
  510. ----第一天
  511. left join
  512. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  513. {database_name}.h_member a
  514. left join
  515. (select toUInt64(mem_id) mem_id from
  516. (select Distinct(mem_id) mem_id,date
  517. from {database_name}.{table_name_login_tom}
  518. where date = '{ymd_tom}'
  519. ) ) b on a.id=b.mem_id
  520. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  521. and b.mem_id !=0
  522. group by a.app_id ,a.agent_id
  523. ) b on h.app_id =b.app_id and h.agent_id =b.agent_id
  524. ----第二天
  525. left join
  526. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  527. {database_name}.h_member a
  528. left join
  529. (select toUInt64(mem_id) mem_id from
  530. (select Distinct(mem_id) mem_id,date
  531. from {database_name}.{table_name_login_tom_after}
  532. where date = '{ymd_tom_after}'
  533. ) ) b on a.id=b.mem_id
  534. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  535. and b.mem_id !=0
  536. group by a.app_id ,a.agent_id
  537. ) c on h.app_id =c.app_id and h.agent_id = c.agent_id
  538. --第三天
  539. left join
  540. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  541. {database_name}.h_member a
  542. left join
  543. (select toUInt64(mem_id) mem_id from
  544. (select Distinct(mem_id) mem_id,date
  545. from {database_name}.{table_name_login_seven_day}
  546. where date = '{ymd_seven_day}'
  547. ) ) b on a.id=b.mem_id
  548. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  549. and b.mem_id !=0
  550. group by a.app_id ,a.agent_id
  551. ) seven on h.app_id = seven.app_id and h.agent_id = seven.agent_id
  552. --第七天
  553. left join
  554. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  555. {database_name}.h_member a
  556. left join
  557. (select toUInt64(mem_id) mem_id from
  558. (select Distinct(mem_id) mem_id,date
  559. from {database_name}.{table_name_login_fifteen_day}
  560. where date = '{ymd_fifteen_day}'
  561. ) ) b on a.id=b.mem_id
  562. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  563. and b.mem_id !=0
  564. group by a.app_id ,a.agent_id
  565. ) fifteen on h.app_id =fifteen.app_id and h.agent_id = fifteen.agent_id
  566. --第15天
  567. left join
  568. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  569. {database_name}.h_member a
  570. left join
  571. (select toUInt64(mem_id) mem_id from
  572. (select Distinct(mem_id) mem_id,date
  573. from {database_name}.{table_name_login_thirty_day}
  574. where date = '{ymd_thirty_day}'
  575. ) ) b on a.id=b.mem_id
  576. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  577. and b.mem_id !=0
  578. group by a.app_id ,a.agent_id
  579. ) thirty on h.app_id =thirty.app_id and h.agent_id = thirty.agent_id
  580. --第30天
  581. left join
  582. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  583. {database_name}.h_member a
  584. left join
  585. (select toUInt64(mem_id) mem_id from
  586. (select Distinct(mem_id) mem_id,date
  587. from {database_name}.{table_name_login_fortyfive_day}
  588. where date = '{ymd_fortyfive_day}'
  589. ) ) b on a.id=b.mem_id
  590. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  591. and b.mem_id !=0
  592. group by a.app_id ,a.agent_id
  593. ) fortyfive on h.app_id =fortyfive.app_id and h.agent_id = fortyfive.agent_id
  594. --第45天
  595. left join
  596. (select '{ymd}',a.app_id,a.agent_id,count(*) ct from
  597. {database_name}.h_member a
  598. left join
  599. (select toUInt64(mem_id) mem_id from
  600. (select Distinct(mem_id) mem_id,date
  601. from {database_name}.{table_name_login_sixty_day}
  602. where date = '{ymd_sixty_day}'
  603. ) ) b on a.id=b.mem_id
  604. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  605. and b.mem_id !=0
  606. group by a.app_id ,a.agent_id
  607. ) sixty on h.app_id =sixty.app_id and h.agent_id = sixty.agent_id
  608. --第60天
  609. left join
  610. (select app_id ,count(*) as ct from {database_name}.h_member hm
  611. where create_time <={timestamp_tom}
  612. group by app_id ) d on h.app_id =d.app_id
  613. --游戏用户数量
  614. left join
  615. (select count(distinct(mem_id)) as ct,app_id from {database_name}.{table_name_login_today}
  616. where date = '{ymd}'
  617. group by app_id ) e on h.app_id =e.app_id
  618. --今日活跃用户数量
  619. )
  620. as keep_data
  621. group by channel) v on x.channel= v.channel
  622. '''
  623. print(ck_sql)
  624. ck.execute(ck_sql)
  625. def dw_daily_channel_cost(ymd):
  626. def table_name(datatime_tmp, datatime_realtime):
  627. datatime_use = min(datatime_tmp, datatime_realtime)
  628. str_year = datatime_use.tm_year
  629. str_mon = datatime_use.tm_mon
  630. str_mon = str_mon if str_mon > 9 else '0' + str(str_mon)
  631. res = 'h_log_mem_login_{}{}'.format(str_year, str_mon)
  632. return res
  633. logging.info("run> dw_daily_channel_cost")
  634. datatime_ymd = datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))).timetuple()
  635. datatime_ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  636. days=1)).timetuple()
  637. datatime_ymd_tom_after = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  638. days=2)).timetuple()
  639. datatime_ymd_seven_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  640. days=7)).timetuple()
  641. datatime_ymd_fifteen_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  642. days=15)).timetuple()
  643. datatime_ymd_thirty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  644. days=30)).timetuple()
  645. datatime_ymd_fortyfive_day = (
  646. datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  647. days=45)).timetuple()
  648. datatime_ymd_sixty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  649. days=60)).timetuple()
  650. datatime_realtime = datetime.now().timetuple()
  651. # datatime_str
  652. ymd_tom = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  653. days=1)).strftime('%Y-%m-%d')
  654. ymd_tom_after = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  655. days=2)).strftime('%Y-%m-%d')
  656. ymd_seven_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  657. days=7)).strftime('%Y-%m-%d')
  658. ymd_fifteen_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  659. days=15)).strftime('%Y-%m-%d')
  660. ymd_thirty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  661. days=30)).strftime('%Y-%m-%d')
  662. ymd_fortyfive_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  663. days=45)).strftime('%Y-%m-%d')
  664. ymd_sixty_day = (datetime.strptime(ymd, '%Y-%m-%d').astimezone(timezone(timedelta(hours=8))) + timedelta(
  665. days=60)).strftime('%Y-%m-%d')
  666. # timestamp
  667. timestamp_ymd = time.mktime(datatime_ymd)
  668. timestamp_tom = time.mktime(datatime_ymd_tom)
  669. # table_name
  670. table_name_login_today = table_name(datatime_ymd, datatime_realtime)
  671. table_name_login_tom = table_name(datatime_ymd_tom, datatime_realtime)
  672. table_name_login_tom_after = table_name(datatime_ymd_tom_after, datatime_realtime)
  673. table_name_login_seven_day = table_name(datatime_ymd_seven_day, datatime_realtime)
  674. table_name_login_fifteen_day = table_name(datatime_ymd_fifteen_day, datatime_realtime)
  675. table_name_login_thirty_day = table_name(datatime_ymd_thirty_day, datatime_realtime)
  676. table_name_login_fortyfive_day = table_name(datatime_ymd_fortyfive_day, datatime_realtime)
  677. table_name_login_sixty_day = table_name(datatime_ymd_sixty_day, datatime_realtime)
  678. sql = f"""
  679. select x.dt,x.channel,pitcher,stage,x.platform,x.book,
  680. ifnull(view_count,0),ifnull(click_count,0),
  681. ifnull(follow_user,0),ifnull(cost,0)/100 as cost,
  682. ifnull(web_view_count,0) web_view_count,
  683. ifnull(platform_view_count,0) platform_view_count,
  684. ifnull(web_order_count,0) web_order_count,
  685. if(stage ='趣程15期' or stage ='趣程26期' or stage ='趣程30期','GDT','MP') type
  686. ,0 require_roi,0 require_mult,
  687. ifnull(y.reg_num,0),ifnull(w.create_user_num,0),
  688. v.today_active_user_rate,
  689. v.second_stay_rate,
  690. v.third_stay_rate,
  691. v.seven_stay_rate,
  692. v.fifteen_stay_rate,
  693. v.thirty_stay_rate,
  694. v.fortyfive_stay_rate,
  695. v.sixty_stay_rate,
  696. v.game_user_sum
  697. from
  698. ( select dt, channel,stage,pitcher,platform,book from channel_info_daily cid
  699. where dt='{ymd}' and channel !=''
  700. and channel in
  701. (select distinct(channel) from channel_by_account_daily cbad
  702. where dt='{ymd}'
  703. and (type ='GDT' or type='MP')
  704. )
  705. ) x -- 只允许渠道MP、GDT
  706. left join
  707. (select channel,sum(cost) as cost,sum(view_count) as view_count,sum(valid_click_count) as click_count,
  708. sum(from_follow_uv) as follow_user,
  709. sum(web_view_count) as web_view_count,
  710. sum(platform_view_count) as platform_view_count,
  711. sum(web_order_count) as web_order_count
  712. from
  713. (select account_id,cost,view_count,valid_click_count,
  714. round(valid_click_count*official_account_follow_rate,0) as from_follow_uv,
  715. 0 as web_view_count,
  716. 0 as platform_view_count,
  717. 0 as web_order_count
  718. from daily_vx where date='{ymd} 00:00:00'
  719. union
  720. select account_id,cost,view_count,valid_click_count,from_follow_uv,
  721. ifnull(web_commodity_page_view_count,0) as web_view_count,
  722. ifnull(platform_page_view_count,0) as platform_view_count,
  723. ifnull(web_order_count,0) as web_order_count
  724. from daily_qq where date='{ymd} 00:00:00' ) a
  725. left join
  726. (select account_id,channel from channel_by_account_daily where dt='{ymd}') b
  727. on a.account_id=b.account_id group by channel)
  728. z on x.channel=z.channel
  729. left join
  730. (
  731. select h.name as channel ,DATE(FROM_UNIXTIME(origin.create_time)) as wx_date,
  732. count(*) as reg_num
  733. from
  734. db_mp.h_member origin left join
  735. (select a.name,d.app_id,d.agent_id from quchen_text.advertiser_vx a
  736. left join db_mp.h_game b on a.book = b.name
  737. left join db_mp.mp_mp_conf c on a.name= c.wx_name
  738. left join db_mp.mp_conf_agent d on c.id=d.advertiser_conf_id
  739. where d.app_id =b.id
  740. group by d.app_id ,d.agent_id ) h
  741. on origin.app_id = h.app_id and origin.agent_id = h.agent_id
  742. where h.name is not null
  743. and origin.create_time > {timestamp_ymd} and origin.create_time < {timestamp_tom}
  744. group by name,wx_date
  745. ) y on x.channel= y.channel
  746. left join
  747. (
  748. select h.name as channel,DATE(FROM_UNIXTIME(c.create_time)) as wx_date,
  749. count(*) as create_user_num
  750. from db_mp.h_mg_role a
  751. left join db_mp.h_mem_game b on a.mg_mem_id = b.id
  752. left join db_mp.h_member c on b.mem_id = c.id
  753. left join
  754. (select a.name,d.app_id,d.agent_id from quchen_text.advertiser_vx a
  755. left join db_mp.h_game b on a.book = b.name
  756. left join db_mp.mp_mp_conf c on a.name= c.wx_name
  757. left join db_mp.mp_conf_agent d on c.id=d.advertiser_conf_id
  758. where d.app_id =b.id
  759. group by d.app_id ,d.agent_id ) h
  760. on c.app_id = h.app_id and c.agent_id = h.agent_id
  761. where h.name is not null
  762. and c.create_time >= {timestamp_ymd} and c.create_time <= {timestamp_tom}
  763. group by h.name,wx_date
  764. order by wx_date desc
  765. ) w on x.channel= w.channel
  766. left join
  767. (
  768. select channel ,
  769. if(max(d_ct)=0,0,ifnull(max(e_ct),0)/max(d_ct)) as today_active_user_rate,
  770. if(sum(a_ct)=0,0,ifnull(sum(b_ct),0)/sum(a_ct)) as second_stay_rate,
  771. if(sum(a_ct)=0,0,ifnull(sum(c_ct),0)/sum(a_ct)) as third_stay_rate,
  772. if(sum(a_ct)=0,0,ifnull(sum(seven_ct),0)/sum(a_ct)) as seven_stay_rate,
  773. if(sum(a_ct)=0,0,ifnull(sum(fifteen_ct),0)/sum(a_ct)) as fifteen_stay_rate,
  774. if(sum(a_ct)=0,0,ifnull(sum(thirty_ct),0)/sum(a_ct)) as thirty_stay_rate,
  775. if(sum(a_ct)=0,0,ifnull(sum(fortyfive_ct),0)/sum(a_ct)) as fortyfive_stay_rate,
  776. if(sum(a_ct)=0,0,ifnull(sum(sixty_ct),0)/sum(a_ct)) as sixty_stay_rate,
  777. max(d_ct) game_user_sum from
  778. (select h.name as channel ,
  779. a.ct as a_ct,b.ct as b_ct,c.ct as c_ct,d.ct as d_ct,e.ct as e_ct,
  780. seven.ct as seven_ct,fifteen.ct as fifteen_ct,thirty.ct as thirty_ct,
  781. fortyfive.ct as fortyfive_ct,sixty.ct as sixty_ct
  782. from
  783. (select a.name as name,d.app_id as app_id ,d.agent_id as agent_id
  784. from quchen_text.advertiser_vx a
  785. left join db_mp.h_game b on a.book = b.name
  786. left join db_mp.mp_mp_conf c on a.name= c.wx_name
  787. left join db_mp.mp_conf_agent d on c.id=d.advertiser_conf_id
  788. where d.app_id =b.id
  789. group by d.app_id ,d.agent_id) h
  790. left join
  791. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  792. db_mp.h_member a
  793. left join (select distinct(mem_id) from db_mp.{table_name_login_today}
  794. where date = '{ymd}' ) b on a.id=b.mem_id
  795. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  796. and b.mem_id is not null
  797. group by a.app_id ,a.agent_id ) a on a.app_id = h.app_id and a.agent_id = h.agent_id
  798. left join
  799. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  800. db_mp.h_member a
  801. left join (select distinct(mem_id) from db_mp.{table_name_login_tom}
  802. where date = '{ymd_tom}' ) b on a.id=b.mem_id
  803. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  804. and b.mem_id is not null
  805. group by a.app_id ,a.agent_id ) b on h.app_id =b.app_id and h.agent_id =b.agent_id
  806. left join
  807. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  808. db_mp.h_member a
  809. left join (select distinct(mem_id) from db_mp.{table_name_login_tom_after}
  810. where date = '{ymd_tom_after}' ) b on a.id=b.mem_id
  811. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  812. and b.mem_id is not null
  813. group by a.app_id ,a.agent_id ) c on h.app_id =c.app_id and h.agent_id = c.agent_id
  814. left join
  815. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  816. db_mp.h_member a
  817. left join (select distinct(mem_id) from db_mp.{table_name_login_seven_day}
  818. where date = '{ymd_seven_day}' ) b on a.id=b.mem_id
  819. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  820. and b.mem_id is not null
  821. group by a.app_id ,a.agent_id ) seven on h.app_id =seven.app_id and h.agent_id = seven.agent_id
  822. left join
  823. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  824. db_mp.h_member a
  825. left join (select distinct(mem_id) from db_mp.{table_name_login_fifteen_day}
  826. where date = '{ymd_fifteen_day}' ) b on a.id=b.mem_id
  827. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  828. and b.mem_id is not null
  829. group by a.app_id ,a.agent_id ) fifteen on h.app_id =fifteen.app_id and h.agent_id = fifteen.agent_id
  830. left join
  831. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  832. db_mp.h_member a
  833. left join (select distinct(mem_id) from db_mp.{table_name_login_thirty_day}
  834. where date = '{ymd_thirty_day}' ) b on a.id=b.mem_id
  835. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  836. and b.mem_id is not null
  837. group by a.app_id ,a.agent_id ) thirty on h.app_id =thirty.app_id and h.agent_id = thirty.agent_id
  838. left join
  839. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  840. db_mp.h_member a
  841. left join (select distinct(mem_id) from db_mp.{table_name_login_fortyfive_day}
  842. where date = '{ymd_fortyfive_day}' ) b on a.id=b.mem_id
  843. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  844. and b.mem_id is not null
  845. group by a.app_id ,a.agent_id ) fortyfive on h.app_id =fortyfive.app_id and h.agent_id = fortyfive.agent_id
  846. left join
  847. (select '{ymd}',a.app_id,a.agent_id,count(*) as ct from
  848. db_mp.h_member a
  849. left join (select distinct(mem_id) from db_mp.{table_name_login_sixty_day}
  850. where date = '{ymd_sixty_day}' ) b on a.id=b.mem_id
  851. where a.create_time >={timestamp_ymd} and a.create_time <={timestamp_tom}
  852. and b.mem_id is not null
  853. group by a.app_id ,a.agent_id ) sixty on h.app_id =sixty.app_id and h.agent_id = sixty.agent_id
  854. left join
  855. (select app_id ,count(*) as ct from db_mp.h_member hm
  856. where create_time <={timestamp_tom}
  857. group by app_id ) d on h.app_id =d.app_id
  858. left join
  859. (select count(distinct(mem_id)) as ct,app_id from db_mp.{table_name_login_today}
  860. where date = '{ymd}'
  861. group by app_id ) e on h.app_id =e.app_id
  862. ) as keep_data
  863. group by channel)
  864. v on x.channel= v.channel
  865. """
  866. # print(sql)
  867. data = db.quchen_text.get_data_list(sql)
  868. data1 = []
  869. col = "dt,channel,pitcher,stage,platform,book,view_count,click_count,follow_user,cost,web_view_count,platform_view_count,web_order_count,type,require_roi,require_mult,reg_num,create_user_num,today_active_user_rate,second_stay_rate,third_stay_rate,seven_stay_rate,fifteen_stay_rate,thirty_stay_rate,fortyfive_stay_rate,sixty_stay_rate,game_user_sum"
  870. for i in data:
  871. i[0] = str(i[0])
  872. i[9] = str(i[9])
  873. i[6] = float(i[6])
  874. i[7] = float(i[7])
  875. i[8] = float(i[8])
  876. i[9] = float(i[9])
  877. i[10] = float(i[10])
  878. i[11] = float(i[11])
  879. i[12] = float(i[12])
  880. i[18] = float(i[18]) if i[18] else 0
  881. i[19] = float(i[19]) if i[19] else 0
  882. i[20] = float(i[20]) if i[20] else 0
  883. i[21] = float(i[21]) if i[21] else 0
  884. i[22] = float(i[22]) if i[22] else 0
  885. i[23] = float(i[23]) if i[23] else 0
  886. i[24] = float(i[24]) if i[24] else 0
  887. i[25] = float(i[25]) if i[25] else 0
  888. i[26] = float(i[26]) if i[26] else 0
  889. data1.append(tuple(i))
  890. for _ in data1:
  891. print(_)
  892. ck.execute(f"alter table game_data.dw_daily_channel_cost drop partition '{ymd}' ")
  893. logging.info(len(data1))
  894. ck.insertMany("game_data.dw_daily_channel_cost", col, tuple(data1))
  895. def channel_by_account_daily(ymd):
  896. """返回当天消耗账户对应的公众号表"""
  897. logging.info("run> channel_by_account_daily")
  898. sql = """replace into channel_by_account_daily
  899. select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel,type from
  900. (select account_id,name,'GDT' type from advertiser_qq
  901. union
  902. select account_id,name,'MP' type from advertiser_vx
  903. union
  904. select advertiser_id,channel,'BYTEDANCE' type from advertiser_bytedance
  905. ) a
  906. left join
  907. (select b.account_id,b.name from
  908. (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
  909. left join account_change b on a.end_time=b.end_time and a.account_id=b.account_id) b on a.account_id=b.account_id""".format(
  910. ymd)
  911. db.quchen_text.execute(sql)
  912. def channel_info_daily(ymd):
  913. """获取公众号某天的期数,投手,平台,书籍
  914. @ return [[]]
  915. """
  916. # 获取现在的全量公众号信息
  917. logging.info("run> channel_info_daily")
  918. sql = f"""select '{ymd}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
  919. select name from advertiser_vx where name is not null group by name-- 公众号全量表
  920. union
  921. select name from account_change group by name
  922. union
  923. select channel as name from pitcher_change group by channel
  924. union
  925. select name from platform_change group by name
  926. union
  927. select name from book_change group by name) a
  928. left join
  929. ( select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,
  930. ifnull(platform,'') platform,ifnull(book,'') book
  931. from advertiser_vx
  932. where name is not null
  933. and start_date <= '{ymd}'
  934. and if(end_date is null,1,end_date >= '{ymd}')
  935. group by name,stage,pitcher,platform,book
  936. ) b on a.name=b.name
  937. """
  938. data = db.quchen_text.get_data_list(sql)
  939. pitcher_change = db.quchen_text.getData(
  940. "select b.channel as channel,pitcher from "
  941. "(select max(start_time) as start_time,channel from pitcher_change "
  942. " where start_time<='{}' GROUP BY channel) a"
  943. " left join pitcher_change b on a.start_time=b.start_time and a.channel=b.channel".format(ymd))
  944. platform_change = db.quchen_text.getData(
  945. "select b.name as channel,current_platform as platform from (select max(change_date) as change_date,name from platform_change "
  946. "where change_date<='{}' GROUP BY name) a "
  947. "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
  948. book_change = db.quchen_text.getData(
  949. "select b.name as channel,book from (select max(start_time) as start_time,name from book_change "
  950. "where start_time<='{}' GROUP BY name) a "
  951. "left join book_change b on a.start_time=b.start_time and a.name=b.name".format(ymd))
  952. stage_change = db.quchen_text.getData(
  953. "select channel,stage from (select max(start_date) as start_date,channel from stage_change "
  954. "where start_date<='{}' GROUP BY channel) a "
  955. "left join stage_change using(start_date,channel)".format(ymd))
  956. for i in data:
  957. for j in pitcher_change:
  958. if i[1] == j[0]:
  959. i[3] = j[1]
  960. for k in platform_change:
  961. if i[1] == k[0]:
  962. i[4] = k[1]
  963. for h in book_change:
  964. if i[1] == h[0]:
  965. i[5] = h[1]
  966. for m in stage_change:
  967. if i[1] == m[0]:
  968. i[2] = m[1]
  969. insert_sql = "replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
  970. db.quchen_text.executeMany(insert_sql, data)
  971. def ods_order(dt):
  972. sql = """ replace into ods_order
  973. select
  974. case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
  975. when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
  976. ELSE order_time end date,
  977. stage,platform,channel,channel_id,user_id,
  978. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  979. when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
  980. ELSE order_time end order_time,
  981. case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
  982. when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
  983. ELSE reg_time end reg_time,
  984. amount,from_novel,order_id,2 from `order` where date=UNIX_TIMESTAMP('{}')
  985. """.format(dt)
  986. db.quchen_text.execute(sql)
  987. def order_account_text():
  988. db.quchen_text.execute("truncate order_account_text")
  989. with open('./wending_account_config.csv', encoding='utf-8') as f:
  990. for i in f.readlines():
  991. db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
  992. if __name__ == '__main__':
  993. # channel_info_daily('2021-02-06')
  994. # dw_daily_channel_cost('2021-09-19')
  995. # exit()
  996. # channel_by_account_daily('2021-02-05')
  997. for i in dt.getDateLists('2021-09-01', '2022-01-02'):
  998. # print(i)
  999. # channel_by_account_daily(i)
  1000. # channel_info_daily(i)
  1001. #
  1002. # dw_daily_channel_cost(i)
  1003. # # ods_order('2021-05-06')
  1004. # platform_data_sum(i)
  1005. # dw_daily_channel_cost_ck(i)
  1006. dw_daily_channel_cost_ck_gamedatasum(i)