root/stats/trunk/feedtree_stats.py

Revision 169, 1.9 kB (checked in by dsandler, 5 years ago)

--

Line 
1 import MySQLdb
2 from duration import *
3
4 DEFAULT_WINDOW = 3600 * 12 # 12 hours
5
6 def bytify(n):
7     n = float(n)
8     units=('B','K','M','G')
9     u=0
10     while n > 1024.0 and u < len(units)-1:
11        u += 1
12        n /= 1024.0
13     return "%.2f%s" % (n, units[u])
14
15
16 def nodes(password, age=DEFAULT_WINDOW):
17     db = MySQLdb.connect('stats.feedtree.net', 'feedtree', password)
18     db.select_db('feedtree')
19     cu = db.cursor()
20     cu.execute('''
21         select distinct nodeid from reports where NOW() - INTERVAL %d SECOND < ts
22         ;
23     ''' % age);
24     ids = [x[0] for x in cu.fetchall()]
25    
26     data = {}
27    
28     for i in ids:
29         cu.execute('''
30             select version,uptime,UNIX_TIMESTAMP(ts),memsize,ip,app,svn from reports where nodeid = '%s' order by ts desc limit 1
31             ;
32         ''' % i)
33         r = cu.fetchone()
34        
35         ts = r[2]
36
37         data[i] = {
38             'nodeid': i,
39             'version': r[0],
40             'uptime': r[1]/1000,
41             'upstr': duration(r[1]/1000, SHORT),
42             'ts': ts,
43             'memsize':r[3],
44             'mem':bytify(r[3]),
45             'ip': r[4],
46             'app': r[5] or 'proxy',
47             'svn': r[6] or '',
48         }
49
50         cu.execute('''
51             select topic,entries,polling from feeds
52             where nodeid = '%s' and ts = FROM_UNIXTIME(%d)
53             ;
54         ''' % (i, ts))
55
56         feeds = []
57
58         for f in cu.fetchall():
59             feeds.append({
60                 'topic': f[0],
61                 'entries': f[1],
62                 'polling': f[2]
63             })
64
65         data[i]['feeds'] = feeds
66
67         scribes = {}
68        
69         cu.execute('''
70             select topic,parent from scribes
71             where nodeid = '%s' and ts = FROM_UNIXTIME(%d)
72             ;
73         ''' % (i, ts))
74
75         for s in cu.fetchall():
76             scribes[s[0]] = s[1]
77        
78         data[i]['scribes'] = scribes
79
80     return data
81
Note: See TracBrowser for help on using the browser.