source: branches/0.7-stable/lib/Stats.cc @ 998

Last change on this file since 998 was 998, checked in by Peter Johansson, 14 years ago

Fixes #434. Authors are now ignored in the plot if and only if plot
would be y=0.

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