-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress.py
181 lines (150 loc) · 6.09 KB
/
progress.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
from datetime import timedelta
import pandas as pd
import pickle
import sf_bot
import math
from datetime import datetime
"""returns a pandas dataframe after parsing the dates"""
def parse_df_dates(df):
for c in df.columns:
df[c] = pd.to_datetime(df[c], infer_datetime_format=True)
return df
"""Returns the currently working assignment pandas series with last completed as input"""
def get_currently_working(df):
# gets the index of the dataframe with highest date
temp = df.idxmax(axis=1)
current = pd.Series()
cols = list(df.columns)
to_drop = []
for t in temp.index:
# skip if the student has completed the orientation
if temp[t] == 'R3':
to_drop.append(t)
continue
# At least one assignment was verfied
if not pd.isnull(temp[t]):
nxt = cols.index(temp[t]) + 1
# if nxt<len(cols):
current[t] = cols[nxt]
# else:
# current[t]=cols[nxt-1]
# no assingment verified
else:
current[t] = 'A1-a'
return current, to_drop
"""Returns the last completed assignment pandas series with last completed as input"""
def last_completed(completed):
# get last completed date
completed = completed.T
temp = completed.max(axis=0)
temp = temp - datetime.now()
# impossible case used as flag for no work completed
temp.fillna(value=timedelta(days=7), inplace=True)
last = pd.Series()
for t in temp.index:
# to weeks
last[t] = int(temp[t].days / 7)
return last
"""Computes the status by subracting doj and today"""
def compute_status(doj, curr):
days_per_assignment = 7
stat = pd.Series(dtype=timedelta)
# date after joining for each student
days_elapsed = datetime.now() - doj
# print(curr)
for i in days_elapsed.index:
# check the currently working assignment
if 'A' in curr[i]:
assignment = int(curr[i][1])
elif curr[i][0] == 'M' or curr[i][0] == 'R':
assignment = 7
else:
assignment = 8
# if in assingments
if assignment < 7:
stat[i] = days_elapsed[i] - \
timedelta(days=days_per_assignment * assignment)
# else if in motor project
elif assignment == 7:
# buffer period of 45 days for completion
stat[i] = timedelta(days=-7) if days_elapsed[i] < timedelta(days=days_per_assignment *
6 + 45) else days_elapsed[i] - timedelta(days=days_per_assignment * 6 + 45)
return stat
# draft a slack messsage
def create_message(status, current, o_data, user_df, last):
work_dict = {
'A1-a': 'Assignment 1a', 'A1-b': 'Assignment 1b', 'A2-a': 'Assignment 2-a', 'A2-b': 'Assignment 2-b', 'A3-a': 'Assignment 3-a', 'A3-b': 'Assignment 3-b', 'A4-a': 'Assignment 4-a', 'A4-b': 'Assignment 4-b', 'A5-a': 'Assignment 5-a', 'A5-b': 'Assignment 5-b', 'A6-a': 'Assignment 6-a', 'A6-b': 'Assignment 6-b', 'M1': 'Project module 1', 'M2': 'Project module 2', 'M3': 'Project module 3', 'R1': 'Review 1', 'R2': 'Review 2', 'R3': 'Review 3'
}
global msg
msg = '_*Weekly Performance Update:*_\n+ => Lead and - => Lag'
for i in status.index:
# remove i
msg += '\n*' + user_df.loc[i]['full_name'] + \
" (<@" + user_df.loc[i]['id'] + ">)" + \
"*:\n In " + work_dict[str(current[i])]
if o_data.loc[i]['Track'] != 2:
# convert the days into weeks (and rounding it off)
weeks = math.ceil(int(str(status[i]).split(' ')[0]) / 7 + 1)
weeks = '-' + str(weeks) if weeks > 0 else '+' + str(abs(weeks))
if weeks != '+0':
msg += ": _" + weeks + " Week(s)_"
else:
msg += ": on track"
msg += '\n'
# impossible flag i.e. not yet started
if last[i] == 1:
msg += 'No work completed yet'
elif last[i] == 0:
msg += 'Last work completed this week'
else:
msg += 'Last work completed before %d week(s) ' % abs(last[i])
return msg
'''
Returns Parsed spreadsheet data
Arguments: None
Returns:
o_data-> A pandas dataframe consisting of students D.o.J and their o_data
completed -> A pandas dataframe consting of dates when the assignments were completed
'''
def get_progress():
'''
# usernames,duration,post_it=arg_parser.get_parsed_arguments()
# print(usernames,duration,post_it)
'''
o_data = pd.read_csv(
'orientation/orientation_data.csv', sep=',', index_col=0)
o_data['doj'] = pd.to_datetime(o_data['doj'], infer_datetime_format=True)
completed = parse_df_dates(pd.read_csv(
'orientation/completed.csv', sep=',', index_col=0))
# get D.o.J which is the date when the first assignment was sent
doj = o_data['doj']
# print(doj)
# get current work
current, to_drop = get_currently_working(df=completed)
# get last completed
last = last_completed(completed)
# drop if completed orientation only from doj,o_data,last,completed
completed.drop(to_drop, inplace=True), doj.drop(to_drop, inplace=True)
o_data.drop(to_drop, inplace=True), last.drop(to_drop, inplace=True)
# save them if anything was dropped
if to_drop != []:
# print('Updating the data (SCT)')
completed.to_csv('orientation/completed.csv', sep=',')
o_data.to_csv('orientation/orientation_data.csv', sep=',')
# calculate expected dates
status = compute_status(doj, current)
# print('Status is:\n {}'.format(status))
# making slack user_name the primary key
user_df = sf_bot.get_userdf()
user_df.index = user_df['name']
del user_df['name']
# drafting a message to be posted on the slack
msg = create_message(status, current, o_data, user_df, last)
print('Message is %s' % msg)
# #post to aashish
# sf_bot.bot.chat.post_message('U39BM0PCN',msg,as_user=True)
# post to sf_ta
sf_bot.post_message(msg, "sf_ta")
return msg
if __name__ == "__main__":
get_progress()