source: trunk/lib/Stats.h @ 693

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

Fixes #339. Change to GPLv3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 693 2008-09-11 20:42:56Z jari $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2007 Peter Johansson
10
11  This file is part of svndigest, http://dev.thep.lu.se/svndigest
12
13  svndigest is free software; you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 3 of the License, or
16  (at your option) any later version.
17
18  svndigest is distributed in the hope that it will be useful, but
19  WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "LineTypeParser.h"
28
29#include <subversion-1/svn_types.h>
30
31#include <map>
32#include <istream>
33#include <set>
34#include <string>
35#include <vector>
36
37namespace theplu{
38namespace svndigest{
39
40  ///
41  /// Class taking care of statistics from svn.
42  ///
43  class Stats
44  {
45  public:
46    ///
47    /// @brief Default Constructor
48    ///
49    explicit Stats(const std::string& path);
50
51    /**
52       \brief Destructor
53    */
54    virtual ~Stats(void);
55
56    ///
57    /// @brief adding \a n line(s) to \a user from \a revision to the stats
58    ///
59    void add(const std::string& user, const unsigned int& revision,
60             const LineTypeParser::line_type&, unsigned int n=1); 
61
62    ///
63    /// @return set of authors
64    ///
65    const std::set<std::string>& authors(void) const;
66
67    ///
68    ///
69    ///
70    unsigned int code(const std::string& user="all") const; 
71
72
73    ///
74    ///
75    ///
76    unsigned int comments(const std::string& user="all") const; 
77
78    ///
79    ///
80    ///
81    unsigned int empty(const std::string& user="all") const; 
82
83    ///
84    ///
85    ///
86    svn_revnum_t last_changed_rev(void) const;
87
88    ///
89    ///
90    ///
91    unsigned int lines(const std::string& user="all") const; 
92
93    /**
94       Load object from a stream.
95       
96       \return revision the cache represents - 0 if failed
97     */
98    svn_revnum_t load_cache(std::istream&);
99
100    /**
101       Do the parsing from revision \a rev
102    */
103    void parse(const std::string&, svn_revnum_t rev=0);
104
105    ///
106    /// Create statistics graph.
107    ///
108    std::string plot(const std::string&, const std::string&) const;
109
110    ///
111    /// Plotting code, comment, other, and total in same plot (for
112    /// 'all' not individual authors).
113    ///
114    void plot_summary(const std::string& output) const;
115
116    /**
117       Send Stats to a stream.
118     */
119    void print(std::ostream&) const;
120
121    ///
122    /// @brief Clear all statistics
123    ///
124    void reset(void); 
125
126    ///
127    /// \return latest revision for whole project
128    ///
129    svn_revnum_t revision(void) const { return revision_; }
130
131    /**
132       \return resulting Stats
133    */
134    Stats& operator+=(const Stats&);
135
136    /**
137       \return number of lines for \a author and \a linetype from
138       revision \a rev.
139
140       \throw if \a author does not exist
141     */
142    size_t operator()(int linetype, std::string author, svn_revnum_t rev) const;
143
144  protected:
145    typedef std::map<std::string, std::vector<unsigned int> > Author2Vector;
146    typedef Author2Vector::iterator A2VIter;
147    typedef Author2Vector::const_iterator A2VConstIter;
148
149    void accumulate_stats(svn_revnum_t rev=0);
150    void add_author(std::string);
151    void add_authors(std::set<std::string>::const_iterator, 
152                     std::set<std::string>::const_iterator);
153
154    // references to data
155    inline Author2Vector& code_stats(void) 
156    { return stats_[LineTypeParser::code]; }
157    inline Author2Vector& comment_stats(void) 
158    { return stats_[LineTypeParser::comment]; }
159    inline Author2Vector& copyright_stats(void) 
160    { return stats_[LineTypeParser::copyright]; }
161    inline Author2Vector& other_stats(void) 
162    { return stats_[LineTypeParser::other]; }
163    inline Author2Vector& comment_or_copy_stats(void) 
164    { return stats_[LineTypeParser::comment_or_copy]; }
165    inline Author2Vector& total_stats(void) 
166    { return stats_[LineTypeParser::total]; }
167
168    // const references to data
169    inline const Author2Vector& code_stats(void) const
170    { return stats_[LineTypeParser::code]; }
171    inline const Author2Vector& comment_stats(void) const
172    { return stats_[LineTypeParser::comment]; }
173    inline const Author2Vector& copyright_stats(void) const
174    { return stats_[LineTypeParser::copyright]; }
175    inline const Author2Vector& other_stats(void) const
176    { return stats_[LineTypeParser::other]; }
177    inline const Author2Vector& comment_or_copy_stats(void) const
178    { return stats_[LineTypeParser::comment_or_copy]; }
179    inline const Author2Vector& total_stats(void) const
180    { return stats_[LineTypeParser::total]; }
181
182    /**
183       add range [\a first, \a last) to \a map
184     */
185    void map_add(Author2Vector::const_iterator first, 
186                 Author2Vector::const_iterator last,
187                 Author2Vector& map);
188
189    ///
190    /// Init statistics graph.
191    ///
192    void plot_init(const std::string& output) const;
193
194    std::set<std::string> authors_;
195
196    const std::vector<unsigned int>& get_vector(const Author2Vector&, 
197                                         std::string user) const;
198  private:
199    void accumulate(std::vector<unsigned int>& vec,
200                    svn_revnum_t rev=0) const;
201    void add(std::vector<unsigned int>& vec, unsigned int rev, bool x,
202             unsigned int n);
203
204    virtual void do_parse(const std::string&, svn_revnum_t)=0;
205   
206    // Change this string if cache format is changed in such a way
207    // that all old cache files are obsolete.
208    inline std::string cache_check_str(void) const 
209    {return "CACHE FILE VERSION 6";} 
210
211    void calc_all(void);
212    void calc_comment_or_copy(void);
213    void calc_total(void);
214    unsigned int get_back(const Author2Vector&, std::string user) const;
215    void load(std::istream& is, Author2Vector& m);
216    void print(std::ostream& os, const Author2Vector& m) const;
217
218    svn_revnum_t revision_; // Should be the latest revision for whole project
219    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
220
221    std::vector<Author2Vector> stats_; // from linetype to a2v
222
223    // using compiler generated copy constructor
224    //Stats(const Stats&);
225    // no assignment
226    Stats& operator=(const Stats&);
227
228  };
229}} // end of namespace svndigest end of namespace theplu
230
231#endif
Note: See TracBrowser for help on using the repository browser.