-
Notifications
You must be signed in to change notification settings - Fork 1
/
redmineXMPPbot.py
executable file
·195 lines (137 loc) · 6.11 KB
/
redmineXMPPbot.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
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
############################# Deprecated, see redmine_stats.py
#!/usr/bin/python2.7
from jabberbot import JabberBot, botcmd
import datetime
import logging
import psycopg2
import sys
import time;
from config import username, password, chatroom, adminuser, ignoreUsers, xmppHandles, userConfig, conn_string, firstNames, thresholdDefault
connected = False
def announce(message):
print message
debug('Trying to announce in ' + chatroom + ': ' + message)
bot.send(chatroom, message, None, 'groupchat')
time.sleep(1)
def debug(message):
print message
#bot.send(adminuser, message)
class SystemInfoJabberBot(JabberBot):
@botcmd
def serverinfo( self, mess, args):
"""Displays information about the server"""
version = open('/proc/version').read().strip()
loadavg = open('/proc/loadavg').read().strip()
return '%snn%s' % ( version, loadavg, )
@botcmd
def time( self, mess, args):
"""Displays current server time"""
return str(datetime.datetime.now())
@botcmd
def rot13( self, mess, args):
"""Returns passed arguments rot13'ed"""
return args.encode('rot13')
@botcmd
def whoami(self, mess, args):
"""Tells you your username"""
return mess.getFrom().getStripped()
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')
#print bot.muc_room_participants(chatroom);
def main():
# 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")
sql = "select u.login, max(te.updated_on) from time_entries te, users u where u.status = 1 and u.id = te.user_id and hours > 0 group by u.login order by max(te.updated_on);"
cursor.execute(sql)
data = cursor.fetchall()
# Calculate late users first
lateUsers = []
for row in data:
hoursSinceLastLog = (datetime.datetime.now() - row[1]).total_seconds() / 60 / 60
debug(str(row) + ' ' + str(hoursSinceLastLog))
redmineHandle = row[0]
threshold = thresholdDefault;
if redmineHandle in userConfig and 'threshold' in userConfig[redmineHandle]:
threshold = userConfig[row[0]]['threshold']
if hoursSinceLastLog > threshold and row[0] not in ignoreUsers:
maker = row[0]
if maker in xmppHandles:
maker = xmppHandles[maker]
#lateUsers.append(maker + ' (' + str(round(hoursSinceLastLog, 1)) + ' > ' + str(threshold) + ')');
lateUsers.append(maker)
# Calculate last logged date (reported if user has logged 0 hours in last 7 days)
sql = "select u.login, max(te.updated_on) last_entry from time_entries te, users u where u.status = 1 and u.id = te.user_id and hours > %f group by u.login order by max(te.updated_on);" % 1 #honestLogHours
cursor.execute(sql)
lastHonestLog = {}
while row in cursor:
lastHonestLog[row[0]] = row[1]
# Calculate total hours
sql = "select u.login, sum(hours), min(spent_on) from time_entries te, users u where u.status = 1 and u.id = te.user_id and te.spent_on >= now() - INTERVAL '7 days' and te.spent_on <= now() group by u.login order by sum(hours);"
cursor.execute(sql)
data = cursor.fetchall()
hoursLoggedStr = ''
dateMin = datetime.date.max
for row in data:
if (row[2] < dateMin):
dateMin = row[2]
maker = row[0]
if maker in firstNames:
maker = firstNames[maker]
hoursLoggedStr += maker + ': ' + str(round(row[1], 1)) + ' '
if row[1] < 0.1:
hoursLoggedStr += '(' + lastHonestLog[row[0]].stftime('%b %d %Y') + ') '
else:
debug("NOPE!!!!! This persons log is ok: %s" % row[1])
debug(row)
hoursLoggedStr += ' '
hoursLoggedStr += '(since ' + str(dateMin) + ') (test)\n'
# Calculate hours per project, last 28 days
sql = "select u.login, sum(hours), min(spent_on) from time_entries te, users u where u.id = te.user_id and te.spent_on >= now() - INTERVAL '28 days' group by u.login order by sum(hours);"
cursor.execute(sql)
data = cursor.fetchall()
hoursLast28days = ''
for row in data:
maker = row[0]
if maker in firstNames:
maker = firstNames[maker]
hoursLast28days += maker + ': ' + str(row[1]) + ' (since ' + str(row[2]) + ')\n'
# Now for the announcements
bot.join_room(chatroom, 'credilbot')
time.sleep(1)
if lateUsers:
announce(', '.join(lateUsers) + ' have not logged time within their set threshold (default '+ str(thresholdDefault) +' hours)')
announce('Total numbers of hours logged in last 7 days')
announce(hoursLoggedStr)
if __name__ == "__main__":
main()
#while 1:
# debug(str(datetime.datetime.now()))
# time.sleep(5)
#bot.serve_forever()