-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
352 lines (308 loc) · 11.2 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""`main` is the top level module for your Flask application."""
from pprint import pprint
# Import the Flask Framework
import flask
from flask import Flask
from flask import redirect
from flask import request
from google.appengine.api import memcache
from util import login
from lever import LeverClient
app = Flask(__name__)
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
APP_NAME = 'Catapult'
TEAM_FEEDBACK_KEY = u"Did the candidate give you any information about their interests that would help determine team fit?"
ANYTHING_ELSE_TO_KNOW_KEY = u"Is there anything else we should consider when making the final hiring decision?"
lever_client = LeverClient()
def _extract_fields_as_keyval(fields, key, allow_missing=False):
for field in fields:
if field['text'] == key:
return field['value']
if allow_missing:
return ''
raise KeyError(key)
def _truncate_header(header):
# Strip out the word "Engineering" because it's redundant
if header['interview_type'].startswith('Engineering - '):
header['interview_type'] = header['interview_type'][len('Engineering - '):]
try:
score = int(header['score'][0])
header['score'] = str(score)
except Exception:
pass
return header
class InterviewTypes(object):
PROBLEM_SOLVING = 'problem solving'
SYSTEM_DESIGN = 'system design'
PLAYS_WELL = 'plays well with others'
OWNERSHIP = 'ownership'
FEEDBACK_ORDERING = [
InterviewTypes.PROBLEM_SOLVING,
InterviewTypes.SYSTEM_DESIGN,
InterviewTypes.PLAYS_WELL,
InterviewTypes.OWNERSHIP,
]
def _assign_arbitrary_feedback_ordering(feedback):
feedback_title = feedback['text'].lower()
for rank, interview_type in enumerate(FEEDBACK_ORDERING):
if interview_type in feedback_title:
# Increment by 1 since 0 is reserved for phone interviews, which
# must come first :P
return rank + 1
return None
def _compile_feedback(candidate_id):
feedbacks = lever_client.get_candidate_feedback(candidate_id)
headers = []
def arbitrary_order_to_be_consistent_with_docs(feedback):
# Regardless of if it's an arbitrary order or not, phone interviews
# always go first.
if 'phone' in feedback['text'].lower():
return 0
if _assign_arbitrary_feedback_ordering(feedback) is not None:
return _assign_arbitrary_feedback_ordering(feedback)
return feedback['completedAt']
feedbacks = sorted(
feedbacks,
key=arbitrary_order_to_be_consistent_with_docs,
)
feedbacks = [
feedback for feedback in feedbacks
if feedback['completedAt'] is not None
and not feedback['text'].startswith("Intern Evaluations")
]
for feedback in feedbacks:
try:
# TODO: We can probably cache this forever
user = memcache.get(feedback['user'])
if user is None:
user = lever_client.get_user(feedback['user'])
memcache.add(
feedback['user'],
user,
60 * 60 * 24 * 7,
)
feedback['username'] = user['name']
feedback['score'] = _extract_fields_as_keyval(
feedback['fields'],
u'Rating',
)
header = dict(
score=feedback['score'],
interviewer=user['name'].strip(),
interview_type=feedback['text'].strip(),
)
headers.append(_truncate_header(header))
FIELD_BLACKLIST = [
TEAM_FEEDBACK_KEY,
ANYTHING_ELSE_TO_KNOW_KEY,
u'Rating',
]
feedback['feedback_text'] = feedback['fields'][0]['value']
feedback['feedback_texts'] = [dict(
header=field['text'],
text=field['value'],
) for field in feedback['fields']
if field['text'] not in FIELD_BLACKLIST
]
# There are two types of team feedback, the old "Team Suggestion"
# and the newer, really long one defined by TEAM_FEEDBACK_KEY.
# Account for both of these and conditionally include them in the
# feedback payload
feedback['team_suggestion'] = _extract_fields_as_keyval(
feedback['fields'],
u'Team Suggestions',
allow_missing=True,
)
feedback['team_feedback'] = _extract_fields_as_keyval(
feedback['fields'],
TEAM_FEEDBACK_KEY,
allow_missing=True,
)
feedback['anything_else_we_should_know'] = _extract_fields_as_keyval(
feedback['fields'],
ANYTHING_ELSE_TO_KNOW_KEY,
allow_missing=True,
)
except Exception as e:
print e
print 11111, 'failure!'
pprint(feedback)
return headers, feedbacks
def _is_intern_form_v2(fields):
# New form was released that allowed the reviewer to provide notes for each
# evaluation metric, whereas before there was one overall notes field.
# Figure out which one we're presenting for.
notes_field_count = 0
for field in fields:
if field['text'].lower().startswith('notes'):
notes_field_count += 1
if notes_field_count > 1:
return True
return False
def _determine_intern_fields_form_v1(fields):
cleaned_fields = dict(
overall_score=None,
notes=None,
other_random_fields=[],
)
for field in fields:
if field['text'].lower().startswith('overall'):
cleaned_fields['overall_score'] = field['value']
continue
if field['text'].lower().startswith('notes'):
cleaned_fields['notes'] = field['value']
continue
clean_name = field['text'].split('-')[0].strip()
cleaned_fields['other_random_fields'].append(dict(
label=clean_name,
text=field['value'],
))
return cleaned_fields
def _determine_intern_fields_form_v2(fields):
cleaned_fields = dict(
overall_score=None,
notes=None,
other_random_fields=[],
)
current_eval_field = {}
for field in fields:
if field['text'].lower().startswith('overall'):
cleaned_fields['overall_score'] = field['value']
continue
if field['text'].lower().startswith('notes'):
# Notes field corresponds to previous eval field in v2. Append to
# fields list then clear the slate.
current_eval_field['notes'] = field['value']
cleaned_fields['other_random_fields'].append(current_eval_field)
current_eval_field = {}
continue
clean_name = field['text'].split('-')[0].strip()
if current_eval_field:
# Have a current eval being considering means there was no notes,
# so we can persist the current as is and assume this iteration is
# starting a next one.
cleaned_fields['other_random_fields'].append(current_eval_field)
current_eval_field = {}
current_eval_field = dict(
label=clean_name,
text=field['value'],
)
if current_eval_field:
cleaned_fields['other_random_fields'].append(current_eval_field)
return cleaned_fields
def _determine_intern_fields(fields):
if _is_intern_form_v2(fields):
return _determine_intern_fields_form_v2(fields)
else:
return _determine_intern_fields_form_v1(fields)
@app.route('/')
@login.admin_required
@login.company_login_required
@login.login_required
def home():
"""Return a friendly HTTP greeting."""
return flask.render_template(
'home.html',
title=APP_NAME,
)
@app.route('/fetch_feedback', methods=['POST'])
@login.login_required
@login.company_login_required
@login.admin_required
def fetch_feedback():
"""Return a friendly HTTP greeting."""
candidate_id = request.form['candidate_id']
return redirect(
'/feedback/%s' % (candidate_id,),
)
@app.route('/treb')
@login.login_required
@login.company_login_required
@login.admin_required
def treb():
return flask.render_template(
'trebuchet.html',
title=APP_NAME,
)
@app.route('/fetch_internevals', methods=['POST'])
@login.login_required
@login.company_login_required
@login.admin_required
def fetch_internevals():
"""Return a friendly HTTP greeting."""
candidate_id = request.form['candidate_id']
return redirect(
'/trebuchet/%s' % (candidate_id,),
)
def _more_than_n_months_old(timestamp, n=7):
# https://jira.yelpcorp.com/browse/ENGREC-259, change lookback period to 7 months
import datetime
dt = datetime.datetime.fromtimestamp(timestamp/1000)
return datetime.datetime.now() - datetime.timedelta(days=n * 30) > dt
@app.route('/trebuchet/<candidate_id>')
@login.login_required
@login.company_login_required
@login.admin_required
def intern_thing(candidate_id):
"""Return a friendly HTTP greeting."""
feedbacks = lever_client.get_candidate_feedback(candidate_id)
feedbacks = [feedback for feedback in feedbacks if feedback['completedAt'] is not None]
feedbacks = sorted(
feedbacks,
key=lambda x: x['completedAt'],
)
headers = []
final_feedbacks = []
for feedback in feedbacks:
if not feedback['text'].startswith('Intern Evaluations'):
continue
if _more_than_n_months_old(feedback['completedAt'], n=7):
continue
user = memcache.get(feedback['user'])
if user is None:
user = lever_client.get_user(feedback['user'])
memcache.add(
feedback['user'],
user,
60 * 60 * 24 * 7,
)
cleaned_fields = _determine_intern_fields(feedback['fields'])
cleaned_fields['username'] = user['name']
final_feedbacks.append(cleaned_fields)
headers.append(dict(
score=cleaned_fields['overall_score'],
interviewer=user['name'].strip(),
))
return flask.render_template(
'trebuchet.html',
feedbacks=final_feedbacks,
candidate=lever_client.get_candidate(candidate_id),
headers=headers,
)
@app.route('/feedback/<candidate_id>')
@login.login_required
@login.company_login_required
@login.admin_required
def feedback(candidate_id):
headers, feedbacks = _compile_feedback(candidate_id)
candidate = lever_client.get_candidate(candidate_id)
return flask.render_template(
'home.html',
title=APP_NAME,
candidate_id=candidate_id,
candidate=candidate,
headers=headers,
feedbacks=feedbacks,
team_feedback_key=TEAM_FEEDBACK_KEY,
anything_to_know_key=ANYTHING_ELSE_TO_KNOW_KEY,
)
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def page_error(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500
app.secret_key = 'Change me.'