source: trunk/lib/Stats.cc @ 1008

Last change on this file since 1008 was 1008, checked in by Peter Johansson, 13 years ago

refs #405. support for fomrat none which is not really a format but images will be ignored.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.9 KB
Line 
1// $Id: Stats.cc 1008 2010-01-03 18:36:08Z 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  std::string Stats::plot(const std::string& filename,
364                          const std::string& linetype) const
365  {
366    if (Configuration::instance().image_format()=="none")
367      return filename;
368    assert(total_stats().size());
369    Graph gp(filename+"."+Configuration::instance().image_format(),
370             Configuration::instance().image_format());
371    const Author2Vector* stat=NULL;
372    if (linetype=="total")
373      stat = &total_stats();
374    else if (linetype=="code")
375      stat = &code_stats();
376    else if (linetype=="comments")
377      stat = &comment_or_copy_stats();
378    else if (linetype=="empty")
379      stat = &other_stats();
380    assert(stat);
381    assert(stat->size());
382    assert(stat->find("all")!=stat->end());
383    std::vector<unsigned int> total=get_vector(*stat, "all");   
384    double yrange_max=1.03 * max_element(total) +1.0;
385    gp.ymax(yrange_max);
386
387    typedef std::vector<std::pair<std::string, std::vector<unsigned int> > > vec_type;
388    vec_type author_cont;
389    author_cont.reserve(stat->size());
390    for (std::set<std::string>::const_iterator i=authors_.begin(); 
391         i != authors_.end(); ++i) {
392      assert(stat->find(*i)!=stat->end());
393      const std::vector<unsigned int>& vec = get_vector(*stat,*i);
394      if (max_element(vec)) {
395        author_cont.push_back(std::make_pair(*i,vec));
396      }
397    }
398
399    LessReversed<std::vector<unsigned int> > lr;
400    PairSecondCompare<std::string, std::vector<unsigned int>, 
401      LessReversed<std::vector<unsigned int> > > compare(lr);
402    std::sort(author_cont.begin(), author_cont.end(), compare);
403
404    vec_type::iterator end(author_cont.end());
405    vec_type::iterator i(author_cont.begin());
406    const vec_type::size_type maxauthors=8;
407    int authskip=author_cont.size()-maxauthors;
408    if (authskip>1) {
409      // only use others if there is more than 1 author to be skipped,
410      // there is no reason to add only 1 author to others.
411      vec_type::iterator j(i);
412      i+=authskip;
413      std::vector<unsigned int> init(revision()+1);
414      std::vector<unsigned int> others =
415        std::accumulate(j, i, init, PairValuePlus<std::string,unsigned int>());
416      unsigned char r, g, b;
417      std::string label("others");
418      Colors::instance().get_color(label, r,g,b);
419      gp.current_color(r,g,b);
420      gp.plot(others, label, others.back());
421    }
422    for ( ; i!=end; ++i) {
423      unsigned char r, g, b;
424      Colors::instance().get_color(i->first,r,g,b);
425      gp.current_color(r,g,b);
426      gp.plot(i->second, i->first, get_back(*stat, i->first));
427    }
428    gp.current_color(255,0,0);
429    gp.plot(total, "total", get_back(*stat, "all"));
430
431    return filename;
432  }
433
434
435  void Stats::plot_summary(const std::string& filename) const
436  {
437    if (Configuration::instance().image_format()=="none")
438      return; 
439    Graph gp(filename+"."+Configuration::instance().image_format(),
440             Configuration::instance().image_format());
441    std::vector<unsigned int> total = get_vector(total_stats(), "all");
442    double yrange_max=1.03*total.back()+1;
443    gp.ymax(yrange_max);
444   
445    std::vector<unsigned int> x(get_vector(code_stats(), "all"));
446    gp.current_color(255,255,0);
447    gp.plot(x, "code", x.back());
448
449    x = get_vector(comment_or_copy_stats(), "all");
450    gp.current_color(0,0,255);
451    gp.plot(x, "comment", x.back());
452
453    x = get_vector(other_stats(), "all");
454    gp.current_color(0,255,0);
455    gp.plot(x, "other", x.back());
456
457    gp.current_color(255,0,0);
458    gp.plot(total, "total", total.back());
459  }
460
461
462  void Stats::print(std::ostream& os) const
463  {
464    os << cache_check_str() << "\n";
465    os << last_changed_rev() << " ";
466    os << authors().size() << "\n";
467
468    std::copy(authors().begin(), authors().end(), 
469              std::ostream_iterator<std::string>(os, "\n"));
470    os << cache_check_str() << "\n";
471    for (size_t i=0; i<stats_.size(); ++i){
472      print(os, stats_[i]);
473      os << cache_check_str() << "\n";
474    }
475  }
476
477
478  void Stats::print(std::ostream& os, const Author2Vector& m) const
479  {
480    for (A2VConstIter i(m.begin()); i!=m.end(); ++i){
481      os << i->first << "\n";
482      assert(i->second.size());
483      if (i->second[0])
484        os << 0 << " " << i->second[0] << " ";
485      for (size_t j=1; j<i->second.size(); ++j) {
486        // only print if stats changes in this rev
487        if (i->second[j] != i->second[j-1]) {
488          os << j << " " << i->second[j] - i->second[j-1] << " ";
489        }
490      }
491      os << "\n";
492    }
493  }
494
495  void Stats::reset(void)
496  {
497    for (size_t i=0; i<stats_.size(); ++i){
498      stats_[i].clear();
499      std::vector<unsigned int>& tmp = stats_[i]["all"];
500      std::fill(tmp.begin(), tmp.end(), 0);
501      tmp.resize(revision_+1);
502    }
503    authors_.clear();
504  }
505
506
507  Stats& Stats::operator+=(const Stats& rhs)
508  {
509    revision_ = std::max(revision_, rhs.revision_);
510    last_changed_rev_ = std::max(last_changed_rev_, rhs.last_changed_rev_);
511    add_authors(rhs.authors().begin(), rhs.authors().end());
512    assert(stats_.size()==rhs.stats_.size());
513    for (size_t i=0; i<stats_.size(); ++i)
514      map_add(rhs.stats_[i].begin(), rhs.stats_[i].end(), stats_[i]);
515   
516    return *this;
517  }
518
519 
520  size_t Stats::operator()(int linetype, std::string author, 
521                           svn_revnum_t rev) const
522  {
523    assert(linetype<=LineTypeParser::total);
524    assert(static_cast<size_t>(linetype) < stats_.size());
525    assert(rev>=0);
526    A2VConstIter i = stats_[linetype].find(author);
527    if (i==stats_[linetype].end()){
528      std::stringstream msg;
529      msg << __FILE__ << ": author: " << author << " does not exist"; 
530      throw std::runtime_error(msg.str());
531    }
532    assert(rev < static_cast<svn_revnum_t>(i->second.size()));
533    return i->second[rev];
534  }
535
536}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.