Пример #1
0
 def list(self):
     id = int(self.get_argument('id',0))
     unique = int(self.get_argument('unique',0))
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     # scope 
     q = Scope.mgr().Q()
     id and q.filter(id=id)
     if unique:
         q.filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,partner_id=partner_id,
                  version_name=version_name,product_name=product_name)
     else:
         platform_id and q.filter(platform_id=platform_id)
         run_id and q.filter(run_id=run_id)
         plan_id and q.filter(plan_id=plan_id)
         partner_id and q.filter(partner_id=partner_id)
         version_name and q.filter(version_name=version_name)
         product_name and q.filter(product_name=product_name)
     # pagination
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     count = q.count()
     page_count = (count+psize-1)/psize
     scopes = q.orderby('id')[(page-1)*psize:page*psize]
     self.render('data/scope_list.html',
                 unique=unique,platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),run_dict=self.run_dict(),
                 page=page, psize=psize, 
                 count=count, page_count=page_count,scopes = scopes
                 )
Пример #2
0
 def get_all_factstat_proportion(self, mode, start, end):
     key = '%s_%s_%s_%s' % (self.pfactstat, mode, start, end)
     res = self.cache.get(key)
     if not res:
         factory_list = Factory.mgr().Q().data()
         res = {}
         for i in factory_list:
             partner_list = Partnerv2.mgr().Q().filter(factory_id=i.id)[:]
             scope_proportion_list = []
             for p in partner_list:
                 scope = Scope.mgr().Q().filter(platform_id=6,
                                                run_id='',
                                                plan_id='',
                                                partner_id=p.partner_id,
                                                version_name='',
                                                product_name='')[0]
                 if scope:
                     temp = {
                         'proportion': p['proportion'],
                         'scope_id': scope.id,
                         'partner_id': p['partner_id']
                     }
                     scope_proportion_list.append(temp)
             #basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode=mode,start=start,end=end)
             basic = BasicStat.mgr().get_data_by_multi_scope_proportion(
                 scope_proportion_list, mode=mode, start=start, end=end)
             res[i.id] = basic
         self.cache.set(key, res, 1800)
     return res
Пример #3
0
 def save(self):
     id = int(self.get_argument('id', 0))
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     status = self.get_argument('status', 'pas')
     print 'aa', self.get_argument('mask', '')
     mask = [
         self.get_argument(i, '') for i in self.request.arguments
         if i.startswith('mask__')
     ]
     mask = ','.join([i for i in mask if i])
     modes = [
         self.get_argument(i, '') for i in self.request.arguments
         if i.startswith('mode__')
     ]
     modes = ','.join([i for i in modes if i])
     print mask, modes
     if id:
         s = Scope.mgr(ismaster=True).Q().filter(id=id)[0]
     else:
         s = Scope.new()
     s.platform_id, s.run_id, s.plan_id = platform_id, run_id, plan_id
     s.partner_id, s.version_name, s.product_name = partner_id, version_name, product_name
     s.status, s.mask, s.modes = status, mask, modes
     s = s.save()
     self.json2dwz('200',
                   'closeCurrent',
                   'dlist',
                   forwardUrl='scope/list?id=%s' % s.id)
Пример #4
0
 def get_all_factstat(self, mode, start, end):
     key = '%s_%s_%s_%s' % (self.lfactstat, mode, start, end)
     #res = self.cache.get(key)
     #if not res:
     factory_list = Factory.mgr().Q().data()
     res = {}
     for i in factory_list:
         partner_list = Partner.mgr().Q().filter(factory_id=i.id)[:]
         scopeid_list = []
         for p in partner_list:
             scope = Scope.mgr().Q().filter(platform_id=6,
                                            run_id='',
                                            plan_id='',
                                            partner_id=p.partner_id,
                                            version_name='',
                                            product_name='')[0]
             if scope:
                 scopeid_list.append(scope.id)
         basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,
                                                         mode=mode,
                                                         start=start,
                                                         end=end)
         res[i.id] = basic
     #self.cache.set(key,res,1800)
     return res
Пример #5
0
 def visit_chart(self, mode):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else: 
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     days = [tody-datetime.timedelta(days=i) for i in range(7 if mode=='week' else 30,0,-1)]
     start = self.get_argument('start','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d') 
     elif mode == 'week':
         start = tody - datetime.timedelta(days=7)
     else: 
         start = tody - datetime.timedelta(days=30)
     delta = tody - start
     days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
     visits = []
     if scope:
         for day in days:
             visit = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
             for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=day):
                 visit[i['type']] = i
             visit['title'] = day.strftime('%Y-%m-%d')
             visits.append(visit)
     x_axis = ['%s'%i.strftime('%m-%d') for i in days] 
     results = {}
     for v in visits:
         for t in v:
             if t == 'title':
                 continue
             results.setdefault(t,{'pv':[],'uv':[],'title':PAGE_TYPE[t][2]})
             for k in ['pv','uv']:
                 results[t][k].append(v[t][k])
     for t in results:
         for k in ['pv','uv']:
             results[t][k] = ','.join([str(i) for i in results[t][k]])
     self.render('data/visit_chart.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,partner_id=partner_id,
                 date=yest.strftime('%Y-%m-%d'),version_name=version_name,
                 product_name=product_name,run_list=self.run_list(),plan_list=self.plan_list(),
                 mode=mode,x_axis=x_axis,results=results,visits=visits,start=start.strftime('%Y-%m-%d')
                 )
Пример #6
0
 def index(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     # scope 
     else:
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date()
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = tody - datetime.timedelta(days=30)
     basics,topn_sch,topn_hw,b_books,c_books = [],[],[],[],[]
     visit_y = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
     visit_l = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
     if scope:
         # basic stat
         dft = dict([(i,0) for i in BasicStatv3._fields])
         basic_y = BasicStatv3.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)[0] 
         basic_l = BasicStatv3.mgr().Q(time=last).filter(scope_id=scope.id,mode='day',time=last)[0] 
         basic_m = BasicStatv3.mgr().get_data(scope.id,'day',start,tody,ismean=True)
         basic_p = BasicStatv3.mgr().get_peak(scope.id,'day',start,tody)
         basic_y,basic_l = basic_y or dft,basic_l or dft
         basic_y['title'],basic_l['title'] = '昨日统计','前日统计'
         basic_m['title'],basic_p['title'] = '每日平均','历史峰值'
         basics = [basic_y,basic_l,basic_m,basic_p]
         # page visit
         for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=yest):
             visit_y[i['type']] = i
         for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=last):
             visit_l[i['type']] = i
         # topN search & hotword
         q = TopNStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=yest)
         topn_sch = q.filter(type='search').orderby('no')[:10]
         topn_hw = q.filter(type='hotword').orderby('no')[:10]
         # books of by-book & by-chapter
         q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
         b_books = Service.inst().fill_book_info(q.filter(charge_type='book').orderby('fee','DESC')[:10])
         c_books = Service.inst().fill_book_info(q.filter(charge_type='chapter').orderby('fee','DESC')[:10])
     self.render('data/basic.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),plan_list=self.plan_list(),date=tody.strftime('%Y-%m-%d'),
                 basics = basics,visit_y=visit_y,visit_l=visit_l,topn_sch = topn_sch,
                 topn_hw=topn_hw,b_books=b_books,c_books=c_books
                 )
Пример #7
0
 def tag(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     order_field = self.get_argument('orderField', 'pay_user')
     assert order_field in ('pay_user', 'free_user', 'pay_down',
                            'free_down', 'pv', 'uv', 'fee')
     #perm
     run_list = self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]
     if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
         if len(run_id_list) == len(
                 Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
         scope = None
     else:
         # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     count, tags = 0, []
     if scope:
         q = BookTagStat.mgr().Q(time=yest).filter(scope_id=scope.id,
                                                   mode='day',
                                                   time=yest)
         count = q.count()
         tags = q.orderby(order_field,
                          'DESC')[(page - 1) * psize:page * psize]
     # pagination
     page_count = (count + psize - 1) / psize
     self.render('data/book_tag.html',
                 platform_id=platform_id,
                 run_id=run_id,
                 plan_id=plan_id,
                 date=yest.strftime('%Y-%m-%d'),
                 partner_id=partner_id,
                 version_name=version_name,
                 product_name=product_name,
                 run_list=self.run_list(),
                 plan_list=self.plan_list(),
                 page=page,
                 psize=psize,
                 count=count,
                 page_count=page_count,
                 tags=tags,
                 order_field=order_field)
Пример #8
0
 def index(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     factory_id = int(self.get_argument('factory_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     query_mode = self.get_argument('query_mode','')
     group = self.get_argument('group','')
     order_field = self.get_argument('orderField','user_run')
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     start = tody - datetime.timedelta(days=30)
     # factory
     q = Factory.mgr().Q()
     group and q.filter(group=group)
     factory_list = self.filter_factory_acc(q[:],query_mode)
     #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
     cuser = self.get_current_user() 
     if len(factory_list)>150 and (cuser['name'] not in SAFE_USER):
         factory_list = []
     if factory_id == 0 and factory_list:
         factory_id = factory_list[0].id
     partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
     # scope list
     scopeid_list = []
     for i in partner_list:
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=i.partner_id,version_name=version_name,product_name=product_name)[0]
         if scope:
             scopeid_list.append(scope.id)
Пример #9
0
 def refer(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     pkey = self.get_argument('pkey', '1S1')
     #perm
     run_list = self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]
     if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
         if len(run_id_list) == len(
                 Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
         scope = None
     else:
         # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     count, tags = 0, []
     if scope:
         q = BookReferStat.mgr().Q(time=yest).filter(scope_id=scope.id,
                                                     mode='day',
                                                     time=yest,
                                                     p_key=pkey)
         count = q.count()
         books = q.orderby('uv', 'DESC')[(page - 1) * psize:page * psize]
         books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count + psize - 1) / psize
     self.render('data/book_refer.html',
                 platform_id=platform_id,
                 run_id=run_id,
                 plan_id=plan_id,
                 date=yest.strftime('%Y-%m-%d'),
                 partner_id=partner_id,
                 version_name=version_name,
                 product_name=product_name,
                 run_list=self.run_list(),
                 plan_list=self.plan_list(),
                 page=page,
                 psize=psize,
                 count=count,
                 page_count=page_count,
                 books=books,
                 pkey=pkey)
Пример #10
0
 def import_toptag_bybook(self, mode, start):
     '''
     mode: hour,day,week,month
     start: start time
     return: top tags by download user count of each book in it
     '''
     if mode != 'day':
         return
     start, end = self.normlize_time(mode, start)
     q = Scope.mgr().Q().extra(
         "status in ('pas','nice') AND find_in_set('book',mask)")
     import_num = 0
     for scope in q:
         result = {}
         for i in BookStat.mgr().Q(time=start).filter(scope_id=scope.id,
                                                      mode=mode,
                                                      time=start):
             info = Service.inst().extern.get_book_info2(i.book_id)
             if info and 'tag' in info.keys():
                 for t in info['tag'].split(','):
                     t = t.lower().strip()
                     if t in result:
                         result[t]['pay_down'] += i.pay_down
                         result[t]['free_down'] += i.free_down
                         result[t]['pay_user'] += i.pay_user
                         result[t]['free_user'] += i.free_user
                         result[t]['pv'] += i.pv
                         result[t]['uv'] += i.uv
                         result[t]['fee'] += i.fee
                     else:
                         result[t] = {
                             'pay_down': i.pay_down,
                             'free_down': i.free_down,
                             'pay_user': i.pay_user,
                             'free_user': i.free_user,
                             'pv': i.pv,
                             'uv': i.uv,
                             'fee': i.fee
                         }
         for t in result:
             try:
                 item = result[t]
                 stat = BookTagStat.new()
                 stat.scope_id = scope['id']
                 stat.mode = mode
                 stat.time = start
                 stat.tag = t.encode('utf-8')
                 stat.pay_down = item['pay_down']
                 stat.free_down = item['free_down']
                 stat.pay_user = item['pay_user']
                 stat.free_user = item['free_user']
                 stat.pv = item['pv']
                 stat.uv = item['uv']
                 stat.fee = item['fee']
                 stat.save()
                 import_num += 1
             except Exception, e:
                 logging.error('%s\n', str(e), exc_info=True)
Пример #11
0
 def edit(self):
     sid = int(self.get_argument('id'))
     scope = Scope.mgr(ismaster=1).Q().filter(id=sid)[0]
     scope.mask = scope.mask.split(',')
     scope.modes = scope.modes.split(',')
     self.render('data/scope_edit.html',
                 mask=STAT_MASK,
                 modes=STAT_MODE,
                 scope=scope,
                 run_list=self.run_list())
Пример #12
0
 def edit(self):
     sid = int(self.get_argument('id'))
     scope = Scope.mgr(ismaster=1).Q().filter(id=sid)[0]
     scope.mask = scope.mask.split(',')
     scope.modes = scope.modes.split(',')
     self.render('data/scope_edit.html',
                  mask = STAT_MASK,
                  modes = STAT_MODE,
                  scope = scope,
                  run_list = self.run_list())
Пример #13
0
    def update_new_user_run_stat(self,start,mode):
        print "updating new user run..."
        start,end = self.normlize_time(mode,start)
        import_num = 0 

        # get parnter list
        partner = Partner.mgr().get_all_partner()
        partner = partner[:]
        partner.insert(0,{'partner_id':0})
        #partner = [{'partner_id':110010},{'partner_id':110011},{'partner_id':0}]

        # get scope_id and update
        for i in partner:
        # A partner matchs '110/d{3}' means it is wp7 or wp8 ,don's need update,and ios partner don't update ether
            pattern = re.compile(r'^11\d{4}$')
            match = pattern.match(str(i['partner_id']))
            if match:
                print i['partner_id'],' do not update'
                continue
            if i['partner_id'] in DO_NOT_UPDATE_NEW_USER_RUN_PARTNERID_LIST:
                print i['partner_id'],' do not update'
                continue

            # not wp7 wp8    
            scope = Scope.mgr().Q().filter(partner_id=i['partner_id'],product_name='',run_id=0,version_name='')[0]
            scope_update = Scope.mgr().get_scopes_by_parnter_id_for_update_new_user_run(i['partner_id']) 
            if i['partner_id'] == 0:
                scope_update = [{'id':39},{'id':40},{'id':41},{'id':42},{'id':151988},{'id':321972},{'id':330925},{'id':443939},{'id':661502},{'id':669196},{'id':749766},{'id':876486},{'id':985700},{'id':1186259}]
            if scope and scope_update: 
                sum_new_user_run = BasicStat.mgr().get_sum_new_user_run_by_scope_ids(scope_update,start,mode)
                if sum_new_user_run['new_user_run'] == None:
                    sum_new_user_run['new_user_run'] = 0 
                try:
                    s = BasicStat.mgr(ismaster=1).Q(time=start).filter(scope_id=scope['id'],mode=mode,time=start)[0]
                    if s:
                        s.new_user_run = int(sum_new_user_run['new_user_run'])
                        s.save()
                        import_num += 1
                except Exception,e:
                    s = None
                    logging.error('%s\n',str(e),exc_info=True)
Пример #14
0
 def import_toptag_bybook(self, mode, start):
     '''
     mode: hour,day,week,month
     start: start time
     return: top tags by download user count of each book in it
     '''
     if mode != 'day':
         return
     start,end = self.normlize_time(mode,start)
     q = Scope.mgr().Q().extra("status in ('pas','nice') AND find_in_set('book',mask)")
     import_num = 0
     for scope in q:
         result = {}
         for i in BookStat.mgr().Q(time=start).filter(scope_id=scope.id,mode=mode,time=start):
             info = Service.inst().extern.get_book_info2(i.book_id)
             if info and 'tag' in info.keys():
                 for t in info['tag'].split(','):
                     t = t.lower().strip()
                     if t in result:
                         result[t]['pay_down'] += i.pay_down
                         result[t]['free_down'] += i.free_down
                         result[t]['pay_user'] += i.pay_user
                         result[t]['free_user'] += i.free_user
                         result[t]['pv'] += i.pv
                         result[t]['uv'] += i.uv
                         result[t]['fee'] += i.fee
                     else:
                         result[t] = {'pay_down':i.pay_down,'free_down':i.free_down,'pay_user':i.pay_user,
                                      'free_user':i.free_user,'pv':i.pv,'uv':i.uv,'fee':i.fee}
         for t in result:
             try:
                 item = result[t]
                 stat = BookTagStat.new()
                 stat.scope_id = scope['id']
                 stat.mode = mode
                 stat.time = start
                 stat.tag = t.encode('utf-8')
                 stat.pay_down = item['pay_down']
                 stat.free_down = item['free_down']
                 stat.pay_user = item['pay_user']
                 stat.free_user = item['free_user']
                 stat.pv = item['pv']
                 stat.uv = item['uv']
                 stat.fee = item['fee']
                 stat.save()
                 import_num += 1
             except Exception,e:
                 logging.error('%s\n',str(e),exc_info=True)
Пример #15
0
 def update_zero_new_user_run_stat(self,start,mode,should_update_zero_new_user_run_partner_list):
     print "updating zero new user run..."
     start,end = self.normlize_time(mode,start)
     import_num = 0 
     for partner in should_update_zero_new_user_run_partner_list:
     #find scope_id 
         scope = Scope.mgr().Q().filter(partner_id=partner,product_name='',run_id=0,version_name='')[0]
         if scope:
             try: 
                 s = BasicStat.mgr(ismaster=1).Q(time=start).filter(scope_id=scope['id'],mode=mode,time=start)[0]
                 if s:
                     s.new_user_run = s.new_user_visit 
                     s.save()
                     import_num += 1
             except Exception,e:
                 logging.error('%s\n',str(e),exc_info=True)
Пример #16
0
 def list(self):
     id = int(self.get_argument('id', 0))
     unique = int(self.get_argument('unique', 0))
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     # scope
     q = Scope.mgr().Q()
     id and q.filter(id=id)
     if unique:
         q.filter(platform_id=platform_id,
                  run_id=run_id,
                  plan_id=plan_id,
                  partner_id=partner_id,
                  version_name=version_name,
                  product_name=product_name)
     else:
         platform_id and q.filter(platform_id=platform_id)
         run_id and q.filter(run_id=run_id)
         plan_id and q.filter(plan_id=plan_id)
         partner_id and q.filter(partner_id=partner_id)
         version_name and q.filter(version_name=version_name)
         product_name and q.filter(product_name=product_name)
     # pagination
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     count = q.count()
     page_count = (count + psize - 1) / psize
     scopes = q.orderby('id')[(page - 1) * psize:page * psize]
     self.render('data/scope_list.html',
                 unique=unique,
                 platform_id=platform_id,
                 run_id=run_id,
                 plan_id=plan_id,
                 partner_id=partner_id,
                 version_name=version_name,
                 product_name=product_name,
                 run_list=self.run_list(),
                 run_dict=self.run_dict(),
                 page=page,
                 psize=psize,
                 count=count,
                 page_count=page_count,
                 scopes=scopes)
Пример #17
0
 def get_all_partner_fee_daily(self, mode, start, end):
     res = {}
     partner_list = Partner.mgr().Q().data()
     for p in partner_list:
         scope = Scope.mgr().Q().filter(platform_id=6,
                                        run_id='',
                                        plan_id='',
                                        partner_id=p.partner_id,
                                        version_name='',
                                        product_name='')[0]
         if scope:
             recharge = BasicStat.mgr().get_recharge_by_multi_scope(
                 [scope.id], mode=mode, start=start, end=end)
             #fee = int(recharge['consumefee']) + int(recharge['msgfee'])
             fee = float(recharge['consumefee']) + recharge['msgfee']
             res[p.partner_id] = fee
     return res
Пример #18
0
    def partner(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        partner_id = int(self.get_argument('partner_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        order_field = self.get_argument('orderField','user_run')
        #order_field = 'user_run'
        assert order_field in ('user_run','new_user_run','user_visit','new_user_visit','pay_user','active_user_visit',
                    'visits','cpay_down','bpay_down','cpay_user','bpay_user','cfree_down','bfree_down','cfree_user',
                    'cpay_user','cfee','bfee','batch_pv','batch_uv','batch_fee') 
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        
        tody = self.get_date()
        yest = tody - datetime.timedelta(days=1)
        start = tody - datetime.timedelta(days=1)
        partner = Partner.mgr().get_all_partner()
        partner_id_list = []
        for i in partner:
            partner_id_list.append(i['partner_id'])
        scope = Scope.mgr().get_scope_ids_by_partner_ids_and_version_name(partner_id_list,version_name,partner_id)
        count = 0
        page_count = 0
        partner_stat = []
        if scope:
            scope_id_list = []
            for i in scope:
                scope_id_list.append(i['scope_id']) 
            partner_stat = BasicStatv3.mgr().get_all_data_by_multi_scope(scope_id_list,start,end=yest)
            partner_stat = partner_stat.orderby(order_field,'DESC')
            count = len(partner_stat)
            partner_stat = partner_stat[(page-1)*psize:page*psize]
            for stat in partner_stat:
                for i in scope:
                    if stat['scope_id'] == i['scope_id']:
                        stat['partner_id'] = i['partner_id']
            page_count = (count+psize-1)/psize

        self.render('data/basic_partner.html',
            platform_id=platform_id,run_id=run_id,plan_id=plan_id,page=page,psize=psize,count=count,page_count=page_count,
            partner_id=partner_id,version_name=version_name,product_name=product_name,order_field=order_field,
            run_list=self.run_list(),plan_list=self.plan_list(),date=tody.strftime('%Y-%m-%d'),start=start.strftime('%Y-%m-%d'),
            partner_stat=partner_stat)
Пример #19
0
 def get_scope(self, **kargs):
     '''
     get scope according to params
     kargs:platform_id,run_id,plan_id,partner_id,version_name,product_name
     '''
     platform_id = kargs.get('platform_id',None)
     run_id = kargs.get('run_id',None)
     plan_id = kargs.get('plan_id',None)
     partner_id= kargs.get('partner_id',None)
     version_name = kargs.get('version_name',None)
     product_name = kargs.get('product_name',None)
     # Query
     q = Scope.mgr().Q()
     platform_id and q.filter(platform_id=platform_id)
     run_id and q.filter(run_id=run_id)
     plan_id and q.filter(plan_id=plan_id)
     partner_id and q.filter(partner_id=partner_id)
     version_name and q.filter(version_name=version_name)
     product_name and q.filter(product_name=product_name)
     return q[0]
Пример #20
0
 def get_scope(self, **kargs):
     '''
     get scope according to params
     kargs:platform_id,run_id,plan_id,partner_id,version_name,product_name
     '''
     platform_id = kargs.get('platform_id', None)
     run_id = kargs.get('run_id', None)
     plan_id = kargs.get('plan_id', None)
     partner_id = kargs.get('partner_id', None)
     version_name = kargs.get('version_name', None)
     product_name = kargs.get('product_name', None)
     # Query
     q = Scope.mgr().Q()
     platform_id and q.filter(platform_id=platform_id)
     run_id and q.filter(run_id=run_id)
     plan_id and q.filter(plan_id=plan_id)
     partner_id and q.filter(partner_id=partner_id)
     version_name and q.filter(version_name=version_name)
     product_name and q.filter(product_name=product_name)
     return q[0]
Пример #21
0
 def tag(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     order_field = self.get_argument('orderField','pay_user')
     assert order_field in ('pay_user','free_user','pay_down','free_down','pv','uv','fee')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) ==len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     count,tags = 0,[]
     if scope:
         q = BookTagStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
         count = q.count()
         tags = q.orderby(order_field,'DESC')[(page-1)*psize:page*psize]
     # pagination
     page_count = (count+psize-1)/psize
     self.render('data/book_tag.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                 count=count, page_count=page_count, tags=tags, order_field = order_field
                 )
Пример #22
0
 def refer(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     pkey = self.get_argument('pkey','1S1')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) ==len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     count,tags = 0,[]
     if scope:
         q = BookReferStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest,p_key=pkey)
         count = q.count()
         books = q.orderby('uv','DESC')[(page-1)*psize:page*psize]
         books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count+psize-1)/psize
     self.render('data/book_refer.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                 count=count, page_count=page_count, books=books, pkey=pkey
                 )
Пример #23
0
 def update_zero_new_user_run_stat(
         self, start, mode, should_update_zero_new_user_run_partner_list):
     print "updating zero new user run..."
     start, end = self.normlize_time(mode, start)
     import_num = 0
     for partner in should_update_zero_new_user_run_partner_list:
         #find scope_id
         scope = Scope.mgr().Q().filter(partner_id=partner,
                                        product_name='',
                                        run_id=0,
                                        version_name='')[0]
         if scope:
             try:
                 s = BasicStat.mgr(ismaster=1).Q(time=start).filter(
                     scope_id=scope['id'], mode=mode, time=start)[0]
                 if s:
                     s.new_user_run = s.new_user_visit
                     s.save()
                     import_num += 1
             except Exception, e:
                 logging.error('%s\n', str(e), exc_info=True)
Пример #24
0
 def index(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else: 
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date()
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = tody - datetime.timedelta(days=30)
     visit_y = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
     visit_l = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
     if scope:
         # page visit
         for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=yest):
             visit_y[i['type']] = i
         for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=last):
             visit_l[i['type']] = i
     self.render('data/visit.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=tody.strftime('%Y-%m-%d'),
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),plan_list = self.plan_list(), visit_y=visit_y,visit_l=visit_l,
                 )
Пример #25
0
 def index(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     factory_id = int(self.get_argument('factory_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     query_mode = self.get_argument('query_mode', '')
     group = self.get_argument('group', '')
     order_field = self.get_argument('orderField', 'user_run')
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     start = tody - datetime.timedelta(days=30)
     # factory
     q = Factory.mgr().Q()
     group and q.filter(group=group)
     factory_list = self.filter_factory_acc(q[:], query_mode)
     #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
     cuser = self.get_current_user()
     if len(factory_list) > 150 and (cuser['name'] not in SAFE_USER):
         factory_list = []
     if factory_id == 0 and factory_list:
         factory_id = factory_list[0].id
     partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
     # scope list
     scopeid_list = []
     for i in partner_list:
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=i.partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
         if scope:
             scopeid_list.append(scope.id)
Пример #26
0
 def save(self):
     id = int(self.get_argument('id',0))
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     status = self.get_argument('status','pas')
     print 'aa',self.get_argument('mask','')
     mask = [self.get_argument(i,'') for i in self.request.arguments if i.startswith('mask__')]
     mask = ','.join([i for i in mask if i])
     modes = [self.get_argument(i,'') for i in self.request.arguments if i.startswith('mode__')]
     modes = ','.join([i for i in modes if i])
     print mask,modes
     if id:
         s = Scope.mgr(ismaster=True).Q().filter(id=id)[0]
     else:
         s = Scope.new()
     s.platform_id,s.run_id,s.plan_id = platform_id,run_id,plan_id
     s.partner_id,s.version_name,s.product_name = partner_id,version_name,product_name
     s.status,s.mask,s.modes = status,mask,modes
     s = s.save()
     self.json2dwz('200','closeCurrent','dlist',forwardUrl='scope/list?id=%s'%s.id)
Пример #27
0
 def book_stat(self):
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     charge_type = self.get_argument('charge_type','')
     factory_id = int(self.get_argument('factory_id',0))
     order_field = self.get_argument('orderField','fee')
     query_mode = self.get_argument('query_mode','accounting_book')
     group = self.get_argument('group','')
     assert order_field in ('pay_user','free_user','pay_down','free_down','pv','uv','fee','batch_fee','batch_pv','batch_uv','real_fee')
     action = self.get_argument('action','')
     # perm
     q = Factory.mgr().Q()
     group and q.filter(group=group)
     factory_list = self.filter_factory_acc(q[:],query_mode)
     # for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
     cuser = self.get_current_user() 
     if len(factory_list)>150 and (cuser['name'] not in SAFE_USER):
         factory_list = []
     if factory_id == 0 and factory_list:
         factory_id = factory_list[0].id
     partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
     # scope list
     scopeid_list = []
     for i in partner_list:
         scope = Scope.mgr().Q().filter(platform_id=6,run_id=0,partner_id=i.partner_id)[0]
         if scope:
             scopeid_list.append(scope.id)
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')   
     else:
         start = yest
     count,books = 0,[]
     q = BookStat.mgr().get_accouting_multy_scope_stat(scopeid_list,order_field,start)
     charge_type and q.filter(charge_type=charge_type)
     q = q.orderby(order_field,'DESC')
     count = len(q)
     if action == 'export':
         books = q.data()
         for i in books:
             i['batch_fee'] = "%0.2f"%i['batch_fee'] 
     else:
         books = q[(page-1)*psize:page*psize]
         for i in books:
             i['batch_fee'] = "%0.2f"%i['batch_fee'] 
     books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count+psize-1)/psize
     for book in books:
         book['fee'] = "%.01f"%float(book['fee'])
         book['real_fee'] = "%.01f"%float(book['real_fee'])
     if action == 'export':
         books = Service.inst().fill_book_count_info(books)
         while (self.do_books_have_two_or_empty_title(books)): 
             books = self.remove_books_two_or_empty_title(books) 
         title = [('time','时间'),('book_id','书ID'),('name','书名'),('author','作者'),
                  ('cp','版权'),('category_2','类别'),('category_1','子类'),('category_0','三级分类'),('state','状态'),
                  ('charge_type','计费类型'),('fee','收益'),('real_fee','主账户收益'),('pay_down','付费下载数'),
                  ('pay_user','付费下载用户数'),('free_down','免费下载数'),('free_user','免费下载用户数'),('pv','简介访问数'),('uv','简介访问人数'),
                  ('batch_fee','批量订购阅饼消费')]
         xls = Excel().generate(title,books,1)
         filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
         self.set_header('Content-Disposition','attachment;filename=%s'%filename)
         self.finish(xls)
     else:
         self.render('data/accounting_book.html',
                     page=page, psize=psize,
                     count=count,page_count=page_count,books=books,charge_type=charge_type,
                     order_field = order_field,start=start.strftime('%Y-%m-%d'),factory_id=factory_id,factory_list=factory_list,query_mode=query_mode
                     )
Пример #28
0
    def index(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        factory_id = int(self.get_argument('factory_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','')
        group = self.get_argument('group','')
        order_field = self.get_argument('orderField','user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = tody - datetime.timedelta(days=30)
        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory(q[:],query_mode)
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partner.mgr().Q().filter(factory_id=factory_id)[:]
        # scope list
        scopeid_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                      partner_id=i.partner_id,version_name=version_name,product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
        # basic stat
        basics = []
        dft = dict([(i,0) for i in BasicStat._fields])
        if query_mode == 'basic':
            start = self.get_argument('start','')
            date = self.get_argument('date','')
            action = self.get_argument('action','')
            if not start:
                start = tody - datetime.timedelta(days=7)
            else:
                start = datetime.datetime.strptime(start,'%Y-%m-%d') 
            _start,days = start,[]
            while _start < tody:
                _end = _start + datetime.timedelta(days=1)
                basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode='day',start=_start,end=_end)
                basic['title'] = _start.strftime('%m-%d')
                basics.append(basic)
                days.append(_start)
                _start = _end
            # for chart
            x_axis = ['%s'%i.strftime('%m-%d') for i in days]
            results = {}
            excludes = ('id','scope_id','mode','time','bfee','cfee','recharge','uptime','batch_fee','batch_pv','batch_uv')
            for k in [i for i in BasicStat._fields if i not in excludes]:
                results[k] ={'title':BasicStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
            for i in basics:
                i['feesum'] = float(i['consumefee'])+i['msgfee']
            fee_list = [i['feesum'] for i in basics]
            m_fee = '%.1f'%(sum(fee_list)/len(fee_list))
            p_fee = max(fee_list)
            # mean and peak
            basic_m = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,'day',start,tody,ismean=True)
            basic_p = BasicStat.mgr().get_peak_by_multi_scope(scopeid_list,'day',start,tody)
            basic_m['feesum'],basic_p['feesum'] = m_fee,p_fee
            basic_m['title'],basic_p['title'] = '每日平均','历史峰值'
            basics.extend([basic_m,basic_p])
            if action == 'export':  
                excel_title = [('title','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),
                ('user_visit','访问用户'),('new_user_visit','新增访问用户'),('pay_user','付费用户'),
                ('active_user_visit','活跃用户'),('visits','访问PV'),
                ('cpay_down','章付费数'),('bpay_down','本付费数'),
                ('cpay_user','章付费用户'),('bpay_user','本付费用户'),
                ('cfree_down','章免费数'),('bfree_down','本免费数'),
                ('cfree_user','章免费用户'),('bfree_user','本免费用户'),
                ('cfee','章月饼消费'),('bfee','本月饼消费')]
                #('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV')]
                xls = Excel().generate(excel_title,basics,1) 
                filename = 'factstat_%s.xls' % (yest.strftime('%Y-%m-%d'))
                self.set_header('Content-Disposition','attachment;filename=%s'%filename)
                self.finish(xls)
            else:
                self.render('data/factstat.html',
                        platform_id=platform_id,run_id=run_id,plan_id=plan_id,factory_id=factory_id,
                        version_name=version_name,product_name=product_name,query_mode=query_mode,
                        basics=basics,start=start.strftime('%Y-%m-%d'),date=yest.strftime('%Y-%m-%d'),
                        factory_list=factory_list,x_axis=x_axis,results=results
                        )

        elif query_mode == 'product':
            page = int(self.get_argument('pageNum',1))
            psize = int(self.get_argument('numPerPage',20))
            start = self.get_argument('start','')
            date = self.get_argument('date','')
            action = self.get_argument('action','')
            if start != date:
                if not start:
                    start = tody - datetime.timedelta(days=2)
                else:
                    start = datetime.datetime.strptime(start,'%Y-%m-%d')
                if not date:
                    date = yest
                else:
                    date = datetime.datetime.strptime(date,'%Y-%m-%d')
                end = date + datetime.timedelta(days=1)
            
                scopeid_list = []
                scopeid_product = {}
                if factory_id == -1:
                    partnerid_list = [0]
                else:
                    partnerid_list = [i.partner_id for i in partner_list]
                for i in partnerid_list:
                    if product_name != '':
                        scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                                    partner_id=i,version_name=version_name,product_name=product_name).extra("product_name<>''")
                    else:
                        scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                                    partner_id=i,version_name=version_name).extra("product_name<>''")
                        #if len(scopes) > 200 and action != 'export':#limit scope longth, for a better performance, user normally do not review multi-days product detail
                            #scopes = scopes[0:200]
                    for scope in scopes:
                        scopeid_list.append(scope.id)
                        scopeid_product[scope.id] = scope.product_name
                #result = BasicStat.mgr().get_multi_days_model_data(scopeid_list,'day',start,end,page,psize,order_field)
                result = BasicStat.mgr().get_multi_days_model_data(scopeid_list,'day',start,end)
                count,model_list = result['count'],result['list']
                for model in model_list:
                    model['product_name'] = scopeid_product[model['scope_id']]
                    model['time'] = model['time'].strftime('%Y-%m-%d') 
                model_list = self.cumulative_stat_by_keys(model_list,'time','product_name')
                model_list = self.bubblesort(model_list,'product_name')
                model_list = self.bubblesort_asc(model_list,'time')
                count = len(model_list)
                if action == 'export':
                    title = [('product_name','机型'),('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),('user_visit','访问用户'),
                        ('new_user_visit','新增访问用户'),('pay_user','付费用户'),('active_user_visit','活跃用户'),('visits','访问PV'),('cpay_down','按章付费数'),
                        ('bpay_down','按本付费数'),('cpay_user','按章付费用户'),('bpay_user','按本付费用户'),('cfree_down','按章免费数'),('bfree_down','按本免费数'),
                        ('cfree_user','按章免费用户'),('bfree_user','按本免费用户')]
                    xls = Excel().generate(title,model_list,1)
                    filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
                    self.set_header('Content-Disposition','attachment;filename=%s'%filename)
                    self.finish(xls)
                else:
                    model_list = model_list[(page-1)*psize:page*psize]  
                    self.render('data/factstat_product.html',
                        count=count,psize=psize,page=page,factory_id=factory_id,
                        query_mode=query_mode,product_name=product_name,basics=model_list,
                        start=start.strftime('%Y-%m-%d'),date=date.strftime('%Y-%m-%d'),
                        factory_list=factory_list, order_field=order_field
                        )
            else:
                if not start:
                    start = yest
                else:
                    start = datetime.datetime.strptime(start,'%Y-%m-%d')
                if not date:
                    date = yest
                else:
                    date = datetime.datetime.strptime(date,'%Y-%m-%d')
                scopeid_list = []
                scopeid_product = {}
                if factory_id == -1:
                    partnerid_list = [0]
                else:
                    partnerid_list = [i.partner_id for i in partner_list]
                for i in partnerid_list:
                    if not product_name:
                        scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                        partner_id=i,version_name=version_name).extra("product_name<>''")
                    else:
                        scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                        partner_id=i,version_name=version_name,product_name=product_name)
                    for scope in scopes:
                        scopeid_list.append(scope.id)
                        scopeid_product[scope.id] = scope.product_name
                result = BasicStat.mgr().get_all_model_data(scopeid_list,'day',yest,tody)
                count,model_list = result['count'],result['list'][:]
                for model in model_list:
                    model['product_name'] = scopeid_product[model['scope_id']]
                    model['time'] = start.strftime('%Y-%m-%d') 
                 
                #find key_list
                if not model_list:
                    tmp = []
                else:
                    tmp = model_list[0].keys()
                key_list = [i for i in tmp if i not in ('scope_id','time','product_name')]
                
                #find product_name_list
                tmp = []
                for model in model_list:
                    tmp.append(model['product_name'])
                tmp_set = set(tmp)
                product_name_list = [i for i in tmp_set]
                #get dft res_list
                res_list = []
                for i in product_name_list:
                    res_model = {}
                    res_model['product_name'] = i
                    res_model['time'] = start.strftime('%Y-%m-%d')
                    for key in key_list:
                        res_model[key] = 0
                    res_list.append(res_model)
                #Cumulative data 
                for model in model_list:
                    for res in res_list:
                        if res['product_name'] == model['product_name']:
                            for key in key_list:
                                res[key] += model[key]
                
                count = len(res_list)
                res_list = self.bubblesort(res_list,order_field)
                if action == 'export': 
                    title = [('product_name','机型'),('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),('user_visit','访问用户'),
                    ('new_user_visit','新增访问用户'),('pay_user','付费用户'),('active_user_visit','活跃用户'),('visits','访问PV'),('cpay_down','按章付费数'),
                    ('bpay_down','按本付费数'),('cpay_user','按章付费用户'),('bpay_user','按本付费用户'),('cfree_down','按章免费数'),('bfree_down','按本免费数'),
                    ('cfree_user','按章免费用户'),('bfree_user','按本免费用户')]
                    xls = Excel().generate(title,res_list,1)
                    filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
                    self.set_header('Content-Disposition','attachment;filename=%s'%filename)
                    self.finish(xls)
                else:
                    res_list = res_list[(page-1)*psize:page*psize]
                    self.render('data/factstat_product_singleday.html',
                        count=count,psize=psize,page=page,factory_id=factory_id,
                        query_mode=query_mode,product_name=product_name,basics=res_list,
                        start=start.strftime('%Y-%m-%d'),date=date.strftime('%Y-%m-%d'),
                        factory_list=factory_list, order_field=order_field
                        )
             
        elif query_mode == 'fact' or query_mode == 'business':
            basics = []
            count = len(factory_list)
            all_stats = Service.inst().stat.get_all_factstat('day',yest,tody)
            acc_stats = {}
            for i in factory_list:
                basic = all_stats.get(i.id,None)
                if basic:
                    for k in basic:
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)
                    basic['title'] = i.name
                    basics.append(basic)
             
            acc_stats['title'] = '总计'
            basics.sort(cmp=lambda x,y:cmp(x[order_field],y[order_field]),reverse=True)
            basics= basics[(page-1)*psize:page*psize]
            if basics:
                basics += [acc_stats]
            groups = Factory.mgr().all_groups()
            self.render('data/factstat_fact.html',
                    count=count,psize=psize,page=page,query_mode=query_mode,basics=basics,
                    factory_id=factory_id,factory_list=factory_list,start=None,product_name='',
                    date=yest.strftime('%Y-%m-%d'),order_field=order_field,group=group,groups=groups
                    )
        
        elif query_mode == 'factory_sum':
            start = self.get_argument('start','')
            if not start:
                start = tody - datetime.timedelta(days=7)
            else: 
                start = datetime.datetime.strptime(start,'%Y-%m-%d') 
            #factory sum stat
            fact_sums=[]
            acc_fact_sum={}
            fact_list = [long(i.id) for i in factory_list]
            _start,days = start,[]
            while _start < tody:
                _end = _start + datetime.timedelta(days=1)
                #for the user who access other people's factory sum stat
                if len(fact_list) == 0:
                    _start = _end
                    continue
                
                fact_sum = {}
                in_vals = ','.join(["'%s'"%i for i in fact_list])
                sql = """
                SELECT SUM(visits) AS visits,SUM(imei) AS imei,SUM(user_run) AS user_run,SUM(new_user_run) AS new_user_run,
                SUM(user_visit) AS user_visit, SUM(new_user_visit) AS new_user_visit,SUM(active_user_visit) AS active_user_visit, 
                SUM(user_retention) AS user_retention,SUM(pay_user) AS pay_user, SUM(cpay_down) AS cpay_down, SUM(cfree_down) AS cfree_down,
                SUM(bpay_down) AS bpay_down, SUM(bfree_down) AS bfree_down,SUM(cpay_user) AS cpay_user, SUM(cfree_user) AS cfree_user, 
                SUM(bpay_user) AS bpay_user, SUM(bfree_user) AS bfree_user, SUM(bpay_user) AS bpay_user,SUM(bfree_user) AS bfree_user, SUM(cfee) AS cfee, 
                SUM(bfee) AS bfee FROM factory_sum_stat WHERE time = '%s' AND factory_id IN (%s) """ % (_start,in_vals)
                q = FactorySumStat.mgr().raw(sql)
                if q[0]['visits'] == None:
                    fact_sum = dict([(i,0) for i in FactorySumStat._fields])
                else:
                    fact_sum = q[0]
                for k in fact_sum:
                    acc_fact_sum[k] = fact_sum[k] + acc_fact_sum.get(k,0)
                fact_sum['title'] = _start.strftime('%Y-%m-%d')
                fact_sums.append(fact_sum)
                days.append(_start)
                _start = _end
            acc_fact_sum['title'] = '总计'
            fact_sums.append(acc_fact_sum)
            #for charts
            x_axis = ['%s'%i.strftime('%m-%d') for i in days]
            results = {}
            excludes = ('id','time','factory_id','visits','imei','bfee','cfee','uptime')
            for k in [i for i in FactorySumStat._fields if i not in excludes]: 
                results[k] ={'title':FactorySumStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in fact_sums[:-1]])}
            
            #render
            count = len(days)*len(factory_list)
            groups = Factory.mgr().all_groups()
            self.render('data/factstat_sum.html',
                count=count,psize=psize,page=page,query_mode=query_mode,results=results,x_axis=x_axis,fact_sums=fact_sums,
                factory_id=factory_id,factory_list=factory_list,product_name='',start=start.strftime('%Y-%m-%d'),
                date=yest.strftime('%Y-%m-%d'),order_field=order_field,group=group,groups=groups
                )

        elif query_mode == 'recharge_log':
            page = int(self.get_argument('pageNum',1))
            psize = int(self.get_argument('numPerPage',20))
            phone = self.get_argument('phone','')
            
            tody = self.get_date(0)
            start = self.get_argument('start','')
            end = self.get_argument('end','')
            if not start:
                start = tody - datetime.timedelta(days=30)
                start = start.strftime('%Y-%m-%d')
            if not end: 
                end = tody.strftime('%Y-%m-%d')

            #user...
            user = self.get_current_user()
            query_count = QueryTimeCount.mgr().get_user_today_query_count(user['name'],tody.strftime('%Y-%m-%d'))
            if user:
                qt = QueryTimeCount.new()
                qt.user = user['name']
                qt.save()
            
            if query_count<=Query_Limit_Count or user['name']=='admin':
                data = Service.inst().fill_recharge_log_info(phone,start,end)
                recharge_log = data['res']
                if not recharge_log:
                    recharge_log = []
                recharge_log[(page-1)*psize:page*psize]
                is_show = True
            else:
                recharge_log = [] 
                is_show = False
            count = len(recharge_log)
            self.render('data/factstat_recharge_log.html',
                    count=count,psize=psize,page=page,recharge_log=recharge_log,start=start,date=end,
                    query_mode=query_mode,phone=phone,is_show=is_show
                    )

        elif query_mode == 'consume_log':
            page = int(self.get_argument('pageNum',1))
            psize = int(self.get_argument('numPerPage',20))
            usr = self.get_argument('usr','')

            tody = self.get_date(0)
            start = self.get_argument('start','')
            end = self.get_argument('end','')

            #user...
            user = self.get_current_user()
            query_count = QueryTimeCount.mgr().get_user_today_query_count(user['name'],tody.strftime('%Y-%m-%d'))
            if user:
                qt = QueryTimeCount.new()
                qt.user = user['name']
                qt.save()
            dft = {'obj':[],'page':{u'totalPage': 0, u'endPage': 0, u'totals': 0, u'startPage': 1, u'perPages': 20, u'currentPage': 1}}
            obj = [] 
            if query_count<=Query_Limit_Count or user['name']=='admin':
                if not usr:
                    data = dft
                else:
                    data = Service.inst().fill_consume_log_info(usr,page,psize)
                
                obj = data['obj']
                count = data['page']['totals']
                page = data['page']['currentPage']
                psize = data['page']['perPages']
                if not obj:
                    obj = []
                is_show = True
            else:
                obj = []
                count = 0
                page = 1
                psize = psize
                is_show = False
            self.render('data/factstat_consume_log.html',
                count=count,psize=psize,page=page,obj=obj,query_mode=query_mode,is_show=is_show,usr=usr
                )
Пример #29
0
    def chart(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        partner_id = int(self.get_argument('partner_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',400))
        charge_type = self.get_argument('charge_type','')
        book_ids = self.get_argument('book_ids','')
        bookid_list = [i for i in book_ids.split(',') if i]
        action = self.get_argument('action','')
        #perm
        run_list=self.run_list()
        run_list = self.filter_run_id_perms(run_list=run_list)
        run_id_list = [run['run_id'] for run in run_list]      
        if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
            if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
                run_id = 0  # user has all run_id perms
            else:
                run_id = run_id_list[0]
        if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
            scope = None
        else:
        # scope 
            scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                      partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start','')
        
        if start:
            start = datetime.datetime.strptime(start,'%Y-%m-%d')
        else:
            start = tody - datetime.timedelta(days=7)
        # x_axis
        delta = tody - start
        days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
        x_axis = ['%s'%i.strftime('%m-%d') for i in days]
        count,books = 0,[]
        if scope:
            # books of by-book & by-chapter
            result = BookStat.mgr().get_data(scope.id,'day',start,tody,charge_type,bookid_list,page,psize)
            count = result['count']
            books = Service.inst().fill_book_info(result['list'])
            while (self.do_books_have_two_or_empty_title(books)): #this is for Fu Rong's request, but books can't deepcopy, so I have to do this way, so egg pain !
                books = self.remove_books_two_or_empty_title(books)

        # pagination
        results = {}
        excludes = ('id','scope_id','mode','time','uptime','category_1','category_0','charge_type','author','book_id','arpu','category_0')
        for k in [i for i in BookStat._fields if i not in excludes]:
            results[k] ={'title':BookStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in books])}
        page_count = (count+psize-1)/psize
        if action == 'export':
            title = [('time','时间'),('book_id','书ID'),('name','书名'),('author','作者'),
                    ('cp','版权'),('category_2','类别'),('category_1','子类'),('category_0','三级分类'),('state','状态'),
                    ('charge_type','计费类型'),('fee','收益'),('pay_down','付费下载数'),
                    ('pay_user','付费下载用户数'),('free_down','免费下载数'),
                    ('free_user','免费下载用户数'),('pv','简介访问数'),('uv','简介访问人数'),
                    ('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV')]
            xls = Excel().generate(title,books,1)
            filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/book_chart.html',x_axis=x_axis,results=results,
                    platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                    partner_id=partner_id,version_name=version_name,product_name=product_name,
                    run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                    count=count, page_count=page_count, books=books, charge_type=charge_type,
                    book_ids=book_ids,start=start.strftime('%Y-%m-%d')
                    )
Пример #30
0
    def index(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        factory_id = int(self.get_argument('factory_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', '')
        group = self.get_argument('group', '')
        order_field = self.get_argument('orderField', 'user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = tody - datetime.timedelta(days=30)
        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory(q[:], query_mode)
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partner.mgr().Q().filter(factory_id=factory_id)[:]
        # scope list
        scopeid_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                           run_id=run_id,
                                           plan_id=plan_id,
                                           partner_id=i.partner_id,
                                           version_name=version_name,
                                           product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
        # basic stat
        basics = []
        dft = dict([(i, 0) for i in BasicStat._fields])
        if query_mode == 'basic':
            start = self.get_argument('start', '')
            date = self.get_argument('date', '')
            action = self.get_argument('action', '')
            if not start:
                start = tody - datetime.timedelta(days=7)
            else:
                start = datetime.datetime.strptime(start, '%Y-%m-%d')
            _start, days = start, []
            while _start < tody:
                _end = _start + datetime.timedelta(days=1)
                basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,
                                                                mode='day',
                                                                start=_start,
                                                                end=_end)
                basic['title'] = _start.strftime('%m-%d')
                basics.append(basic)
                days.append(_start)
                _start = _end
            # for chart
            x_axis = ['%s' % i.strftime('%m-%d') for i in days]
            results = {}
            excludes = ('id', 'scope_id', 'mode', 'time', 'bfee', 'cfee',
                        'recharge', 'uptime', 'batch_fee', 'batch_pv',
                        'batch_uv')
            for k in [i for i in BasicStat._fields if i not in excludes]:
                results[k] = {
                    'title': BasicStat._fieldesc[k],
                    'data': ','.join([str(i.get(k, 0)) for i in basics])
                }
            for i in basics:
                i['feesum'] = float(i['consumefee']) + i['msgfee']
            fee_list = [i['feesum'] for i in basics]
            m_fee = '%.1f' % (sum(fee_list) / len(fee_list))
            p_fee = max(fee_list)
            # mean and peak
            basic_m = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,
                                                              'day',
                                                              start,
                                                              tody,
                                                              ismean=True)
            basic_p = BasicStat.mgr().get_peak_by_multi_scope(
                scopeid_list, 'day', start, tody)
            basic_m['feesum'], basic_p['feesum'] = m_fee, p_fee
            basic_m['title'], basic_p['title'] = '每日平均', '历史峰值'
            basics.extend([basic_m, basic_p])
            if action == 'export':
                excel_title = [('title', '时间'), ('user_run', '启动用户'),
                               ('new_user_run', '新增启动用户'),
                               ('user_visit', '访问用户'),
                               ('new_user_visit', '新增访问用户'),
                               ('pay_user', '付费用户'),
                               ('active_user_visit', '活跃用户'),
                               ('visits', '访问PV'), ('cpay_down', '章付费数'),
                               ('bpay_down', '本付费数'), ('cpay_user', '章付费用户'),
                               ('bpay_user', '本付费用户'), ('cfree_down', '章免费数'),
                               ('bfree_down', '本免费数'), ('cfree_user', '章免费用户'),
                               ('bfree_user', '本免费用户'), ('cfee', '章月饼消费'),
                               ('bfee', '本月饼消费')]
                #('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV')]
                xls = Excel().generate(excel_title, basics, 1)
                filename = 'factstat_%s.xls' % (yest.strftime('%Y-%m-%d'))
                self.set_header('Content-Disposition',
                                'attachment;filename=%s' % filename)
                self.finish(xls)
            else:
                self.render('data/factstat.html',
                            platform_id=platform_id,
                            run_id=run_id,
                            plan_id=plan_id,
                            factory_id=factory_id,
                            version_name=version_name,
                            product_name=product_name,
                            query_mode=query_mode,
                            basics=basics,
                            start=start.strftime('%Y-%m-%d'),
                            date=yest.strftime('%Y-%m-%d'),
                            factory_list=factory_list,
                            x_axis=x_axis,
                            results=results)

        elif query_mode == 'product':
            page = int(self.get_argument('pageNum', 1))
            psize = int(self.get_argument('numPerPage', 20))
            start = self.get_argument('start', '')
            date = self.get_argument('date', '')
            action = self.get_argument('action', '')
            if start != date:
                if not start:
                    start = tody - datetime.timedelta(days=2)
                else:
                    start = datetime.datetime.strptime(start, '%Y-%m-%d')
                if not date:
                    date = yest
                else:
                    date = datetime.datetime.strptime(date, '%Y-%m-%d')
                end = date + datetime.timedelta(days=1)

                scopeid_list = []
                scopeid_product = {}
                if factory_id == -1:
                    partnerid_list = [0]
                else:
                    partnerid_list = [i.partner_id for i in partner_list]
                for i in partnerid_list:
                    if product_name != '':
                        scopes = Scope.mgr().Q().filter(
                            platform_id=platform_id,
                            run_id=run_id,
                            plan_id=plan_id,
                            partner_id=i,
                            version_name=version_name,
                            product_name=product_name).extra(
                                "product_name<>''")
                    else:
                        scopes = Scope.mgr().Q().filter(
                            platform_id=platform_id,
                            run_id=run_id,
                            plan_id=plan_id,
                            partner_id=i,
                            version_name=version_name).extra(
                                "product_name<>''")
                        #if len(scopes) > 200 and action != 'export':#limit scope longth, for a better performance, user normally do not review multi-days product detail
                        #scopes = scopes[0:200]
                    for scope in scopes:
                        scopeid_list.append(scope.id)
                        scopeid_product[scope.id] = scope.product_name
                #result = BasicStat.mgr().get_multi_days_model_data(scopeid_list,'day',start,end,page,psize,order_field)
                result = BasicStat.mgr().get_multi_days_model_data(
                    scopeid_list, 'day', start, end)
                count, model_list = result['count'], result['list']
                for model in model_list:
                    model['product_name'] = scopeid_product[model['scope_id']]
                    model['time'] = model['time'].strftime('%Y-%m-%d')
                model_list = self.cumulative_stat_by_keys(
                    model_list, 'time', 'product_name')
                model_list = self.bubblesort(model_list, 'product_name')
                model_list = self.bubblesort_asc(model_list, 'time')
                count = len(model_list)
                if action == 'export':
                    title = [
                        ('product_name', '机型'), ('time', '时间'),
                        ('user_run', '启动用户'), ('new_user_run', '新增启动用户'),
                        ('user_visit', '访问用户'), ('new_user_visit', '新增访问用户'),
                        ('pay_user', '付费用户'), ('active_user_visit', '活跃用户'),
                        ('visits', '访问PV'), ('cpay_down', '按章付费数'),
                        ('bpay_down', '按本付费数'), ('cpay_user', '按章付费用户'),
                        ('bpay_user', '按本付费用户'), ('cfree_down', '按章免费数'),
                        ('bfree_down', '按本免费数'), ('cfree_user', '按章免费用户'),
                        ('bfree_user', '按本免费用户')
                    ]
                    xls = Excel().generate(title, model_list, 1)
                    filename = 'factstat_product_%s.xls' % (
                        yest.strftime('%Y-%m-%d'))
                    self.set_header('Content-Disposition',
                                    'attachment;filename=%s' % filename)
                    self.finish(xls)
                else:
                    model_list = model_list[(page - 1) * psize:page * psize]
                    self.render('data/factstat_product.html',
                                count=count,
                                psize=psize,
                                page=page,
                                factory_id=factory_id,
                                query_mode=query_mode,
                                product_name=product_name,
                                basics=model_list,
                                start=start.strftime('%Y-%m-%d'),
                                date=date.strftime('%Y-%m-%d'),
                                factory_list=factory_list,
                                order_field=order_field)
            else:
                if not start:
                    start = yest
                else:
                    start = datetime.datetime.strptime(start, '%Y-%m-%d')
                if not date:
                    date = yest
                else:
                    date = datetime.datetime.strptime(date, '%Y-%m-%d')
                scopeid_list = []
                scopeid_product = {}
                if factory_id == -1:
                    partnerid_list = [0]
                else:
                    partnerid_list = [i.partner_id for i in partner_list]
                for i in partnerid_list:
                    if not product_name:
                        scopes = Scope.mgr().Q().filter(
                            platform_id=platform_id,
                            run_id=run_id,
                            plan_id=plan_id,
                            partner_id=i,
                            version_name=version_name).extra(
                                "product_name<>''")
                    else:
                        scopes = Scope.mgr().Q().filter(
                            platform_id=platform_id,
                            run_id=run_id,
                            plan_id=plan_id,
                            partner_id=i,
                            version_name=version_name,
                            product_name=product_name)
                    for scope in scopes:
                        scopeid_list.append(scope.id)
                        scopeid_product[scope.id] = scope.product_name
                result = BasicStat.mgr().get_all_model_data(
                    scopeid_list, 'day', yest, tody)
                count, model_list = result['count'], result['list'][:]
                for model in model_list:
                    model['product_name'] = scopeid_product[model['scope_id']]
                    model['time'] = start.strftime('%Y-%m-%d')

                #find key_list
                if not model_list:
                    tmp = []
                else:
                    tmp = model_list[0].keys()
                key_list = [
                    i for i in tmp
                    if i not in ('scope_id', 'time', 'product_name')
                ]

                #find product_name_list
                tmp = []
                for model in model_list:
                    tmp.append(model['product_name'])
                tmp_set = set(tmp)
                product_name_list = [i for i in tmp_set]
                #get dft res_list
                res_list = []
                for i in product_name_list:
                    res_model = {}
                    res_model['product_name'] = i
                    res_model['time'] = start.strftime('%Y-%m-%d')
                    for key in key_list:
                        res_model[key] = 0
                    res_list.append(res_model)
                #Cumulative data
                for model in model_list:
                    for res in res_list:
                        if res['product_name'] == model['product_name']:
                            for key in key_list:
                                res[key] += model[key]

                count = len(res_list)
                res_list = self.bubblesort(res_list, order_field)
                if action == 'export':
                    title = [
                        ('product_name', '机型'), ('time', '时间'),
                        ('user_run', '启动用户'), ('new_user_run', '新增启动用户'),
                        ('user_visit', '访问用户'), ('new_user_visit', '新增访问用户'),
                        ('pay_user', '付费用户'), ('active_user_visit', '活跃用户'),
                        ('visits', '访问PV'), ('cpay_down', '按章付费数'),
                        ('bpay_down', '按本付费数'), ('cpay_user', '按章付费用户'),
                        ('bpay_user', '按本付费用户'), ('cfree_down', '按章免费数'),
                        ('bfree_down', '按本免费数'), ('cfree_user', '按章免费用户'),
                        ('bfree_user', '按本免费用户')
                    ]
                    xls = Excel().generate(title, res_list, 1)
                    filename = 'factstat_product_%s.xls' % (
                        yest.strftime('%Y-%m-%d'))
                    self.set_header('Content-Disposition',
                                    'attachment;filename=%s' % filename)
                    self.finish(xls)
                else:
                    res_list = res_list[(page - 1) * psize:page * psize]
                    self.render('data/factstat_product_singleday.html',
                                count=count,
                                psize=psize,
                                page=page,
                                factory_id=factory_id,
                                query_mode=query_mode,
                                product_name=product_name,
                                basics=res_list,
                                start=start.strftime('%Y-%m-%d'),
                                date=date.strftime('%Y-%m-%d'),
                                factory_list=factory_list,
                                order_field=order_field)

        elif query_mode == 'fact' or query_mode == 'business':
            basics = []
            count = len(factory_list)
            all_stats = Service.inst().stat.get_all_factstat('day', yest, tody)
            acc_stats = {}
            for i in factory_list:
                basic = all_stats.get(i.id, None)
                if basic:
                    for k in basic:
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)
                    basic['title'] = i.name
                    basics.append(basic)

            acc_stats['title'] = '总计'
            basics.sort(cmp=lambda x, y: cmp(x[order_field], y[order_field]),
                        reverse=True)
            basics = basics[(page - 1) * psize:page * psize]
            if basics:
                basics += [acc_stats]
            groups = Factory.mgr().all_groups()
            self.render('data/factstat_fact.html',
                        count=count,
                        psize=psize,
                        page=page,
                        query_mode=query_mode,
                        basics=basics,
                        factory_id=factory_id,
                        factory_list=factory_list,
                        start=None,
                        product_name='',
                        date=yest.strftime('%Y-%m-%d'),
                        order_field=order_field,
                        group=group,
                        groups=groups)

        elif query_mode == 'factory_sum':
            start = self.get_argument('start', '')
            if not start:
                start = tody - datetime.timedelta(days=7)
            else:
                start = datetime.datetime.strptime(start, '%Y-%m-%d')
            #factory sum stat
            fact_sums = []
            acc_fact_sum = {}
            fact_list = [long(i.id) for i in factory_list]
            _start, days = start, []
            while _start < tody:
                _end = _start + datetime.timedelta(days=1)
                #for the user who access other people's factory sum stat
                if len(fact_list) == 0:
                    _start = _end
                    continue

                fact_sum = {}
                in_vals = ','.join(["'%s'" % i for i in fact_list])
                sql = """
                SELECT SUM(visits) AS visits,SUM(imei) AS imei,SUM(user_run) AS user_run,SUM(new_user_run) AS new_user_run,
                SUM(user_visit) AS user_visit, SUM(new_user_visit) AS new_user_visit,SUM(active_user_visit) AS active_user_visit, 
                SUM(user_retention) AS user_retention,SUM(pay_user) AS pay_user, SUM(cpay_down) AS cpay_down, SUM(cfree_down) AS cfree_down,
                SUM(bpay_down) AS bpay_down, SUM(bfree_down) AS bfree_down,SUM(cpay_user) AS cpay_user, SUM(cfree_user) AS cfree_user, 
                SUM(bpay_user) AS bpay_user, SUM(bfree_user) AS bfree_user, SUM(bpay_user) AS bpay_user,SUM(bfree_user) AS bfree_user, SUM(cfee) AS cfee, 
                SUM(bfee) AS bfee FROM factory_sum_stat WHERE time = '%s' AND factory_id IN (%s) """ % (
                    _start, in_vals)
                q = FactorySumStat.mgr().raw(sql)
                if q[0]['visits'] == None:
                    fact_sum = dict([(i, 0) for i in FactorySumStat._fields])
                else:
                    fact_sum = q[0]
                for k in fact_sum:
                    acc_fact_sum[k] = fact_sum[k] + acc_fact_sum.get(k, 0)
                fact_sum['title'] = _start.strftime('%Y-%m-%d')
                fact_sums.append(fact_sum)
                days.append(_start)
                _start = _end
            acc_fact_sum['title'] = '总计'
            fact_sums.append(acc_fact_sum)
            #for charts
            x_axis = ['%s' % i.strftime('%m-%d') for i in days]
            results = {}
            excludes = ('id', 'time', 'factory_id', 'visits', 'imei', 'bfee',
                        'cfee', 'uptime')
            for k in [i for i in FactorySumStat._fields if i not in excludes]:
                results[k] = {
                    'title': FactorySumStat._fieldesc[k],
                    'data':
                    ','.join([str(i.get(k, 0)) for i in fact_sums[:-1]])
                }

            #render
            count = len(days) * len(factory_list)
            groups = Factory.mgr().all_groups()
            self.render('data/factstat_sum.html',
                        count=count,
                        psize=psize,
                        page=page,
                        query_mode=query_mode,
                        results=results,
                        x_axis=x_axis,
                        fact_sums=fact_sums,
                        factory_id=factory_id,
                        factory_list=factory_list,
                        product_name='',
                        start=start.strftime('%Y-%m-%d'),
                        date=yest.strftime('%Y-%m-%d'),
                        order_field=order_field,
                        group=group,
                        groups=groups)

        elif query_mode == 'recharge_log':
            page = int(self.get_argument('pageNum', 1))
            psize = int(self.get_argument('numPerPage', 20))
            phone = self.get_argument('phone', '')

            tody = self.get_date(0)
            start = self.get_argument('start', '')
            end = self.get_argument('end', '')
            if not start:
                start = tody - datetime.timedelta(days=30)
                start = start.strftime('%Y-%m-%d')
            if not end:
                end = tody.strftime('%Y-%m-%d')

            #user...
            user = self.get_current_user()
            query_count = QueryTimeCount.mgr().get_user_today_query_count(
                user['name'], tody.strftime('%Y-%m-%d'))
            if user:
                qt = QueryTimeCount.new()
                qt.user = user['name']
                qt.save()

            if query_count <= Query_Limit_Count or user['name'] == 'admin':
                data = Service.inst().fill_recharge_log_info(phone, start, end)
                recharge_log = data['res']
                if not recharge_log:
                    recharge_log = []
                recharge_log[(page - 1) * psize:page * psize]
                is_show = True
            else:
                recharge_log = []
                is_show = False
            count = len(recharge_log)
            self.render('data/factstat_recharge_log.html',
                        count=count,
                        psize=psize,
                        page=page,
                        recharge_log=recharge_log,
                        start=start,
                        date=end,
                        query_mode=query_mode,
                        phone=phone,
                        is_show=is_show)

        elif query_mode == 'consume_log':
            page = int(self.get_argument('pageNum', 1))
            psize = int(self.get_argument('numPerPage', 20))
            usr = self.get_argument('usr', '')

            tody = self.get_date(0)
            start = self.get_argument('start', '')
            end = self.get_argument('end', '')

            #user...
            user = self.get_current_user()
            query_count = QueryTimeCount.mgr().get_user_today_query_count(
                user['name'], tody.strftime('%Y-%m-%d'))
            if user:
                qt = QueryTimeCount.new()
                qt.user = user['name']
                qt.save()
            dft = {
                'obj': [],
                'page': {
                    u'totalPage': 0,
                    u'endPage': 0,
                    u'totals': 0,
                    u'startPage': 1,
                    u'perPages': 20,
                    u'currentPage': 1
                }
            }
            obj = []
            if query_count <= Query_Limit_Count or user['name'] == 'admin':
                if not usr:
                    data = dft
                else:
                    data = Service.inst().fill_consume_log_info(
                        usr, page, psize)

                obj = data['obj']
                count = data['page']['totals']
                page = data['page']['currentPage']
                psize = data['page']['perPages']
                if not obj:
                    obj = []
                is_show = True
            else:
                obj = []
                count = 0
                page = 1
                psize = psize
                is_show = False
            self.render('data/factstat_consume_log.html',
                        count=count,
                        psize=psize,
                        page=page,
                        obj=obj,
                        query_mode=query_mode,
                        is_show=is_show,
                        usr=usr)
Пример #31
0
    def factorydaily(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        factory_id = int(self.get_argument('factory_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','accounting_basic')
        group = self.get_argument('group','')
        order_field = self.get_argument('orderField','user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start','')
        date = self.get_argument('date','')
        action = self.get_argument('action','')
        
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],query_mode)
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list)>150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        start_time = None
        user = self.get_current_user()
        if user['name'] in SAFE_USER:
            start_time = datetime.datetime(2012,12,31)
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        # scope list
        scopeid_list = []
        scope_proportion_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                      partner_id=i.partner_id,version_name=version_name,product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
                temp = {'proportion':i['proportion'],'scope_id':scope.id,'partner_id':i['partner_id']}
                scope_proportion_list.append(temp)
        # basic stat
        basics = []
        dft = dict([(i,0) for i in BasicStat._fields])
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start,'%Y-%m-%d') 
        _start,days = start,[]
        if start_time:
            if start_time > _start: #限定开始时间
                _start = start_time
        else:
            _start = datetime.datetime(3012,01,01) # 没配置的不现实
        while _start < tody:
            _end = _start + datetime.timedelta(days=1)
            #basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode='day',start=_start,end=_end)
            basic = BasicStat.mgr().get_data_by_multi_scope_proportion(scope_proportion_list,mode='day',start=_start,end=_end)
            #Special factory Coefficient 
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d')>= statttime:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(int(basic[i])) * coefficient #要和商务月报数据保持一致,所以这么处理
                        basic[i] = "%.01f" % basic[i]
                        basic[i] = float(basic[i]) * sfc
                        basic[i] = int(basic[i])
                else:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * coefficient
                        basic[i] = long(basic[i])
            #Special factory Coefficient2
            if factory_id in special_factory_list2:
                statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
                sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d')>= statttime2:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * sfc2
                        basic[i] = int(basic[i])
            
            basic['title'] = _start.strftime('%Y-%m-%d')
            basics.append(basic)
            days.append(_start)
            _start = _end
        # for chart
        x_axis = ['%s'%i.strftime('%m-%d') for i in days]
        results = {}
        excludes = ('id','scope_id','mode','time','bfee','cfee','recharge','uptime','batch_fee','batch_pv','batch_uv','visits','user_visit',
                    'new_user_visit','active_user_visit','imei','user_retention')
        for k in [i for i in BasicStat._fields if i not in excludes]:
            results[k] ={'title':BasicStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
        for i in basics:
            i['feesum'] = float(i['consumefee'])+i['msgfee']
            i['feesum'] = int(i['feesum'])
        acc_stats = {}
        for basic in basics:
            for field in basic:
                if field != 'title':
                    basic[field] = int(basic[field])
                    acc_stats[field] = basic[field] + acc_stats.get(field,0)
        acc_stats['title'] = '总计'
        if basics:
            basics.append(acc_stats)
        acc = {'title':'总计'}
        if action == 'export':  
            excel_title = [('title','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),
            ('pay_user','付费用户'),
            ('cpay_down','章付费数'),('bpay_down','本付费数'),
            ('cpay_user','章付费用户'),('bpay_user','本付费用户'),
            ('cfree_down','章免费数'),('bfree_down','本免费数'),
            ('cfree_user','章免费用户'),('bfree_user','本免费用户'),
            ('feesum','收入')]
            xls = Excel().generate(excel_title,basics,1) 
            filename = 'accounting_factorydaily_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/accounting_factorydaily.html',
                    latform_id=platform_id,run_id=run_id,plan_id=plan_id,factory_id=factory_id,tpl_titles=ACCOUNTING_FACTORY_TITLE,
                    version_name=version_name,product_name=product_name,query_mode=query_mode,
                    basics=basics,start=start.strftime('%Y-%m-%d'),date=yest.strftime('%Y-%m-%d'),
                    factory_list=factory_list,x_axis=x_axis,results=results)
Пример #32
0
 def delete(self):
     sid = int(self.get_argument('id'))
     scope = Scope.mgr(ismaster=1).Q().filter(id=sid)[0]
     scope and scope.delete()
     self.json2dwz('200', 'forward', 'dlist', forwardUrl='scope/list')
Пример #33
0
    def update_new_user_run_stat(self, start, mode):
        print "updating new user run..."
        start, end = self.normlize_time(mode, start)
        import_num = 0

        # get parnter list
        partner = Partner.mgr().get_all_partner()
        partner = partner[:]
        partner.insert(0, {'partner_id': 0})
        #partner = [{'partner_id':110010},{'partner_id':110011},{'partner_id':0}]

        # get scope_id and update
        for i in partner:
            # A partner matchs '110/d{3}' means it is wp7 or wp8 ,don's need update,and ios partner don't update ether
            pattern = re.compile(r'^11\d{4}$')
            match = pattern.match(str(i['partner_id']))
            if match:
                print i['partner_id'], ' do not update'
                continue
            if i['partner_id'] in DO_NOT_UPDATE_NEW_USER_RUN_PARTNERID_LIST:
                print i['partner_id'], ' do not update'
                continue

            # not wp7 wp8
            scope = Scope.mgr().Q().filter(partner_id=i['partner_id'],
                                           product_name='',
                                           run_id=0,
                                           version_name='')[0]
            scope_update = Scope.mgr(
            ).get_scopes_by_parnter_id_for_update_new_user_run(i['partner_id'])
            if i['partner_id'] == 0:
                scope_update = [{
                    'id': 39
                }, {
                    'id': 40
                }, {
                    'id': 41
                }, {
                    'id': 42
                }, {
                    'id': 151988
                }, {
                    'id': 321972
                }, {
                    'id': 330925
                }, {
                    'id': 443939
                }, {
                    'id': 661502
                }, {
                    'id': 669196
                }, {
                    'id': 749766
                }, {
                    'id': 876486
                }, {
                    'id': 985700
                }, {
                    'id': 1186259
                }]
            if scope and scope_update:
                sum_new_user_run = BasicStat.mgr(
                ).get_sum_new_user_run_by_scope_ids(scope_update, start, mode)
                if sum_new_user_run['new_user_run'] == None:
                    sum_new_user_run['new_user_run'] = 0
                try:
                    s = BasicStat.mgr(ismaster=1).Q(time=start).filter(
                        scope_id=scope['id'], mode=mode, time=start)[0]
                    if s:
                        s.new_user_run = int(sum_new_user_run['new_user_run'])
                        s.save()
                        import_num += 1
                except Exception, e:
                    s = None
                    logging.error('%s\n', str(e), exc_info=True)
Пример #34
0
    def chart(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        partner_id = int(self.get_argument('partner_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 400))
        charge_type = self.get_argument('charge_type', '')
        book_ids = self.get_argument('book_ids', '')
        bookid_list = [i for i in book_ids.split(',') if i]
        action = self.get_argument('action', '')
        #perm
        run_list = self.run_list()
        run_list = self.filter_run_id_perms(run_list=run_list)
        run_id_list = [run['run_id'] for run in run_list]
        if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
            if len(run_id_list) == len(
                    Run.mgr().Q().extra("status<>'hide'").data()):
                run_id = 0  # user has all run_id perms
            else:
                run_id = run_id_list[0]
        if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
            scope = None
        else:
            # scope
            scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                           run_id=run_id,
                                           plan_id=plan_id,
                                           partner_id=partner_id,
                                           version_name=version_name,
                                           product_name=product_name)[0]
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start', '')

        if start:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        else:
            start = tody - datetime.timedelta(days=7)
        # x_axis
        delta = tody - start
        days = [start + datetime.timedelta(days=i) for i in range(delta.days)]
        x_axis = ['%s' % i.strftime('%m-%d') for i in days]
        count, books = 0, []
        if scope:
            # books of by-book & by-chapter
            result = BookStat.mgr().get_data(scope.id, 'day', start, tody,
                                             charge_type, bookid_list, page,
                                             psize)
            count = result['count']
            books = Service.inst().fill_book_info(result['list'])
            while (
                    self.do_books_have_two_or_empty_title(books)
            ):  #this is for Fu Rong's request, but books can't deepcopy, so I have to do this way, so egg pain !
                books = self.remove_books_two_or_empty_title(books)

        # pagination
        results = {}
        excludes = ('id', 'scope_id', 'mode', 'time', 'uptime', 'category_1',
                    'category_0', 'charge_type', 'author', 'book_id', 'arpu',
                    'category_0')
        for k in [i for i in BookStat._fields if i not in excludes]:
            results[k] = {
                'title': BookStat._fieldesc[k],
                'data': ','.join([str(i.get(k, 0)) for i in books])
            }
        page_count = (count + psize - 1) / psize
        if action == 'export':
            title = [('time', '时间'), ('book_id', '书ID'), ('name', '书名'),
                     ('author', '作者'), ('cp', '版权'), ('category_2', '类别'),
                     ('category_1', '子类'), ('category_0', '三级分类'),
                     ('state', '状态'), ('charge_type', '计费类型'), ('fee', '收益'),
                     ('pay_down', '付费下载数'), ('pay_user', '付费下载用户数'),
                     ('free_down', '免费下载数'), ('free_user', '免费下载用户数'),
                     ('pv', '简介访问数'), ('uv', '简介访问人数'),
                     ('batch_fee', '批量订购阅饼消费'), ('batch_pv', '批量订购PV'),
                     ('batch_uv', '批量订购UV')]
            xls = Excel().generate(title, books, 1)
            filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            self.render('data/book_chart.html',
                        x_axis=x_axis,
                        results=results,
                        platform_id=platform_id,
                        run_id=run_id,
                        plan_id=plan_id,
                        date=yest.strftime('%Y-%m-%d'),
                        partner_id=partner_id,
                        version_name=version_name,
                        product_name=product_name,
                        run_list=self.run_list(),
                        plan_list=self.plan_list(),
                        page=page,
                        psize=psize,
                        count=count,
                        page_count=page_count,
                        books=books,
                        charge_type=charge_type,
                        book_ids=book_ids,
                        start=start.strftime('%Y-%m-%d'))
Пример #35
0
 def arpu_multi_days(self, mode):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     # scope 
     scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     days = [tody-datetime.timedelta(days=i) for i in range(7 if mode=='week' else 30,0,-1)]
     start = self.get_argument('start','')
     action = self.get_argument('action','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')
     elif mode == 'week':
         start = tody - datetime.timedelta(days=7)
     else:
         start = tody - datetime.timedelta(days=30)
     delta = tody - start
     days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
     basics = []
     
     # scope list
     all_fact_id = Factory.mgr().get_all_factory()
     scopeid_list = []
     for fact_id in all_fact_id:
         partner_list = Partner.mgr().Q().filter(factory_id=fact_id['id'])[:]
         for i in partner_list:
             scope2 = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id, 
                 partner_id=i.partner_id,version_name=version_name,product_name=product_name)[0]
             if scope2:
                 scopeid_list.append(scope2.id)
     
     if scope:
         for i in days:
             dft = dict([(j,0) for j in BasicStat._fields])
             s = BasicStat.mgr().Q(time=i).filter(scope_id=scope.id,mode='day',time=i)[0] or dft
             recharge = BasicStat.mgr().get_recharge_by_multi_scope_one_day(scopeid_list, i, mode='day')
             arpu_7days_stat = Arpu7DaysArpuStat.mgr().get_arpu_7days_stat(i)[0]
             arpu_30days_stat = Arpu30DaysArpuFeeStat.mgr().get_arpu_30_days_stat(i)[0]
             arpu_90days_stat = Arpu90DaysArpuFeeStat.mgr().get_arpu_90_days_stat(i)[0]
             s['title'] = i.strftime('%Y-%m-%d')
             s['recharge'] = recharge
             s['arpu_30days'],s['arpu_90days'] = 0,0
             # 7 days arpu and new_user_visit
             if arpu_7days_stat:
                 s['new_user_visit'] = arpu_7days_stat['new_user_visit']
                 if arpu_7days_stat['new_user_visit'] == 0:
                     s['arpu_7days'] = 0
                     s['one_week_fee'] = 0
                 else:
                     s['arpu_7days'] = '%.03f' % (arpu_7days_stat['one_week_fee']/arpu_7days_stat['new_user_visit'])
                     s['one_week_fee'] = arpu_7days_stat['one_week_fee']
             else:
                 s['new_user_visit'] = 0
                 s['arpu_7days'] = 0
                 s['one_week_fee'] = 0
             #30 days arpu
             if arpu_30days_stat:
                 if s['new_user_visit'] != 0:
                     s['arpu_30days'] = '%0.3f' % (arpu_30days_stat['thirty_days_fee']/s['new_user_visit'])
             else:
                 s['arpu_30days'] = 0
             #90 days arpu
             if arpu_90days_stat:
                 if s['new_user_visit'] != 0:
                     s['arpu_90days'] = '%0.3f' % (arpu_90days_stat['ninety_days_fee']/s['new_user_visit'])
             else:
                 s['arpu_90days'] = 0
             # arpu
             if s['cpay_user'] != 0:
                 s['arpu'] = '%.03f' % (s['recharge']/s['user_run'])
             else:
                 s['arpu'] = 0
             basics.append(s)
     x_axis = ['%s'%i.strftime('%m-%d') for i in days] 
     results = {}
     arpu_list = []
     excludes = ('id','scope_id','mode','time','recharge','uptime','bfree_down','visits','cfree_user','bfee','imei','user_retention','cfee','batch_uv',
         'pay_user','cfree_down','bpay_user','batch_pv','user_visit','batch_fee','active_user_visit','cpay_down','bfree_user','bpay_down')
     for k in [i for i in BasicStat._fields if i not in excludes]:
         results[k] ={'title':BasicStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
     for basic in basics:
         arpu_list.append(basic['arpu'])
     arpu_data = ','.join(arpu_list)
     results['arpu'] = {'data':arpu_data,'title':'arpu'}
     self.render('data/arpu.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,partner_id=partner_id,
                 version_name=version_name,product_name=product_name,date=yest.strftime('%Y-%m-%d'),
                 run_list=self.run_list(),plan_list=self.plan_list(),mode=mode,x_axis=x_axis,
                 basics=basics,results=results,start=start.strftime('%Y-%m-%d')
                 )
Пример #36
0
 def index(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     charge_type = self.get_argument('charge_type','')
     order_field = self.get_argument('orderField','fee')
     assert order_field in ('pay_user','free_user','pay_down','free_down','pv','uv','fee','batch_fee','batch_pv','batch_uv','real_fee')
     action = self.get_argument('action','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')   
     else:
         start = yest
     if start > yest:
         start = yest
     count,books = 0,[]
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(scope_id=scope.id,mode='day',start=start,end=tody,charge_type=charge_type)  
             q = q.orderby(order_field,'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[(page-1)*psize:page*psize]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field,'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[(page-1)*psize:page*psize]
         books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count+psize-1)/psize
     if action == 'export':
         books = Service.inst().fill_book_count_info(books)
         while (self.do_books_have_two_or_empty_title(books)): 
             books = self.remove_books_two_or_empty_title(books) 
         title = [('time','时间'),('book_id','书ID'),('name','书名'),('author','作者'),
                  ('cp','版权'),('category_2','类别'),('category_1','子类'),('category_0','三级分类'),('state','状态'),
                  ('charge_type','计费类型'),('fee','收益'),('pay_down','付费下载数'),
                  ('pay_user','付费下载用户数'),('free_down','免费下载数'),
                  ('free_user','免费下载用户数'),('pv','简介访问数'),('uv','简介访问人数'),
                  ('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV'),
                  ('chapterCount','总章数'),('wordCount','总字数')]
         xls = Excel().generate(title,books,1)
         filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
         self.set_header('Content-Disposition','attachment;filename=%s'%filename)
         self.finish(xls)
     else:
         self.render('data/book.html',
                     platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                     partner_id=partner_id,version_name=version_name,product_name=product_name,
                     run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                     count=count,page_count=page_count,books=books,charge_type=charge_type,
                     order_field = order_field,start=start.strftime('%Y-%m-%d')
                     )
Пример #37
0
    def start_importing(self, mode, now=None, stats=None, partner_ids=None):
        '''
        entry to analyze and import stat into mysql
        '''
        n0 = datetime.datetime.now()
        # clear cache
        self.down.reset_cache()
        self.service.reset_cache()
        # extra sql for partner_ids
        extra = None
        if partner_ids:
            extra = 'partner_id in (%s)' % partner_ids
            self.down.set_extra(extra)
            self.service.set_extra(extra)

        # normalize time
        if not now:
            now = datetime.datetime.now() - datetime.timedelta(days=1)
        if mode == 'day':
            start = time_start(now,'day') 
        elif mode == 'week' and now.weekday() == 0:
            start = time_start(now,'week') 
        elif mode == 'month' and now.day == 1:
            start = time_start(now,'month')
        else:
            print '%s %s not supported' % (mode,now)
            return

        # import the partner and product to scope
        if not (stats or partner_ids):
            self.import_partner_productname(mode,start)
       
        # loop all the valid scopes
        q = Scope.mgr().Q().extra("status in ('pas','nice')")
        extra and q.extra(extra)
        for scope in q:
            mask = scope.mask.split(',')
            modes = scope.modes.split(',')
            if mode not in modes:
                print 'ignoring ',mode,scope.id,scope.modes
                continue

            print 'processing',scope.id,mask,modes
            
            # patch basic stat
            if 'basic' in mask and stats and 'patch_basic' in stats:
                self.patch_basic(mode,start,**scope)

            if 'book' in mask and stats and 'patch_book' in stats:
                self.patch_book(mode,start,**scope)
            
            # basic stat
            if 'basic' in mask and (not stats or 'basic' in stats):
                self.import_basic_stat(mode,start,**scope)
        
            # visit stat of page types
            if 'visit' in mask and (not stats or 'visit' in stats):
                self.import_visit_stat(mode,start,**scope)

            # topN of the page types
            if 'topn' in mask and (not stats or 'topn' in stats):
                self.import_topN(mode,start,**scope)

            # book stat
            if 'book' in mask and (not stats or 'book' in stats):
                self.import_book_stat(mode,start,num=0,**scope)
                self.import_bookrefer_stat(mode,start,['search_main','search_button','hot_search_word'],**scope)
            
            # machine stat
            if 'product' in mask and (not stats or 'product' in stats):
                self.import_product_stat(mode,start,num=9,**scope)

        # update new_user_run stat
        if not stats or 'update' in stats:
            self.update_new_user_run_stat(start,mode)
            self.update_zero_new_user_run_stat(start,mode,SHOULD_UPDATE_ZERO_NEW_USER_RUN_PARTNER_LIST)
 
        # logstatV2 -> logstatv3 
        if not stats or 'basicv3' in stats:
            self.import_factory_database(mode,start)
       
        # merge basic to factory sum
        if not stats or 'factory' in stats:
            self.import_factory_stat(mode,start)

        # merge tag according to book stat
        if not stats or 'tag' in stats:
            self.import_toptag_bybook(mode,start)

        print mode,start,'time:',datetime.datetime.now()-n0
        return True
Пример #38
0
    def basic_chart(self, mode):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        partner_id = int(self.get_argument('partner_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        run_list = [i for i in self.run_list() if i['run_id'] in IOS_RUN_ID_LIST]
        
        if run_id == 0: #不限
            scope = Scope.mgr().get_ios_scope_id_list()
            tody = self.get_date(1) + datetime.timedelta(days=1)
            yest = tody - datetime.timedelta(days=1)
            days = [tody-datetime.timedelta(days=i) for i in range(7 if mode=='week' else 30,0,-1)]
            start = self.get_argument('start','')
            action = self.get_argument('action','')
            if start:
                start = datetime.datetime.strptime(start,'%Y-%m-%d')
            elif mode == 'week':
                start = tody - datetime.timedelta(days=7)
            else:
                start = tody - datetime.timedelta(days=30)
            delta = tody - start
            days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
            basics = []
            if scope:
                if partner_id:
                    scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                        partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
                    if not scope:
                        scope_id_list = []
                        basics = []
                    else:
                        scope_id_list = str(scope['id'])
                        for i in days:
                            dft = dict([(j,0) for j in BasicStatv3._fields])
                            basic = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,i)[0]
                            basic['title'] = i.strftime('%Y-%m-%d')
                            basics.append(basic)
                else:
                    scope_id_list = ','.join([str(i['id']) for i in scope]) 
                    for i in days:
                        dft = dict([(j,0) for j in BasicStatv3._fields])
                        basic = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,i)[0]
                        basic['title'] = i.strftime('%Y-%m-%d')
                        basics.append(basic)
                for basic in basics:
                    basic['cfee'] = long(basic['cfee'])
                    basic['bfee'] = long(basic['bfee'])
                    basic['batch_fee'] = long(basic['batch_fee'])

        else: 
            # scope
            scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                      partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
            tody = self.get_date(1) + datetime.timedelta(days=1)
            yest = tody - datetime.timedelta(days=1)
            days = [tody-datetime.timedelta(days=i) for i in range(7 if mode=='week' else 30,0,-1)]
            start = self.get_argument('start','')
            action = self.get_argument('action','')
            if start:
                start = datetime.datetime.strptime(start,'%Y-%m-%d')
            elif mode == 'week':
                start = tody - datetime.timedelta(days=7)
            else:
                start = tody - datetime.timedelta(days=30)
            delta = tody - start
            days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
            basics = []
            if scope:
                for i in days:
                    dft = dict([(j,0) for j in BasicStatv3._fields])
                    s = BasicStatv3.mgr().Q(time=i).filter(scope_id=scope.id,mode='day',time=i)[0] or dft
                    s['title'] = i.strftime('%Y-%m-%d')
                    basics.append(s)
                for basic in basics:
                    basic['cfee'] = long(basic['cfee'])
                    basic['bfee'] = long(basic['bfee'])
                    basic['batch_fee'] = long(basic['batch_fee'])
        x_axis = ['%s'%i.strftime('%m-%d') for i in days] 
        results = {}
        excludes = ('id','scope_id','mode','time','recharge','uptime')
        for k in [i for i in BasicStatv3._fields if i not in excludes]:
            results[k] ={'title':BasicStatv3._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
        if action == 'export':
            excel_title = [('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),
                    ('user_visit','访问用户'),('new_user_visit','新增访问用户'),('pay_user','付费用户'),
                    ('active_user_visit','活跃用户'),('visits','访问PV'),
                    ('cpay_down','章付费数'),('bpay_down','本付费数'),
                    ('cpay_user','章付费用户'),('bpay_user','本付费用户'),
                    ('cfree_down','章免费数'),('bfree_down','本免费数'),
                    ('cfree_user','章免费用户'),('bfree_user','本免费用户'),
                    ('cfee','章月饼消费'),('bfee','本月饼消费'),('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV')
                ]
            xls = Excel().generate(excel_title,basics,1) 
            filename = 'basic_%s_%s.xls' % (mode,yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/ios_basic_chart.html',
                platform_id=platform_id,run_id=run_id,plan_id=plan_id,partner_id=partner_id,
                version_name=version_name,product_name=product_name,date=yest.strftime('%Y-%m-%d'),
                run_list=run_list,mode=mode,x_axis=x_axis,
                basics=basics,results=results,start=start.strftime('%Y-%m-%d')
                )
Пример #39
0
 def basic(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     
     tody = self.get_date()
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     run_list = [i for i in self.run_list() if i['run_id'] in IOS_RUN_ID_LIST]
     basics = []
     if run_id == 0: #不限
         if partner_id:
             scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                 partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
             if scope:
                 scope_id_list = str(scope['id'])
                 dft = dict([(i,0) for i in BasicStatv3._fields])
                 basic_y = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,yest)[0]
                 basic_l = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,last)[0]
                 basic_y,basic_l = basic_y or dft,basic_l or dft
                 basic_y['title'],basic_l['title'] = '昨日统计','前日统计'
                 basics = [basic_y,basic_l]
                 for basic in basics:
                     basic['cfee'] = long(basic['cfee'])
                     basic['bfee'] = long(basic['bfee'])
                     basic['batch_fee'] = long(basic['batch_fee'])
         else:
             scope = Scope.mgr().get_ios_scope_id_list()
             if scope:
                 scope_id_list = ','.join([str(i['id']) for i in scope]) 
                 dft = dict([(i,0) for i in BasicStatv3._fields])
                 basic_y = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,yest)[0]
                 basic_l = BasicStatv3.mgr().get_sum_data_by_multi_scope(scope_id_list,last)[0]
                 basic_y,basic_l = basic_y or dft,basic_l or dft
                 basic_y['title'],basic_l['title'] = '昨日统计','前日统计'
                 basics = [basic_y,basic_l]
                 for basic in basics:
                     basic['cfee'] = long(basic['cfee'])
                     basic['bfee'] = long(basic['bfee'])
                     basic['batch_fee'] = long(basic['batch_fee'])
     else:
         # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
         tody = self.get_date()
         yest = tody - datetime.timedelta(days=1)
         last = yest - datetime.timedelta(days=1)
         start = tody - datetime.timedelta(days=30)
         basics,topn_sch,topn_hw,b_books,c_books = [],[],[],[],[]
         visit_y = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
         visit_l = dict([(i,{'pv':0,'uv':0}) for i in PAGE_TYPE])
         if scope:
             # basic stat
             dft = dict([(i,0) for i in BasicStatv3._fields])
             basic_y = BasicStatv3.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)[0] 
             basic_l = BasicStatv3.mgr().Q(time=last).filter(scope_id=scope.id,mode='day',time=last)[0] 
             basic_m = BasicStatv3.mgr().get_data(scope.id,'day',start,tody,ismean=True)
             basic_p = BasicStatv3.mgr().get_peak(scope.id,'day',start,tody)
             basic_y,basic_l = basic_y or dft,basic_l or dft
             basic_y['title'],basic_l['title'] = '昨日统计','前日统计'
             basic_m['title'],basic_p['title'] = '每日平均','历史峰值'
             basics = [basic_y,basic_l,basic_m,basic_p]
             for basic in basics:
                 basic['cfee'] = long(basic['cfee'])
                 basic['bfee'] = long(basic['bfee'])
                 basic['batch_fee'] = long(basic['batch_fee'])
             # page visit
             for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=yest):
                 visit_y[i['type']] = i
             for i in VisitStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=last):
                 visit_l[i['type']] = i
             # topN search & hotword
             q = TopNStat.mgr().Q().filter(scope_id=scope.id,mode='day',time=yest)
             topn_sch = q.filter(type='search').orderby('no')[:10]
             topn_hw = q.filter(type='hotword').orderby('no')[:10]
             # books of by-book & by-chapter
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
             b_books = Service.inst().fill_book_info(q.filter(charge_type='book').orderby('fee','DESC')[:10])
             c_books = Service.inst().fill_book_info(q.filter(charge_type='chapter').orderby('fee','DESC')[:10])
     self.render('data/ios_basic.html',
                 run_id=run_id,
                 partner_id=partner_id,
                 run_list=run_list,date=tody.strftime('%Y-%m-%d'),
                 basics = basics
                 )
Пример #40
0
 def delete(self):
     sid = int(self.get_argument('id'))
     scope = Scope.mgr(ismaster=1).Q().filter(id=sid)[0]
     scope and scope.delete()
     self.json2dwz('200','forward','dlist',forwardUrl='scope/list')
Пример #41
0
    def factorydaily(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        factory_id = int(self.get_argument('factory_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', 'accounting_basic')
        group = self.get_argument('group', '')
        order_field = self.get_argument('orderField', 'user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start', '')
        date = self.get_argument('date', '')
        action = self.get_argument('action', '')

        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], query_mode)
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list) > 150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        start_time = None
        user = self.get_current_user()
        if user['name'] in SAFE_USER:
            start_time = datetime.datetime(2012, 12, 31)
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        # scope list
        scopeid_list = []
        scope_proportion_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                           run_id=run_id,
                                           plan_id=plan_id,
                                           partner_id=i.partner_id,
                                           version_name=version_name,
                                           product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
                temp = {
                    'proportion': i['proportion'],
                    'scope_id': scope.id,
                    'partner_id': i['partner_id']
                }
                scope_proportion_list.append(temp)
        # basic stat
        basics = []
        dft = dict([(i, 0) for i in BasicStat._fields])
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        _start, days = start, []
        if start_time:
            if start_time > _start:  #限定开始时间
                _start = start_time
        else:
            _start = datetime.datetime(3012, 01, 01)  # 没配置的不现实
        while _start < tody:
            _end = _start + datetime.timedelta(days=1)
            #basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode='day',start=_start,end=_end)
            basic = BasicStat.mgr().get_data_by_multi_scope_proportion(
                scope_proportion_list, mode='day', start=_start, end=_end)
            #Special factory Coefficient
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                    'starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d') >= statttime:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(int(
                            basic[i])) * coefficient  #要和商务月报数据保持一致,所以这么处理
                        basic[i] = "%.01f" % basic[i]
                        basic[i] = float(basic[i]) * sfc
                        basic[i] = int(basic[i])
                else:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * coefficient
                        basic[i] = long(basic[i])
            #Special factory Coefficient2
            if factory_id in special_factory_list2:
                statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                    'starttime']
                sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d') >= statttime2:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * sfc2
                        basic[i] = int(basic[i])

            basic['title'] = _start.strftime('%Y-%m-%d')
            basics.append(basic)
            days.append(_start)
            _start = _end
        # for chart
        x_axis = ['%s' % i.strftime('%m-%d') for i in days]
        results = {}
        excludes = ('id', 'scope_id', 'mode', 'time', 'bfee', 'cfee',
                    'recharge', 'uptime', 'batch_fee', 'batch_pv', 'batch_uv',
                    'visits', 'user_visit', 'new_user_visit',
                    'active_user_visit', 'imei', 'user_retention')
        for k in [i for i in BasicStat._fields if i not in excludes]:
            results[k] = {
                'title': BasicStat._fieldesc[k],
                'data': ','.join([str(i.get(k, 0)) for i in basics])
            }
        for i in basics:
            i['feesum'] = float(i['consumefee']) + i['msgfee']
            i['feesum'] = int(i['feesum'])
        acc_stats = {}
        for basic in basics:
            for field in basic:
                if field != 'title':
                    basic[field] = int(basic[field])
                    acc_stats[field] = basic[field] + acc_stats.get(field, 0)
        acc_stats['title'] = '总计'
        if basics:
            basics.append(acc_stats)
        acc = {'title': '总计'}
        if action == 'export':
            excel_title = [('title', '时间'), ('user_run', '启动用户'),
                           ('new_user_run', '新增启动用户'), ('pay_user', '付费用户'),
                           ('cpay_down', '章付费数'), ('bpay_down', '本付费数'),
                           ('cpay_user', '章付费用户'), ('bpay_user', '本付费用户'),
                           ('cfree_down', '章免费数'), ('bfree_down', '本免费数'),
                           ('cfree_user', '章免费用户'), ('bfree_user', '本免费用户'),
                           ('feesum', '收入')]
            xls = Excel().generate(excel_title, basics, 1)
            filename = 'accounting_factorydaily_%s.xls' % (
                yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            self.render('data/accounting_factorydaily.html',
                        latform_id=platform_id,
                        run_id=run_id,
                        plan_id=plan_id,
                        factory_id=factory_id,
                        tpl_titles=ACCOUNTING_FACTORY_TITLE,
                        version_name=version_name,
                        product_name=product_name,
                        query_mode=query_mode,
                        basics=basics,
                        start=start.strftime('%Y-%m-%d'),
                        date=yest.strftime('%Y-%m-%d'),
                        factory_list=factory_list,
                        x_axis=x_axis,
                        results=results)
Пример #42
0
 def bookcategory(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     charge_type = self.get_argument('charge_type', '')
     order_field = self.get_argument('orderField', 'fee')
     cate_type = self.get_argument('cate_type', '')
     action = self.get_argument('action', '')
     #perm
     run_list = self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]
     if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
         if len(run_id_list) == len(
                 Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
         scope = None
     else:
         # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start', '')
     if start:
         start = datetime.datetime.strptime(start, '%Y-%m-%d')
     else:
         start = yest
     if start > yest:
         start = yest
     count, books = 0, []
     res_all = {}
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(
                 scope_id=scope.id,
                 mode='day',
                 start=start,
                 end=tody,
                 charge_type=charge_type)
             q = q.orderby(order_field, 'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,
                                                    mode='day',
                                                    time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field, 'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
     books = self.get_fill_category_id(books)
     cates = self.counting_category(books)
     cates = self.fill_category_name(cates)
     cates = self.bubblesort(cates, 'fee')
     if action == 'export':
         title = [('category_no3', '类别'), ('category_no2', '二级分类'),
                  ('category_no1', '三级分类'), ('fee', '阅饼消费'),
                  ('batch_fee', '批量下载阅饼消费')]
         xls = Excel().generate(title, cates, 1)
         filename = 'category.xls'
         self.set_header('Content-Disposition',
                         'attachment;filename=%s' % filename)
         self.finish(xls)
     else:
         self.render('data/book_category2.html',
                     platform_id=platform_id,
                     run_id=run_id,
                     plan_id=plan_id,
                     date=yest.strftime('%Y-%m-%d'),
                     partner_id=partner_id,
                     version_name=version_name,
                     product_name=product_name,
                     run_list=self.run_list(),
                     plan_list=self.plan_list(),
                     page=page,
                     psize=psize,
                     count=count,
                     charge_type=charge_type,
                     cates=cates,
                     start=start.strftime('%Y-%m-%d'))
Пример #43
0
 def category(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     charge_type = self.get_argument('charge_type', '')
     order_field = self.get_argument('orderField', 'fee')
     cate_type = self.get_argument('cate_type', '')
     assert order_field in ('pay_user', 'free_user', 'pay_down',
                            'free_down', 'pv', 'uv', 'fee', 'batch_fee',
                            'batch_pv', 'batch_uv')
     action = self.get_argument('action', '')
     #perm
     run_list = self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]
     if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
         if len(run_id_list) == len(
                 Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
         scope = None
     else:
         # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start', '')
     if start:
         start = datetime.datetime.strptime(start, '%Y-%m-%d')
     else:
         start = yest
     if start > yest:
         start = yest
     count, books = 0, []
     res_all = {}
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(
                 scope_id=scope.id,
                 mode='day',
                 start=start,
                 end=tody,
                 charge_type=charge_type)
             q = q.orderby(order_field, 'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,
                                                    mode='day',
                                                    time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field, 'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         books = Service.inst().fill_book_info(books)
         res0, res1, res2 = self.counting_category_stat(books)
         #merge dict
         if not cate_type:
             res_all = dict(dict(res0, **res1), **res2)
         elif cate_type == 'cate0':
             res_all = res0
         elif cate_type == 'cate1':
             res_all = res1
         else:
             res_all = res2
         count = len(res_all.keys())
         #dict->list
         res_list = []
         for i in res_all:
             res_list.append({i: res_all[i]})
         #sort
         res_list = self.bubblesort_double_dict_in_list(res_list, 'fee')
         self.render('data/book_category.html',
                     platform_id=platform_id,
                     run_id=run_id,
                     plan_id=plan_id,
                     date=yest.strftime('%Y-%m-%d'),
                     partner_id=partner_id,
                     version_name=version_name,
                     product_name=product_name,
                     run_list=self.run_list(),
                     plan_list=self.plan_list(),
                     page=page,
                     psize=psize,
                     count=count,
                     res_list=res_list,
                     charge_type=charge_type,
                     cate_type=cate_type,
                     start=start.strftime('%Y-%m-%d'))
Пример #44
0
 def basic_chart(self, mode):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     days = [tody-datetime.timedelta(days=i) for i in range(7 if mode=='week' else 30,0,-1)]
     start = self.get_argument('start','')
     action = self.get_argument('action','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')
     elif mode == 'week':
         start = tody - datetime.timedelta(days=7)
     else:
         start = tody - datetime.timedelta(days=30)
     delta = tody - start
     days = [start+datetime.timedelta(days=i) for i in range(delta.days)]
     basics = []
     if scope:
         for i in days:
             dft = dict([(j,0) for j in BasicStatv3._fields])
             s = BasicStatv3.mgr().Q(time=i).filter(scope_id=scope.id,mode='day',time=i)[0] or dft
             s['title'] = i.strftime('%Y-%m-%d')
             basics.append(s)
     x_axis = ['%s'%i.strftime('%m-%d') for i in days] 
     results = {}
     excludes = ('id','scope_id','mode','time','recharge','uptime')
     for k in [i for i in BasicStatv3._fields if i not in excludes]:
         results[k] ={'title':BasicStatv3._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
     if action == 'export':
         excel_title = [('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),
             ('user_visit','访问用户'),('new_user_visit','新增访问用户'),('pay_user','付费用户'),
             ('active_user_visit','活跃用户'),('visits','访问PV'),
             ('cpay_down','章付费数'),('bpay_down','本付费数'),
             ('cpay_user','章付费用户'),('bpay_user','本付费用户'),
             ('cfree_down','章免费数'),('bfree_down','本免费数'),
             ('cfree_user','章免费用户'),('bfree_user','本免费用户'),
             ('cfee','章月饼消费'),('bfee','本月饼消费'),('batch_fee','批量订购阅饼消费'),('batch_pv','批量订购PV'),('batch_uv','批量订购UV')
         ]
         xls = Excel().generate(excel_title,basics,1) 
         filename = 'basic_%s_%s.xls' % (mode,yest.strftime('%Y-%m-%d'))
         self.set_header('Content-Disposition','attachment;filename=%s'%filename)
         self.finish(xls)
     else:
         self.render('data/basic_chart.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,partner_id=partner_id,
                 version_name=version_name,product_name=product_name,date=yest.strftime('%Y-%m-%d'),
                 run_list=self.run_list(),plan_list=self.plan_list(),mode=mode,x_axis=x_axis,
                 basics=basics,results=results,start=start.strftime('%Y-%m-%d')
                 )
Пример #45
0
    def productdaily(self):
        order_field = self.get_argument('orderField','user_run')
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        group = self.get_argument('group','')
        psize = int(self.get_argument('numPerPage',20))
        start = self.get_argument('start','')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        action = self.get_argument('action','')
        factory_id = int(self.get_argument('factory_id',0))
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        query_mode = self.get_argument('query_mode','accounting_product') 
        if not start:
            start = yest
        else:
            start = datetime.datetime.strptime(start,'%Y-%m-%d')
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],'accounting_product')
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list)>150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        else:
            start_time = datetime.datetime(2014,12,31) 
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012,12,31)
            coefficient = 1.0

        if start_time:
            if start_time > start:
                start = start_time
        
        scopeid_product,model_list = {},[]
        if factory_id == -1:
            #partnerid_list = [0]
            partner_list = []
        for i in partner_list:
            scopeid_list = []
            if not product_name:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                partner_id=i['partner_id'],version_name=version_name).extra("product_name<>''")
            else:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                partner_id=i['partner_id'],version_name=version_name,product_name=product_name)
            for scope in scopes:
                scopeid_list.append(scope.id)
                scopeid_product[scope.id] = scope.product_name
            result = BasicStat.mgr().get_one_day_model_data_proportion(i['proportion'],scopeid_list,'day',start)
            model_list += result
        count = len(model_list)
        for model in model_list:
            model['product_name'] = scopeid_product[model['scope_id']]
            model['time'] = start.strftime('%Y-%m-%d') 
        
        #find key_list
        if not model_list:
            tmp = []
        else:
            tmp = model_list[0].keys()
        key_list = [i for i in tmp if i not in ('scope_id','time','product_name')]
                
        #find product_name_list
        tmp = []
        for model in model_list:
            tmp.append(model['product_name'])
        tmp_set = set(tmp)
        product_name_list = [i for i in tmp_set]
            
        #get dft res_list
        res_list = []
        for i in product_name_list:
            res_model = {}
            res_model['product_name'] = i
            res_model['time'] = start.strftime('%Y-%m-%d')
            for key in key_list:
                res_model[key] = 0
            res_list.append(res_model)
        #Cumulative data 
        for model in model_list:
            for res in res_list:
                if res['product_name'] == model['product_name']:
                    for key in key_list:
                        res[key] += model[key]
                
        count = len(res_list)
        res_list = self.bubblesort(res_list,order_field)
        #Special factory Coefficient 
        if factory_id in special_factory_list:
            statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
            sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d')>= statttime:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * coefficient * sfc # coefficient
                            res[i] = long(res[i])
            else:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * coefficient # coefficient
                            res[i] = long(res[i])
        #Special factory Coefficient2
        if factory_id in special_factory_list2:
            statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d')>= statttime2:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * sfc2 # coefficient
                            res[i] = long(res[i])
 
        if action == 'export': 
            title = [('product_name','机型'),('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),('user_visit','访问用户'),
                ('new_user_visit','新增访问用户'),('pay_user','付费用户'),('active_user_visit','活跃用户'),('visits','访问PV'),('cpay_down','按章付费数'),
                ('bpay_down','按本付费数'),('cpay_user','按章付费用户'),('bpay_user','按本付费用户'),('cfree_down','按章免费数'),('bfree_down','按本免费数'),
                ('cfree_user','按章免费用户'),('bfree_user','按本免费用户')]
            xls = Excel().generate(title,res_list,1)
            filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            res_list = res_list[(page-1)*psize:page*psize]
            self.render('data/accounting_productdaily.html',
                    count=count,psize=psize,page=page,factory_id=factory_id,
                    query_mode=query_mode,product_name=product_name,basics=res_list,
                    start=start.strftime('%Y-%m-%d'),
                    factory_list=factory_list, order_field=order_field)
Пример #46
0
    def start_importing(self, mode, now=None, stats=None, partner_ids=None):
        '''
        entry to analyze and import stat into mysql
        '''
        n0 = datetime.datetime.now()
        # clear cache
        self.down.reset_cache()
        self.service.reset_cache()
        # extra sql for partner_ids
        extra = None
        if partner_ids:
            extra = 'partner_id in (%s)' % partner_ids
            self.down.set_extra(extra)
            self.service.set_extra(extra)

        # normalize time
        if not now:
            now = datetime.datetime.now() - datetime.timedelta(days=1)
        if mode == 'day':
            start = time_start(now, 'day')
        elif mode == 'week' and now.weekday() == 0:
            start = time_start(now, 'week')
        elif mode == 'month' and now.day == 1:
            start = time_start(now, 'month')
        else:
            print '%s %s not supported' % (mode, now)
            return

        # import the partner and product to scope
        if not (stats or partner_ids):
            self.import_partner_productname(mode, start)

        # loop all the valid scopes
        q = Scope.mgr().Q().extra("status in ('pas','nice')")
        extra and q.extra(extra)
        for scope in q:
            mask = scope.mask.split(',')
            modes = scope.modes.split(',')
            if mode not in modes:
                print 'ignoring ', mode, scope.id, scope.modes
                continue

            print 'processing', scope.id, mask, modes

            # patch basic stat
            if 'basic' in mask and stats and 'patch_basic' in stats:
                self.patch_basic(mode, start, **scope)

            if 'book' in mask and stats and 'patch_book' in stats:
                self.patch_book(mode, start, **scope)

            # basic stat
            if 'basic' in mask and (not stats or 'basic' in stats):
                self.import_basic_stat(mode, start, **scope)

            # visit stat of page types
            if 'visit' in mask and (not stats or 'visit' in stats):
                self.import_visit_stat(mode, start, **scope)

            # topN of the page types
            if 'topn' in mask and (not stats or 'topn' in stats):
                self.import_topN(mode, start, **scope)

            # book stat
            if 'book' in mask and (not stats or 'book' in stats):
                self.import_book_stat(mode, start, num=0, **scope)
                self.import_bookrefer_stat(
                    mode, start,
                    ['search_main', 'search_button', 'hot_search_word'],
                    **scope)

            # machine stat
            if 'product' in mask and (not stats or 'product' in stats):
                self.import_product_stat(mode, start, num=9, **scope)

        # update new_user_run stat
        if not stats or 'update' in stats:
            self.update_new_user_run_stat(start, mode)
            self.update_zero_new_user_run_stat(
                start, mode, SHOULD_UPDATE_ZERO_NEW_USER_RUN_PARTNER_LIST)

        # logstatV2 -> logstatv3
        if not stats or 'basicv3' in stats:
            self.import_factory_database(mode, start)

        # merge basic to factory sum
        if not stats or 'factory' in stats:
            self.import_factory_stat(mode, start)

        # merge tag according to book stat
        if not stats or 'tag' in stats:
            self.import_toptag_bybook(mode, start)

        print mode, start, 'time:', datetime.datetime.now() - n0
        return True
Пример #47
0
 def category(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     charge_type = self.get_argument('charge_type','')
     order_field = self.get_argument('orderField','fee')
     cate_type = self.get_argument('cate_type','')
     assert order_field in ('pay_user','free_user','pay_down','free_down','pv','uv','fee','batch_fee','batch_pv','batch_uv')
     action = self.get_argument('action','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')   
     else:
         start = yest
     if start > yest:
         start = yest
     count,books = 0,[]
     res_all = {}
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(scope_id=scope.id,mode='day',start=start,end=tody,charge_type=charge_type)  
             q = q.orderby(order_field,'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field,'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         books = Service.inst().fill_book_info(books)
         res0,res1,res2 = self.counting_category_stat(books)
         #merge dict
         if not cate_type:
             res_all = dict(dict(res0,**res1),**res2)
         elif cate_type == 'cate0':
             res_all = res0
         elif cate_type == 'cate1':
             res_all = res1
         else:
             res_all = res2
         count = len(res_all.keys())
         #dict->list
         res_list = []
         for i in res_all:
             res_list.append({i:res_all[i]})
         #sort
         res_list = self.bubblesort_double_dict_in_list(res_list,'fee')
         self.render('data/book_category.html',
                     platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                     partner_id=partner_id,version_name=version_name,product_name=product_name,
                     run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                     count=count,res_list=res_list,charge_type=charge_type,cate_type=cate_type,
                     start=start.strftime('%Y-%m-%d')
                     )
Пример #48
0
    def productdaily(self):
        order_field = self.get_argument('orderField', 'user_run')
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        group = self.get_argument('group', '')
        psize = int(self.get_argument('numPerPage', 20))
        start = self.get_argument('start', '')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        action = self.get_argument('action', '')
        factory_id = int(self.get_argument('factory_id', 0))
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        query_mode = self.get_argument('query_mode', 'accounting_product')
        if not start:
            start = yest
        else:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], 'accounting_product')
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list) > 150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        else:
            start_time = datetime.datetime(2014, 12, 31)
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012, 12, 31)
            coefficient = 1.0

        if start_time:
            if start_time > start:
                start = start_time

        scopeid_product, model_list = {}, []
        if factory_id == -1:
            #partnerid_list = [0]
            partner_list = []
        for i in partner_list:
            scopeid_list = []
            if not product_name:
                scopes = Scope.mgr().Q().filter(
                    platform_id=platform_id,
                    run_id=run_id,
                    plan_id=plan_id,
                    partner_id=i['partner_id'],
                    version_name=version_name).extra("product_name<>''")
            else:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,
                                                run_id=run_id,
                                                plan_id=plan_id,
                                                partner_id=i['partner_id'],
                                                version_name=version_name,
                                                product_name=product_name)
            for scope in scopes:
                scopeid_list.append(scope.id)
                scopeid_product[scope.id] = scope.product_name
            result = BasicStat.mgr().get_one_day_model_data_proportion(
                i['proportion'], scopeid_list, 'day', start)
            model_list += result
        count = len(model_list)
        for model in model_list:
            model['product_name'] = scopeid_product[model['scope_id']]
            model['time'] = start.strftime('%Y-%m-%d')

        #find key_list
        if not model_list:
            tmp = []
        else:
            tmp = model_list[0].keys()
        key_list = [
            i for i in tmp if i not in ('scope_id', 'time', 'product_name')
        ]

        #find product_name_list
        tmp = []
        for model in model_list:
            tmp.append(model['product_name'])
        tmp_set = set(tmp)
        product_name_list = [i for i in tmp_set]

        #get dft res_list
        res_list = []
        for i in product_name_list:
            res_model = {}
            res_model['product_name'] = i
            res_model['time'] = start.strftime('%Y-%m-%d')
            for key in key_list:
                res_model[key] = 0
            res_list.append(res_model)
        #Cumulative data
        for model in model_list:
            for res in res_list:
                if res['product_name'] == model['product_name']:
                    for key in key_list:
                        res[key] += model[key]

        count = len(res_list)
        res_list = self.bubblesort(res_list, order_field)
        #Special factory Coefficient
        if factory_id in special_factory_list:
            statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
            sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d') >= statttime:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(
                                res[i]) * coefficient * sfc  # coefficient
                            res[i] = long(res[i])
            else:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(res[i]) * coefficient  # coefficient
                            res[i] = long(res[i])
        #Special factory Coefficient2
        if factory_id in special_factory_list2:
            statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d') >= statttime2:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(res[i]) * sfc2  # coefficient
                            res[i] = long(res[i])

        if action == 'export':
            title = [('product_name', '机型'), ('time', '时间'),
                     ('user_run', '启动用户'), ('new_user_run', '新增启动用户'),
                     ('user_visit', '访问用户'), ('new_user_visit', '新增访问用户'),
                     ('pay_user', '付费用户'), ('active_user_visit', '活跃用户'),
                     ('visits', '访问PV'), ('cpay_down', '按章付费数'),
                     ('bpay_down', '按本付费数'), ('cpay_user', '按章付费用户'),
                     ('bpay_user', '按本付费用户'), ('cfree_down', '按章免费数'),
                     ('bfree_down', '按本免费数'), ('cfree_user', '按章免费用户'),
                     ('bfree_user', '按本免费用户')]
            xls = Excel().generate(title, res_list, 1)
            filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            res_list = res_list[(page - 1) * psize:page * psize]
            self.render('data/accounting_productdaily.html',
                        count=count,
                        psize=psize,
                        page=page,
                        factory_id=factory_id,
                        query_mode=query_mode,
                        product_name=product_name,
                        basics=res_list,
                        start=start.strftime('%Y-%m-%d'),
                        factory_list=factory_list,
                        order_field=order_field)
Пример #49
0
 def bookcategory(self):
     platform_id = int(self.get_argument('platform_id',6))
     run_id = int(self.get_argument('run_id',0))
     plan_id = int(self.get_argument('plan_id',0))
     partner_id = int(self.get_argument('partner_id',0))
     version_name = self.get_argument('version_name','').replace('__','.')
     product_name = self.get_argument('product_name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     charge_type = self.get_argument('charge_type','')
     order_field = self.get_argument('orderField','fee')
     cate_type = self.get_argument('cate_type','')
     action = self.get_argument('action','')
     #perm
     run_list=self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]      
     if run_id == 0 and run_id_list: # has perm and doesn't select a run_id
         if len(run_id_list) == len(Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0: # don't has perm and selete a run_id
         scope = None
     else:
     # scope 
         scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                   partner_id=partner_id,version_name=version_name,product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start','')
     if start:
         start = datetime.datetime.strptime(start,'%Y-%m-%d')   
     else:
         start = yest
     if start > yest:
         start = yest
     count,books = 0,[]
     res_all = {}
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(scope_id=scope.id,mode='day',start=start,end=tody,charge_type=charge_type)  
             q = q.orderby(order_field,'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,mode='day',time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field,'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[:]
     books = self.get_fill_category_id(books)
     cates = self.counting_category(books)
     cates = self.fill_category_name(cates)
     cates = self.bubblesort(cates,'fee')        
     if action == 'export': 
         title = [('category_no3','类别'),('category_no2','二级分类'),('category_no1','三级分类'),('fee','阅饼消费'),('batch_fee','批量下载阅饼消费')]
         xls = Excel().generate(title,cates,1)
         filename = 'category.xls'
         self.set_header('Content-Disposition','attachment;filename=%s'%filename)
         self.finish(xls)
     else:
         self.render('data/book_category2.html',
                 platform_id=platform_id,run_id=run_id,plan_id=plan_id,date=yest.strftime('%Y-%m-%d'),
                 partner_id=partner_id,version_name=version_name,product_name=product_name,
                 run_list=self.run_list(),plan_list=self.plan_list(),page=page, psize=psize,
                 count=count,charge_type=charge_type,cates=cates,
                 start=start.strftime('%Y-%m-%d'))
Пример #50
0
 def book_stat(self):
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     charge_type = self.get_argument('charge_type', '')
     factory_id = int(self.get_argument('factory_id', 0))
     order_field = self.get_argument('orderField', 'fee')
     query_mode = self.get_argument('query_mode', 'accounting_book')
     group = self.get_argument('group', '')
     assert order_field in ('pay_user', 'free_user', 'pay_down',
                            'free_down', 'pv', 'uv', 'fee', 'batch_fee',
                            'batch_pv', 'batch_uv', 'real_fee')
     action = self.get_argument('action', '')
     # perm
     q = Factory.mgr().Q()
     group and q.filter(group=group)
     factory_list = self.filter_factory_acc(q[:], query_mode)
     # for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
     cuser = self.get_current_user()
     if len(factory_list) > 150 and (cuser['name'] not in SAFE_USER):
         factory_list = []
     if factory_id == 0 and factory_list:
         factory_id = factory_list[0].id
     partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
     # scope list
     scopeid_list = []
     for i in partner_list:
         scope = Scope.mgr().Q().filter(platform_id=6,
                                        run_id=0,
                                        partner_id=i.partner_id)[0]
         if scope:
             scopeid_list.append(scope.id)
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start', '')
     if start:
         start = datetime.datetime.strptime(start, '%Y-%m-%d')
     else:
         start = yest
     count, books = 0, []
     q = BookStat.mgr().get_accouting_multy_scope_stat(
         scopeid_list, order_field, start)
     charge_type and q.filter(charge_type=charge_type)
     q = q.orderby(order_field, 'DESC')
     count = len(q)
     if action == 'export':
         books = q.data()
         for i in books:
             i['batch_fee'] = "%0.2f" % i['batch_fee']
     else:
         books = q[(page - 1) * psize:page * psize]
         for i in books:
             i['batch_fee'] = "%0.2f" % i['batch_fee']
     books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count + psize - 1) / psize
     for book in books:
         book['fee'] = "%.01f" % float(book['fee'])
         book['real_fee'] = "%.01f" % float(book['real_fee'])
     if action == 'export':
         books = Service.inst().fill_book_count_info(books)
         while (self.do_books_have_two_or_empty_title(books)):
             books = self.remove_books_two_or_empty_title(books)
         title = [('time', '时间'), ('book_id', '书ID'), ('name', '书名'),
                  ('author', '作者'), ('cp', '版权'), ('category_2', '类别'),
                  ('category_1', '子类'), ('category_0', '三级分类'),
                  ('state', '状态'), ('charge_type', '计费类型'), ('fee', '收益'),
                  ('real_fee', '主账户收益'), ('pay_down', '付费下载数'),
                  ('pay_user', '付费下载用户数'), ('free_down', '免费下载数'),
                  ('free_user', '免费下载用户数'), ('pv', '简介访问数'),
                  ('uv', '简介访问人数'), ('batch_fee', '批量订购阅饼消费')]
         xls = Excel().generate(title, books, 1)
         filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
         self.set_header('Content-Disposition',
                         'attachment;filename=%s' % filename)
         self.finish(xls)
     else:
         self.render('data/accounting_book.html',
                     page=page,
                     psize=psize,
                     count=count,
                     page_count=page_count,
                     books=books,
                     charge_type=charge_type,
                     order_field=order_field,
                     start=start.strftime('%Y-%m-%d'),
                     factory_id=factory_id,
                     factory_list=factory_list,
                     query_mode=query_mode)
Пример #51
0
 def index(self):
     platform_id = int(self.get_argument('platform_id', 6))
     run_id = int(self.get_argument('run_id', 0))
     plan_id = int(self.get_argument('plan_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     version_name = self.get_argument('version_name', '').replace('__', '.')
     product_name = self.get_argument('product_name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     charge_type = self.get_argument('charge_type', '')
     order_field = self.get_argument('orderField', 'fee')
     assert order_field in ('pay_user', 'free_user', 'pay_down',
                            'free_down', 'pv', 'uv', 'fee', 'batch_fee',
                            'batch_pv', 'batch_uv', 'real_fee')
     action = self.get_argument('action', '')
     #perm
     run_list = self.run_list()
     run_list = self.filter_run_id_perms(run_list=run_list)
     run_id_list = [run['run_id'] for run in run_list]
     if run_id == 0 and run_id_list:  # has perm and doesn't select a run_id
         if len(run_id_list) == len(
                 Run.mgr().Q().extra("status<>'hide'").data()):
             run_id = 0  # user has all run_id perms
         else:
             run_id = run_id_list[0]
     if run_id not in run_id_list and run_id != 0:  # don't has perm and selete a run_id
         scope = None
     else:
         # scope
         scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                        run_id=run_id,
                                        plan_id=plan_id,
                                        partner_id=partner_id,
                                        version_name=version_name,
                                        product_name=product_name)[0]
     tody = self.get_date(1) + datetime.timedelta(days=1)
     yest = tody - datetime.timedelta(days=1)
     last = yest - datetime.timedelta(days=1)
     start = self.get_argument('start', '')
     if start:
         start = datetime.datetime.strptime(start, '%Y-%m-%d')
     else:
         start = yest
     if start > yest:
         start = yest
     count, books = 0, []
     if scope:
         if start != yest:
             q = BookStat.mgr().get_data_by_multi_days(
                 scope_id=scope.id,
                 mode='day',
                 start=start,
                 end=tody,
                 charge_type=charge_type)
             q = q.orderby(order_field, 'DESC')
             count = len(q)
             if action == 'export':
                 books = q.data()
             else:
                 books = q[(page - 1) * psize:page * psize]
         else:
             q = BookStat.mgr().Q(time=yest).filter(scope_id=scope.id,
                                                    mode='day',
                                                    time=yest)
             charge_type and q.filter(charge_type=charge_type)
             count = q.count()
             q = q.orderby(order_field, 'DESC')
             if action == 'export':
                 books = q.data()
             else:
                 books = q[(page - 1) * psize:page * psize]
         books = Service.inst().fill_book_info(books)
     # pagination
     page_count = (count + psize - 1) / psize
     if action == 'export':
         books = Service.inst().fill_book_count_info(books)
         while (self.do_books_have_two_or_empty_title(books)):
             books = self.remove_books_two_or_empty_title(books)
         title = [('time', '时间'), ('book_id', '书ID'), ('name', '书名'),
                  ('author', '作者'), ('cp', '版权'), ('category_2', '类别'),
                  ('category_1', '子类'), ('category_0', '三级分类'),
                  ('state', '状态'), ('charge_type', '计费类型'), ('fee', '收益'),
                  ('pay_down', '付费下载数'), ('pay_user', '付费下载用户数'),
                  ('free_down', '免费下载数'), ('free_user', '免费下载用户数'),
                  ('pv', '简介访问数'), ('uv', '简介访问人数'),
                  ('batch_fee', '批量订购阅饼消费'), ('batch_pv', '批量订购PV'),
                  ('batch_uv', '批量订购UV'), ('chapterCount', '总章数'),
                  ('wordCount', '总字数')]
         xls = Excel().generate(title, books, 1)
         filename = 'book_%s.xls' % (yest.strftime('%Y-%m-%d'))
         self.set_header('Content-Disposition',
                         'attachment;filename=%s' % filename)
         self.finish(xls)
     else:
         self.render('data/book.html',
                     platform_id=platform_id,
                     run_id=run_id,
                     plan_id=plan_id,
                     date=yest.strftime('%Y-%m-%d'),
                     partner_id=partner_id,
                     version_name=version_name,
                     product_name=product_name,
                     run_list=self.run_list(),
                     plan_list=self.plan_list(),
                     page=page,
                     psize=psize,
                     count=count,
                     page_count=page_count,
                     books=books,
                     charge_type=charge_type,
                     order_field=order_field,
                     start=start.strftime('%Y-%m-%d'))