Authors: Author of Year.
[gitstats.git] / statgit
blobd5dad74bacf9091d99df2cef82837b2bb3feef0c
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.author_of_year = {} # year -> author -> commits
98 self.commits_by_month = {} # month -> commits
100 lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
101 for line in lines:
102 parts = line.split(' ')
103 stamp = int(parts[0])
104 author = ' '.join(parts[1:])
106 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
107 if yymm in self.author_of_month:
108 if author in self.author_of_month[yymm]:
109 self.author_of_month[yymm][author] += 1
110 else:
111 self.author_of_month[yymm][author] = 1
112 else:
113 self.author_of_month[yymm] = {}
114 self.author_of_month[yymm][author] = 1
115 if yymm in self.commits_by_month:
116 self.commits_by_month[yymm] += 1
117 else:
118 self.commits_by_month[yymm] = 1
120 yy = datetime.datetime.fromtimestamp(stamp).year
121 if yy in self.author_of_year:
122 if author in self.author_of_year[yy]:
123 self.author_of_year[yy][author] += 1
124 else:
125 self.author_of_year[yy][author] = 1
126 else:
127 self.author_of_year[yy] = {}
128 self.author_of_year[yy][author] = 1
130 print self.author_of_month
132 def getActivityByDayOfWeek(self):
133 return self.activity_by_day_of_week
135 def getActivityByHourOfDay(self):
136 return self.activity_by_hour_of_day
138 def getAuthorInfo(self, author):
139 commits = int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author))
140 commits_frac = (100 * float(commits)) / self.getTotalCommits()
141 date_first = '0000-00-00'
142 date_last = '0000-00-00'
143 rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
144 rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
145 date_first = self.revToDate(rev_first)
146 date_last = self.revToDate(rev_last)
148 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
149 return res
151 def getAuthors(self):
152 lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
153 return lines.split('\n')
155 def getTags(self):
156 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
157 return lines.split('\n')
159 def getTagDate(self, tag):
160 return self.revToDate('tags/' + tag)
162 def getTotalAuthors(self):
163 return self.total_authors
165 def getTotalCommits(self):
166 return self.total_commits
168 def getTotalFiles(self):
169 return self.total_files
171 def getTotalLOC(self):
172 return self.total_lines
174 def revToDate(self, rev):
175 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
176 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
178 class ReportCreator:
179 def __init__(self):
180 pass
182 def create(self, data, path):
183 self.data = data
184 self.path = path
186 class HTMLReportCreator(ReportCreator):
187 def create(self, data, path):
188 ReportCreator.create(self, data, path)
190 f = open(path + "/index.html", 'w')
191 format = '%Y-%m-%d %H:%m:%S'
192 self.printHeader(f)
194 f.write('<h1>StatGit</h1>')
196 f.write('<dl>');
197 f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
198 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
199 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
200 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
201 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
202 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
203 f.write('</dl>');
205 f.write("""<ul>
206 <li><a href="activity.html">Activity</a></li>
207 <li><a href="authors.html">Authors</a></li>
208 <li><a href="files.html">Files</a></li>
209 <li><a href="lines.html">Lines</a></li>
210 </ul>
211 """)
213 f.write('<h2>Tags</h2>')
214 f.write('<table>')
215 f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
216 for tag in data.getTags():
217 f.write('<tr><td>%s</td><td></td></tr>' % tag)
218 f.write('</table>')
220 f.write('</body>\n</html>');
221 f.close()
223 # activity.html
224 f = open(path + '/activity.html', 'w')
225 self.printHeader(f)
226 f.write('<h1>Activity</h1>')
228 f.write('<h2>Last 30 days</h2>')
230 f.write('<h2>Last 12 months</h2>')
232 f.write('\n<h2>Hour of Day</h2>\n\n')
233 hour_of_day = data.getActivityByHourOfDay()
234 f.write('<table><tr><th>Hour</th>')
235 for i in range(1, 25):
236 f.write('<th>%d</th>' % i)
237 f.write('</tr>\n<tr><th>Commits</th>')
238 for i in range(0, 24):
239 if i in hour_of_day:
240 f.write('<td>%d</td>' % hour_of_day[i])
241 else:
242 f.write('<td>0</td>')
243 f.write('</tr>\n<tr><th>%</th>')
244 totalcommits = data.getTotalCommits()
245 for i in range(0, 24):
246 if i in hour_of_day:
247 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
248 else:
249 f.write('<td>0.00</td>')
250 f.write('</tr></table>')
252 ### Day of Week
253 # TODO show also by hour of weekday?
254 f.write('\n<h2>Day of Week</h2>\n\n')
255 day_of_week = data.getActivityByDayOfWeek()
256 f.write('<table>')
257 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
258 for d in range(0, 7):
259 f.write('<tr>')
260 f.write('<th>%d</th>' % (d + 1))
261 if d in day_of_week:
262 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
263 else:
264 f.write('<td>0</td>')
265 f.write('</tr>')
266 f.write('</table>')
268 f.close()
270 # authors.html
271 f = open(path + '/authors.html', 'w')
272 self.printHeader(f)
274 f.write('<h1>Authors</h1>')
276 f.write('\n<h2>List of authors</h2>\n\n')
278 f.write('<table class="authors">')
279 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
280 for author in data.getAuthors():
281 info = data.getAuthorInfo(author)
282 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']))
283 f.write('</table>')
285 f.write('\n<h2>Author of Month</h2>\n\n')
286 f.write('<table>')
287 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
288 for yymm in reversed(sorted(data.author_of_month.keys())):
289 authordict = data.author_of_month[yymm]
290 authors = getkeyssortedbyvalues(authordict)
291 authors.reverse()
292 commits = data.author_of_month[yymm][authors[0]]
293 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]))
295 f.write('</table>')
297 f.write('\n<h2>Author of Year</h2>\n\n')
298 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
299 for yy in reversed(sorted(data.author_of_year.keys())):
300 authordict = data.author_of_year[yy]
301 authors = getkeyssortedbyvalues(authordict)
302 authors.reverse()
303 commits = data.author_of_year[yy][authors[0]]
304 f.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy, authors[0], commits))
305 f.write('</table>')
307 f.write('</body></html>')
308 f.close()
309 pass
311 def printHeader(self, f):
312 f.write("""<html>
313 <head>
314 <title>StatGit</title>
315 <link rel="stylesheet" href="statgit.css" type="text/css" />
316 </head>
317 <body>
318 """)
321 usage = """
322 Usage: statgit [options] <gitpath> <outputpath>
324 Options:
325 -o html
328 if len(sys.argv) < 3:
329 print usage
330 sys.exit(0)
332 gitpath = sys.argv[1]
333 outputpath = sys.argv[2]
335 print 'Git path: %s' % gitpath
336 print 'Output path: %s' % outputpath
338 os.chdir(gitpath)
340 print 'Collecting data...'
341 data = GitDataCollector()
342 data.collect(gitpath)
344 print 'Generating report...'
345 report = HTMLReportCreator()
346 report.create(data, outputpath)