source: trunk/lib/Stats.h

Last change on this file was 1478, checked in by Peter Johansson, 9 years ago

closes #387

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 1478 2012-05-29 10:17:04Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2010, 2012 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#include "Vector.h"
29
30#include <subversion-1/svn_types.h>
31
32#include <map>
33#include <istream>
34#include <set>
35#include <string>
36#include <vector>
37
38namespace theplu{
39namespace svndigest{
40
41  ///
42  /// Class taking care of statistics from svn.
43  ///
44  class Stats
45  {
46  public:
47    ///
48    /// @brief Default Constructor
49    ///
50    explicit Stats(const std::string& path);
51
52    /**
53       \brief Destructor
54    */
55    virtual ~Stats(void);
56
57    ///
58    /// @return set of authors
59    ///
60    const std::set<std::string>& authors(void) const;
61
62    ///
63    /// \return number of code lines for \a user for latest revision.
64    ///
65    unsigned int code(const std::string& user="all") const;
66
67
68    ///
69    /// \return number of comments lines for \a user for latest revision.
70    ///
71    unsigned int comments(const std::string& user="all") const;
72
73    ///
74    /// \return number of empty lines for \a user for latest revision.
75    ///
76    unsigned int empty(const std::string& user="all") const;
77
78    ///
79    /// \return revison node was modified
80    ///
81    svn_revnum_t last_changed_rev(void) const;
82
83    ///
84    /// \return number of lines for \a user for latest revision
85    ///
86    unsigned int lines(const std::string& user="all") const;
87
88    /**
89       Load object from a stream.
90
91       \a latest_ver is set to true if cache_file is latest version;
92       otherwise it is set to false.
93
94       \return revision the cache represents - 0 if failed
95     */
96    svn_revnum_t load_cache(std::istream&, bool& latest_ver);
97
98    /**
99       Do the parsing for \a path. Revisions from \a rev will be parsed.
100    */
101    void parse(const std::string& path, svn_revnum_t rev=0);
102
103    /**
104       Send Stats to a stream.
105     */
106    void print(std::ostream&) const;
107
108    ///
109    /// @brief Clear all statistics
110    ///
111    void reset(void);
112
113    /**
114       set vectors in stats_ to size \a rev
115    */
116    void resize(svn_revnum_t rev);
117
118    ///
119    /// \return latest revision for whole project
120    ///
121    svn_revnum_t revision(void) const;
122
123    /**
124       \brief set (greatest) revision to be ignored
125     */
126    void ignore_rev(svn_revnum_t rev);
127
128    /**
129       \return resulting Stats
130    */
131    Stats& operator+=(const Stats&);
132
133    /**
134       \return number of lines for \a author and \a linetype from
135       revision \a rev.
136
137       \throw if \a author does not exist
138     */
139    size_t operator()(int linetype, std::string author, svn_revnum_t rev) const;
140
141  protected:
142    typedef std::map<std::string, SumVector> Author2Vector;
143    typedef Author2Vector::iterator A2VIter;
144    typedef Author2Vector::const_iterator A2VConstIter;
145
146    std::vector<Author2Vector> stats_; // from linetype to a2v
147
148    /**
149     */
150    void add(const std::vector<std::map<std::string, SparseVector> >& data);
151
152    void add_author(std::string);
153    void add_authors(std::set<std::string>::const_iterator,
154                     std::set<std::string>::const_iterator);
155
156    // references to data
157    inline Author2Vector& code_stats(void)
158    { return stats_[LineTypeParser::code]; }
159    inline Author2Vector& comment_stats(void)
160    { return stats_[LineTypeParser::comment]; }
161    inline Author2Vector& copyright_stats(void)
162    { return stats_[LineTypeParser::copyright]; }
163    inline Author2Vector& other_stats(void)
164    { return stats_[LineTypeParser::other]; }
165    inline Author2Vector& comment_or_copy_stats(void)
166    { return stats_[LineTypeParser::comment_or_copy]; }
167    inline Author2Vector& total_stats(void)
168    { return stats_[LineTypeParser::total]; }
169
170    // const references to data
171    inline const Author2Vector& code_stats(void) const
172    { return stats_[LineTypeParser::code]; }
173    inline const Author2Vector& comment_stats(void) const
174    { return stats_[LineTypeParser::comment]; }
175    inline const Author2Vector& copyright_stats(void) const
176    { return stats_[LineTypeParser::copyright]; }
177    inline const Author2Vector& other_stats(void) const
178    { return stats_[LineTypeParser::other]; }
179    inline const Author2Vector& comment_or_copy_stats(void) const
180    { return stats_[LineTypeParser::comment_or_copy]; }
181    inline const Author2Vector& total_stats(void) const
182    { return stats_[LineTypeParser::total]; }
183
184    /**
185       const access to ignore_rev_
186     */
187    const svn_revnum_t& ignore_rev(void) const;
188
189    /**
190       add range [\a first, \a last) to \a map
191     */
192    void map_add(Author2Vector::const_iterator first,
193                 Author2Vector::const_iterator last,
194                 Author2Vector& map);
195
196    std::set<std::string> authors_;
197
198    const SumVector& get_vector(const Author2Vector&,
199                                const std::string& user) const;
200  private:
201    /// one liner used in cache file to validate that cache file was
202    /// created current configuration
203    std::string config_code_;
204
205    void add(SumVector& vec, svn_revnum_t rev, unsigned int n);
206
207    /**
208       \return config_code unless ignore_rev_!=0 in which case
209       ignore_rev_ is appended to returned string
210     */
211    std::string config_code(void) const;
212
213    /**
214       Parse statistics for fundamental categories, i.e., code,
215       comment, empty, and copyright for each author. Ignore revisions
216       earlier than \a first_rev.
217     */
218    virtual void do_parse(const std::string&, svn_revnum_t first_rev)=0;
219
220    /// load cache file version 7
221    svn_revnum_t load_cache7(std::istream&);
222    /// load cache file version 8
223    svn_revnum_t load_cache8(std::istream&);
224
225    // Change this string if cache format is changed in such a way
226    // that all old cache files are obsolete.
227    inline std::string cache_check_str(void) const
228    {return "CACHE FILE VERSION 7";}
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    /**
236       Finds the largets element by iterating through the entire
237       vector. Inherited classes should implement their own version
238       when it is possible to get the largest element in faster than
239       in linear time.
240
241       \return the largest largest element in \a v.
242    */
243    virtual unsigned int max_element(const SumVector& v) const;
244
245    void print(std::ostream& os, const Author2Vector& m) const;
246
247    svn_revnum_t ignore_rev_;
248    svn_revnum_t revision_; // Should be the latest revision for whole project
249    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
250
251    // using compiler generated copy constructor
252    //Stats(const Stats&);
253    // no assignment
254    Stats& operator=(const Stats&);
255
256    friend class StatsPlotter;
257  };
258}} // end of namespace svndigest end of namespace theplu
259
260#endif
Note: See TracBrowser for help on using the repository browser.