source: trunk/lib/Stats.cc @ 703

Last change on this file since 703 was 703, checked in by Peter Johansson, 15 years ago

fixed some issues that addresses #338. Want to add some tests before allowing --ignore-cache and closing ticket:338.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.9 KB
Line 
1// $Id: Stats.cc 703 2008-11-24 03:58:58Z 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://dev.thep.lu.se/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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Stats.h"
24
25#include "Functor.h"
26#include "GnuplotFE.h"
27#include "SVNblame.h"
28#include "SVNinfo.h"
29#include "utility.h"
30
31#include <algorithm>
32#include <cassert>
33#include <cstdlib>
34#include <fstream>
35#include <iostream>
36#include <iterator>
37#include <map>
38#include <numeric>
39#include <string>
40#include <sstream>
41#include <unistd.h>
42#include <utility>
43#include <vector>
44
45
46namespace theplu{
47namespace svndigest{
48
49
50  Stats::Stats(const std::string& path)
51    : stats_(std::vector<Author2Vector>(LineTypeParser::total+1))
52  {
53    // Make sure latest revision is set properly
54    SVNinfo svn_info(path);
55    revision_=svn_info.rev();
56    last_changed_rev_=svn_info.last_changed_rev();
57    reset();
58  }
59
60
61  Stats::~Stats(void)
62  {
63  }
64
65
66  void Stats::accumulate(std::vector<unsigned int>& vec,
67                         svn_revnum_t rev) const
68  {
69    assert(rev>0);
70    if (vec.empty()){
71      // just to allow call to vec.back() below
72      vec.resize(1,0);
73    }
74    else if (vec.begin()+rev-1 < vec.end())
75      std::partial_sum(vec.begin()+rev-1,vec.end(),vec.begin()+rev-1);
76    // static_cast to remove annoying compiler warning
77    if (vec.size() < static_cast<size_t>(revision()+1))
78      vec.resize(revision()+1, vec.back());
79  }
80
81
82  void Stats::accumulate_stats(svn_revnum_t rev)
83  {
84    if (!rev)
85      rev = 1;
86    for (std::set<std::string>::const_iterator iter(authors().begin());
87         iter!=authors().end(); ++iter) {
88      std::vector<unsigned int>& code = code_stats()[*iter];
89      accumulate(code, rev);
90      std::vector<unsigned int>& comments = comment_stats()[*iter];
91      accumulate(comments, rev);
92      std::vector<unsigned int>& other = other_stats()[*iter];
93      accumulate(other, rev);
94      std::vector<unsigned int>& copyright = copyright_stats()[*iter];
95      accumulate(copyright, rev);
96    }
97  }
98
99
100  void Stats::add(const std::string& user, const unsigned int& rev, 
101                  const LineTypeParser::line_type& lt, unsigned int n)
102  {
103    assert(user.size());
104    add_author(user);
105
106    add(code_stats()[user], rev, lt==LineTypeParser::code, n);
107    add(comment_stats()[user], rev, lt==LineTypeParser::comment, n);
108    add(other_stats()[user], rev, lt==LineTypeParser::other, n);
109    add(copyright_stats()[user], rev, lt==LineTypeParser::copyright, n);
110  }
111
112
113  void Stats::add(std::vector<unsigned int>& vec, unsigned int rev, bool x,
114                  unsigned int n)
115  { 
116    if (vec.size() < rev+1){
117      vec.reserve(rev+1);
118      vec.resize(rev);
119      if (x) {
120        assert(vec.size()+1<vec.max_size());
121        vec.push_back(n);
122      }
123      else {
124        assert(vec.size()+1<vec.max_size());
125        vec.push_back(0);
126      }
127    }
128    else if (x)
129      vec[rev]+=n;
130  }
131
132
133  void Stats::add_author(std::string name)
134  {
135    authors_.insert(name);
136  }
137
138
139  void Stats::add_authors(std::set<std::string>::const_iterator first, 
140                          std::set<std::string>::const_iterator last)
141  {
142    authors_.insert(first, last);
143  }
144
145
146  const std::set<std::string>& Stats::authors(void) const
147  {
148    return authors_;
149  }
150
151
152  void Stats::calc_all(void)
153  {
154    std::vector<unsigned int> init(revision()+1);
155    code_stats()["all"]=std::accumulate(code_stats().begin(), 
156                                        code_stats().end(), init,
157                                        PairValuePlus<std::string,unsigned int>());
158    comment_stats()["all"]=std::accumulate(comment_stats().begin(), 
159                                           comment_stats().end(), init, 
160                                           PairValuePlus<std::string,unsigned int>());
161    other_stats()["all"]=std::accumulate(other_stats().begin(), 
162                                         other_stats().end(), init,
163                                         PairValuePlus<std::string,unsigned int>());
164    copyright_stats()["all"]=std::accumulate(copyright_stats().begin(), 
165                                             copyright_stats().end(), init,
166                                             PairValuePlus<std::string,unsigned int>());
167    VectorPlus<unsigned int> vp;
168    comment_or_copy_stats()["all"] = 
169      vp(comment_stats()["all"], copyright_stats()["all"]);
170
171    total_stats()["all"] = 
172      vp(vp(code_stats()["all"], comment_or_copy_stats()["all"]),
173            other_stats()["all"]);
174  }
175
176
177  void Stats::calc_total(void)
178  {
179    for (std::set<std::string>::const_iterator iter(authors().begin());
180         iter!=authors().end(); ++iter) {
181      std::vector<unsigned int>& code = code_stats()[*iter];
182      std::vector<unsigned int>& comments = comment_stats()[*iter];
183      std::vector<unsigned int>& other = other_stats()[*iter];
184      std::vector<unsigned int>& copy = copyright_stats()[*iter];
185
186      VectorPlus<unsigned int> vp;
187      total_stats()[*iter] = vp(vp(vp(code, comments),other),copy);
188    }
189
190  }
191
192
193  void Stats::calc_comment_or_copy(void)
194  {
195    for (std::set<std::string>::const_iterator iter(authors().begin());
196         iter!=authors().end(); ++iter) {
197      std::vector<unsigned int>& comments = comment_stats()[*iter];
198      std::vector<unsigned int>& copy = copyright_stats()[*iter];
199
200      VectorPlus<unsigned int> vp;
201      comment_or_copy_stats()[*iter] = vp(comments, copy);
202    }
203
204  }
205
206
207  unsigned int Stats::code(const std::string& user) const
208  {
209    return get_back(code_stats(), user);
210  }
211
212
213  unsigned int Stats::comments(const std::string& user) const
214  {
215    return get_back(comment_or_copy_stats(), user);
216  }
217
218
219  unsigned int Stats::empty(const std::string& user) const
220  {
221    return get_back(other_stats(), user);
222  }
223
224
225  unsigned int Stats::get_back(const Author2Vector& m, std::string user) const
226  {
227    A2VConstIter iter(m.find(std::string(user)));
228    if (iter==m.end() || iter->second.empty()) 
229      return 0;
230    return iter->second.back();
231  }
232
233
234  const std::vector<unsigned int>& Stats::get_vector(const Author2Vector& m, 
235                                              std::string user) const
236  {
237    A2VConstIter iter(m.find(std::string(user)));
238    if (iter==m.end()) 
239      throw std::runtime_error(user+std::string(" not found i Stats"));
240    return iter->second;
241  }
242
243
244  svn_revnum_t Stats::last_changed_rev(void) const
245  {
246    return last_changed_rev_;
247  }
248
249
250  unsigned int Stats::lines(const std::string& user) const
251  {
252    return get_back(total_stats(), user);
253  }
254
255
256  void Stats::load(std::istream& is, Author2Vector& m)
257  {
258    while (m.size() < authors().size()+1 && is.good()) {
259      std::string name;
260      std::getline(is, name);
261      std::vector<unsigned int>& vec=m[name];
262      std::string line;
263      std::getline(is, line);
264      std::stringstream ss(line);
265      while (ss.good()) {
266        svn_revnum_t rev=0;
267        unsigned int count=0;
268        ss >> rev;
269        ss >> count;
270        assert(rev<=revision_);
271        if (!count)
272          break;
273        vec.resize(std::max(vec.size(),static_cast<size_t>(rev+1)));
274        vec[rev]=count;
275      }
276      accumulate(vec);
277    }
278  }
279
280
281  svn_revnum_t Stats::load_cache(std::istream& is)
282  {
283    std::string str;
284    getline(is, str);
285    if (str!=cache_check_str())
286      return 0;
287    svn_revnum_t rev;
288    is >> rev;
289    reset();
290    size_t a_size=0;
291    is >> a_size;
292    while (authors().size()<a_size && is.good()){
293      getline(is, str);
294      if (str.size())
295        add_author(str);
296    }
297    getline(is, str);
298    if (str!=cache_check_str()) {
299      return 0;
300    }
301    for (size_t i=0; i<stats_.size(); ++i){
302      load(is, stats_[i]);
303      getline(is, str);
304      if (str!=cache_check_str()) {
305        return 0;
306      }
307    }
308    return rev;
309  }
310
311
312  void Stats::map_add(A2VConstIter first1, A2VConstIter last1, 
313                      Author2Vector& map)
314  {
315    A2VIter first2(map.begin());
316    Author2Vector::key_compare compare;
317    while ( first1 != last1) { 
318      // key of first1 less than key of first2
319      if (first2==map.end() || compare(first1->first,first2->first)) {
320        first2 = map.insert(first2, *first1);
321        ++first1;
322      }
323      // key of first2 less than key of first1
324      else if ( compare(first2->first, first1->first)) {
325        ++first2;
326      }
327      // keys are equivalent
328      else {
329        VectorPlus<Author2Vector::mapped_type::value_type> vp;
330        first2->second = vp(first1->second, first2->second);
331        ++first1;
332        ++first2;
333      }
334    }
335  }
336
337
338  void Stats::parse(const std::string& path, svn_revnum_t rev)
339  {
340    do_parse(path, rev);
341    calc_comment_or_copy();
342    calc_total();
343    calc_all();
344    assert(total_stats().size());
345    assert(code_stats().size());
346    assert(comment_or_copy_stats().size());
347    assert(other_stats().size());
348  }
349
350  std::string Stats::plot(const std::string& filename,
351                          const std::string& linetype) const
352  {
353    assert(total_stats().size());
354    plot_init(filename);
355    GnuplotFE* gp=GnuplotFE::instance();
356    const Author2Vector* stat=NULL;
357    if (linetype=="total")
358      stat = &total_stats();
359    else if (linetype=="code")
360      stat = &code_stats();
361    else if (linetype=="comments")
362      stat = &comment_or_copy_stats();
363    else if (linetype=="empty")
364      stat = &other_stats();
365    assert(stat);
366    assert(stat->size());
367    assert(stat->find("all")!=stat->end());
368    std::vector<unsigned int> total=get_vector(*stat, "all");   
369    double yrange_max=1.03*total.back()+1;
370    gp->yrange(yrange_max);
371
372    typedef std::vector<std::pair<std::string, std::vector<unsigned int> > > vec_type;
373    vec_type author_cont;
374    author_cont.reserve(stat->size());
375    for (std::set<std::string>::const_iterator i=authors_.begin(); 
376         i != authors_.end(); ++i) {
377      if (lines(*i)) {
378        assert(stat->find(*i)!=stat->end());
379        author_cont.push_back(std::make_pair(*i,get_vector(*stat,*i)));
380      }
381    }
382
383    LessReversed<std::vector<unsigned int> > lr;
384    PairSecondCompare<std::string, std::vector<unsigned int>, 
385      LessReversed<std::vector<unsigned int> > > compare(lr);
386    std::sort(author_cont.begin(), author_cont.end(), compare);
387
388    size_t plotno=author_cont.size();
389    std::stringstream ss;
390    vec_type::iterator end(author_cont.end());
391    for (vec_type::iterator i(author_cont.begin()); i!=end; ++i) {
392      ss.str("");
393      ss << "set key height " << 2*plotno;
394      gp->command(ss.str());
395      ss.str("");
396      ss << get_back(*stat, i->first) << " " << i->first;
397      gp->yrange(yrange_max);
398      gp->linetitle(ss.str());
399      ss.str("");
400      ss << "steps " << --plotno+2;
401      gp->linestyle(ss.str());
402      gp->plot(i->second);
403    }
404    ss.str("");
405    ss << get_back(*stat, "all") << " total";
406    gp->command("set key height 0");
407    gp->linetitle(ss.str());
408    gp->linestyle("steps 1");
409    gp->plot(total);
410
411    gp->command("unset multiplot");
412    gp->yrange();
413
414    return filename;
415  }
416
417
418  void Stats::plot_init(const std::string& filename) const
419  {
420    GnuplotFE* gp=GnuplotFE::instance();
421    gp->command("set term png");
422    gp->command("set output '"+filename+"'");
423    gp->command("set xtics nomirror");
424    gp->command("set ytics nomirror");
425    gp->command("set key default");
426    gp->command("set key left Left reverse");
427    gp->command("set multiplot");
428  }
429
430
431  void Stats::plot_summary(const std::string& filename) const
432  {
433    plot_init(filename);
434    GnuplotFE* gp=GnuplotFE::instance();
435    std::vector<unsigned int> total = get_vector(total_stats(), "all");
436    double yrange_max=1.03*total.back()+1;
437    gp->yrange(yrange_max);
438    std::stringstream ss;
439   
440    ss.str("");
441    std::vector<unsigned int> x(get_vector(code_stats(), "all"));
442    ss << x.back() << " code";
443    gp->command("set key height 2");
444    gp->linetitle(ss.str());
445    gp->linestyle("steps 2");
446    gp->plot(x);
447
448    ss.str("");
449    x = get_vector(comment_or_copy_stats(), "all");
450    ss << x.back() << " comment";
451    gp->command("set key height 4");
452    gp->linetitle(ss.str());
453    gp->linestyle("steps 3");
454    gp->plot(x);
455
456    ss.str("");
457    x = get_vector(other_stats(), "all");
458    ss << x.back() << " other";
459    gp->command("set key height 6");
460    gp->linetitle(ss.str());
461    gp->linestyle("steps 4");
462    gp->plot(x);
463
464    ss.str("");
465    ss << total.back() << " total";
466    gp->command("set key height 0");
467    gp->linetitle(ss.str());
468    gp->linestyle("steps 1");
469    gp->plot(total);
470
471    gp->command("unset multiplot");
472    gp->yrange();
473  }
474
475
476  void Stats::print(std::ostream& os) const
477  {
478    os << cache_check_str() << "\n";
479    os << last_changed_rev() << " ";
480    os << authors().size() << "\n";
481
482    std::copy(authors().begin(), authors().end(), 
483              std::ostream_iterator<std::string>(os, "\n"));
484    os << cache_check_str() << "\n";
485    for (size_t i=0; i<stats_.size(); ++i){
486      print(os, stats_[i]);
487      os << cache_check_str() << "\n";
488    }
489  }
490
491
492  void Stats::print(std::ostream& os, const Author2Vector& m) const
493  {
494    for (A2VConstIter i(m.begin()); i!=m.end(); ++i){
495      os << i->first << "\n";
496      assert(i->second.size());
497      if (i->second[0])
498        os << 0 << " " << i->second[0] << " ";
499      for (size_t j=1; j<i->second.size(); ++j) {
500        // only print if stats changes in this rev
501        if (i->second[j] != i->second[j-1]) {
502          os << j << " " << i->second[j] - i->second[j-1] << " ";
503        }
504      }
505      os << "\n";
506    }
507  }
508
509  void Stats::reset(void)
510  {
511    for (size_t i=0; i<stats_.size(); ++i){
512      stats_[i].clear();
513      stats_[i]["all"]=std::vector<unsigned int>(revision_+1);
514    }
515    authors_.clear();
516  }
517
518
519  Stats& Stats::operator+=(const Stats& rhs)
520  {
521    revision_ = std::max(revision_, rhs.revision_);
522    last_changed_rev_ = std::max(last_changed_rev_, rhs.last_changed_rev_);
523    add_authors(rhs.authors().begin(), rhs.authors().end());
524    assert(stats_.size()==rhs.stats_.size());
525    for (size_t i=0; i<stats_.size(); ++i)
526      map_add(rhs.stats_[i].begin(), rhs.stats_[i].end(), stats_[i]);
527   
528    return *this;
529  }
530
531 
532  size_t Stats::operator()(int linetype, std::string author, 
533                           svn_revnum_t rev) const
534  {
535    assert(linetype<=LineTypeParser::total);
536    assert(static_cast<size_t>(linetype) < stats_.size());
537    assert(rev>=0);
538    A2VConstIter i = stats_[linetype].find(author);
539    if (i==stats_[linetype].end()){
540      std::stringstream msg;
541      msg << __FILE__ << ": author: " << author << " does not exist"; 
542      throw std::runtime_error(msg.str());
543    }
544    assert(rev < static_cast<svn_revnum_t>(i->second.size()));
545    return i->second[rev];
546  }
547
548}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.