""" @desc 公众号维度的数据处理 @auth ck """ from model.DateUtils import DateUtils from model.DataBaseUtils import MysqlUtils, CkUtils import logging du = DateUtils() db = MysqlUtils() ck = CkUtils() def dw_channel(): sql = """ select dt1,channel1,pitcher,stage,platform,book, if(stage ='趣程15期' or stage ='趣程26期' or stage ='趣程30期','GDT','MP'), order_count,create_user_num,reg_num, game_user_sum,third_stay_rate,sixty_stay_rate, fortyfive_stay_rate, thirty_stay_rate,fifteen_stay_rate,seven_stay_rate, second_stay_rate, today_active_user_rate, order_user,order_amount, first_order_count,first_order_user,first_order_amount, view_count,click_count,follow_user, cost,reg_order_count,reg_order_user,reg_order_amount, web_view_count,platform_view_count,web_order_count, reg_order_user_again, reg_order_user_again3, reg_order_user_again4, reg_order_user_again5, reg_order_user_again6 from ( select if(dt4='1970-01-01',dt,dt4) dt1,if(channel4='',channel,channel4) channel1, create_user_num,reg_num,today_active_user_rate,second_stay_rate ,third_stay_rate , game_user_sum ,seven_stay_rate, fifteen_stay_rate,thirty_stay_rate, fortyfive_stay_rate,sixty_stay_rate, pitcher,stage,platform,book,cost,view_count,click_count, ---基础属性和消耗数据 follow_user,web_view_count,platform_view_count,web_order_count,type, require_roi,require_mult,order_count,order_user,order_amount from (select * from game_data.dw_daily_platform_cost) aa full outer join (select date as dt4,channel as channel4,count(1) as order_count, ---账面充值 count(distinct user_id) as order_user,sum(amount) as order_amount from game_data.order where status=2 and channel in (select DISTINCT (channel) from game_data.dw_daily_channel_cost ddcc) group by date,channel) dd on dt=dt4 and channel=channel4) a left outer join ( select sum(if(user_order_count>=2,1,0)) reg_order_user_again,channel channel6,toDate(reg_date) dt6, ---复冲人数 sum(if(user_order_count>=3,1,0)) reg_order_user_again3, sum(if(user_order_count>=4,1,0)) reg_order_user_again4, sum(if(user_order_count>=5,1,0)) reg_order_user_again5, sum(if(user_order_count>=6,1,0)) reg_order_user_again6 from (select formatDateTime(reg_time,'%Y-%m-%d') reg_date,channel,count(1) user_order_count from game_data.order where status=2 group by formatDateTime(reg_time,'%Y-%m-%d') ,user_id,channel) x group by reg_date,channel ) f on dt1=dt6 and channel1=channel6 left outer join ( select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt2, ---新用户累计充值数据 channel as channel2, sum(amount) as reg_order_amount, count(distinct user_id) as reg_order_user, count(1) as reg_order_count from game_data.order where status=2 and reg_time>'2019-03-18 00:00:00' group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel) b on dt1=dt2 and channel1=channel2 left outer join (select date as dt3,channel as channel3,count(1) as first_order_count, ---新用户首日充值 count(distinct user_id) as first_order_user,sum(amount) as first_order_amount from game_data.order where status=2 and toDate(reg_time)=date group by date,channel) c on dt1=dt3 and channel1=channel3 having order_amount+cost+reg_order_amount>0""" data = ck.execute(sql) isql = "replace into dw_channel values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" # db.dm.execute("truncate table dw_channel") db.dm.executeMany(isql, data) def dw_channel_user_daily(): sql = """ select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt, channel, count(distinct if(subtractDays(date, 1)>=toDate(reg_time),null,user_id)) dc1, count(distinct if(subtractDays(date, 2)>=toDate(reg_time),null,user_id)) dc2, count(distinct if(subtractDays(date, 3)>=toDate(reg_time),null,user_id)) dc3, count(distinct if(subtractDays(date, 4)>=toDate(reg_time),null,user_id)) dc4, count(distinct if(subtractDays(date, 5)>=toDate(reg_time),null,user_id)) dc5, count(distinct if(subtractDays(date, 6)>=toDate(reg_time),null,user_id)) dc6, count(distinct if(subtractDays(date, 7)>=toDate(reg_time),null,user_id)) dc7, count(distinct if(subtractDays(date, 8)>=toDate(reg_time),null,user_id)) dc8, count(distinct if(subtractDays(date, 9)>=toDate(reg_time),null,user_id)) dc9, count(distinct if(subtractDays(date, 10)>=toDate(reg_time),null,user_id)) dc10, count(distinct if(subtractDays(date, 11)>=toDate(reg_time),null,user_id)) dc11, count(distinct if(subtractDays(date, 12)>=toDate(reg_time),null,user_id)) dc12, count(distinct if(subtractDays(date, 13)>=toDate(reg_time),null,user_id)) dc13, count(distinct if(subtractDays(date, 14)>=toDate(reg_time),null,user_id)) dc14, count(distinct if(subtractDays(date, 15)>=toDate(reg_time),null,user_id)) dc15, count(distinct if(subtractDays(date, 16)>=toDate(reg_time),null,user_id)) dc16, count(distinct if(subtractDays(date, 17)>=toDate(reg_time),null,user_id)) dc17, count(distinct if(subtractDays(date, 18)>=toDate(reg_time),null,user_id)) dc18, count(distinct if(subtractDays(date, 19)>=toDate(reg_time),null,user_id)) dc19, count(distinct if(subtractDays(date, 20)>=toDate(reg_time),null,user_id)) dc20, count(distinct if(subtractDays(date, 21)>=toDate(reg_time),null,user_id)) dc21, count(distinct if(subtractDays(date, 22)>=toDate(reg_time),null,user_id)) dc22, count(distinct if(subtractDays(date, 23)>=toDate(reg_time),null,user_id)) dc23, count(distinct if(subtractDays(date, 24)>=toDate(reg_time),null,user_id)) dc24, count(distinct if(subtractDays(date, 25)>=toDate(reg_time),null,user_id)) dc25, count(distinct if(subtractDays(date, 26)>=toDate(reg_time),null,user_id)) dc26, count(distinct if(subtractDays(date, 27)>=toDate(reg_time),null,user_id)) dc27, count(distinct if(subtractDays(date, 28)>=toDate(reg_time),null,user_id)) dc28, count(distinct if(subtractDays(date, 29)>=toDate(reg_time),null,user_id)) dc29, count(distinct if(subtractDays(date, 30)>=toDate(reg_time),null,user_id)) dc30 from game_data.order where status=2 and reg_time>'2019-03-18 00:00:00' group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel""" data = ck.execute(sql) isql = "insert into dw_channel_user_daily values " \ "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s," \ "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s," \ "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" db.dm.execute("truncate table dw_channel_user_daily") db.dm.executeMany(isql, data) def dw_channel_amount_daily(): sql = """ select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt, channel as channel, sum(if(subtractDays(date, 1)>=toDate(reg_time),0,amount)) as da1, if (dt=toDate(reg_time),0,amount)),null) as da2, if(dt=toDate(reg_time),0,amount)),null) as da3, if(dt=toDate(reg_time),0,amount)),null) as da4, if(dt=toDate(reg_time),0,amount)),null) as da5, if(dt=toDate(reg_time),0,amount)),null) as da6, if(dt=toDate(reg_time),0,amount)),null) as da7, if(dt=toDate(reg_time),0,amount)),null) as da8, if(dt=toDate(reg_time),0,amount)),null) as da9, if(dt=toDate(reg_time),0,amount)),null) as da10, if(dt=toDate(reg_time),0,amount)),null) as da11, if(dt=toDate(reg_time),0,amount)),null) as da12, if(dt=toDate(reg_time),0,amount)),null) as da13, if(dt=toDate(reg_time),0,amount)),null) as da14, if(dt=toDate(reg_time),0,amount)),null) as da15, if(dt=toDate(reg_time),0,amount)),null) as da16, if(dt=toDate(reg_time),0,amount)),null) as da17, if(dt=toDate(reg_time),0,amount)),null) as da18, if(dt=toDate(reg_time),0,amount)),null) as da19, if(dt=toDate(reg_time),0,amount)),null) as da20, if(dt=toDate(reg_time),0,amount)),null) as da21, if(dt=toDate(reg_time),0,amount)),null) as da22, if(dt=toDate(reg_time),0,amount)),null) as da23, if(dt=toDate(reg_time),0,amount)),null) as da24, if(dt=toDate(reg_time),0,amount)),null) as da25, if(dt=toDate(reg_time),0,amount)),null) as da26, if(dt=toDate(reg_time),0,amount)),null) as da27, if(dt=toDate(reg_time),0,amount)),null) as da28, if(dt=toDate(reg_time),0,amount)),null) as da29, if(dt=toDate(reg_time),0,amount)),null) as da30, if(dt=toDate(reg_time),0,amount)),null) as da31, if(dt=toDate(reg_time),0,amount)),null) as da32, if(dt=toDate(reg_time),0,amount)),null) as da33, if(dt=toDate(reg_time),0,amount)),null) as da34, if(dt=toDate(reg_time),0,amount)),null) as da35, if(dt=toDate(reg_time),0,amount)),null) as da36, if(dt=toDate(reg_time),0,amount)),null) as da37, if(dt=toDate(reg_time),0,amount)),null) as da38, if(dt=toDate(reg_time),0,amount)),null) as da39, if(dt=toDate(reg_time),0,amount)),null) as da40, if(dt=toDate(reg_time),0,amount)),null) as da41, if(dt=toDate(reg_time),0,amount)),null) as da42, if(dt=toDate(reg_time),0,amount)),null) as da43, if(dt=toDate(reg_time),0,amount)),null) as da44, if(dt=toDate(reg_time),0,amount)),null) as da45, if(dt=toDate(reg_time),0,amount)),null) as da46, if(dt=toDate(reg_time),0,amount)),null) as da47, if(dt=toDate(reg_time),0,amount)),null) as da48, if(dt=toDate(reg_time),0,amount)),null) as da49, if(dt=toDate(reg_time),0,amount)),null) as da50, if(dt=toDate(reg_time),0,amount)),null) as da51, if(dt=toDate(reg_time),0,amount)),null) as da52, if(dt=toDate(reg_time),0,amount)),null) as da53, if(dt=toDate(reg_time),0,amount)),null) as da54, if(dt=toDate(reg_time),0,amount)),null) as da55, if(dt=toDate(reg_time),0,amount)),null) as da56, if(dt=toDate(reg_time),0,amount)),null) as da57, if(dt=toDate(reg_time),0,amount)),null) as da58, if(dt=toDate(reg_time),0,amount)),null) as da59, if(dt=toDate(reg_time),0,amount)),null) as da60, if(dt=toDate(reg_time),0,amount)),null) as dm3, if(dt=toDate(reg_time),0,amount)),null) as dm4, if(dt=toDate(reg_time),0,amount)),null) as dm5 from game_data.order where status=2 and reg_time>'2019-03-18 00:00:00' group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel """ data = ck.execute(sql) isql = "insert into dw_channel_amount_daily values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s," \ "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" db.dm.execute("truncate table dw_channel_amount_daily") db.dm.executeMany(isql, data) def dw_channel_amount_daily_reverse(): sql = """ select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt, channel as channel, sum(if(date=today(),amount,0)) ba1, sum(if(addDays(date,1)=today(),amount,0)) ba2, sum(if(addDays(date,2)=today(),amount,0)) ba3, sum(if(addDays(date,3)=today(),amount,0)) ba4, sum(if(addDays(date,4)=today(),amount,0)) ba5, sum(if(addDays(date,6)=today(),amount,0)) ba6, sum(if(addDays(date,7)=today(),amount,0)) ba7 from game_data.order where status=2 and reg_time>'2019-03-18 00:00:00' and date>=subtractDays(today(),7) group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel""" data = ck.execute(sql) isql = "replace into dw_channel_amount_daily_reverse values (%s,%s,%s,%s,%s,%s,%s,%s,%s)" db.dm.execute("truncate table dw_channel_amount_daily_reverse") db.dm.executeMany(isql, data) def del_channel(): sql = 'delete from dw_channel where channel in ("腾云文渊", "青渊文学", "玉竹文渊", "赤瞳伴读","扶摇阅文")' db.dm.execute(sql) def dw_channel_daily(): logging.info('公众号数据处理,开始') logging.info('run> dw_channel') logging.info('公众号基本数据处理,开始') dw_channel() # 公众号基本数据 logging.info('公众号基本数据处理,结束') logging.info('用户激活数据处理,开始') dw_channel_user_daily() # 用户激活 logging.info('用户激活数据处理,结束') logging.info('用户充值数据处理,开始') dw_channel_amount_daily() # 用户充值 logging.info('用户充值数据处理,结束') logging.info('用户充值数据倒序处理,开始') dw_channel_amount_daily_reverse() # 用户充值倒序 logging.info('用户充值数据倒序处理,结束') logging.info('删除代投,开始') del_channel() # 删除代投的号 logging.info('删除代投,结束') logging.info('公众号数据处理,开始') if __name__ == '__main__': # dw_channel_daily() # dw_daily_channel() dw_channel() # dw_channel_amount_daily() # dw_channel_user_daily() # dw_channel_amount_daily_reverse() # dw_channel() # del_channel()