stat_user.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os
  4. import sys
  5. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  6. sys.path.insert(0, PROJECT_ROOT)
  7. from script.base import get_logger
  8. logger = get_logger(__name__)
  9. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
  10. import django
  11. django.setup()
  12. from apilib.utils_datetime import to_datetime
  13. from apps.web.user.models import ConsumeRecord,MyUser,RechargeRecord
  14. from apilib.monetary import RMB
  15. #统计脚本
  16. #全网有多少用户
  17. def stat_user(queryDict):
  18. objs = MyUser.get_collection().find(queryDict,{'sex':1,'openId':1})
  19. openIdList = []
  20. allCount,maleCount,femaleCount = 0,0,0
  21. for obj in objs:
  22. sex = obj.get('sex',0)
  23. openIdList.append(obj.get('openId',''))
  24. allCount += 1
  25. if sex == 0:
  26. femaleCount += 1
  27. elif sex == 1:
  28. maleCount += 1
  29. return len(set(openIdList)),maleCount,femaleCount,allCount
  30. print u'正在分析,全网有多少用户,请您稍等......'
  31. userCount,maleCount,femaleCount,allCount = stat_user({})
  32. print u'全网共有:%s用户,男:%s,女:%s,总的记录条数:%s' % (userCount,maleCount,femaleCount,allCount)
  33. print u'正在计算月度活跃用户数目......'
  34. startTime = to_datetime('2019-08-28 00:00:00')
  35. endTime = to_datetime('2019-09-28 00:00:00')
  36. userCount,maleCount,femaleCount,allCount = stat_user({'last_login':{'$gte':startTime,'$lte':endTime}})
  37. print u'月度共有:%s用户,男:%s,女:%s,总的记录条数:%s' % (userCount,maleCount,femaleCount,allCount)
  38. print u'正在计算月度消费总量......'
  39. objs = ConsumeRecord.get_collection().find({'dateTimeAdded':{'$gte':startTime,'$lte':endTime},'isNormal':True})
  40. print u'月度一共有:%s单' % objs.count()
  41. print u'正在计算月度消费总量......'
  42. objs = RechargeRecord.objects.filter(dateTimeAdded__gte=startTime,dateTimeAdded__lte=endTime,result= 'success')
  43. allMoney = RMB(0.0)
  44. for obj in objs:
  45. allMoney = allMoney + obj.money
  46. print u'月度一共有:%s元' % allMoney