返回顶部

通过t_log文件计算次日留存

计算留存率

#!/usr/bin/env python
#_*_ encoding:utf-8 _*_

import os,sys,time,datetime
'''
次留: 14号注册   15号的登录用户数     / 14号注册的用户数    1天 
3留:  14日注册    16日登陆的用户数      /14日注册用户数     2天
7留:  14日注册    20日登录的用户数      /14日注册用户数     6天
'''
###当前时间的几个小时内的
#now_times =time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) #当前时间
#last_times=((datetime.datetime.now()-datetime.timedelta(minutes=60)).strftime("%Y-%m-%d %H:%M"))  #前一个小时

#首次注册时间,以天为单位 
last_times=str(datetime.datetime.strptime("2017-05-17 00:00:00",'%Y-%m-%d %H:%M:%S'))   #开始时间
now_times=str(datetime.datetime.strptime("2017-05-17 23:59:59",'%Y-%m-%d %H:%M:%S'))     #结束时间

#登录时间,以天为单位
last_times1=str(datetime.datetime.strptime("2017-05-18 00:00:00",'%Y-%m-%d %H:%M:%S'))   #开始时间
now_times2=str(datetime.datetime.strptime("2017-05-18 23:59:59",'%Y-%m-%d %H:%M:%S'))     #结束时间

t_log='tlog.log'    #T_log文件名
result = []
result1 = []
with open(t_log,'rt') as f:
    for i in f:
        if i.startswith('GameSvrState'):
            registeredtime=i.split('|')[4] #注册时间
            openid=i.split('|')[1]        #用户OPENID
            logintime=i.split('|')[5]     #登录时间 
            if last_times <=registeredtime <=now_times:
                if openid==1:
                    continue    
                #print i
                result.append(openid +'\n')

            if last_times1 <=logintime <=now_times2:   #登录时间15号
                if last_times <=registeredtime <=now_times:   #注册时间
                    print i
                    if openid==1:
                        continue
                    result1.append(openid +'\n')

#print last_times,'之间',now_times,'注册人数:   ',len(list(set(result)))
registerenumbers=len(list(set(result)))   #注册人数
#print oneday
loginagain=len(list(set(result1))) #第二天登录用户

#print float(loginagain)/float(registerenumbers)*100,'%'
print "%.2f%%" %(float(loginagain)/float(registerenumbers)*100)
View Code

 

posted on 2017-05-18 16:30  augustyang  阅读(301)  评论(0编辑  收藏  举报

导航