source: branches/0.7-stable/lib/Stats.h @ 994

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

Fixed bug so default codons are not added twice to configuration
file. The bug caused some comment lines to be interpreted as code
lines. Cache file version is upgraded to prevent that old cache file
(which reflects this bug) are used to create new reports. A notice is
sent to stdout telling the user the cache file is out-dated.

fixes #431

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 994 2009-12-24 23:46:54Z 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    ///
194    /// Init statistics graph.
195    ///
196    void plot_init(const std::string& output) const;
197
198    std::set<std::string> authors_;
199
200    const std::vector<unsigned int>& get_vector(const Author2Vector&, 
201                                         std::string user) const;
202  private:
203    /**
204       \a vec is resized to revision().
205       vec is accumulated such that
206       vec[rev] = vec[rev-1] + vec[rev]
207       vec[rev+1] = vec[rev] + vec[rev+1]
208       et cetera
209     */
210    void accumulate(std::vector<unsigned int>& vec,
211                    svn_revnum_t rev=1) const;
212    void add(std::vector<unsigned int>& vec, unsigned int rev, bool x,
213             unsigned int n);
214
215    /**
216       Parse statistics for fundamental categories, i.e., code,
217       comment, empty, and copyright for each author. Ignore revisions
218       earlier than \a first_rev.
219     */
220    virtual void do_parse(const std::string&, svn_revnum_t first_rev)=0;
221   
222    // Change this string if cache format is changed in such a way
223    // that all old cache files are obsolete.
224    inline std::string cache_check_str(void) const 
225    {return "CACHE FILE VERSION 7";} 
226
227    inline std::string prev_cache_check_str(void) const 
228    {return "CACHE FILE VERSION 6";} 
229
230    void calc_all(void);
231    void calc_comment_or_copy(void);
232    void calc_total(void);
233    unsigned int get_back(const Author2Vector&, std::string user) const;
234    void load(std::istream& is, Author2Vector& m);
235    void print(std::ostream& os, const Author2Vector& m) const;
236
237    svn_revnum_t revision_; // Should be the latest revision for whole project
238    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
239
240    std::vector<Author2Vector> stats_; // from linetype to a2v
241
242    // using compiler generated copy constructor
243    //Stats(const Stats&);
244    // no assignment
245    Stats& operator=(const Stats&);
246
247  };
248}} // end of namespace svndigest end of namespace theplu
249
250#endif
Note: See TracBrowser for help on using the repository browser.