-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedutils.py
executable file
·189 lines (145 loc) · 5.83 KB
/
feedutils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
# Helpers for producing data feeds
import cgi
import datetime
import json
import random
import sys
import time
import MySQLdb
import sql
def SendPacket(packet):
packet['timestamp'] = int(time.time())
print json.dumps(packet)
sys.stdout.flush()
def GetCursor():
# Read config from a file
with open('/srv/config/summaryfeed') as f:
flags = json.loads(f.read())
db = MySQLdb.connect(user = flags['dbuser'],
db = flags['dbname'],
passwd = flags['dbpassword'],
host = flags.get('dbhost', 'localhost'))
cursor = db.cursor(MySQLdb.cursors.DictCursor)
return cursor
def SendGroups(cursor):
groups = []
cursor.execute('select * from groups;')
for row in cursor:
groups.append([row['name'], row['members'].split(' ')])
SendPacket({'type': 'groups',
'payload': groups})
def GetGroupMembers(cursor, groupname):
members = []
cursor.execute('select * from groups where name="%s";'
% groupname)
if cursor.rowcount > 0:
row = cursor.fetchone()
for member in row['members'].split(' '):
members.append(member)
return members
def ResolveGroupMembers(cursor, usersliststring, table, window_size):
showusers = []
one_day = datetime.timedelta(days=1)
start_of_window = datetime.datetime.now()
start_of_window -= one_day * window_size
for userish in usersliststring.lstrip(' ').split(' '):
if userish.startswith('g:'):
group_name = userish.split(':')[1]
if group_name == 'all':
cursor.execute('select distinct(username), max(day) from %s '
'where day > date(%s) group by username;'
%(table,
sql.FormatSqlValue('timestamp',
start_of_window)))
for row in cursor:
showusers.append(row['username'])
else:
for user in GetGroupMembers(cursor, group_name):
showusers.append(user)
else:
showusers.append(userish)
if len(showusers) == 0:
showusers = ['mikalstill']
return showusers
def SendTriagers(cursor, window_size):
SendUsers(cursor, window_size, 'bugtriagesummary')
def SendClosers(cursor, window_size):
SendUsers(cursor, window_size, 'bugclosesummary')
def SendUsers(cursor, window_size, table, project=None):
one_day = datetime.timedelta(days=1)
start_of_window = datetime.datetime.now()
start_of_window -= one_day * window_size
plike = '%'
if project:
plike = '%%%s%%' % project
all_reviewers = []
cursor.execute('select distinct(username), max(day) from %s '
'where data like "%s" and day > date(%s) group by username;'
%(table, plike,
sql.FormatSqlValue('timestamp', start_of_window)))
for row in cursor:
all_reviewers.append((row['username'], row['max(day)'].isoformat()))
SendPacket({'type': 'users-all',
'payload': all_reviewers})
def SendKeepAlive():
SendPacket({'type': 'keepalive'})
def SendDebug(message):
SendPacket({'type': 'debug',
'payload': message})
def GetInitial(eventname, showusers, project, initial_size):
cursor = GetCursor()
# Fetch the last seven days of results to start off with
last_time = 0
one_day = datetime.timedelta(days=1)
SendGroups(cursor)
SendUsers(cursor, initial_size, '%ssummary' % eventname, project=project)
SendPacket({'type': 'users-present', 'payload': showusers})
for username in showusers:
day = datetime.datetime.now()
day = datetime.datetime(day.year, day.month, day.day)
day -= one_day * (initial_size - 1)
for i in range(initial_size):
timestamp = sql.FormatSqlValue('timestamp', day)
cursor.execute('select * from %ssummary where username="%s" '
'and day=date(%s);'
%(eventname, username, timestamp))
packet = {'type': 'initial-value',
'user': username,
'day': day.isoformat()}
if cursor.rowcount > 0:
row = cursor.fetchone()
packet['payload'] = json.loads(row['data']).get(project, 0)
packet['written-at'] = row['epoch']
if row['epoch'] > last_time:
last_time = row['epoch']
else:
packet['payload'] = 0
SendPacket(packet)
day += one_day
SendPacket({'type': 'initial-value-ends'})
return last_time
def GetUpdates(eventname, showusers, project, last_time):
while True:
time.sleep(60)
# Rebuild the DB connection in case the DB went away
cursor = GetCursor()
SendKeepAlive()
SendDebug('Querying for updates after %d, server time %s'
%(last_time, datetime.datetime.now()))
for username in showusers:
ts = datetime.datetime.now()
ts -= datetime.timedelta(days=5)
cursor.execute('select * from %ssummary where username="%s" '
'and epoch > %d and day > date(%s);'
%(eventname, username, last_time,
sql.FormatSqlValue('timestamp', ts)))
for row in cursor:
SendPacket(
{'type': 'update-value',
'user': username,
'written-at': row['epoch'],
'day': row['day'].isoformat(),
'payload': json.loads(row['data']).get(project, 0)})
if row['epoch'] > last_time:
last_time = row['epoch']