source: trunk/lib/Stats.cc @ 84

Last change on this file since 84 was 84, checked in by Jari Häkkinen, 15 years ago

Added copyright statement. Bumped version number to pre0.3. Cleaned up code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1// $Id: Stats.cc 84 2006-03-13 22:04:34Z jari $
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 >> latest_revision_;
54  }
55
56
57  std::vector<u_int> Stats::accumulated(void) const
58  {
59    // sum of all users
60    std::vector<u_int> sum(latest_revision_+1, 0);
61    sum=std::accumulate(map_.begin(), map_.end(), sum,
62                        PairValuePlus<std::string,u_int>());
63
64    // calculate accumulated sum
65    std::vector<u_int> accum(sum.size());
66    std::partial_sum(sum.begin(),sum.end(),accum.begin());
67    assert(sum.size()==accum.size());
68    return accum;
69  }
70
71  std::vector<u_int> Stats::accumulated(const std::string& user) const
72  {
73    if (!map_.count(user))
74      return std::vector<u_int>();
75    std::vector<u_int> vec=(map_.find(user))->second;
76 
77    if (vec.size() < latest_revision_+1)
78      vec.insert(vec.end(), latest_revision_+1-vec.size(), 0);
79
80    std::vector<u_int> accum(vec.size());
81    std::partial_sum(vec.begin(),vec.end(),accum.begin());
82    return accum;
83  }
84
85  void Stats::add(const std::string& user, const u_int& rev)
86  {
87    std::vector<u_int>* vec = &(map_[user]);
88    if (vec->size() < rev+1){
89      vec->reserve(latest_revision_ + 1);
90      vec->insert(vec->end(), rev - vec->size(),0);
91      vec->push_back(1);
92    }
93    else
94      (*vec)[rev]++;
95  }
96
97
98  bool Stats::parse(const std::string& path)
99  {
100    // Calling svn blame
101    if (blame(path))
102      return true;
103
104    // Check if file is binary
105    std::ifstream is("svnstat.tmp");
106    std::string line;
107    getline(is,line,' ');
108    if (line==std::string("Skipping")){
109      getline(is,line,' ');
110      if (line==std::string("binary")){
111        is.close();
112        return true;
113      }
114    }
115    is.close();
116
117    is.open("svnstat.tmp");
118    while (getline(is,line, '\n')){
119      if (!line.size()) // skip empty line
120        continue;
121      std::stringstream ss(line);
122      u_int revision;
123      std::string user;
124      ss >> revision;
125      ss >> user;
126      add(user, revision);
127    }
128    is.close();
129    return false;
130  }
131
132
133  std::string Stats::plot(const std::string& name) const
134  {
135    GnuplotFE* gp=GnuplotFE::instance();
136    gp->command(std::string("set term png; set output '")+name+"'");
137    gp->command(std::string("set title '")+name+"'");
138    gp->command("set key default");
139    gp->command("set key left Left reverse");
140    gp->command("set multiplot");
141    std::vector<u_int> total=accumulated();   
142    double yrange_max=1.03*total.back()+1;
143    gp->yrange(yrange_max);
144    size_t plotno=1;
145    std::stringstream ss;
146    for (MapConstIter_ i= map_.begin(); i != map_.end(); i++) {
147      ss.str("");
148      ss << "set key height " << 2*plotno;
149      gp->command(ss.str());
150      std::vector<u_int> x=accumulated(i->first);
151      ss.str("");
152      ss << x.back() << " " << i->first;
153      gp->yrange(yrange_max);
154      gp->linetitle(ss.str());
155      ss.str("");
156      ss << "steps " << ++plotno;
157      gp->linestyle(ss.str());
158      gp->plot(x);
159    }
160    ss.str("");
161    ss << total.back() << " total";
162    gp->command("set key height 0");
163    gp->linetitle(ss.str());
164    gp->linestyle("steps 1");
165    gp->plot(total);
166
167    gp->command("unset multiplot");
168    gp->yrange();
169
170    return name;
171  }
172
173  Stats& Stats::operator+=(const Stats& other)
174  {
175    for (MapConstIter_ o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i)
176    {
177      std::pair<MapIter_,bool> result = map_.insert(*o_i);
178      if (!result.second)
179        map_[(*(result.first)).first] = 
180          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
181 
182    }
183    return *this;
184  }
185
186}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.