source: trunk/lib/Stats.h @ 1124

Last change on this file since 1124 was 1124, checked in by Peter Johansson, 11 years ago

detecting old cache files and re-writing them in new format (version 8). closes #443 and #289

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 1124 2010-07-07 05:35:17Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2010 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    /// \return number of code lines for \a user for latest revision.
69    ///
70    unsigned int code(const std::string& user="all") const; 
71
72
73    ///
74    /// \return number of comments lines for \a user for latest revision.
75    ///
76    unsigned int comments(const std::string& user="all") const; 
77
78    ///
79    /// \return number of empty lines for \a user for latest revision.
80    ///
81    unsigned int empty(const std::string& user="all") const; 
82
83    ///
84    /// \return revison node was modified
85    ///
86    svn_revnum_t last_changed_rev(void) const;
87
88    ///
89    /// \return number of lines for \a user for latest revision
90    ///
91    unsigned int lines(const std::string& user="all") const; 
92
93    /**
94       Load object from a stream.
95
96       \a latest_ver is set to true if cache_file is latest version;
97       otherwise it is set to false.
98
99       \return revision the cache represents - 0 if failed
100     */
101    svn_revnum_t load_cache(std::istream&, bool& latest_ver);
102
103    /**
104       Do the parsing for \a path. Revisions from \a rev will be parsed.
105    */
106    void parse(const std::string& path, svn_revnum_t rev=0);
107
108    ///
109    /// Create statistics graph.
110    ///
111    std::string plot(const std::string&, const std::string&) const;
112
113    ///
114    /// Plotting code, comment, other, and total in same plot (for
115    /// 'all' not individual authors).
116    ///
117    void plot_summary(const std::string& output) const;
118
119    /**
120       Send Stats to a stream.
121     */
122    void print(std::ostream&) const;
123
124    ///
125    /// @brief Clear all statistics
126    ///
127    void reset(void); 
128
129    ///
130    /// \return latest revision for whole project
131    ///
132    svn_revnum_t revision(void) const { return revision_; }
133
134    /**
135       \return resulting Stats
136    */
137    Stats& operator+=(const Stats&);
138
139    /**
140       \return number of lines for \a author and \a linetype from
141       revision \a rev.
142
143       \throw if \a author does not exist
144     */
145    size_t operator()(int linetype, std::string author, svn_revnum_t rev) const;
146
147  protected:
148    typedef std::map<std::string, std::vector<unsigned int> > Author2Vector;
149    typedef Author2Vector::iterator A2VIter;
150    typedef Author2Vector::const_iterator A2VConstIter;
151
152    /**
153       Calculate accumalated statistics for fundamental statistics,
154       i.e., code, comment, empty, and copyright for each author.
155       \see accumulate
156     */
157    void accumulate_stats(svn_revnum_t rev=1);
158    void add_author(std::string);
159    void add_authors(std::set<std::string>::const_iterator, 
160                     std::set<std::string>::const_iterator);
161
162    // references to data
163    inline Author2Vector& code_stats(void) 
164    { return stats_[LineTypeParser::code]; }
165    inline Author2Vector& comment_stats(void) 
166    { return stats_[LineTypeParser::comment]; }
167    inline Author2Vector& copyright_stats(void) 
168    { return stats_[LineTypeParser::copyright]; }
169    inline Author2Vector& other_stats(void) 
170    { return stats_[LineTypeParser::other]; }
171    inline Author2Vector& comment_or_copy_stats(void) 
172    { return stats_[LineTypeParser::comment_or_copy]; }
173    inline Author2Vector& total_stats(void) 
174    { return stats_[LineTypeParser::total]; }
175
176    // const references to data
177    inline const Author2Vector& code_stats(void) const
178    { return stats_[LineTypeParser::code]; }
179    inline const Author2Vector& comment_stats(void) const
180    { return stats_[LineTypeParser::comment]; }
181    inline const Author2Vector& copyright_stats(void) const
182    { return stats_[LineTypeParser::copyright]; }
183    inline const Author2Vector& other_stats(void) const
184    { return stats_[LineTypeParser::other]; }
185    inline const Author2Vector& comment_or_copy_stats(void) const
186    { return stats_[LineTypeParser::comment_or_copy]; }
187    inline const Author2Vector& total_stats(void) const
188    { return stats_[LineTypeParser::total]; }
189
190    /**
191       add range [\a first, \a last) to \a map
192     */
193    void map_add(Author2Vector::const_iterator first, 
194                 Author2Vector::const_iterator last,
195                 Author2Vector& map);
196
197    std::set<std::string> authors_;
198
199    const std::vector<unsigned int>& get_vector(const Author2Vector&, 
200                                         std::string user) const;
201  private:
202    /// one liner used in cache file to validate that cache file was
203    /// created current configuration
204    std::string config_code_;
205
206    /**
207       \a vec is resized to revision().
208       vec is accumulated such that
209       vec[rev] = vec[rev-1] + vec[rev]
210       vec[rev+1] = vec[rev] + vec[rev+1]
211       et cetera
212     */
213    void accumulate(std::vector<unsigned int>& vec,
214                    svn_revnum_t rev=1) const;
215    void add(std::vector<unsigned int>& vec, unsigned int rev, bool x,
216             unsigned int n);
217
218    /**
219       Parse statistics for fundamental categories, i.e., code,
220       comment, empty, and copyright for each author. Ignore revisions
221       earlier than \a first_rev.
222     */
223    virtual void do_parse(const std::string&, svn_revnum_t first_rev)=0;
224   
225    /// load cache file version 7
226    svn_revnum_t load_cache7(std::istream&);
227    /// load cache file version 8
228    svn_revnum_t load_cache8(std::istream&);
229
230    /**
231       called from plot(2)
232     */
233    void plot(const std::string& basename, const std::string& linetype,
234              const std::string& format) const;
235
236    /**
237       called from plot_summary(1)
238     */
239    void plot_summary(const std::string& basename, 
240                      const std::string& format) const;
241
242    // Change this string if cache format is changed in such a way
243    // that all old cache files are obsolete.
244    inline std::string cache_check_str(void) const 
245    {return "CACHE FILE VERSION 7";} 
246
247    void calc_all(void);
248    void calc_comment_or_copy(void);
249    void calc_total(void);
250    unsigned int get_back(const Author2Vector&, std::string user) const;
251    void load(std::istream& is, Author2Vector& m);
252    /**
253       Finds the largets element by iterating through the entire
254       vector. Inherited classes should implement their own version
255       when it is possible to get the largest element in faster than
256       in linear time.
257
258       \return the largest largest element in \a v.
259    */
260    virtual unsigned int max_element(const std::vector<unsigned int>& v) const; 
261
262    void print(std::ostream& os, const Author2Vector& m) const;
263
264    svn_revnum_t revision_; // Should be the latest revision for whole project
265    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
266
267    std::vector<Author2Vector> stats_; // from linetype to a2v
268
269    // using compiler generated copy constructor
270    //Stats(const Stats&);
271    // no assignment
272    Stats& operator=(const Stats&);
273
274  };
275}} // end of namespace svndigest end of namespace theplu
276
277#endif
Note: See TracBrowser for help on using the repository browser.