dealer_stats.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os
  4. import sys
  5. import datetime
  6. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  7. sys.path.insert(0, PROJECT_ROOT)
  8. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
  9. import django
  10. django.setup()
  11. from apilib.utils_datetime import to_datetime
  12. from apps.web.constant import Const
  13. from apps.web.device.models import Device
  14. from apps.web.dealer.models import DealerRechargeRecord
  15. # 统计东郡的经销商数据,按照设备量的大小,从高统计
  16. import pymongo
  17. client=pymongo.MongoClient(host='211.159.224.10',port=27119,username = 'dba',password = 'dayuan@2020..') #连接本地的服务端
  18. db=client.spider #指定操作的数据库
  19. collection=db.dongjun_devices #指定操作的集合
  20. # 遍历设备,并登记到经销商
  21. dealerDict = {}
  22. devs = collection.find({'createDate':{'$gte':1596211200000}})
  23. devList = []
  24. for dev in devs:
  25. devList.append(dev)
  26. for dev in devList:
  27. tel = dev.get('contactMobilePhone','')
  28. if tel not in dealerDict:
  29. dealerDict[tel] = {'count':1,'cityName':dev.get('cityName',''),'typeName':dev.get('equipTypeName',''),'name':dev.get('name','')}
  30. else:
  31. dealerDict[tel]['count'] += 1
  32. dealerList = [(k,v['count'],v['cityName'],v['typeName'],v['name']) for k,v in dealerDict.items()]
  33. def cmp_xy(x,y):
  34. if x[1] < y[1]:
  35. return -1
  36. elif x[1] == y[1]:
  37. return 0
  38. return 1
  39. dealerList.sort(cmp=cmp_xy, reverse=True)
  40. for dealer in dealerList[0:100000]:
  41. print dealer[0],dealer[1],dealer[2],dealer[3],dealer[4]