Optimized getAuthors().
[gitstats.git] / statgit
blob818a4f6e872d87d33635af0ef9a70ff2e1d7a04b
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 self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
78 # author of the month
79 self.author_of_month = {} # month -> author -> commits
80 self.author_of_year = {} # year -> author -> commits
81 self.commits_by_month = {} # month -> commits
82 self.first_commit_stamp = 0
83 self.last_commit_stamp = 0
85 # TODO also collect statistics for "last 30 days"/"last 12 months"
86 lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
87 for line in lines:
88 # linux-2.6 says "<unknown>" for one line O_o
89 parts = line.split(' ')
90 author = ''
91 try:
92 stamp = int(parts[0])
93 except ValueError:
94 stamp = 0
95 if len(parts) > 1:
96 author = ' '.join(parts[1:])
97 date = datetime.datetime.fromtimestamp(float(stamp))
99 # First and last commit stamp
100 if self.last_commit_stamp == 0:
101 self.last_commit_stamp = stamp
102 self.first_commit_stamp = stamp
104 # activity
105 # hour
106 hour = date.hour
107 if hour in self.activity_by_hour_of_day:
108 self.activity_by_hour_of_day[hour] += 1
109 else:
110 self.activity_by_hour_of_day[hour] = 1
112 # day
113 day = date.weekday()
114 if day in self.activity_by_day_of_week:
115 self.activity_by_day_of_week[day] += 1
116 else:
117 self.activity_by_day_of_week[day] = 1
119 # author stats
120 if author not in self.authors:
121 self.authors[author] = {}
122 # TODO commits
123 if 'last_commit_stamp' not in self.authors[author]:
124 self.authors[author]['last_commit_stamp'] = stamp
125 self.authors[author]['first_commit_stamp'] = stamp
126 if 'commits' in self.authors[author]:
127 self.authors[author]['commits'] += 1
128 else:
129 self.authors[author]['commits'] = 1
131 # author of the month/year
132 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
133 if yymm in self.author_of_month:
134 if author in self.author_of_month[yymm]:
135 self.author_of_month[yymm][author] += 1
136 else:
137 self.author_of_month[yymm][author] = 1
138 else:
139 self.author_of_month[yymm] = {}
140 self.author_of_month[yymm][author] = 1
141 if yymm in self.commits_by_month:
142 self.commits_by_month[yymm] += 1
143 else:
144 self.commits_by_month[yymm] = 1
146 yy = datetime.datetime.fromtimestamp(stamp).year
147 if yy in self.author_of_year:
148 if author in self.author_of_year[yy]:
149 self.author_of_year[yy][author] += 1
150 else:
151 self.author_of_year[yy][author] = 1
152 else:
153 self.author_of_year[yy] = {}
154 self.author_of_year[yy][author] = 1
156 def getActivityByDayOfWeek(self):
157 return self.activity_by_day_of_week
159 def getActivityByHourOfDay(self):
160 return self.activity_by_hour_of_day
162 def getAuthorInfo(self, author):
163 a = self.authors[author]
165 commits = a['commits']
166 commits_frac = (100 * float(commits)) / self.getTotalCommits()
167 date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp']).strftime('%Y-%m-%d')
168 date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp']).strftime('%Y-%m-%d')
170 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
171 return res
173 def getAuthors(self):
174 return self.authors.keys()
176 def getFirstCommitDate(self):
177 return datetime.datetime.fromtimestamp(self.first_commit_stamp)
179 def getLastCommitDate(self):
180 return datetime.datetime.fromtimestamp(self.last_commit_stamp)
182 def getTags(self):
183 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
184 return lines.split('\n')
186 def getTagDate(self, tag):
187 return self.revToDate('tags/' + tag)
189 def getTotalAuthors(self):
190 return self.total_authors
192 def getTotalCommits(self):
193 return self.total_commits
195 def getTotalFiles(self):
196 return self.total_files
198 def getTotalLOC(self):
199 return self.total_lines
201 def revToDate(self, rev):
202 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
203 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
205 class ReportCreator:
206 def __init__(self):
207 pass
209 def create(self, data, path):
210 self.data = data
211 self.path = path
213 class HTMLReportCreator(ReportCreator):
214 def create(self, data, path):
215 ReportCreator.create(self, data, path)
217 f = open(path + "/index.html", 'w')
218 format = '%Y-%m-%d %H:%m:%S'
219 self.printHeader(f)
221 f.write('<h1>StatGit</h1>')
223 self.printNav(f)
225 f.write('<dl>');
226 f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
227 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
228 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
229 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
230 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
231 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
232 f.write('</dl>');
235 #f.write('<h2>Tags</h2>')
236 #f.write('<table>')
237 #f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
238 #for tag in data.getTags():
239 # f.write('<tr><td>%s</td><td></td></tr>' % tag)
240 #f.write('</table>')
242 f.write('</body>\n</html>');
243 f.close()
245 # activity.html
246 f = open(path + '/activity.html', 'w')
247 self.printHeader(f)
248 f.write('<h1>Activity</h1>')
249 self.printNav(f)
251 f.write('<h2>Last 30 days</h2>')
253 f.write('<h2>Last 12 months</h2>')
255 f.write('\n<h2>Hour of Day</h2>\n\n')
256 hour_of_day = data.getActivityByHourOfDay()
257 f.write('<table><tr><th>Hour</th>')
258 for i in range(1, 25):
259 f.write('<th>%d</th>' % i)
260 f.write('</tr>\n<tr><th>Commits</th>')
261 for i in range(0, 24):
262 if i in hour_of_day:
263 f.write('<td>%d</td>' % hour_of_day[i])
264 else:
265 f.write('<td>0</td>')
266 f.write('</tr>\n<tr><th>%</th>')
267 totalcommits = data.getTotalCommits()
268 for i in range(0, 24):
269 if i in hour_of_day:
270 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
271 else:
272 f.write('<td>0.00</td>')
273 f.write('</tr></table>')
275 ### Day of Week
276 # TODO show also by hour of weekday?
277 f.write('\n<h2>Day of Week</h2>\n\n')
278 day_of_week = data.getActivityByDayOfWeek()
279 f.write('<table>')
280 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
281 for d in range(0, 7):
282 f.write('<tr>')
283 f.write('<th>%d</th>' % (d + 1))
284 if d in day_of_week:
285 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
286 else:
287 f.write('<td>0</td>')
288 f.write('</tr>')
289 f.write('</table>')
291 f.close()
293 # authors.html
294 f = open(path + '/authors.html', 'w')
295 self.printHeader(f)
297 f.write('<h1>Authors</h1>')
298 self.printNav(f)
300 f.write('\n<h2>List of authors</h2>\n\n')
302 f.write('<table class="authors">')
303 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
304 for author in data.getAuthors():
305 info = data.getAuthorInfo(author)
306 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']))
307 f.write('</table>')
309 f.write('\n<h2>Author of Month</h2>\n\n')
310 f.write('<table>')
311 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
312 for yymm in reversed(sorted(data.author_of_month.keys())):
313 authordict = data.author_of_month[yymm]
314 authors = getkeyssortedbyvalues(authordict)
315 authors.reverse()
316 commits = data.author_of_month[yymm][authors[0]]
317 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]))
319 f.write('</table>')
321 f.write('\n<h2>Author of Year</h2>\n\n')
322 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
323 for yy in reversed(sorted(data.author_of_year.keys())):
324 authordict = data.author_of_year[yy]
325 authors = getkeyssortedbyvalues(authordict)
326 authors.reverse()
327 commits = data.author_of_year[yy][authors[0]]
328 f.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy, authors[0], commits))
329 f.write('</table>')
331 f.write('</body></html>')
332 f.close()
333 pass
335 def printHeader(self, f):
336 f.write("""<html>
337 <head>
338 <title>StatGit</title>
339 <link rel="stylesheet" href="statgit.css" type="text/css" />
340 </head>
341 <body>
342 """)
344 def printNav(self, f):
345 f.write("""
346 <div class="nav">
347 <li><a href="index.html">General</a></li>
348 <li><a href="activity.html">Activity</a></li>
349 <li><a href="authors.html">Authors</a></li>
350 <li><a href="files.html">Files</a></li>
351 <li><a href="lines.html">Lines</a></li>
352 </ul>
353 </div>
354 """)
357 usage = """
358 Usage: statgit [options] <gitpath> <outputpath>
360 Options:
361 -o html
364 if len(sys.argv) < 3:
365 print usage
366 sys.exit(0)
368 gitpath = sys.argv[1]
369 outputpath = sys.argv[2]
371 print 'Git path: %s' % gitpath
372 print 'Output path: %s' % outputpath
374 os.chdir(gitpath)
376 print 'Collecting data...'
377 data = GitDataCollector()
378 data.collect(gitpath)
380 print 'Generating report...'
381 report = HTMLReportCreator()
382 report.create(data, outputpath)