KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
statskeeper.hpp
Go to the documentation of this file.
1 // File : statskeeper.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _STATSKEEPER_HPP_
20 #define _STATSKEEPER_HPP_
21 
22 // THIS HAS TO BE A SINGLETON!!!!
23 
24 #include <string>
25 #include <map>
26 #include <ctime>
27 #include <boost/thread.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/locks.hpp>
30 #include <boost/shared_ptr.hpp>
31 #include "boost_ptree.hpp"
32 #include "logstream.hpp"
33 #include "statable_queue.hpp"
34 
35 namespace kisscpp
36 {
37  typedef std::map<std::string, std::deque<double> > GatheredStatsMapType;
38  typedef GatheredStatsMapType::iterator GatheredStatsMapTypeIterator;
39 
40  typedef std::map<std::string, double > StatsMapType;
41  typedef StatsMapType::iterator StatsMapTypeIterator;
42  typedef boost::shared_ptr<StatsMapType> SharedStatsMapType;
43 
44  typedef std::vector<double> StatsHistoryType;
45  typedef std::map<std::string, StatsHistoryType> StatsHistoryMapType;
46  typedef StatsHistoryMapType::iterator StatsHistoryMapTypeIterator;
47  typedef boost::shared_ptr<StatsHistoryMapType> SharedStatsHistoryMapType;
48 
49  typedef std::map<std::string, sharedStatAbleQ > QueueStatsMapType;
50  typedef QueueStatsMapType::iterator QueueStatsMapTypeIterator;
51 
52 
53  // The StasKeeper class is impelemented as a singleton.
54  // For those of you with the opinion that singleton is an anti-patern, I have this to say:
55  // Just because you don't know when a technique is valid, doesn't mean it's always invalid.
56  // There is no such thing as an Anti-patern. There is however, such a thing as applying a
57  // patern inapropriately. It is not the patern that is at fault. The developer and everyone
58  // allowing that developer to do it, are the ones that need fixing. So, go back to your
59  // text on the subject, and revise the material untill you get it.
61  {
62  public:
63  static StatsKeeper* instance(unsigned long int gp = 300, // gp - Gather Period as a number of seconds (defaults to 5minutes)
64  unsigned long int hl = 12); // hl - history length, i.e. Number of historic gathered periods to keep. (default to 12 periods so that we have an hours worth of history.)
65  void start ();
66  void stop ();
67 
69  {
70  kisscpp::LogStream log(__PRETTY_FUNCTION__);
71  stop();
72  };
73 
74  void setStatValue (std::string id, double value = 0);
75  void increment (std::string id, double value = 1);
76  void decrement (std::string id, double value = 1);
77  void addStatableQueue(std::string id, sharedStatAbleQ ssq);
78 
82 
83  protected:
84  private:
85  StatsKeeper () { kisscpp::LogStream log(__PRETTY_FUNCTION__); } // Private to prevent copying.
86  StatsKeeper (StatsKeeper const&){ kisscpp::LogStream log(__PRETTY_FUNCTION__); }; // Private to prevent copying.
87  StatsKeeper& operator=(StatsKeeper const&){ kisscpp::LogStream log(__PRETTY_FUNCTION__); }; // Private to prevent assignment.
88 
89  StatsKeeper(unsigned long int gp,
90  unsigned long int hl) :
91  gatherPeriod (gp),
92  historyLength(hl),
93  running(false)
94  {
95  kisscpp::LogStream log(__PRETTY_FUNCTION__);
96  start();
97  }
98 
99  void gatherStats();
100 
101  static StatsKeeper *singleton_instance;
102  unsigned long int gatherPeriod;
103  unsigned long int historyLength;
104  bool running;
105  boost::mutex statMutex;
106  boost::thread_group threadGroup;
107  GatheredStatsMapType gatheredStatsMap;
108  StatsMapType statsMap;
109  time_t startTime;
110  QueueStatsMapType queueStatsMap;
111  };
112 }
113 
114 #endif // _STATSKEEPER_HPP_
GatheredStatsMapType::iterator GatheredStatsMapTypeIterator
Definition: statskeeper.hpp:38
Definition: statskeeper.hpp:60
void stop()
Definition: statskeeper.cpp:47
StatsHistoryMapType::iterator StatsHistoryMapTypeIterator
Definition: statskeeper.hpp:46
boost::shared_ptr< StatsMapType > SharedStatsMapType
Definition: statskeeper.hpp:42
void addStatableQueue(std::string id, sharedStatAbleQ ssq)
Definition: statskeeper.cpp:83
SharedStatsMapType getCurrentStats()
Definition: statskeeper.cpp:89
SharedStatsMapType getLastGatheredStats()
Definition: statskeeper.cpp:112
StatsMapType::iterator StatsMapTypeIterator
Definition: statskeeper.hpp:41
SharedStatsHistoryMapType getFullStatsHistory()
Definition: statskeeper.cpp:128
QueueStatsMapType::iterator QueueStatsMapTypeIterator
Definition: statskeeper.hpp:50
~StatsKeeper()
Definition: statskeeper.hpp:68
boost::shared_ptr< StatsHistoryMapType > SharedStatsHistoryMapType
Definition: statskeeper.hpp:47
static StatsKeeper * instance(unsigned long int gp=300, unsigned long int hl=12)
Definition: statskeeper.cpp:26
boost::shared_ptr< StatAbleQueue > sharedStatAbleQ
Definition: statable_queue.hpp:20
std::map< std::string, sharedStatAbleQ > QueueStatsMapType
Definition: statskeeper.hpp:49
std::map< std::string, StatsHistoryType > StatsHistoryMapType
Definition: statskeeper.hpp:45
std::map< std::string, double > StatsMapType
Definition: statskeeper.hpp:40
Definition: logstream.hpp:145
void setStatValue(std::string id, double value=0)
Definition: statskeeper.cpp:54
std::map< std::string, std::deque< double > > GatheredStatsMapType
Definition: statskeeper.hpp:37
void start()
Definition: statskeeper.cpp:37
void increment(std::string id, double value=1)
Definition: statskeeper.cpp:61
void decrement(std::string id, double value=1)
Definition: statskeeper.cpp:72
std::vector< double > StatsHistoryType
Definition: statskeeper.hpp:44