1 | // $Id: ClassicStats.cc 487 2007-10-13 22:20:17Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Peter Johansson |
---|
5 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
6 | |
---|
7 | This file is part of svndigest, http://trac.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 "ClassicStats.h" |
---|
26 | |
---|
27 | #include "Functor.h" |
---|
28 | #include "GnuplotFE.h" |
---|
29 | #include "SVNblame.h" |
---|
30 | #include "SVNinfo.h" |
---|
31 | #include "utility.h" |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <cassert> |
---|
35 | #include <cstdlib> |
---|
36 | #include <fstream> |
---|
37 | #include <iostream> |
---|
38 | #include <iterator> |
---|
39 | #include <map> |
---|
40 | #include <numeric> |
---|
41 | #include <string> |
---|
42 | #include <sstream> |
---|
43 | #include <unistd.h> |
---|
44 | #include <utility> |
---|
45 | #include <vector> |
---|
46 | |
---|
47 | |
---|
48 | namespace theplu{ |
---|
49 | namespace svndigest{ |
---|
50 | |
---|
51 | |
---|
52 | ClassicStats::ClassicStats(const std::string& path) |
---|
53 | : Stats(path) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | std::vector<u_int> ClassicStats::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> ClassicStats::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 ClassicStats::add(const std::string& user, const u_int& rev, |
---|
89 | const Parser::line_type& lt) |
---|
90 | { |
---|
91 | assert(user.size()); |
---|
92 | add_author(user); |
---|
93 | |
---|
94 | std::vector<u_int>* total = &(total_[user]); |
---|
95 | assert(total); |
---|
96 | if (total->size() < rev+1){ |
---|
97 | total->reserve(revision() + 1); |
---|
98 | total->insert(total->end(), rev - total->size(), 0); |
---|
99 | total->push_back(1); |
---|
100 | } |
---|
101 | else |
---|
102 | ++(*total)[rev]; |
---|
103 | |
---|
104 | std::vector<u_int>* code = &(code_[user]); |
---|
105 | assert(code); |
---|
106 | if (code->size() < rev+1){ |
---|
107 | code->reserve(revision() + 1); |
---|
108 | code->insert(code->end(), rev - code->size(), 0); |
---|
109 | if (lt == Parser::code) |
---|
110 | code->push_back(1); |
---|
111 | else |
---|
112 | code->push_back(0); |
---|
113 | } |
---|
114 | else if (lt == Parser::code) |
---|
115 | ++(*code)[rev]; |
---|
116 | |
---|
117 | std::vector<u_int>* comments = &(comments_[user]); |
---|
118 | assert(comments); |
---|
119 | if (comments->size() < rev+1){ |
---|
120 | comments->reserve(revision() + 1); |
---|
121 | comments->insert(comments->end(), rev - comments->size(), 0); |
---|
122 | if (lt == Parser::comment) |
---|
123 | comments->push_back(1); |
---|
124 | else |
---|
125 | comments->push_back(0); |
---|
126 | } |
---|
127 | else if (lt == Parser::comment) |
---|
128 | ++(*comments)[rev]; |
---|
129 | |
---|
130 | std::vector<u_int>* empty = &(empty_[user]); |
---|
131 | assert(empty); |
---|
132 | if (empty->size() < rev+1){ |
---|
133 | empty->reserve(revision() + 1); |
---|
134 | empty->insert(empty->end(), rev - empty->size(), 0); |
---|
135 | if (lt == Parser::empty) |
---|
136 | empty->push_back(1); |
---|
137 | else |
---|
138 | empty->push_back(0); |
---|
139 | } |
---|
140 | else if (lt == Parser::empty) |
---|
141 | ++(*empty)[rev]; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | bool ClassicStats::do_load_cache(std::istream& is) |
---|
146 | { |
---|
147 | svn_revnum_t rev; |
---|
148 | is >> rev; |
---|
149 | if (rev<last_changed_rev()){ |
---|
150 | return false; // cache is not up to date |
---|
151 | } |
---|
152 | reset(); |
---|
153 | size_t a_size=0; |
---|
154 | is >> a_size; |
---|
155 | std::string str; |
---|
156 | while (authors().size()<a_size){ |
---|
157 | getline(is, str); |
---|
158 | assert(str.size()); |
---|
159 | add_author(str); |
---|
160 | } |
---|
161 | getline(is, str); |
---|
162 | if (str!=code_cache()){ |
---|
163 | return false; |
---|
164 | } |
---|
165 | load(is, code_); |
---|
166 | getline(is, str); |
---|
167 | getline(is, str); |
---|
168 | if (str!=comments_cache()){ |
---|
169 | return false; |
---|
170 | } |
---|
171 | load(is, comments_); |
---|
172 | getline(is, str); |
---|
173 | getline(is, str); |
---|
174 | if (str!=empty_cache()){ |
---|
175 | return false; |
---|
176 | } |
---|
177 | load(is, empty_); |
---|
178 | getline(is, str); |
---|
179 | getline(is, str); |
---|
180 | if (str!=total_cache()){ |
---|
181 | return false; |
---|
182 | } |
---|
183 | load(is, total_); |
---|
184 | getline(is,str); |
---|
185 | getline(is,str); |
---|
186 | return str==end_of_cache(); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void ClassicStats::load(std::istream& is, Map_& m) |
---|
191 | { |
---|
192 | while (m.size() < authors().size() && is.good()) { |
---|
193 | std::string name; |
---|
194 | std::getline(is, name); |
---|
195 | assert(name.size()); |
---|
196 | std::vector<u_int>& vec=m[name]; |
---|
197 | size_t revs=0; |
---|
198 | is >> revs; |
---|
199 | vec.reserve(revs); |
---|
200 | while (vec.size() < revs) { |
---|
201 | u_int tmp; |
---|
202 | is >> tmp; |
---|
203 | vec.push_back(tmp); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | void ClassicStats::do_parse(const std::string& path) |
---|
210 | { |
---|
211 | Parser parser(path); |
---|
212 | std::vector<Parser::line_type>::const_iterator count=parser.type().begin(); |
---|
213 | |
---|
214 | SVNblame svn_blame(path); |
---|
215 | while (svn_blame.valid()) { |
---|
216 | add(svn_blame.author(), svn_blame.revision(), *count); |
---|
217 | svn_blame.next_line(); |
---|
218 | ++count; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | std::string ClassicStats::do_plot(const std::string& filename, |
---|
224 | const std::string& linetype) const |
---|
225 | { |
---|
226 | plot_init(filename); |
---|
227 | GnuplotFE* gp=GnuplotFE::instance(); |
---|
228 | const Map_* stat=NULL; |
---|
229 | if (linetype=="total") |
---|
230 | stat = &total_; |
---|
231 | else if (linetype=="code") |
---|
232 | stat = &code_; |
---|
233 | else if (linetype=="comments") |
---|
234 | stat = &comments_; |
---|
235 | else if (linetype=="empty") |
---|
236 | stat = &empty_; |
---|
237 | assert(stat); |
---|
238 | std::vector<u_int> total=accumulated(*stat); |
---|
239 | double yrange_max=1.03*total.back()+1; |
---|
240 | gp->yrange(yrange_max); |
---|
241 | |
---|
242 | typedef std::vector<std::pair<std::string, std::vector<u_int> > > vec_type; |
---|
243 | vec_type author_cont; |
---|
244 | author_cont.reserve(stat->size()); |
---|
245 | for (MapConstIter_ i= stat->begin(); i != stat->end(); ++i) { |
---|
246 | author_cont.push_back(std::make_pair(i->first, |
---|
247 | accumulated(*stat,i->first))); |
---|
248 | } |
---|
249 | |
---|
250 | LessReversed<std::vector<u_int> > lr; |
---|
251 | PairSecondCompare<std::string, std::vector<u_int>, |
---|
252 | LessReversed<std::vector<u_int> > > compare(lr); |
---|
253 | std::sort(author_cont.begin(), author_cont.end(), compare); |
---|
254 | |
---|
255 | size_t plotno=author_cont.size(); |
---|
256 | std::stringstream ss; |
---|
257 | vec_type::iterator end(author_cont.end()); |
---|
258 | for (vec_type::iterator i(author_cont.begin()); i!=end; ++i) { |
---|
259 | ss.str(""); |
---|
260 | ss << "set key height " << 2*plotno; |
---|
261 | gp->command(ss.str()); |
---|
262 | ss.str(""); |
---|
263 | ss << i->second.back() << " " << i->first; |
---|
264 | gp->yrange(yrange_max); |
---|
265 | gp->linetitle(ss.str()); |
---|
266 | ss.str(""); |
---|
267 | ss << "steps " << --plotno+2; |
---|
268 | gp->linestyle(ss.str()); |
---|
269 | gp->plot(i->second); |
---|
270 | } |
---|
271 | ss.str(""); |
---|
272 | ss << total.back() << " total"; |
---|
273 | gp->command("set key height 0"); |
---|
274 | gp->linetitle(ss.str()); |
---|
275 | gp->linestyle("steps 1"); |
---|
276 | gp->plot(total); |
---|
277 | |
---|
278 | gp->command("unset multiplot"); |
---|
279 | gp->yrange(); |
---|
280 | |
---|
281 | return filename; |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | void ClassicStats::do_print(std::ostream& os) const |
---|
286 | { |
---|
287 | os << last_changed_rev() << " "; |
---|
288 | os << authors().size() << " "; |
---|
289 | |
---|
290 | std::copy(authors().begin(), authors().end(), |
---|
291 | std::ostream_iterator<std::string>(os, "\n")); |
---|
292 | os << code_cache() << "\n"; |
---|
293 | do_print(os, code_); |
---|
294 | os << "\n" << comments_cache() << "\n"; |
---|
295 | do_print(os, comments_); |
---|
296 | os << "\n" << empty_cache() << "\n"; |
---|
297 | do_print(os, empty_); |
---|
298 | os << "\n" << total_cache() << "\n"; |
---|
299 | do_print(os, total_); |
---|
300 | os << "\n" << end_of_cache() << "\n"; |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | void ClassicStats::do_print(std::ostream& os, const Map_& m) const |
---|
305 | { |
---|
306 | for (MapConstIter_ i(m.begin()); i!=m.end(); ++i){ |
---|
307 | os << i->first << "\n"; |
---|
308 | os << i->second.size() << " "; |
---|
309 | std::copy(i->second.begin(), i->second.end(), |
---|
310 | std::ostream_iterator<u_int>(os, " ")); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | std::vector<u_int> ClassicStats::vector(std::string type, |
---|
316 | std::string user) const |
---|
317 | { |
---|
318 | const Map_* map = NULL; |
---|
319 | if (type=="code") |
---|
320 | map = &code_; |
---|
321 | else if (type=="comments") |
---|
322 | map = &comments_; |
---|
323 | else if (type=="other") |
---|
324 | map = &empty_; |
---|
325 | assert(map && "type is of invalid type"); |
---|
326 | if (!map->count(user)) |
---|
327 | return std::vector<u_int>(last_changed_rev(),0); |
---|
328 | const std::vector<u_int>& vec=(map->find(user))->second; |
---|
329 | |
---|
330 | std::vector<u_int> accum(vec.size()); |
---|
331 | std::partial_sum(vec.begin(),vec.end(),accum.begin()); |
---|
332 | // static_cast to remove annoying compiler warning |
---|
333 | if (accum.size() < static_cast<size_t>(revision()+1)) |
---|
334 | accum.insert(accum.end(), revision()+1-vec.size(), accum.back()); |
---|
335 | return accum; |
---|
336 | |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | ClassicStats& ClassicStats::operator+=(const ClassicStats& rhs) |
---|
341 | { |
---|
342 | base_add(rhs.code_.begin(), rhs.code_.end(), code_); |
---|
343 | base_add(rhs.comments_.begin(), rhs.comments_.end(), comments_); |
---|
344 | base_add(rhs.empty_.begin(), rhs.empty_.end(), empty_); |
---|
345 | base_add(rhs.total_.begin(), rhs.total_.end(), total_); |
---|
346 | |
---|
347 | base_add(rhs); |
---|
348 | return *this; |
---|
349 | } |
---|
350 | |
---|
351 | }} // end of namespace svndigest and namespace theplu |
---|