-
Notifications
You must be signed in to change notification settings - Fork 1
/
emailHours.py
executable file
·297 lines (211 loc) · 9.91 KB
/
emailHours.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
#!/usr/bin/python2.7
from jabberbot import JabberBot, botcmd
from datetime import datetime, date , timedelta
import logging
import pprint
import psycopg2
import sys
import time;
from redmine_stats import groupByTopParent, lastTimeEntry
import os.path
from subprocess import call
from email.mime.text import MIMEText
import smtplib
from config import username, password, chatroom, adminuser, ignoreUsers, xmppHandles, userConfig, conn_string, firstNames, docURL
from configMonthly import monthly
def debug(message):
print message
#bot.send(adminuser, message)
def notify(redmineUser, message):
host = username.split('@')[1]
xmppUser = ''
if redmineUser in xmppHandles:
xmppUser = xmppHandles[redmineUser] + '@' + host
#debug("Trying to notify {0} : {1}".format(xmppUser, message))
bot.send(xmppUser, message)
time.sleep(1)
# count business days from fromDay to untilDay inclusively
def calcBuisnessDays(fromDay=1, untilDay=31, calcBuisnessDaysOnly=True):
if not calcBuisnessDaysOnly:
return untilDay + 1 - fromDay
now = datetime.now()
#holidays = [datetime.date(2013, 8, 14)] # you can add more here
holidays = [] # you can add more here
businessdays = 0
for i in range(fromDay, untilDay +1):
try:
thisdate = date(now.year, now.month, i)
except(ValueError):
break
if thisdate.weekday() < 5 and thisdate not in holidays: # Monday == 0, Sunday == 6
businessdays += 1
return businessdays
def dayFraction(beginHour = 9, endHour = 18):
currentSecond= datetime.now().second
currentMinute = datetime.now().minute
currentHour = datetime.now().hour
nowHour = currentHour + float(currentMinute)/60 + float(currentSecond)/(60*60)
#debug("%f = %f + %f/60 + %f/(60*60)" % (nowHour, currentHour, currentMinute, currentSecond))
frac = max(min((float(nowHour)-float(beginHour)) / (float(endHour) - float(beginHour)), 1), 0)
#debug(frac)
return frac
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
#bot = SystemInfoJabberBot(username,password)
#debug('Hello Julien, je suis connecte')
"""
Determine if we are within normal updating times
"""
def inAnnounceTime():
hour_start = 11
hour_end = 18
now=datetime.now()
if now.weekday() >= 0 and now.weekday() <= 4 and now.hour >= hour_start and now.hour < hour_end:
return True
else:
return False
### Determine first if there was a new time entry, how old was the last annoucement
### and thus we should report time
def goReport(user):
lastEntryAtLastPassPath = "/tmp/monthlyObjecttives.%s.lastEntry.txt" % user
doContinue = False
doUpdate = False
lastEntryForUser = lastTimeEntry(user)
lastEntryForUserStr = lastEntryForUser.strftime("%Y/%m/%d %H:%M")
if not os.path.isfile(lastEntryAtLastPassPath):
doContinue = True
else:
content = ''
with open(lastEntryAtLastPassPath, 'r') as content_file:
content = content_file.read()
secsSinceLastEntry = time.time() - float(lastEntryForUser.strftime("%s"))
secsSinceLastAnnounce = time.time() - os.path.getmtime(lastEntryAtLastPassPath)
"""
Update if:
- A new time entry has been made, at any time
- If we are in announce time:
- If it has been at least 6 hours since the last entry and 5 minutes since the last announce
- If it has been 2 hours since the last announce
"""
if lastEntryForUserStr != content:
doContinue = True
elif inAnnounceTime():
if (secsSinceLastEntry > 6*60*60 and secsSinceLastAnnounce > 5*60) or secsSinceLastAnnounce > 2*60*60:
doUpdate = True
os.utime(lastEntryAtLastPassPath, None)
if doContinue:
f = open(lastEntryAtLastPassPath, 'w')
f.write(lastEntryForUserStr)
f.close
return doContinue or doUpdate
def main():
if len(sys.argv) < 2:
print "\nUser to query and email as arguments\n"
sys.exit()
userQuery = sys.argv[1]
# print the connection string we will use to connect
#debug("Connecting to database...")
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
#debug("Connected!\n")
for (user, data) in monthly.items():
#if goReport(user) == False:
# continue
### Now continue reporting for that user
#debug("Doing %s..." % user)
##Number of hours per project spent since beginning of month
sql = ("select p.id, p.parent_id, p.identifier, sum(te.hours) as month, "\
"sum(case when spent_on = current_date then te.hours else 0 end) as today, "\
"max(te.updated_on) as s "\
"from time_entries te, projects p, users u "\
"where u.login = '%s' and u.id = te.user_id "\
" and spent_on between date_trunc('month', current_date) and now() "\
" and te.project_id = p.id "\
"group by p.id, p.identifier, p.parent_id "\
"order by s desc ") % user
#print sql
cursor.execute(sql, (user))
dataForMonth = cursor.fetchall()
dataForMonth = groupByTopParent(dataForMonth)
#Build a hash from that
hoursWorked = {}
for id, row in dataForMonth.items():
hoursWorked[row['identifier']] = {'month' : float(row['hoursMonth']), 'today' : float(row['hoursToday']), 'lastEntry' : row['lastUpdate']}
# Calculate date stuff
now = datetime.now()
thisdate = date(now.year, now.month, now.day)
# weekday = thisdate.weekday() < 5
weekday = True # Always assume it's a weekday as we want to see the weekday plan on a weekend
buisnessDays = calcBuisnessDays(1, datetime.now().day, weekday)
buisnessDaysTotal = calcBuisnessDays(1, 32, weekday)
buisnessDaysRemaining = max(calcBuisnessDays(datetime.now().day, 32, weekday), 1)
frac = dayFraction();
forecastRatio = float(buisnessDaysTotal) / (float(buisnessDays) + float(frac))
#debug("%.1f = %.1f / (%.1f + %.1f)" % (forecastRatio, buisnessDaysTotal, buisnessDays, frac))
#debug("Doing %s..." % user)
allProjects = data.keys()
allProjects.append(hoursWorked.keys())
# For each project
deltaTotal = 0; deltaSpreadOutTotal = 0; remainingTodayTotal = 0;
workedTodayTotal = 0; deltaIncreaseTotal = 0; deltaForcastTotal = 0;
lastEntryMax = date(1, 1, 1); lastEntryMax = datetime.combine(lastEntryMax, datetime.min.time())
for(project, hoursPerWeekExpected) in data.items():
if project not in hoursWorked:
hoursWorked[project] = {'month' : 0, 'today' : 0, 'lastEntry' : datetime.now()}
### Calulate expected for 28 days:
# (I actually don't care about last 28 days, so not doing it)
# But keeping the code in case I change my mind
#expected28[user][project] = hoursPerWeekExpected*(27+fraction)/weekday
#Add worked today totals for reporting after the this for loop
workedTodayTotal += hoursWorked[project]['today']
#Calulate expected from beginning of month to today, end of day:
daysPerWeek = 5 if weekday else 7
expectedMonthUntilNow = hoursPerWeekExpected*float(buisnessDays-1+frac)/daysPerWeek
#Generate the delta report
delta = hoursWorked[project]['month'] - expectedMonthUntilNow
deltaTotal += delta
#Forcast delta at end of month
deltaForcast = delta * forecastRatio
deltaForcastTotal += deltaForcast
#Delta spread out: if you wanted 0 delta by end of month, how much would
#you have to work on the remaining business days, including today
expectedMonthUntilYesterdayEOD = hoursPerWeekExpected*(buisnessDays-1)/daysPerWeek
expectedEndOfMonth = hoursPerWeekExpected*(float(buisnessDaysTotal))/daysPerWeek
deltaSpreadOut = (expectedEndOfMonth - (float(hoursWorked[project]['month']) - float(hoursWorked[project]['today'])))/float(buisnessDaysRemaining)
deltaSpreadOutTotal += deltaSpreadOut
#Delta increase: If you stopped working now, how much would your delta spread out increase by
deltaIncrease = (deltaSpreadOut - hoursWorked[project]['today'])/max(buisnessDaysRemaining-1,1)
deltaIncreaseTotal += deltaIncrease
remainingToday = float(deltaSpreadOut) - float(hoursWorked[project]['today'])
remainingTodayTotal += remainingToday
lastEntry = hoursWorked[project]['lastEntry']
diff = datetime.now() - lastEntry
lastEntryMax = max(lastEntryMax, lastEntry)
# To refresh average time entry,
# select login, avg(hours) as hours from time_entries te, users u where user_id = u.id and hours between 0 and 6 group by u.login order by hours;
if lastEntry.date() < datetime.today().date() or diff > timedelta(hours=0.83):
lastEntry = datetime.now()
eta = lastEntry + timedelta(minutes=float(remainingToday)*60)
# Print with more condensed format
reportStr = "{:s}: MTD delta: {:.1f} (Frcst: {:.1f}); Today: {:.1f}/{:.1f} ({:+.1f}); End: {:%H:%M}".format(project, delta, deltaForcast, hoursWorked[project]['today'], deltaSpreadOut, deltaIncrease, eta)
diff = datetime.now() - lastEntryMax
if lastEntryMax.date() < datetime.today().date() or diff > timedelta(hours=1.27):
lastEntryMax = datetime.now()
eta = lastEntryMax + timedelta(minutes=float(remainingTodayTotal)*60)
#start = datetime.datetime.now().time() - timedelta(minutes=float(workedTotalTotal)*60)
reportStr = "Today, {:s} did {:.1f} of {:.1f} hours and, if he does not stop billing, is expected to reach that target at {:%H:%M}".format(userQuery, workedTodayTotal, deltaSpreadOutTotal, eta)
print reportStr
#f = open("/tmp/message.hours.%s.txt" % userQuery, 'w')
#f.write(reportStr)
#f.close
#s = smtplib.SMTP('localhost')
#s.sendmail("[email protected]", whoToEmail, reportStr)
if __name__ == "__main__":
main()