-
Notifications
You must be signed in to change notification settings - Fork 10
/
leaderboard.py
executable file
·29 lines (29 loc) · 1.05 KB
/
leaderboard.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
#!/usr/bin/env python3
import urllib.parse
import urllib.request
import json
from operator import itemgetter
influxUrl = 'http://oort.zwater.us:8086/query'
values = {'db' : 'nixnest',
'q' : 'SELECT sum("value") AS "sum_value" FROM "nixnest"."autogen"."message" WHERE time > now() - 7d GROUP BY "id" FILL(0)'
}
data = urllib.parse.urlencode(values)
data = data.encode('ascii')
request = urllib.request.Request(influxUrl, data)
with urllib.request.urlopen(request) as response:
rawresults = response.read()
results = json.loads(rawresults)
#print(len(results["results"][0]["series"]))
stats = []
x=0
for result in results["results"][0]["series"]:
messageCount = result["values"][0][1]
userId = result["tags"]["id"]
#key = '{ \'id\' : ' + str(userId) + ', \'count\' : ' + str(messageCount) + ' }'
key = {'id':str(userId), 'count':str(messageCount)}
stats.append(key)
#print(stats[1]['count'])
sortStats = sorted(stats, key=lambda k: int(k['count']), reverse=True)
print(json.dumps(sortStats[:10]))
#for test in stats:
# print(test[1])