-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.py
278 lines (252 loc) · 9.23 KB
/
worker.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import gearman
import bson
from os import getenv
from datetime import datetime
from sklearn import linear_model
import pickle
gearman_client = None
key = getenv('SECRETKEY')
def log(level, message):
levels = ['INFO:', 'WARNING:', 'ERROR:']
time = datetime.now().strftime('%H:%M %d/%m/%Y')
print ' '.join([time, levels[level], message])
def get_user_data(username):
log(0, 'Getting db doc for user {}'.format(username))
req_data = bson.BSON.encode({
"key": key,
"database": "feedlark",
"collection": "user",
"query": {
"username": username
},
"projection": {}
})
get_response = gearman_client.submit_job('db-get', str(req_data))
result = bson.BSON(get_response.result).decode()
if result['status'] != 'ok':
log(2, "Error getting db entry for user {}".format(username))
log(2, result['description'])
return None
if "docs" not in result or len(result['docs']) == 0:
log(1, "No docs returned for user {}".format(username))
return None
return result['docs'][0]
def update_user_data(username, data):
'''
Update the document for the given user,
With the dict of updates provided in `data`
'''
log(0, 'Updating db doc for user {}'.format(username))
req_data = bson.BSON.encode({
"key": key,
"database": "feedlark",
"collection": "user",
"data":{
"updates": data,
"selector":{
"username": username
}
}
})
update_response = gearman_client.submit_job('db-update', str(req_data))
result = bson.BSON(update_response.result).decode()
if result['status'] != 'ok':
log(2, 'Error updating db entry for user {}'.format(username))
log(2, result['description'])
return
def get_votes_for_user(username):
'''
Get all the votes that this user has cast on articles
'''
log(0, 'Getting votes for user {}'.format(username))
req_data = bson.BSON.encode({
"key": key,
"database": "feedlark",
"collection": "vote",
"query": {
"username": username
},
"projection": {}
})
get_response = gearman_client.submit_job('db-get', str(req_data))
result = bson.BSON(get_response.result).decode()
if result['status'] != 'ok':
log(2, "Error getting votes for user {}".format(username))
log(2, result['description'])
return None
if 'docs' not in result or len(result['docs']) == 0:
log(1, "No docs returned for user {}".format(username))
return []
return result['docs']
def get_feed_items(feed_url, item_urls):
'''
Fetches the data for each article with its url in item_urls,
From the feed with the url feed_url
'''
log(0, 'Getting feed items for feed {}'.format(feed_url))
req_data = bson.BSON.encode({
"key": key,
"database": "feedlark",
"collection": "feed",
"query":{
"url": feed_url
},
"projection": {
"items": 1
}
})
get_response = gearman_client.submit_job('db-get', str(req_data))
result = bson.BSON(get_response.result).decode()
if result['status'] != 'ok':
log(2, 'Error getting feed {}'.format(feed_url))
log(2, result['description'])
return None
if 'docs' not in result or len(result['docs']) == 0:
log(1, 'No docs returned for feed {}'.format(feed_url))
return None
item_url_set = set(item_urls)
response = [d for d in result['docs'][0]['items'] if ('link' in d and d['link'] in item_url_set)]
return response
def get_topic_crossover(user_data, article_data):
'''
Given the user data and article data,
returns the crossover according to the 'score' gearman worker
'''
log(0, 'Getting topic crossover for article {}'.format(article_data['link']))
req_data = bson.BSON.encode({
"key": key,
"article_words": article_data['topics'],
"user_words": user_data['words']
})
gearman_response = gearman_client.submit_job('score', str(req_data))
result = bson.BSON(gearman_response.result).decode()
if result['status'] != 'ok':
log(2, 'Error getting topic crossover score')
log(2, result['description'])
return None
ans = result['score']
return ans
def build_model(user_data, votes):
'''
Build a model for the user with the given data,
Using the information in the `votes` list
'''
# create a dict of lsts, mapping each feed to the items to be
# taken from that feed to create the votes
log(0, 'Building model...')
feed_items = {}
item_opinion = {}
item_vote_datetime = {}
if len(votes) == 0:
log(0, 'No votes found for user {}, skipping model building'.format(user_data['username']))
return None
log(0, 'Building model for user {} with {} votes'.format(user_data['username'], len(votes)))
for vote in votes:
feed_url = vote['feed_url']
article_url = vote['article_url']
item_vote_datetime[article_url] = vote['vote_datetime']
if vote['feed_url'] in feed_items:
feed_items[feed_url].append(article_url)
else:
feed_items[feed_url] = [article_url]
item_opinion[article_url] = 1 if vote['positive_opinion'] else -1
log(0, 'Votes categorised by feed.')
# have to init the model with the two classes
# cannot be guaranteed the user has both upvoted and downvoted articles
# so create two classes with extreme inputs
x = [[0, 20000000], [100, 0]]
y = [-1, 1]
log(0, 'Initiating model')
model = linear_model.SGDClassifier(loss="log", n_iter=5)
log(0, 'Putting together training data')
for feed in feed_items:
for item in get_feed_items(feed, feed_items[feed]):
inputs = []
# inputs[0] should be the topic crossover
inputs.append(get_topic_crossover(user_data, item))
# inputs[1] should be the diff between
# the vote datetime and the article datetime
time_diff = item_vote_datetime[item['link']] - item['pub_date']
inputs.append(time_diff.total_seconds())
x.append(inputs)
y.append(item_opinion[item['link']])
if len(x) != len(y):
log(2, 'Mismatch in input and output length:')
log(2, str(x))
log(2, str(y))
return None
log(0, 'Training model')
model.fit(x, y)
log(0, 'Pickling model')
pickled_model = pickle.dumps(model)
return pickled_model
def refresh_model(worker, job):
"""
Gearman entry point
"""
bson_input = bson.BSON(job.data)
job_input = bson_input.decode()
log(0, 'refresh-model worker called')
if key is not None:
if 'key' not in job_input or job_input['key'] != key:
log(2, 'Secret key mismatch')
return str(bson.BSON.encode({
'status': 'error',
'description': 'Secret key mismatch'
}))
users_data = []
if 'username' not in job_input:
log(0, 'No username supplied, refreshing all users.')
req_data = bson.BSON.encode({
"key": key,
"database": "feedlark",
"collection": "user",
"query": {},
"projection": {}
})
get_response = gearman_client.submit_job('db-get', str(req_data))
result = bson.BSON(get_response.result).decode()
if not 'docs' in result or len(result['docs']) == 0:
log(2, 'No user docs returned from db')
return str(bson.BSON.encode({
'status': 'error',
'description': 'No user docs returned from db'
}))
users_data = result['docs']
else:
username = job_input['username']
user_data = get_user_data(username)
if user_data is None:
log(2, 'No data recieved from db for given username')
return str(bson.BSON.encode({
'status': 'error',
'description': 'No data recieved from db for given username'
}))
users_data = [user_data]
for user_data in users_data:
log(0, 'Refreshing model for user {}'.format(username))
username = user_data['username']
votes = get_votes_for_user(username)
if votes is None or len(votes) == 0:
log(1, 'No vote data found for given username')
return str(bson.BSON.encode({
'status': 'error',
'description': 'No vote data found for given username'
}))
new_model = build_model(user_data, votes)
user_data['model'] = new_model
update_user_data(username, user_data)
return str(bson.BSON.encode({
'status': 'ok'
}))
def init_gearman_client():
global gearman_client
log(0, 'Creating gearman client')
gearman_client = gearman.GearmanClient(['localhost:4730'])
if __name__ == '__main__':
init_gearman_client()
log(0, "Creating gearman worker 'refresh-model'")
gearman_worker = gearman.GearmanWorker(['localhost:4730'])
gearman_worker.set_client_id('refresh-model')
gearman_worker.register_task('refresh-model', refresh_model)
gearman_worker.work()