-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.py
389 lines (312 loc) · 13.1 KB
/
client.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/python3
import json
import sys
import os
import requests
from evalytics.mappers import Mapper
class FileManager:
def open(self, filename: str, mode: str):
return open(filename, mode)
class EvalyticsRequests:
BASE_URL = "http://evalytics:8080"
def employees(self):
response = requests.get(
url="%s/employees" % self.BASE_URL,
params={})
return self.__get_data_response(response)
def surveys(self):
response = requests.get(
url="%s/surveys" % self.BASE_URL,
params={})
return self.__get_data_response(response)
def reviewers(self):
response = requests.get(
url="%s/reviewers" % self.BASE_URL,
params={})
return self.__get_data_response(response)
def status(self):
response = requests.get(
url="%s/status" % self.BASE_URL,
params={})
return self.__get_data_response(response)
def communications(self, json_reviewers, kind: str):
response = requests.post(
url="%s/communications" % self.BASE_URL,
data={
"reviewers": json_reviewers,
"kind": kind
}
)
return self.__get_data_response(response)
def evalreports(
self,
uids=None,
managers=None,
area=None):
response = requests.post(
url="%s/evalreports" % self.BASE_URL,
data={
"uids": uids,
"managers": managers,
"area": area,
}
)
return self.__get_data_response(response)
def __get_data_response(self, response):
if response.ok:
data = response.json()
data_response = data['response']
if data['success']:
return True, data_response
else:
return False, data_response
else:
return False, response
class EvalyticsClient(EvalyticsRequests, Mapper, FileManager):
EVALS_NOT_SENT_CSV = 'evals_not_sent.csv'
EVALS_WHITELIST = 'evals_whitelist.csv'
def get_employees(self):
success, response = super().employees()
return self.__get_response(success, response, 'employees')
def get_surveys(self):
success, response = super().surveys()
return self.__get_response(success, response, 'surveys')
def get_reviewers(self):
success, response = super().reviewers()
return self.__get_response(success, response, 'reviewers')
def get_status(self):
success, response = super().status()
return self.__get_response(success, response, 'status')
def print_reviewers(self, show_stats: bool = False):
reviewers = self.get_reviewers()
print(json.dumps(reviewers, indent=2))
if show_stats:
print('\n-----------')
print('| STATS |')
print('-----------')
self.__print_reviewers_stats(reviewers)
def print_status(self):
status = self.get_status()
total_evals = 0
total_completed_evals = 0
total_pending_evals = 0
total_inconsistent_evals = 0
print('\nCompleted evals:')
for reviewer, completed_evals in status.get('completed', {}).items():
evals = [uid for uid, e in completed_evals.items()]
total_completed_evals += len(evals)
total_evals += len(evals)
print(" - %s: %s" %(reviewer, evals))
print('Inconsistent evals:')
for reviewer, inconsistent_evals in status.get('inconsistent', {}).items():
evals = [uid for uid, e in inconsistent_evals.items()]
total_inconsistent_evals += len(evals)
print(" - %s: %s\n" %(reviewer, evals))
print('Pending evals:')
reviewers_with_pending_evals = status.get('pending', '[]')
reviewers = super().json_to_reviewers(reviewers_with_pending_evals)
for uid, reviewer in reviewers.items():
total_pending_evals += len(reviewer.evals)
total_evals += len(reviewer.evals)
print(" - %s: %s" %(uid, [e.reviewee for e in reviewer.evals]))
print("\nTotal evals: {}".format(total_evals))
if total_evals > 0:
pending_percentage = total_pending_evals / total_evals * 100
completed_percentage = total_completed_evals / total_evals * 100
print("Completed evals: {0:.2f} % ({1:.0f})".format(
completed_percentage,
total_completed_evals))
print("Pending evals: {0:.2f} % ({1:.0f})".format(
pending_percentage,
total_pending_evals))
print("Inconsitent evals: %s" % total_inconsistent_evals)
def print_inconsistent_files_status(self):
status = self.get_status()
inconsistent_files = {}
print('\nInconsistent evals:')
for reviewer, inconsistent_evals in status.get('inconsistent', {}).items():
evals = []
for uid, evaluation in inconsistent_evals.items():
evals.append(uid)
filename = evaluation['filename']
reasons = inconsistent_files.get(filename, [])
reasons.append({
'reason': evaluation['reason'],
'line': evaluation['line_number']
})
inconsistent_files.update({
filename: reasons
})
print(" - %s: %s\n" %(reviewer, evals))
print('Inconsistent files:')
for filename, reasons in inconsistent_files.items():
print(" - File: '%s'" % filename)
for reason in reasons:
print(' - (Line %s) %s' % (reason['line'], reason['reason']))
print()
def send_communication(self, kind, reviewers, whitelist=None, dry_run: bool = False):
if whitelist is not None:
reviewers = [reviewer
for r_uid, reviewer in reviewers.items()
if r_uid in whitelist]
else:
reviewers = [reviewer for r_uid, reviewer in reviewers.items()]
self.__send_communication(reviewers, kind, dry_run)
def generate_reports(self, whitelist=None):
uids = super().list_to_json(whitelist)
success, response = super().evalreports(uids=uids)
if success:
evals_reports = response['evals_reports']
created = evals_reports['created']
not_created = evals_reports['not_created']
print("Reports created:")
for uid, reports_created in created.items():
print(' - {} report will be shared with {}'.format(uid, reports_created['managers']))
print("Reports NOT created: %s" % not_created)
for uid, reports_not_created in not_created.items():
print(' - {} report will be shared with {}'.format(uid, reports_not_created['managers']))
def get_whitelist(self):
return self.__get_list_from_file(self.EVALS_WHITELIST)
def get_retry_list(self):
return self.__get_list_from_file(self.EVALS_NOT_SENT_CSV)
def help(self, command):
help_message = {
'Message': "Command '{}' not expected".format(command),
'Available commands: [OPTIONS]': {
'reviewers': ['--stats'],
'send_evals': ['--retry', '--whitelist', '--dry-run'],
'send_due_date_comm': ['--retry', '--whitelist', '--dry-run'],
'send_reminders': ['--retry', '--whitelist', '--dry-run'],
'status': ['--inconsisten-files'],
'reports': ['--whitelist'],
}
}
print(json.dumps(help_message))
def __print_reviewers_stats(self, all_reviewers):
reviewers_by_evals_numbers = {}
for reviewer in all_reviewers:
uid = reviewer['employee']['uid']
evals_number = len(reviewer['evals'])
if evals_number in reviewers_by_evals_numbers:
reviewers_by_evals_numbers[evals_number].append(uid)
else:
reviewers_by_evals_numbers.update({
evals_number: [uid]
})
print('\nReviewers per number of evals:')
for evals_number in sorted(reviewers_by_evals_numbers):
reviewers = reviewers_by_evals_numbers[evals_number]
print(' Number of evals: %d' % evals_number)
print(' Reviewers: %s\n' % reviewers)
def __get_response(self, success, response, key):
if success:
return response[key]
else:
if 'error' in response:
print("[Controlled Error] %s" % response['error'])
else:
print("[ERROR] Something failed. key: {}".format(key))
print(" - HTTP code: %s" % response.status_code)
print(" - Error response: %s" % response.content)
return {}
def __get_list_from_file(self, filename):
list_from_file = []
list_file = super().open(filename, "r")
for reviewer in list_file.readlines():
list_from_file.append(reviewer.strip())
list_file.close()
return list_from_file
def __send_communication(self, reviewers, kind: str, dry_run: bool):
json_reviewers = super().reviewer_to_json_object(reviewers)
if dry_run:
for reviewer in reviewers:
print('[DRY-RUN] Reviewer %s has %d evals' % (reviewer.uid, len(reviewer.evals)))
else:
success, response = super().communications(json_reviewers, kind)
if success:
print("Evals sent: %s" % response['comms_sent'])
print("Evals NOT sent: %s" % response['comms_not_sent'])
comms_not_sent = response['comms_not_sent']
if len(comms_not_sent) > 0:
evals_not_sent_file = open(self.EVALS_NOT_SENT_CSV, "w+")
for reviewer in comms_not_sent:
evals_not_sent_file.write("%s\n" % reviewer)
evals_not_sent_file.close()
else:
if os.path.exists(self.EVALS_NOT_SENT_CSV):
os.remove(self.EVALS_NOT_SENT_CSV)
else:
print("[ERROR] Something failed sending emails")
print(" - Error response: {}".format(response['error']))
class CommandFactory(EvalyticsClient):
EMPLOYEES = 'employees'
SURVEYS = 'surveys'
REVIEWERS = 'reviewers'
SEND_EVALS = 'send_evals'
SEND_DUE_DATE_COMM = 'send_due_date_comm'
SEND_REMIDERS = 'send_reminders'
STATUS = 'status'
REPORTS = 'reports'
def execute(self, args):
command = args.pop(0)
dry_run = '--dry-run' in args
with_retry = '--retry' in args
with_whitelist = '--whitelist' in args
whitelist = None
retry_list = None
if dry_run:
args.remove('--dry-run')
if with_retry:
args.remove('--retry')
retry_list = super().get_retry_list()
if with_whitelist:
args.remove('--whitelist')
whitelist = super().get_whitelist()
if command == CommandFactory.EMPLOYEES:
super().get_employees()
elif command == CommandFactory.SURVEYS:
super().get_surveys()
elif command == CommandFactory.REVIEWERS:
show_stats = '--stats' in args
super().print_reviewers(show_stats=show_stats)
elif command in [
CommandFactory.SEND_EVALS,
CommandFactory.SEND_DUE_DATE_COMM,
CommandFactory.SEND_REMIDERS]:
command_to_kind = {
'send_evals': 'process_started',
'send_due_date_comm': 'due_date_reminder',
'send_reminders': 'pending_evals_reminder'
}
kind = command_to_kind.get(command)
reviewers = self.__get_reviewers(kind)
reviewers = super().json_to_reviewers(reviewers)
if with_retry:
whitelist = retry_list
super().send_communication(
kind=kind,
reviewers=reviewers,
dry_run=dry_run,
whitelist=whitelist)
elif command == CommandFactory.STATUS:
if '--inconsistent-files' in args:
super().print_inconsistent_files_status()
else:
super().print_status()
elif command == CommandFactory.REPORTS:
super().generate_reports(whitelist=whitelist)
else:
super().help(command)
def __get_reviewers(self, kind):
if kind == 'process_started':
reviewers = self.get_reviewers()
elif kind in ['due_date_reminder', 'pending_evals_reminder']:
status = self.get_status()
reviewers = status.get('pending', '[]')
else:
raise ValueError(kind)
return reviewers
if __name__ == "__main__":
if len(sys.argv) == 1:
raise Exception("No arguments given")
CommandFactory().execute(sys.argv[1:])