def list(self):
     id = int(self.get_argument('id',0))
     name = self.get_argument('name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     if not name:
         q = AccountingFactoryStart.mgr().Q()
     else:
         fid = Factory.mgr().Q().extra("name LIKE '%%%s%%'"%name)
         if fid:
             fidlist = ','.join([str(i['id']) for i in fid])
             q = AccountingFactoryStart.mgr().Q().extra("factory_id in (%s)"%fidlist)
         else:
             q = []
     count = len(q)
     factories = q[(page-1)*psize:page*psize]
     for i in factories:
         i['time'] = i['time'].strftime('%Y-%m-%d')
     if factories:
         for i in factories:
             try: 
                 factory_name = Factory.mgr().Q().filter(id=i.factory_id)[0]['name']
             except Exception, e:
                 factory_name = ''
             i['factory_name'] = factory_name
Пример #2
0
 def list(self):
     id = int(self.get_argument('id', 0))
     factory_id = int(self.get_argument('factory_id', 0))
     partner_id = int(self.get_argument('partner_id', 0))
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     q = Partner.mgr().Q()
     count = q.count()
     page_count = (count + psize - 1) / psize
     id and q.filter(id=id)
     factory_id and q.filter(factory_id=factory_id)
     partner_id and q.filter(partner_id=partner_id)
     partners = q[(page - 1) * psize:page * psize]
     for i in partners:
         i.factory = Factory.mgr().one(i.factory_id)
     factories = Factory.mgr().Q().data()
     self.render('data/partner_list.html',
                 partner_id=partner_id,
                 factory_id=factory_id,
                 page=page,
                 psize=psize,
                 count=count,
                 page_count=page_count,
                 partners=partners,
                 factories=factories)
Пример #3
0
 def list(self):
     id = int(self.get_argument('id', 0))
     name = self.get_argument('name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     if not name:
         q = AccountingFactoryStart.mgr().Q()
     else:
         fid = Factory.mgr().Q().extra("name LIKE '%%%s%%'" % name)
         if fid:
             fidlist = ','.join([str(i['id']) for i in fid])
             q = AccountingFactoryStart.mgr().Q().extra(
                 "factory_id in (%s)" % fidlist)
         else:
             q = []
     count = len(q)
     factories = q[(page - 1) * psize:page * psize]
     for i in factories:
         i['time'] = i['time'].strftime('%Y-%m-%d')
     if factories:
         for i in factories:
             try:
                 factory_name = Factory.mgr().Q().filter(
                     id=i.factory_id)[0]['name']
             except Exception, e:
                 factory_name = ''
             i['factory_name'] = factory_name
Пример #4
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
Пример #5
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
 def search(self):
     name = self.get_argument('factory_name','')
     resource = self.get_argument('resource','accounting_basic')
     q = Factory.mgr().Q(qtype='select id,id as factory_id,name as factory_name,`group`')
     name and q.extra("name LIKE '%%%s%%'"%name)
     factories = self.filter_factory_acc(q.data(),resource)[:10]
     self.write(ujson.dumps(factories))
Пример #7
0
 def edit(self):
     id = int(self.get_argument('id'))
     partner = Partner.mgr(ismaster=1).one(id)
     factories = Factory.mgr().Q().data()
     self.render('data/partner_edit.html',
                 partner=partner,
                 factories=factories)
Пример #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 search(self):
     name = self.get_argument('factory_name', '')
     resource = self.get_argument('resource', 'accounting_basic')
     q = Factory.mgr().Q(
         qtype='select id,id as factory_id,name as factory_name,`group`')
     name and q.extra("name LIKE '%%%s%%'" % name)
     factories = self.filter_factory_acc(q.data(), resource)[:10]
     self.write(ujson.dumps(factories))
Пример #10
0
    def factorystat(self):
        plan_id = int(self.get_argument('plan_id',0))
        partner_id = int(self.get_argument('partner_id',0))
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        group = self.get_argument('group','') 
        factory_input_name = self.get_argument('factory_name','')

        #factory_name
        q = Factory.mgr().Q()
        factory_list = self.filter_wap_factory(q[:])
        factory_name_list = [str(i['name']) for i in factory_list]
        if factory_input_name != '':
            if factory_input_name in factory_name_list:
                factory_name = factory_input_name
            else:
                factory_name = None
        else:
            if len(factory_list) == 1:
                factory_name = factory_list[0]['name']
            else:
                factory_name = ''

        #datatime
        start = self.get_argument('start','')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start,'%Y-%m-%d')
        end = yest 
        
        count,stats = 0,[]
        stats = WapFactory.mgr().get_facotry_stat_multi_days(start,end)
        if factory_name != '':
            stats = stats.filter(factory_name=factory_name)[:]
            if not stats:
                acc = {'first_visits': 0, 'fee': 0.0, 'factory_name': '', 'time': '总和', 'pay_user': 0, 'visits': 0}
            else:
                acc = {'first_visits': 0, 'fee': 0.0, 'factory_name': stats[0]['factory_name'], 'time': '总和', 'pay_user': 0, 'visits': 0}
                for stat in stats:
                    acc['first_visits'] += stat['first_visits']
                    acc['fee'] += stat['fee']
                    acc['pay_user'] += stat['pay_user']
                    acc['visits'] += stat['visits']
            stats.append(acc)

        count = len(stats) 
        stats = stats[(page-1)*psize:page*psize]
        for stat in stats:
            stat['fee'] = "%.02f" % stat['fee']
            if stat['time'] != '总和':
                stat['time'] = stat['time'].strftime('%Y-%m-%d')
        self.render('data/wapfactory.html',
            factory_name=factory_name,start=start.strftime('%Y-%m-%d'),date=end.strftime('%Y-%m-%d'),
            page=page,psize=psize,count=count,stats=stats)
Пример #11
0
 def save(self):
     id = int(self.get_argument('id', 0))
     name = self.get_argument('name')
     group = self.get_argument('group')
     email = self.get_argument('email', '')
     phone = self.get_argument('phone', '')
     intro = self.get_argument('intro', '')
     status = self.get_argument('status')
     if id:
         f = Factory.mgr(ismaster=True).Q().filter(id=id)[0]
     else:
         f = Factory.new()
     f.name, f.group, f.email, f.phone, f.intro, f.status = name, group, email, phone, intro, status
     f = f.save()
     self.json2dwz('200',
                   'closeCurrent',
                   'dlist',
                   forwardUrl='factory/list?id=%s' % f.id)
Пример #12
0
 def list(self):
     id = int(self.get_argument('id', 0))
     name = self.get_argument('name', '')
     group = self.get_argument('group', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     q = Factory.mgr().Q()
     id and q.filter(id=id)
     name and q.extra("name LIKE '%%%s%%'" % name)
     group and q.filter(group=group)
     count = q.count()
     factories = q[(page - 1) * psize:page * psize]
     groups = Factory.mgr().all_groups()
     self.render('data/factory_list.html',
                 name=name,
                 group=group,
                 page=page,
                 psize=psize,
                 count=count,
                 factories=factories,
                 groups=groups)
Пример #13
0
    def factorystat_bak(self):
        plan_id = int(self.get_argument('plan_id', 0))
        partner_id = int(self.get_argument('partner_id', 0))
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        group = self.get_argument('group', '')
        factory_input_name = self.get_argument('factory_name', '')

        #factory_name
        q = Factory.mgr().Q()
        factory_list = self.filter_wap_factory(q[:])
        factory_name_list = [str(i['name']) for i in factory_list]
        if factory_input_name != '':
            if factory_input_name in factory_name_list:
                factory_name = factory_input_name
            else:
                factory_name = None
        else:
            if len(factory_list) == 1:
                factory_name = factory_list[0]['name']
            else:
                factory_name = ''

        #datatime
        start = self.get_argument('start', '')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        end = yest

        count, stats = 0, []
        stats = WapFactory.mgr().get_facotry_stat_multi_days(start, end)
        if factory_name != '':
            stats = stats.filter(factory_name=factory_name)
        count = len(stats)
        stats = stats[(page - 1) * psize:page * psize]
        for stat in stats:
            stat['fee'] = "%.02f" % stat['fee']
            stat['time'] = stat['time'].strftime('%Y-%m-%d')
        self.render('data/wapfactory.html',
                    factory_name=factory_name,
                    start=start.strftime('%Y-%m-%d'),
                    date=end.strftime('%Y-%m-%d'),
                    page=page,
                    psize=psize,
                    count=count,
                    stats=stats)
def merge_perm_attr_sql():
    '''
    from perm and perm.name find factory id 
    generate perm_attr insert sql
    '''
    ext = "uptime >='2013-12-01' AND id NOT IN (SELECT perm_id FROM perm_attr) and id!=262"
    perms = Perm.mgr().Q().extra(ext)
    if perms:
        for i in perms:
            fname = i['name'].split('-')[3]
            factorys = Factory.mgr().Q().filter(name=fname)
            if not factorys:
                print fname
            i['factory_id'] = factorys[0]['id']
# merge sql
    for i in perms:
        sql1 = "insert into `logstatv3`.`perm_attr`(`id`,`perm_id`,`attr_name`,`attr_val`,`uptime`) values ( NULL,'%s','factory_id','%s',CURRENT_TIMESTAMP);"%(i['id'],i['factory_id'])
        sql2 = "insert into `logstatv3`.`perm_attr`(`id`,`perm_id`,`attr_name`,`attr_val`,`uptime`) values ( NULL,'%s','group','all',CURRENT_TIMESTAMP);"%(i['id'])
        print sql1
        print sql2
Пример #15
0
 def get_wap_factory_stat(self, start, end):
     '''
     get wap factory stat
     start: start time
     end: end time
     '''
     time = start
     stat = []
     q = Factory.mgr().Q()
     factory_list = q[:]
     if factory_list:
         for factory in factory_list:
             partner_list = Partner.mgr().Q().filter(factory_id=factory['id'])[:]
             partner_id_list = [p['partner_id'] for p in partner_list]         
             res = self.get_stat_by_partner_id_list(partner_id_list,time)
             if res:                     
             #针对factory表里有,但partner表里没有对应factory_id的情况 pass
                 res['factory_name'] = factory['name']
                 res['factory_id'] = factory['id']
                 res['partner_id_list'] = partner_id_list
                 stat.append(res)
     return stat
Пример #16
0
 def get_wap_factory_stat(self, start, end):
     '''
     get wap factory stat
     start: start time
     end: end time
     '''
     time = start
     stat = []
     q = Factory.mgr().Q()
     factory_list = q[:]
     if factory_list:
         for factory in factory_list:
             partner_list = Partner.mgr().Q().filter(
                 factory_id=factory['id'])[:]
             partner_id_list = [p['partner_id'] for p in partner_list]
             res = self.get_stat_by_partner_id_list(partner_id_list, time)
             if res:
                 #针对factory表里有,但partner表里没有对应factory_id的情况 pass
                 res['factory_name'] = factory['name']
                 res['factory_id'] = factory['id']
                 res['partner_id_list'] = partner_id_list
                 stat.append(res)
     return stat
Пример #17
0
 def save(self):
     id = int(self.get_argument('pid', 0))
     #id = int(self.get_argument('id',0))
     #factory_id = int(self.get_argument('factory_id'))
     partner_id = int(self.get_argument('partner_id'))
     factory_name = self.get_argument('factory_name')
     factory = Factory.mgr().Q().filter(name=factory_name)
     factory_id = factory[0]['id']
     if id:
         p = Partner.mgr(ismaster=True).Q().filter(id=id)[0]
         p2 = Partnerv2.mgr(ismaster=True).Q().filter(
             factory_id=p['factory_id'])[0]
     else:
         p = Partner.new()
         p2 = Partnerv2.new()
     p.factory_id, p.partner_id = factory_id, partner_id
     p2.factory_id, p2.partner_id = factory_id, partner_id
     p = p.save()
     p2.save()
     self.json2dwz('200',
                   'closeCurrent',
                   'dlist',
                   forwardUrl='partner/list?id=%s' % p.id)
Пример #18
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)
def merge_perm_attr_sql():
    '''
    from perm and perm.name find factory id 
    generate perm_attr insert sql
    '''
    ext = "uptime >='2013-12-01' AND id NOT IN (SELECT perm_id FROM perm_attr) and id!=262"
    perms = Perm.mgr().Q().extra(ext)
    if perms:
        for i in perms:
            fname = i['name'].split('-')[3]
            factorys = Factory.mgr().Q().filter(name=fname)
            if not factorys:
                print fname
            i['factory_id'] = factorys[0]['id']


# merge sql
    for i in perms:
        sql1 = "insert into `logstatv3`.`perm_attr`(`id`,`perm_id`,`attr_name`,`attr_val`,`uptime`) values ( NULL,'%s','factory_id','%s',CURRENT_TIMESTAMP);" % (
            i['id'], i['factory_id'])
        sql2 = "insert into `logstatv3`.`perm_attr`(`id`,`perm_id`,`attr_name`,`attr_val`,`uptime`) values ( NULL,'%s','group','all',CURRENT_TIMESTAMP);" % (
            i['id'])
        print sql1
        print sql2
Пример #20
0
    def businessdaily(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_business')
        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)
        order_field = self.get_argument('orderField','')

        # 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)
        #get facotry start 
        for fact in factory_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=fact['id'])
            if q_start:
                fact['start_time'] = q_start[0]['time']
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012,12,31)
                fact['coefficient'] = q_start[0]['coefficient']
            else:
                fact['start_time'] = None
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012,12,31)
                fact['coefficient'] = 1.0

        basics = []
        count = len(factory_list)
        
        #all_stats = Service.inst().stat.get_all_factstat('day',yest,tody)
        all_stats = Service.inst().stat.get_all_factstat_proportion('day',yest,tody)
        for basic in all_stats:
            all_stats[basic]['sumfee'] = float(all_stats[basic]['consumefee']) + float(all_stats[basic]['msgfee'])
        
        acc_stats = {}
        for i in factory_list:
            factory_id = i['id']
            if not i['start_time']: #start_time
                basic = None
            elif i['start_time'] > yest: 
                basic = None
            else:
                basic = all_stats.get(i.id,None)
            
            if basic:
                if factory_id in special_factory_list:
                    statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
                    sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * i['coefficient'] * sfc  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)
                else:
                    for k in basic:
                        basic[k] = float(basic[k]) * i['coefficient'] #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)
                if factory_id in special_factory_list2:
                    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
                    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * sfc2  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)

                basic['title'] = i.name
                basics.append(basic)
        acc_stats['title'] = '总计'
        if order_field:
            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/accounting_businessdaily.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)
Пример #21
0
    def businessmonthly(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','')
        month = self.get_argument('month','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','accounting_business')
        group = self.get_argument('group','')
        action = self.get_argument('action','') 
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        last_month = tody - datetime.timedelta(days=30)
        last_month = last_month.strftime('%Y-%m')
        order_field = self.get_argument('orderField','')
        if not month or month == '未选择':
            month = last_month
        start = month + '-01'
        start = datetime.datetime.strptime(start,'%Y-%m-%d')
        next_month_start = start + datetime.timedelta(days=monthrange(start.year,start.month)[1])
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],query_mode)
        basics = []
        stats = []
        
        for i in factory_list:
            factory_id = i['id']
            partner_list = Partnerv2.mgr().Q().filter(factory_id=i.id)[:]
            if not partner_list:
                continue
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
            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']
            
            if not partner_list:
                continue
            if not start_time or start_time >= next_month_start: #start_time
                stat = None
                continue
            elif start_time > start:
                start = start_time
            #stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days(start,next_month_start,partner_list)[0]
            stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days_proportion(start,next_month_start,partner_list)
            # Special factory 
            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:
                    stat['fee'] = float(stat['fee']) * sfc 
            # Special factory2
            #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:
            #        stat['fee'] = float(stat['fee']) * sfc2 
            
           
            if stat['fee'] != None:
                stat['fee'] = int(stat['fee'])
                stat['factory'] = i.name
            else:
                stat['fee'] = '0'
                stat['factory'] = i.name
            
            # Special factory ...
            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-01-01': 
                stat['fee'] = 483471
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 966997

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-02-01': 
                stat['fee'] = 778841
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 1090431

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-03-01': 
                stat['fee'] = 1027234
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1438187

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-04-01': 
                stat['fee'] = 1003872
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1405475



            stats.append(stat)
        if order_field:
            stats.sort(cmp=lambda x,y:cmp(x[order_field],y[order_field]),reverse=True)
        
        count = len(stats)
        groups = Factory.mgr().all_groups()
        if action == 'export':
            excel_title = [('factory','厂商名称'),('fee','月收入')]
            xls = Excel().generate(excel_title,stats,1)
            filename = 'accounting_bueiness_monthly_%s.xls' % (start.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/accounting_businessmonthly.html',
                count=count,psize=psize,page=page,query_mode=query_mode,stats=stats,monthly=MONTHLY,
                group=group,groups=groups,month=month,order_field=order_field)
Пример #22
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)
Пример #23
0
 def delete(self):
     id = int(self.get_argument('id'))
     factory = Factory.mgr(ismaster=1).one(id)
     factory.delete()
     self.json2dwz('200', 'forward', 'dlist', forwardUrl='factory/list')
Пример #24
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)
Пример #25
0
    def businessmonthly(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', '')
        month = self.get_argument('month', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', 'accounting_business')
        group = self.get_argument('group', '')
        action = self.get_argument('action', '')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        last_month = tody - datetime.timedelta(days=30)
        last_month = last_month.strftime('%Y-%m')
        order_field = self.get_argument('orderField', '')
        if not month or month == '未选择':
            month = last_month
        start = month + '-01'
        start = datetime.datetime.strptime(start, '%Y-%m-%d')
        next_month_start = start + datetime.timedelta(
            days=monthrange(start.year, start.month)[1])
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], query_mode)
        basics = []
        stats = []

        for i in factory_list:
            factory_id = i['id']
            partner_list = Partnerv2.mgr().Q().filter(factory_id=i.id)[:]
            if not partner_list:
                continue
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
            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']

            if not partner_list:
                continue
            if not start_time or start_time >= next_month_start:  #start_time
                stat = None
                continue
            elif start_time > start:
                start = start_time
            #stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days(start,next_month_start,partner_list)[0]
            stat = MonthlyBusinessStatDaily.mgr(
            ).get_monthly_busiess_stat_multy_days_proportion(
                start, next_month_start, partner_list)
            # Special factory
            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:
                    stat['fee'] = float(stat['fee']) * sfc
            # Special factory2
            #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:
            #        stat['fee'] = float(stat['fee']) * sfc2

            if stat['fee'] != None:
                stat['fee'] = int(stat['fee'])
                stat['factory'] = i.name
            else:
                stat['fee'] = '0'
                stat['factory'] = i.name

            # Special factory ...
            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 483471
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 966997

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 778841
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 1090431

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1027234
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1438187

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1003872
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1405475

            stats.append(stat)
        if order_field:
            stats.sort(cmp=lambda x, y: cmp(x[order_field], y[order_field]),
                       reverse=True)

        count = len(stats)
        groups = Factory.mgr().all_groups()
        if action == 'export':
            excel_title = [('factory', '厂商名称'), ('fee', '月收入')]
            xls = Excel().generate(excel_title, stats, 1)
            filename = 'accounting_bueiness_monthly_%s.xls' % (
                start.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            self.render('data/accounting_businessmonthly.html',
                        count=count,
                        psize=psize,
                        page=page,
                        query_mode=query_mode,
                        stats=stats,
                        monthly=MONTHLY,
                        group=group,
                        groups=groups,
                        month=month,
                        order_field=order_field)
Пример #26
0
 def add(self):
     factories = Factory.mgr().Q().data()
     self.render('data/partner_add.html', factories=factories)
Пример #27
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
                )
Пример #28
0
 def add(self):
     groups = Factory.mgr().all_groups()
     self.render('data/factory_add.html', groups=groups)
Пример #29
0
    start,end = day,day+datetime.timedelta(days=1)
    all_stats = Service.inst().stat.get_all_factstat('day',start,end)
    acc_stats = {}
    for u in factory_dict:
        for f in factory_dict[u]:
            basic = all_stats.get(f.id,None)
            if basic:
                if u in result:
                    result[u] += basic['new_user_run']
                else:
                    result[u] = basic['new_user_run']
    print 'end...',day

if __name__ == "__main__":
    factory_dict = {}
    factory_dict['高兵'] = Factory.mgr().Q().filter(group='高兵').extra("name<>'OPPO智能' AND name<>'OPPO市场'").data()
    factory_dict['廖凯'] = Factory.mgr().Q().filter(group='廖凯').data()
    factory_dict['柳成峰'] = Factory.mgr().Q().filter(group='柳成峰').data()
    factory_dict['贾生亭'] = Factory.mgr().Q().filter(group='贾生亭').data()
    factory_dict['高猛'] = Factory.mgr().Q().filter(group='高猛').data()
    factory_dict['张夏'] = Factory.mgr().Q().filter(group='张夏').data()
    factory_dict['陈少雄'] = Factory.mgr().Q().filter(group='陈少雄').data()
    factory_dict['张婧'] = Factory.mgr().Q().filter(group='张婧').data()
    factory_dict['徐超'] = Factory.mgr().Q().filter(group='徐超').data()
    factory_dict['王超'] = Factory.mgr().Q().filter(group='王超').data()
    factory_dict['吴伟'] = Factory.mgr().Q().filter(group='吴伟').data()
    factory_dict['贾怀青'] = Factory.mgr().Q().filter(group='贾怀青').data()
    factory_dict['OPPO智能'] = Factory.mgr().Q().filter(name='OPPO智能').data()
    factory_dict['OPPO市场'] = Factory.mgr().Q().filter(name='OPPO市场').data()
    result = {}
    #在这里改时间
Пример #30
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)
Пример #31
0
    all_stats = Service.inst().stat.get_all_factstat('day', start, end)
    acc_stats = {}
    for u in factory_dict:
        for f in factory_dict[u]:
            basic = all_stats.get(f.id, None)
            if basic:
                if u in result:
                    result[u] += basic['new_user_run']
                else:
                    result[u] = basic['new_user_run']
    print 'end...', day


if __name__ == "__main__":
    factory_dict = {}
    factory_dict['高兵'] = Factory.mgr().Q().filter(
        group='高兵').extra("name<>'OPPO智能' AND name<>'OPPO市场'").data()
    factory_dict['廖凯'] = Factory.mgr().Q().filter(group='廖凯').data()
    factory_dict['柳成峰'] = Factory.mgr().Q().filter(group='柳成峰').data()
    factory_dict['贾生亭'] = Factory.mgr().Q().filter(group='贾生亭').data()
    factory_dict['高猛'] = Factory.mgr().Q().filter(group='高猛').data()
    factory_dict['张夏'] = Factory.mgr().Q().filter(group='张夏').data()
    factory_dict['陈少雄'] = Factory.mgr().Q().filter(group='陈少雄').data()
    factory_dict['张婧'] = Factory.mgr().Q().filter(group='张婧').data()
    factory_dict['徐超'] = Factory.mgr().Q().filter(group='徐超').data()
    factory_dict['王超'] = Factory.mgr().Q().filter(group='王超').data()
    factory_dict['吴伟'] = Factory.mgr().Q().filter(group='吴伟').data()
    factory_dict['贾怀青'] = Factory.mgr().Q().filter(group='贾怀青').data()
    factory_dict['OPPO智能'] = Factory.mgr().Q().filter(name='OPPO智能').data()
    factory_dict['OPPO市场'] = Factory.mgr().Q().filter(name='OPPO市场').data()
    result = {}
    #在这里改时间
Пример #32
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)
Пример #33
0
    def businessdaily(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_business')
        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)
        order_field = self.get_argument('orderField', '')

        # 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)
        #get facotry start
        for fact in factory_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=fact['id'])
            if q_start:
                fact['start_time'] = q_start[0]['time']
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012, 12, 31)
                fact['coefficient'] = q_start[0]['coefficient']
            else:
                fact['start_time'] = None
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012, 12, 31)
                fact['coefficient'] = 1.0

        basics = []
        count = len(factory_list)

        #all_stats = Service.inst().stat.get_all_factstat('day',yest,tody)
        all_stats = Service.inst().stat.get_all_factstat_proportion(
            'day', yest, tody)
        for basic in all_stats:
            all_stats[basic]['sumfee'] = float(
                all_stats[basic]['consumefee']) + float(
                    all_stats[basic]['msgfee'])

        acc_stats = {}
        for i in factory_list:
            factory_id = i['id']
            if not i['start_time']:  #start_time
                basic = None
            elif i['start_time'] > yest:
                basic = None
            else:
                basic = all_stats.get(i.id, None)

            if basic:
                if factory_id in special_factory_list:
                    statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                        'starttime']
                    sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                        'coefficient']
                    for k in basic:
                        basic[k] = float(
                            basic[k]) * i['coefficient'] * sfc  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)
                else:
                    for k in basic:
                        basic[k] = float(
                            basic[k]) * i['coefficient']  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)
                if factory_id in special_factory_list2:
                    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                        'starttime']
                    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                        'coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * sfc2  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)

                basic['title'] = i.name
                basics.append(basic)
        acc_stats['title'] = '总计'
        if order_field:
            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/accounting_businessdaily.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)
Пример #34
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')
                 )
Пример #35
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)
Пример #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))
        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)
Пример #37
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
                     )
Пример #38
0
 def edit(self):
     id = int(self.get_argument('id'))
     factory = Factory.mgr(ismaster=1).one(id)
     groups = Factory.mgr().all_groups()
     self.render('data/factory_edit.html', factory=factory, groups=groups)