1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #ifndef anna_statistics_Engine_hpp
10 #define anna_statistics_Engine_hpp
12 #include <anna/core/Singleton.hpp>
13 #include <anna/core/util/Millisecond.hpp>
14 #include <anna/core/mt/Mutex.hpp>
22 #include <anna/statistics/Accumulator.hpp>
34 namespace statistics {
39 std::string SampleFile; // Sample file to optional writtings
40 std::string Description;
42 bool IntegerNatureSample;
44 } _concept_identification_t;
46 typedef std::map <int, _concept_identification_t> _concept_identification_map_t;
47 typedef std::map <int, _concept_identification_t>::const_iterator _concept_identification_map_iter;
48 typedef std::map <int, _concept_identification_t>::iterator _concept_identification_map_nc_iter;
50 typedef std::vector <Accumulator*> _accumulator_vector_t;
51 typedef std::vector <Accumulator*>::const_iterator _accumulator_vector_it;
55 * Class used to configure general behaviour of statistics processing
56 * By default, the engine is disabled and #enable must be invoked
59 class Engine : public anna::Singleton <Engine> {
70 * Adds a new statistic concept (for example "Time_between_processA_and_processB", "Database time", etc)
72 * @param description Concept description
73 * @param unit Concept unit description
74 * @param integerNatureSample Most of cases we will measure 'time' with the unit which force integer values
75 * (is more intuitive 850 msecs than 0,850 secs). Then, it is @em true by default.
76 * This is useful to advice better representation for some indicators like minimum/maximum
77 * within integer samples.
79 * @return Assigned concept identification number (sequence)
81 int addConcept(const std::string & description, const std::string & unit, const bool & integerNatureSample = true) throw();
85 * Stops statistics engine
87 void enable(void) throw() { a_enabled = true; }
91 * Starts statistics engine
93 void disable(void) throw() { a_enabled = false; }
96 * Enable sample log for statistics processings. Engine starts with this feature disabled. When a new concept id is added
97 * to the engine, sample log is also disabled for it.
99 * Sample logs are used to dump each processing for a certain concept identification (<unix timestamp>,<processed value>).
102 * @param id Concept identification. If -1 value is provided, all concepts will be activated.
103 * @param sampleFileName Absolute or relative to execution path, and completed with extension '.<concept id>.csv'. Empty string, disables log. Default file name is 'sample'
105 * @return @em false if not concept is registered with provided id
106 * @warning Many systems add concepts dynamically. This method only affects to current concepts registered at statistics engine.
108 bool enableSampleLog(const int & id = -1, const char *sampleFileName = NULL) throw();
112 * Disable sample log for statistics processings
114 * @param id Concept identification.
116 * @return @em false if not concept is registered with provided id
118 bool disableSampleLog(const int & id) throw();
123 * Gets statistic concept information.
125 * @param id Concept identification.
126 * @param description Concept description returned by reference
127 * @param unit Concept unit description returned by reference
128 * @param integerNatureSample nature returned by reference
130 * @return @em false if not concept is registered with provided id
132 bool getConcept(const int & id, std::string & description, std::string & unit, bool & integerNatureSample) const throw();
136 * Boolean about engine state (enabled / disabled)
138 bool enabled(void) const throw() { return (a_enabled); }
141 * There is an advantage creating Accumulators over the engine: the #asXML method will show all the controlled information
142 * easily. Anyway, you could allocate this class objects without using this. Then, this is a helper to create accumulators
143 * and centralize their reports.
145 * @return New allocated accumulator, to be used by the client
147 Accumulator *createAccumulator() throw();
150 * Class string representation
152 * @return String with class content
154 std::string asString(void) const throw();
158 * Class XML representation
160 * @param numberOfDecimals Number of float decimals at XML representation. Default is 2.
162 * @return XML with class content
164 anna::xml::Node* asXML(anna::xml::Node* parent, const int & numberOfDecimals = 2) const throw();
169 Engine(); // private constructor
171 _concept_identification_map_t a_concept_identification_map;
172 _accumulator_vector_t a_accumulators; // you could create accumulators regardless the engine, but this is easier and asXML will show all the information easily
174 int a_sequence_concept_id;
175 anna::Mutex a_mutex; // for logSample
177 bool logSample(const int & conceptId, const anna::Millisecond & unixTimestamp, const double & value) const throw();
179 friend class anna::Singleton <Engine>;
180 friend class Accumulator;