-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredmine_stat.py
164 lines (121 loc) · 4.66 KB
/
redmine_stat.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Daniel Widerin <daniel@widerin.net>
#
# v1.0 20/11/2012 - First basic implementation
import os
import sys
import MySQLdb
class RedmineBase(object):
graph_category = "redmine"
graph_args = "--base 1000 --lower-limit 0"
graph_vlabel = "tickets"
def __init__(self, database, username, password, hostname, port=3306):
self.db = MySQLdb.connect(host=hostname,
user=username,
passwd=password,
port=port,
db=database,
charset='utf8')
self.cursor = self.db.cursor()
def config(self):
for attr in dir(self):
if attr.startswith('graph_'):
print "%s %s" % (attr, getattr(self, attr))
class TicketsByTracker(RedmineBase):
graph_title = "tickets by tracker"
def _result(self, callback):
self.cursor.execute("""\
SELECT count(issues.id) as Amount, trackers.name, trackers.id
FROM issues
INNER JOIN trackers ON issues.tracker_id=trackers.id
GROUP BY issues.tracker_id
ORDER BY trackers.id ASC""")
result = self.cursor.fetchall()
for item in result:
callback(item)
def config(self):
super(TicketsByTracker, self).config()
def row(item):
print "_v%s.label %s" % (item[2], item[1])
print "_v%s.draw AREASTACK" % (item[2])
self._result(row)
def run(self):
def row(item):
print "_v%s.value %d" % (item[2], item[0])
self._result(row)
class OpenTicketsByTracker(TicketsByTracker):
graph_title = "open tickets by tracker"
def _result(self, callback):
self.cursor.execute("""\
SELECT count(issues.id) as Amount, trackers.name, trackers.id
FROM issues
INNER JOIN trackers ON issues.tracker_id=trackers.id
INNER JOIN issue_statuses ON issues.status_id=issue_statuses.id
WHERE issue_statuses.is_closed=0
GROUP BY issues.tracker_id
ORDER BY trackers.id ASC""")
result = self.cursor.fetchall()
for item in result:
callback(item)
class TicketsByOwner(RedmineBase):
graph_title = "tickets by user"
def _result(self, callback):
self.cursor.execute("""\
SELECT count(issues.id) as Amount, users.firstname, users.lastname,
users.id
FROM issues
INNER JOIN users ON users.id=issues.assigned_to_id
GROUP BY issues.assigned_to_id
ORDER BY users.id ASC""")
result = self.cursor.fetchall()
for item in result:
callback(item)
def config(self):
super(TicketsByOwner, self).config()
def row(item):
print "_v%s.label %s %s" % (item[3],
unicode(item[1]).encode("utf-8"),
unicode(item[2]).encode("utf-8"))
print "_v%s.draw AREASTACK" % (item[3])
self._result(row)
def run(self):
def row(item):
print "_v%s.value %d" % (item[3], item[0])
self._result(row)
class OpenTicketByOwner(TicketsByOwner):
graph_title = "open tickets by user"
def _result(self, callback):
self.cursor.execute("""\
SELECT count(issues.id) as Amount, users.firstname, users.lastname,
users.id
FROM issues
INNER JOIN users ON users.id=issues.assigned_to_id
INNER JOIN issue_statuses ON issues.status_id=issue_statuses.id
WHERE issue_statuses.is_closed=0
GROUP BY issues.assigned_to_id
ORDER BY users.id ASC""")
result = self.cursor.fetchall()
for item in result:
callback(item)
if __name__ == '__main__':
host = os.environ.get("host", "127.0.0.1")
username = os.environ.get("username")
password = os.environ.get("password")
database = os.environ.get("database")
port = os.environ.get("port", 3306)
if '_tracker' in sys.argv[0]:
if '_open' in sys.argv[0]:
r = OpenTicketsByTracker(database, username, password, host, port)
else:
r = TicketsByTracker(database, username, password, host, port)
else:
if '_open' in sys.argv[0]:
r = OpenTicketByOwner(database, username, password, host, port)
else:
r = TicketsByOwner(database, username, password, host, port)
if len(sys.argv) > 1 and sys.argv[1] == 'config':
r.config()
else:
r.run()