authors: Author of Month.
[gitstats.git] / statgit
blob26a51b9e8c6a4a3d132e982d4a90c7149f929672
1 #!/usr/bin/python
2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
3 # GPLv2
4 import commands
5 import datetime
6 import os
7 import re
8 import sys
10 def getoutput(cmd):
11 print '>> %s' % cmd
12 output = commands.getoutput(cmd)
13 return output
15 def getkeyssortedbyvalues(dict):
16 return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
18 class DataCollector:
19 def __init__(self):
20 pass
23 # This should be the main function to extract data from the repository.
24 def collect(self, dir):
25 self.dir = dir
28 # : get a dictionary of author
29 def getAuthorInfo(self, author):
30 return None
32 def getActivityByDayOfWeek(self):
33 return {}
35 def getActivityByHourOfDay(self):
36 return {}
39 # Get a list of authors
40 def getAuthors(self):
41 return []
43 def getFirstCommitDate(self):
44 return datetime.datetime.now()
46 def getLastCommitDate(self):
47 return datetime.datetime.now()
49 def getTags(self):
50 return []
52 def getTotalAuthors(self):
53 return -1
55 def getTotalCommits(self):
56 return -1
58 def getTotalFiles(self):
59 return -1
61 def getTotalLOC(self):
62 return -1
64 class GitDataCollector(DataCollector):
65 def collect(self, dir):
66 DataCollector.collect(self, dir)
68 self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
69 self.total_commits = int(getoutput('git-rev-list HEAD |wc -l'))
70 self.total_files = int(getoutput('git-ls-files |wc -l'))
71 self.total_lines = int(getoutput('git-ls-files |xargs cat |wc -l'))
73 self.activity_by_hour_of_day = {} # hour -> commits
74 self.activity_by_day_of_week = {} # day -> commits
76 # activity
77 lines = getoutput('git-rev-list HEAD --pretty=format:%at |grep -v ^commit').split('\n')
78 for stamp in lines:
79 date = datetime.datetime.fromtimestamp(float(stamp))
81 # hour
82 hour = date.hour
83 if hour in self.activity_by_hour_of_day:
84 self.activity_by_hour_of_day[hour] += 1
85 else:
86 self.activity_by_hour_of_day[hour] = 1
88 # day
89 day = date.weekday()
90 if day in self.activity_by_day_of_week:
91 self.activity_by_day_of_week[day] += 1
92 else:
93 self.activity_by_day_of_week[day] = 1
95 # TODO author of the month
96 self.author_of_month = {} # month -> author -> commits
97 self.commits_by_month = {} # month -> commits
99 lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
100 for line in lines:
101 parts = line.split(' ')
102 stamp = int(parts[0])
103 author = ' '.join(parts[1:])
105 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
106 if yymm in self.author_of_month and author in self.author_of_month[yymm]:
107 if author in self.author_of_month[yymm]:
108 self.author_of_month[yymm][author] += 1
109 else:
110 self.author_of_month[yymm][author] = 1
111 else:
112 self.author_of_month[yymm] = {}
113 self.author_of_month[yymm][author] = 1
114 if yymm in self.commits_by_month:
115 self.commits_by_month[yymm] += 1
116 else:
117 self.commits_by_month[yymm] = 1
119 print self.author_of_month
121 def getActivityByDayOfWeek(self):
122 return self.activity_by_day_of_week
124 def getActivityByHourOfDay(self):
125 return self.activity_by_hour_of_day
127 def getAuthorInfo(self, author):
128 commits = int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author))
129 commits_frac = (100 * float(commits)) / self.getTotalCommits()
130 date_first = '0000-00-00'
131 date_last = '0000-00-00'
132 rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
133 rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
134 date_first = self.revToDate(rev_first)
135 date_last = self.revToDate(rev_last)
137 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
138 return res
140 def getAuthors(self):
141 lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
142 return lines.split('\n')
144 def getTags(self):
145 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
146 return lines.split('\n')
148 def getTagDate(self, tag):
149 return self.revToDate('tags/' + tag)
151 def getTotalAuthors(self):
152 return self.total_authors
154 def getTotalCommits(self):
155 return self.total_commits
157 def getTotalFiles(self):
158 return self.total_files
160 def getTotalLOC(self):
161 return self.total_lines
163 def revToDate(self, rev):
164 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
165 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
167 class ReportCreator:
168 def __init__(self):
169 pass
171 def create(self, data, path):
172 self.data = data
173 self.path = path
175 class HTMLReportCreator(ReportCreator):
176 def create(self, data, path):
177 ReportCreator.create(self, data, path)
179 f = open(path + "/index.html", 'w')
180 format = '%Y-%m-%d %H:%m:%S'
181 self.printHeader(f)
183 f.write('<h1>StatGit</h1>')
185 f.write('<dl>');
186 f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
187 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
188 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
189 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
190 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
191 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
192 f.write('</dl>');
194 f.write("""<ul>
195 <li><a href="activity.html">Activity</a></li>
196 <li><a href="authors.html">Authors</a></li>
197 <li><a href="files.html">Files</a></li>
198 <li><a href="lines.html">Lines</a></li>
199 </ul>
200 """)
202 f.write('<h2>Tags</h2>')
203 f.write('<table>')
204 f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
205 for tag in data.getTags():
206 f.write('<tr><td>%s</td><td></td></tr>' % tag)
207 f.write('</table>')
209 f.write('</body>\n</html>');
210 f.close()
212 # activity.html
213 f = open(path + '/activity.html', 'w')
214 self.printHeader(f)
215 f.write('<h1>Activity</h1>')
217 f.write('<h2>Last 30 days</h2>')
219 f.write('<h2>Last 12 months</h2>')
221 f.write('\n<h2>Hour of Day</h2>\n\n')
222 hour_of_day = data.getActivityByHourOfDay()
223 f.write('<table><tr><th>Hour</th>')
224 for i in range(1, 25):
225 f.write('<th>%d</th>' % i)
226 f.write('</tr>\n<tr><th>Commits</th>')
227 for i in range(0, 24):
228 if i in hour_of_day:
229 f.write('<td>%d</td>' % hour_of_day[i])
230 else:
231 f.write('<td>0</td>')
232 f.write('</tr>\n<tr><th>%</th>')
233 totalcommits = data.getTotalCommits()
234 for i in range(0, 24):
235 if i in hour_of_day:
236 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
237 else:
238 f.write('<td>0.00</td>')
239 f.write('</tr></table>')
241 ### Day of Week
242 # TODO show also by hour of weekday?
243 f.write('\n<h2>Day of Week</h2>\n\n')
244 day_of_week = data.getActivityByDayOfWeek()
245 f.write('<table>')
246 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
247 for d in range(0, 7):
248 f.write('<tr>')
249 f.write('<th>%d</th>' % (d + 1))
250 if d in day_of_week:
251 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
252 else:
253 f.write('<td>0</td>')
254 f.write('</tr>')
255 f.write('</table>')
257 f.close()
259 # authors.html
260 f = open(path + '/authors.html', 'w')
261 self.printHeader(f)
263 f.write('<h1>Authors</h1>')
265 f.write('\n<h2>List of authors</h2>\n\n')
267 f.write('<table class="authors">')
268 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
269 for author in data.getAuthors():
270 info = data.getAuthorInfo(author)
271 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last']))
272 f.write('</table>')
274 f.write('\n<h2>Author of Month</h2>\n\n')
275 f.write('<table>')
276 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
277 for yymm in reversed(sorted(data.author_of_month.keys())):
278 authordict = data.author_of_month[yymm]
279 authors = getkeyssortedbyvalues(authordict)
280 authors.reverse()
281 commits = data.author_of_month[yymm][authors[0]]
282 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td></tr>' % (yymm, authors[0], commits, (100 * commits) / data.commits_by_month[yymm], data.commits_by_month[yymm]))
284 f.write('</table>')
286 f.write('\n<h2>Author of Year</h2>\n\n')
287 # TODO
289 f.write('</body></html>')
290 f.close()
291 pass
293 def printHeader(self, f):
294 f.write("""<html>
295 <head>
296 <title>StatGit</title>
297 <link rel="stylesheet" href="statgit.css" type="text/css" />
298 </head>
299 <body>
300 """)
303 usage = """
304 Usage: statgit [options] <gitpath> <outputpath>
306 Options:
307 -o html
310 if len(sys.argv) < 3:
311 print usage
312 sys.exit(0)
314 gitpath = sys.argv[1]
315 outputpath = sys.argv[2]
317 print 'Git path: %s' % gitpath
318 print 'Output path: %s' % outputpath
320 os.chdir(gitpath)
322 print 'Collecting data...'
323 data = GitDataCollector()
324 data.collect(gitpath)
326 print 'Generating report...'
327 report = HTMLReportCreator()
328 report.create(data, outputpath)