source: trunk/lib/Stats.h @ 1290

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

closes #457. Lifting out plotting functions in Stats into a new class
StatsPlotter?. In same manner lifting out print functions from
Node/Directory/File? into a new hierarchy
NodePrinter/DirectoryPrinter/FilePrinter?. To accomplish these splits
without to much recoding some friendships are introduced. These new
classes are only needed for svndigest (not svncopyright) and are
therefore placed in a library named libsvndigest together with Graph
class and first_page functions. Remaining functionality, shared
between svncopyright and svndigest, are located in libsvndigest_core
and linked into both binaries.

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