Advertisement
LexManos

Snooper Analiser

May 11th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. import json, sys, os, math
  2. from pprint import pprint
  3.  
  4. def fixMem(data, key):
  5.     if not key in data:
  6.         return
  7.        
  8.     mem = data[key]
  9.     if not 'MB' in mem:
  10.         data[key] = '%dMB' % int(math.ceil(int(mem) / 1024.0 / 1024.0 / 256.0) * 256)
  11.     else:
  12.         data[key] = '%dMB' % int(math.ceil(int(mem[0:-2]) / 256.0) * 256)
  13.    
  14. def pad(data, key):
  15.     if not key in data:
  16.         return
  17.     ret = {}
  18.     max = 0
  19.     for item in data[key]:
  20.         if len(str(item)) > max: max = len(str(item))
  21.     for item in data[key]:
  22.         ret[str(item).rjust(max, ' ')] = data[key][item]
  23.     data[key] = ret
  24.  
  25. f = open('clients.json').readlines()
  26. data = {}
  27. x = 1
  28. for line in f:
  29.     j = json.loads(line)
  30.     for item in j:
  31.         if item == '_id':
  32.             continue
  33.            
  34.         if not item in data:
  35.             data[item] = {}
  36.            
  37.         if item == 'memoryMax':   fixMem(j, 'memoryMax')
  38.         if item == 'memoryTotal': fixMem(j, 'memoryTotal')
  39.         if item == 'memoryFree':  fixMem(j, 'memoryFree')
  40.        
  41.         if item == 'openGlVersion':
  42.             j['openGlVersion'] = j['openGlVersion'].split(' ')[0][0:3]
  43.            
  44.         if item == 'playersSeen':
  45.             if not 'total' in data['playersSeen']:
  46.                 data['playersSeen']['total'] = 0
  47.                 data['playersSeen']['average'] = 0
  48.                
  49.             c = int(j['playersSeen'])
  50.             data['playersSeen']['total'] += c
  51.            
  52.             if c < 1000: c = math.ceil(c / 100.0) * 100
  53.             else: c = math.ceil(c / 1000.0) * 1000
  54.             j['playersSeen'] = int(c)
  55.            
  56.         if item == 'playersCurrent':
  57.             if not 'total' in data['playersCurrent']:
  58.                 data['playersCurrent']['total'] = 0
  59.                 data['playersCurrent']['average'] = 0
  60.                
  61.             c = int(j['playersCurrent'])
  62.             data['playersCurrent']['total'] += c
  63.            
  64.             if c < 100: c = math.ceil(c / 10.0) * 10
  65.             else: c = math.ceil(c / 100.0) * 100
  66.             j['playersCurrent'] = int(c)
  67.            
  68.         if item == 'playersMax':
  69.             if not 'total' in data[item]:
  70.                 data[item]['total'] = 0
  71.                 data[item]['average'] = 0
  72.                
  73.             c = int(j[item])
  74.             data[item]['total'] += c
  75.            
  76.             if c < 100: c = math.ceil(c / 10.0) * 10
  77.             else: c = math.ceil(c / 100.0) * 100
  78.             j[item] = int(c)
  79.            
  80.         if item == 'osVersion' and 'osName' in j:
  81.             os = j['osName']
  82.            
  83.             if not os in data[item]:
  84.                 data[item][os] = {}
  85.                
  86.             if  not j[item] in data[item][os]:
  87.                 data[item][os][j[item]] = 1
  88.             else:
  89.                 data[item][os][j[item]] += 1
  90.         else:
  91.             if  not j[item] in data[item]:
  92.                 data[item][j[item]] = 1
  93.             else:
  94.                 data[item][j[item]] += 1
  95.            
  96.     print "%d / %d (%0.0f%%)" % (x, len(f), int(math.floor((x / float(len(f))) * 100.0))), "\r",
  97.     sys.stdout.flush()
  98.     x += 1
  99. print ""
  100.  
  101. if 'playersSeen' in data:
  102.     data['playersSeen']['average'] = int(data['playersSeen']['total'] / len(f))
  103. if 'playersCurrent' in data:
  104.     data['playersCurrent']['average'] = int(data['playersCurrent']['total'] / len(f))
  105. if 'playersMax' in data:
  106.     data['playersMax']['average'] = int(data['playersMax']['total'] / len(f))
  107.     data['playersMax'].pop('total')
  108.    
  109. pad(data, 'memoryMax')
  110. pad(data, 'memoryTotal')
  111. pad(data, 'memoryFree')
  112. pad(data, 'playersSeen')
  113. pad(data, 'playersCurrent')
  114. pad(data, 'playersMax')
  115. pprint(data)
  116.  
  117.  
  118. five = 0
  119. total = 0
  120. for ver in data['javaVersion']:
  121.     if ver[0:3] == '1.5':
  122.         five += data['javaVersion'][ver]
  123.     total += data['javaVersion'][ver]
  124. print 'Java 5 userbase: %d/%d (%0.000f%%)' % (five, total, float(five) / float(total) * 100.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement