source: trunk/lib/Stats.cc @ 1026

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

create anchor plot if anchor_format is different from format. refs #279

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