source: trunk/lib/Stats.cc @ 113

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

removed upper and right tics in plots and made the plots transparent

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1// $Id: Stats.cc 113 2006-06-30 08:48:41Z 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 svnstat, http://lev.thep.lu.se/trac/svnstat
8
9  svnstat 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  svnstat 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#include "GnuplotFE.h"
27#include "utility.h"
28
29#include <algorithm>
30#include <cassert>
31#include <cstdlib>
32#include <fstream>
33#include <iostream>
34#include <map>
35#include <numeric>
36#include <sstream>
37#include <string>
38#include <unistd.h>
39#include <utility>
40#include <vector>
41
42
43namespace theplu{
44namespace svnstat{
45
46
47  Stats::Stats(const std::string& path)
48  {
49    // Make sure latest revision is set properly
50    std::map<std::string,std::string> svn_info = info(path);
51    std::stringstream ss;
52    ss << (svn_info.count("Revision") ? svn_info["Revision"] : "0");
53    ss >> revision_;
54    ss.clear();
55    ss << (svn_info.count("Last Changed Rev") ? svn_info["Last Changed Rev"] : "0");
56    ss >> last_changed_rev_;
57  }
58
59
60  std::vector<u_int> Stats::accumulated(void) const
61  {
62    // sum of all users
63    std::vector<u_int> sum(revision_+1, 0);
64    sum=std::accumulate(map_.begin(), map_.end(), sum,
65                        PairValuePlus<std::string,u_int>());
66
67    // calculate accumulated sum
68    std::vector<u_int> accum(sum.size());
69    std::partial_sum(sum.begin(),sum.end(),accum.begin());
70    assert(sum.size()==accum.size());
71    return accum;
72  }
73
74  std::vector<u_int> Stats::accumulated(const std::string& user) const
75  {
76    if (!map_.count(user))
77      return std::vector<u_int>();
78    std::vector<u_int> vec=(map_.find(user))->second;
79 
80    if (vec.size() < 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  {
90    std::vector<u_int>* vec = &(map_[user]);
91    if (vec->size() < rev+1){
92      vec->reserve(revision_ + 1);
93      vec->insert(vec->end(), rev - vec->size(),0);
94      vec->push_back(1);
95    }
96    else
97      (*vec)[rev]++;
98  }
99
100
101  bool Stats::parse(const std::string& path)
102  {
103    // Calling svn blame
104    if (blame(path))
105      return true;
106
107    // Check if file is binary
108    std::ifstream is("svnstat.tmp");
109    std::string line;
110    getline(is,line,' ');
111    if (line==std::string("Skipping")){
112      getline(is,line,' ');
113      if (line==std::string("binary")){
114        is.close();
115        return true;
116      }
117    }
118    is.close();
119
120    is.open("svnstat.tmp");
121    while (getline(is,line, '\n')){
122      if (!line.size()) // skip empty line
123        continue;
124      std::stringstream ss(line);
125      u_int revision;
126      std::string user;
127      ss >> revision;
128      ss >> user;
129      add(user, revision);
130    }
131    is.close();
132    return false;
133  }
134
135
136  std::string Stats::plot(const std::string& filename,
137                          const std::string& title) const
138  {
139    GnuplotFE* gp=GnuplotFE::instance();
140    gp->command("set term png transparent");
141    gp->command("set output '"+filename+"'");
142    gp->command("set title '"+title+"'");
143    gp->command("set xtics nomirror");
144    gp->command("set ytics nomirror");
145    gp->command("set key default");
146    gp->command("set key left Left reverse");
147    gp->command("set multiplot");
148    std::vector<u_int> total=accumulated();   
149    double yrange_max=1.03*total.back()+1;
150    gp->yrange(yrange_max);
151    size_t plotno=1;
152    std::stringstream ss;
153    for (MapConstIter_ i= map_.begin(); i != map_.end(); i++) {
154      ss.str("");
155      ss << "set key height " << 2*plotno;
156      gp->command(ss.str());
157      std::vector<u_int> x=accumulated(i->first);
158      ss.str("");
159      ss << x.back() << " " << i->first;
160      gp->yrange(yrange_max);
161      gp->linetitle(ss.str());
162      ss.str("");
163      ss << "steps " << ++plotno;
164      gp->linestyle(ss.str());
165      gp->plot(x);
166    }
167    ss.str("");
168    ss << total.back() << " total";
169    gp->command("set key height 0");
170    gp->linetitle(ss.str());
171    gp->linestyle("steps 1");
172    gp->plot(total);
173
174    gp->command("unset multiplot");
175    gp->yrange();
176
177    return filename;
178  }
179
180  Stats& Stats::operator+=(const Stats& other)
181  {
182    for (MapConstIter_ o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i)
183    {
184      std::pair<MapIter_,bool> result = map_.insert(*o_i);
185      if (!result.second)
186        map_[(*(result.first)).first] = 
187          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
188 
189    }
190    return *this;
191  }
192
193}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.