-
Notifications
You must be signed in to change notification settings - Fork 5
/
daily_max.py
executable file
·76 lines (68 loc) · 2.46 KB
/
daily_max.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python
#
# (c) 2015 descilla <[email protected]>
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License or any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY. See the
# GNU General Public License for more details.
#
# For a copy of the GNU General Public License
# see <http://www.gnu.org/licenses/>.
#
import glob, os, json, collections, argparse
parser = argparse.ArgumentParser(description='This Script calculates the daily max user and node count by given nodes.json files.')
parser.add_argument('--filepath', required=True, help='location of nodes.json files')
parser.add_argument('--filepattern', required=True, help='filename pattern of node.json files e. g. "nodes_*"', default='*')
args = parser.parse_args()
class DailyMax:
def __init__(self, directory, pattern ='*'):
self.directory = directory
self.pattern = pattern
self.fileNames = []
self.results = {}
self.getFiles()
self.calculateMax()
self.printResults()
def getFiles(self):
for file in glob.iglob(self.directory+'/'+self.pattern):
self.fileNames.append(file)
def calculateMax(self):
for file in self.fileNames:
resource = open(file)
try:
data = json.loads(resource.read())
except ValueError:
print file, 'is broken'
finally:
resource.close()
self.parseJson(data)
def parseJson(self, data):
nodes_online = 0
users_online = 0
day_stamp = data['timestamp'].split('T')[0]
for node in data['nodes'].itervalues():
if 'statistics' in node:
users_online += node['statistics']['clients']
if 'flags' in node:
if node['flags']['online'] == True:
nodes_online += 1
if day_stamp in self.results:
if self.results[day_stamp]['nodes_online'] < nodes_online:
self.results[day_stamp]['nodes_online'] = nodes_online
if self.results[day_stamp]['users_online'] < users_online:
self.results[day_stamp]['users_online'] = users_online
else:
self.results[day_stamp] = {
'nodes_online' : nodes_online,
'users_online' : users_online
}
def printResults(self):
ordered = collections.OrderedDict(sorted(self.results.items()))
print "date\tnodes_online\tusers_online"
for k,v in ordered.iteritems():
print k+'\t'+str(v['nodes_online'])+'\t'+str(v['users_online'])
dmax = DailyMax(args.filepath, pattern = args.filepattern)