# -*- coding: utf-8 -*- #!/usr/bin/env python import os import sys PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) from script.base import get_logger logger = get_logger(__name__) os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"}) import django django.setup() from apilib.utils_datetime import to_datetime from apps.web.user.models import ConsumeRecord,MyUser,RechargeRecord from apilib.monetary import RMB #统计脚本 #全网有多少用户 def stat_user(queryDict): objs = MyUser.get_collection().find(queryDict,{'sex':1,'openId':1}) openIdList = [] allCount,maleCount,femaleCount = 0,0,0 for obj in objs: sex = obj.get('sex',0) openIdList.append(obj.get('openId','')) allCount += 1 if sex == 0: femaleCount += 1 elif sex == 1: maleCount += 1 return len(set(openIdList)),maleCount,femaleCount,allCount print u'正在分析,全网有多少用户,请您稍等......' userCount,maleCount,femaleCount,allCount = stat_user({}) print u'全网共有:%s用户,男:%s,女:%s,总的记录条数:%s' % (userCount,maleCount,femaleCount,allCount) print u'正在计算月度活跃用户数目......' startTime = to_datetime('2019-08-28 00:00:00') endTime = to_datetime('2019-09-28 00:00:00') userCount,maleCount,femaleCount,allCount = stat_user({'last_login':{'$gte':startTime,'$lte':endTime}}) print u'月度共有:%s用户,男:%s,女:%s,总的记录条数:%s' % (userCount,maleCount,femaleCount,allCount) print u'正在计算月度消费总量......' objs = ConsumeRecord.get_collection().find({'dateTimeAdded':{'$gte':startTime,'$lte':endTime},'isNormal':True}) print u'月度一共有:%s单' % objs.count() print u'正在计算月度消费总量......' objs = RechargeRecord.objects.filter(dateTimeAdded__gte=startTime,dateTimeAdded__lte=endTime,result= 'success') allMoney = RMB(0.0) for obj in objs: allMoney = allMoney + obj.money print u'月度一共有:%s元' % allMoney