1 | // $Id: Stats.cc 371 2007-06-19 18:54:41Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Peter Johansson |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
6 | |
---|
7 | This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest |
---|
8 | |
---|
9 | svndigest is free software; you can redistribute it and/or modify it |
---|
10 | under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | svndigest is distributed in the hope that it will be useful, but |
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Stats.h" |
---|
26 | |
---|
27 | #include "GnuplotFE.h" |
---|
28 | #include "SVNblame.h" |
---|
29 | #include "SVNinfo.h" |
---|
30 | #include "utility.h" |
---|
31 | |
---|
32 | #include <algorithm> |
---|
33 | #include <cassert> |
---|
34 | #include <cstdlib> |
---|
35 | #include <iostream> |
---|
36 | #include <map> |
---|
37 | #include <numeric> |
---|
38 | #include <string> |
---|
39 | #include <sstream> |
---|
40 | #include <unistd.h> |
---|
41 | #include <utility> |
---|
42 | #include <vector> |
---|
43 | |
---|
44 | |
---|
45 | namespace theplu{ |
---|
46 | namespace svndigest{ |
---|
47 | |
---|
48 | |
---|
49 | Stats::Stats(const std::string& path) |
---|
50 | { |
---|
51 | // Make sure latest revision is set properly |
---|
52 | SVNinfo svn_info(path); |
---|
53 | revision_=svn_info.rev(); |
---|
54 | last_changed_rev_=svn_info.last_changed_rev(); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | std::vector<u_int> Stats::accumulated(const Map_& map) const |
---|
59 | { |
---|
60 | // sum of all users |
---|
61 | std::vector<u_int> sum(revision_+1); |
---|
62 | sum=std::accumulate(map.begin(), map.end(), sum, |
---|
63 | PairValuePlus<std::string,u_int>()); |
---|
64 | |
---|
65 | // calculate accumulated sum |
---|
66 | std::vector<u_int> accum(sum.size()); |
---|
67 | std::partial_sum(sum.begin(),sum.end(),accum.begin()); |
---|
68 | assert(sum.size()==accum.size()); |
---|
69 | return accum; |
---|
70 | } |
---|
71 | |
---|
72 | std::vector<u_int> Stats::accumulated(const Map_& map, |
---|
73 | const std::string& user) const |
---|
74 | { |
---|
75 | if (!map.count(user)) |
---|
76 | return std::vector<u_int>(last_changed_rev_,0); |
---|
77 | std::vector<u_int> vec=(map.find(user))->second; |
---|
78 | |
---|
79 | // static_cast to remove annoying compiler warning |
---|
80 | if (vec.size() < static_cast<size_t>(revision_+1)) |
---|
81 | vec.insert(vec.end(), revision_+1-vec.size(), 0); |
---|
82 | |
---|
83 | std::vector<u_int> accum(vec.size()); |
---|
84 | std::partial_sum(vec.begin(),vec.end(),accum.begin()); |
---|
85 | return accum; |
---|
86 | } |
---|
87 | |
---|
88 | void Stats::add(const std::string& user, const u_int& rev, |
---|
89 | const Parser::line_type& lt) |
---|
90 | { |
---|
91 | authors_.insert(user); |
---|
92 | |
---|
93 | std::vector<u_int>* total = &(total_[user]); |
---|
94 | assert(total); |
---|
95 | if (total->size() < rev+1){ |
---|
96 | total->reserve(revision_ + 1); |
---|
97 | total->insert(total->end(), rev - total->size(), 0); |
---|
98 | total->push_back(1); |
---|
99 | } |
---|
100 | else |
---|
101 | ++(*total)[rev]; |
---|
102 | |
---|
103 | std::vector<u_int>* code = &(code_[user]); |
---|
104 | assert(code); |
---|
105 | if (code->size() < rev+1){ |
---|
106 | code->reserve(revision_ + 1); |
---|
107 | code->insert(code->end(), rev - code->size(), 0); |
---|
108 | if (lt == Parser::code) |
---|
109 | code->push_back(1); |
---|
110 | else |
---|
111 | code->push_back(0); |
---|
112 | } |
---|
113 | else if (lt == Parser::code) |
---|
114 | ++(*code)[rev]; |
---|
115 | |
---|
116 | std::vector<u_int>* comments = &(comments_[user]); |
---|
117 | assert(comments); |
---|
118 | if (comments->size() < rev+1){ |
---|
119 | comments->reserve(revision_ + 1); |
---|
120 | comments->insert(comments->end(), rev - comments->size(), 0); |
---|
121 | if (lt == Parser::comment) |
---|
122 | comments->push_back(1); |
---|
123 | else |
---|
124 | comments->push_back(0); |
---|
125 | } |
---|
126 | else if (lt == Parser::comment) |
---|
127 | ++(*comments)[rev]; |
---|
128 | |
---|
129 | std::vector<u_int>* empty = &(empty_[user]); |
---|
130 | assert(empty); |
---|
131 | if (empty->size() < rev+1){ |
---|
132 | empty->reserve(revision_ + 1); |
---|
133 | empty->insert(empty->end(), rev - empty->size(), 0); |
---|
134 | if (lt == Parser::empty) |
---|
135 | empty->push_back(1); |
---|
136 | else |
---|
137 | empty->push_back(0); |
---|
138 | } |
---|
139 | else if (lt == Parser::empty) |
---|
140 | ++(*empty)[rev]; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | void Stats::parse(const std::string& path) |
---|
145 | { |
---|
146 | Parser parser(path); |
---|
147 | std::vector<Parser::line_type>::const_iterator count=parser.type().begin(); |
---|
148 | |
---|
149 | SVNblame svn_blame(path); |
---|
150 | while (const SVNblame::blame_information * bi=svn_blame.next()) { |
---|
151 | assert(bi); |
---|
152 | add(bi->author, bi->revision, *count); |
---|
153 | ++count; |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | void Stats::plot_init(const std::string& filename) const |
---|
160 | { |
---|
161 | GnuplotFE* gp=GnuplotFE::instance(); |
---|
162 | gp->command("set term png"); |
---|
163 | gp->command("set output '"+filename+"'"); |
---|
164 | gp->command("set xtics nomirror"); |
---|
165 | gp->command("set ytics nomirror"); |
---|
166 | gp->command("set key default"); |
---|
167 | gp->command("set key left Left reverse"); |
---|
168 | gp->command("set multiplot"); |
---|
169 | } |
---|
170 | |
---|
171 | std::string Stats::plot(const std::string& filename, |
---|
172 | const std::string& linetype) const |
---|
173 | { |
---|
174 | plot_init(filename); |
---|
175 | GnuplotFE* gp=GnuplotFE::instance(); |
---|
176 | const Map_* stat=NULL; |
---|
177 | if (linetype=="total") |
---|
178 | stat = &total_; |
---|
179 | else if (linetype=="code") |
---|
180 | stat = &code_; |
---|
181 | else if (linetype=="comments") |
---|
182 | stat = &comments_; |
---|
183 | else if (linetype=="empty") |
---|
184 | stat = &empty_; |
---|
185 | assert(stat); |
---|
186 | std::vector<u_int> total=accumulated(*stat); |
---|
187 | double yrange_max=1.03*total.back()+1; |
---|
188 | gp->yrange(yrange_max); |
---|
189 | |
---|
190 | |
---|
191 | typedef std::vector<std::pair<std::string, std::vector<u_int> > > vec_type; |
---|
192 | vec_type author_cont; |
---|
193 | author_cont.reserve(stat->size()); |
---|
194 | for (MapConstIter_ i= stat->begin(); i != stat->end(); ++i) { |
---|
195 | author_cont.push_back(std::make_pair(i->first, |
---|
196 | accumulated(*stat,i->first))); |
---|
197 | } |
---|
198 | |
---|
199 | LessReversed<std::vector<u_int> > lr; |
---|
200 | PairSecondCompare<std::string, std::vector<u_int>, |
---|
201 | LessReversed<std::vector<u_int> > > compare(lr); |
---|
202 | std::sort(author_cont.begin(), author_cont.end(), compare); |
---|
203 | |
---|
204 | size_t plotno=author_cont.size(); |
---|
205 | std::stringstream ss; |
---|
206 | vec_type::iterator end(author_cont.end()); |
---|
207 | for (vec_type::iterator i(author_cont.begin()); i!=end; ++i) { |
---|
208 | ss.str(""); |
---|
209 | ss << "set key height " << 2*plotno; |
---|
210 | gp->command(ss.str()); |
---|
211 | ss.str(""); |
---|
212 | ss << i->second.back() << " " << i->first; |
---|
213 | gp->yrange(yrange_max); |
---|
214 | gp->linetitle(ss.str()); |
---|
215 | ss.str(""); |
---|
216 | ss << "steps " << --plotno+2; |
---|
217 | gp->linestyle(ss.str()); |
---|
218 | gp->plot(i->second, ss.str()); |
---|
219 | } |
---|
220 | ss.str(""); |
---|
221 | ss << total.back() << " total"; |
---|
222 | gp->command("set key height 0"); |
---|
223 | gp->linetitle(ss.str()); |
---|
224 | gp->linestyle("steps 1"); |
---|
225 | gp->plot(total); |
---|
226 | |
---|
227 | gp->command("unset multiplot"); |
---|
228 | gp->yrange(); |
---|
229 | |
---|
230 | return filename; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | void Stats::plot_summary(const std::string& filename) const |
---|
235 | { |
---|
236 | plot_init(filename); |
---|
237 | GnuplotFE* gp=GnuplotFE::instance(); |
---|
238 | std::vector<u_int> total=accumulated(total_); |
---|
239 | double yrange_max=1.03*total.back()+1; |
---|
240 | gp->yrange(yrange_max); |
---|
241 | std::stringstream ss; |
---|
242 | |
---|
243 | ss.str(""); |
---|
244 | std::vector<u_int> x=accumulated(code_); |
---|
245 | ss << x.back() << " code"; |
---|
246 | gp->command("set key height 2"); |
---|
247 | gp->linetitle(ss.str()); |
---|
248 | gp->linestyle("steps 2"); |
---|
249 | gp->plot(x); |
---|
250 | |
---|
251 | ss.str(""); |
---|
252 | x=accumulated(comments_); |
---|
253 | ss << x.back() << " comment"; |
---|
254 | gp->command("set key height 4"); |
---|
255 | gp->linetitle(ss.str()); |
---|
256 | gp->linestyle("steps 3"); |
---|
257 | gp->plot(x); |
---|
258 | |
---|
259 | ss.str(""); |
---|
260 | x=accumulated(empty_); |
---|
261 | ss << x.back() << " other"; |
---|
262 | gp->command("set key height 6"); |
---|
263 | gp->linetitle(ss.str()); |
---|
264 | gp->linestyle("steps 4"); |
---|
265 | gp->plot(x); |
---|
266 | |
---|
267 | ss.str(""); |
---|
268 | ss << total.back() << " total"; |
---|
269 | gp->command("set key height 0"); |
---|
270 | gp->linetitle(ss.str()); |
---|
271 | gp->linestyle("steps 1"); |
---|
272 | gp->plot(total); |
---|
273 | |
---|
274 | gp->command("unset multiplot"); |
---|
275 | gp->yrange(); |
---|
276 | |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | Stats& Stats::operator+=(const Stats& other) |
---|
281 | { |
---|
282 | for (MapConstIter_ o_i= other.code_.begin(); |
---|
283 | o_i != other.code_.end(); ++o_i) |
---|
284 | { |
---|
285 | std::pair<MapIter_,bool> result = code_.insert(*o_i); |
---|
286 | if (!result.second) |
---|
287 | code_[(*(result.first)).first] = |
---|
288 | VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second ); |
---|
289 | |
---|
290 | } |
---|
291 | |
---|
292 | for (MapConstIter_ o_i= other.comments_.begin(); |
---|
293 | o_i != other.comments_.end(); ++o_i) |
---|
294 | { |
---|
295 | std::pair<MapIter_,bool> result = comments_.insert(*o_i); |
---|
296 | if (!result.second) |
---|
297 | comments_[(*(result.first)).first] = |
---|
298 | VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second ); |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | for (MapConstIter_ o_i= other.empty_.begin(); |
---|
303 | o_i != other.empty_.end(); ++o_i) |
---|
304 | { |
---|
305 | std::pair<MapIter_,bool> result = empty_.insert(*o_i); |
---|
306 | if (!result.second) |
---|
307 | empty_[(*(result.first)).first] = |
---|
308 | VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second ); |
---|
309 | |
---|
310 | } |
---|
311 | |
---|
312 | for (MapConstIter_ o_i= other.total_.begin(); |
---|
313 | o_i != other.total_.end(); ++o_i) |
---|
314 | { |
---|
315 | std::pair<MapIter_,bool> result = total_.insert(*o_i); |
---|
316 | if (!result.second) |
---|
317 | total_[(*(result.first)).first] = |
---|
318 | VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second ); |
---|
319 | |
---|
320 | } |
---|
321 | |
---|
322 | if (!other.authors().empty()) |
---|
323 | authors_.insert(other.authors().begin(), other.authors().end()); |
---|
324 | return *this; |
---|
325 | } |
---|
326 | |
---|
327 | }} // end of namespace svndigest and namespace theplu |
---|