source: trunk/lib/Stats.h @ 978

Last change on this file since 978 was 978, checked in by Peter Johansson, 12 years ago

refs #429. Convert copyright statements to UTF-8

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