Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / statistics / Engine.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef anna_statistics_Engine_hpp
10 #define anna_statistics_Engine_hpp
11
12 #include <anna/core/Singleton.hpp>
13 #include <anna/core/util/Millisecond.hpp>
14
15 // Standard
16 #include <string>
17 #include <map>
18
19 // Local
20 #include <anna/statistics/Accumulator.hpp>
21
22 namespace anna {
23 namespace xml {
24 class Node;
25 }
26 }
27
28
29
30 namespace anna {
31
32 namespace statistics {
33
34
35 typedef struct {
36
37   std::string SampleFile; // Sample file to optional writtings
38   std::string Description;
39   std::string Unit;
40   bool IntegerNatureSample;
41
42 } _concept_identification_t;
43
44 typedef std::map <int, _concept_identification_t> _concept_identification_map_t;
45 typedef std::map <int, _concept_identification_t>::const_iterator _concept_identification_map_iter;
46 typedef std::map <int, _concept_identification_t>::iterator _concept_identification_map_nc_iter;
47
48
49
50 /**
51 * Class used to configure general behaviour of statistics processing
52 * By default, the engine is disabled and #enable must be invoked
53 */
54
55 class Engine : public anna::Singleton <Engine> {
56
57
58 public:
59
60   // Sets
61
62   /**
63   * Adds a new statistic concept (for example "Time_between_processA_and_processB", "Database time", etc)
64   *
65   * @param description Concept description
66   * @param unit Concept unit description
67   * @param integerNatureSample Most of cases we will measure 'time' with the unit which force integer values
68   *                            (is more intuitive 850 msecs than 0,850 secs). Then, it is @em true by default.
69   *                            This is useful to advice better representation for some indicators like minimum/maximum
70   *                             within integer samples.
71   *
72   * @return Assigned concept identification number (sequence)
73   */
74   int addConcept(const std::string & description, const std::string & unit, const bool & integerNatureSample = true) throw();
75
76
77   /**
78   * Stops statistics engine
79   */
80   void enable(void) throw() { a_enabled = true; }
81
82
83   /**
84   * Starts statistics engine
85   */
86   void disable(void) throw() { a_enabled = false; }
87
88   /**
89   * Enable sample log for statistics processings. Engine starts with this feature disabled. When a new concept id is added
90   * to the engine, sample log is also disabled for it.
91   *
92   * Sample logs are used to dump each processing for a certain concept identification (<unix timestamp>,<processed value>).
93   *
94   *
95   * @param id Concept identification. If -1 value is provided, all concepts will be activated.
96   * @param sampleFileName Absolute or relative to execution path, and completed with extension '.<concept id>.csv'. Empty string, disables log. Default file name is 'sample'
97   *
98   * @return @em false if not concept is registered with provided id
99   * @warning Many systems add concepts dynamically. This method only affects to current concepts registered at statistics engine.
100   */
101   bool enableSampleLog(const int & id = -1, const char *sampleFileName = NULL) throw();
102
103
104   /**
105   * Disable sample log for statistics processings
106   *
107   * @param id Concept identification.
108   *
109   * @return @em false if not concept is registered with provided id
110   */
111   bool disableSampleLog(const int & id) throw();
112
113   // Gets
114
115   /**
116   * Gets statistic concept information.
117   *
118   * @param id Concept identification.
119   * @param description Concept description returned by reference
120   * @param unit Concept unit description returned by reference
121   * @param integerNatureSample nature returned by reference
122   *
123   * @return @em false if not concept is registered with provided id
124   */
125   bool getConcept(const int & id, std::string & description, std::string & unit, bool & integerNatureSample) const throw();
126
127
128   /**
129   * Boolean about engine state (enabled / disabled)
130   */
131   bool enabled(void) const throw() { return (a_enabled); }
132
133
134   /**
135   * Class string representation
136   *
137   * @return String with class content
138   */
139   std::string asString(void) const throw();
140
141
142   /**
143   * Class XML representation
144   *
145   * @param numberOfDecimals Number of float decimals at XML representation. Default is 2.
146   *
147   * @return XML with class content
148   */
149   anna::xml::Node* asXML(anna::xml::Node* parent, const int & numberOfDecimals = 2) const throw();
150
151
152 private:
153
154   Engine(); // private constructor
155
156   _concept_identification_map_t a_concept_identification_map;
157   bool a_enabled;
158   int a_sequence_concept_id;
159
160   bool logSample(const int & conceptId, const anna::Millisecond & unixTimestamp, const double & value) const throw();
161
162   friend class anna::Singleton <Engine>;
163   friend class Accumulator;
164 };
165
166
167 }
168 }
169
170 #endif