source: trunk/lib/Stats.cc @ 183

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

refs #57 modified Stats to no longer skip empty line.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: Stats.cc 183 2006-09-05 09:37:13Z peter $
2
3/*
4  Copyright (C) 2005 Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
6
7  This file is part of svndigest, http://lev.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 "Stats.h"
26
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 <iostream>
36#include <map>
37#include <numeric>
38#include <string>
39#include <sstream>
40#include <unistd.h>
41#include <utility>
42#include <vector>
43
44
45namespace theplu{
46namespace svndigest{
47
48
49  Stats::Stats(const std::string& path)
50  {
51    // Make sure latest revision is set properly
52    SVNinfo svn_info(path);
53    revision_=svn_info.rev();
54    last_changed_rev_=svn_info.last_changed_rev();
55  }
56
57
58  std::vector<u_int> Stats::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> Stats::accumulated(const Map_& map, 
73                                        const std::string& user) const
74  {
75    if (!map.count(user))
76      return std::vector<u_int>();
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 Stats::add(const std::string& user, const u_int& rev, 
89                  const Parser::line_type& lt)
90  {
91    authors_.insert(user);
92
93    std::vector<u_int>* total = &(total_[user]);
94    if (total->size() < rev+1){
95      total->reserve(revision_ + 1);
96      total->insert(total->end(), rev - total->size(), 0);
97      total->push_back(1);
98    }
99    else
100      ++(*total)[rev];
101
102    std::vector<u_int>* code = &(code_[user]);
103    if (code->size() < rev+1){
104      code->reserve(revision_ + 1);
105      code->insert(code->end(), rev - code->size(), 0);
106      if (lt == Parser::code)
107        code->push_back(1);
108      else 
109        code->push_back(0);
110    }
111    else if (lt == Parser::code)
112      ++(*code)[rev];
113
114    std::vector<u_int>* comments = &(comments_[user]);
115    if (comments->size() < rev+1){
116      comments->reserve(revision_ + 1);
117      comments->insert(comments->end(), rev - comments->size(), 0);
118      if (lt == Parser::comment)
119        comments->push_back(1);
120      else 
121        comments->push_back(0);
122    }
123    else if (lt == Parser::comment)
124      ++(*comments)[rev];
125  }
126
127
128  bool Stats::parse(const std::string& path)
129  {
130    SVNblame svn_blame(path);
131    if (svn_blame.binary())
132      return true;
133
134    Parser parser(path);
135    std::vector<Parser::line_type>::const_iterator count=parser.type().begin();
136
137    while (const SVNblame::blame_information * bi=svn_blame.next()) {
138      // to handle symbolic links
139      if (count==parser.type().end())
140        add(bi->author, bi->revision, Parser::empty);
141      else 
142        add(bi->author, bi->revision, *count);
143      ++count;
144    }
145   
146    return false;
147  }
148
149
150  std::string Stats::plot(const std::string& filename,
151                          const std::string& title) const
152  {
153    GnuplotFE* gp=GnuplotFE::instance();
154    gp->command("set term png transparent");
155    gp->command("set output '"+filename+"'");
156    gp->command("set title '"+title+"'");
157    gp->command("set xtics nomirror");
158    gp->command("set ytics nomirror");
159    gp->command("set key default");
160    gp->command("set key left Left reverse");
161    gp->command("set multiplot");
162    std::vector<u_int> total=accumulated(total_);   
163    double yrange_max=1.03*total.back()+1;
164    gp->yrange(yrange_max);
165    size_t plotno=1;
166    std::stringstream ss;
167    for (MapConstIter_ i= total_.begin(); i != total_.end(); ++i) {
168      ss.str("");
169      ss << "set key height " << 2*plotno;
170      gp->command(ss.str());
171      std::vector<u_int> x=accumulated(total_, i->first);
172      ss.str("");
173      ss << x.back() << " " << i->first;
174      gp->yrange(yrange_max);
175      gp->linetitle(ss.str());
176      ss.str("");
177      ss << "steps " << ++plotno;
178      gp->linestyle(ss.str());
179      gp->plot(x);
180    }
181    ss.str("");
182    ss << total.back() << " total";
183    gp->command("set key height 0");
184    gp->linetitle(ss.str());
185    gp->linestyle("steps 1");
186    gp->plot(total);
187
188    gp->command("unset multiplot");
189    gp->yrange();
190
191    return filename;
192  }
193
194
195  Stats& Stats::operator+=(const Stats& other)
196  {
197    for (MapConstIter_ o_i= other.code_.begin(); 
198         o_i != other.code_.end(); ++o_i)
199    {
200      std::pair<MapIter_,bool> result = code_.insert(*o_i);
201      if (!result.second)
202        code_[(*(result.first)).first] = 
203          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
204 
205    }
206 
207    for (MapConstIter_ o_i= other.comments_.begin(); 
208         o_i != other.comments_.end(); ++o_i)
209    {
210      std::pair<MapIter_,bool> result = comments_.insert(*o_i);
211      if (!result.second)
212        comments_[(*(result.first)).first] = 
213          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
214 
215    }
216   
217    for (MapConstIter_ o_i= other.total_.begin(); 
218         o_i != other.total_.end(); ++o_i)
219    {
220      std::pair<MapIter_,bool> result = total_.insert(*o_i);
221      if (!result.second)
222        total_[(*(result.first)).first] = 
223          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
224 
225    }
226   
227    if (!other.authors().empty())
228      authors_.insert(other.authors().begin(), other.authors().end());
229    return *this;
230  }
231
232}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.