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_Accumulator_hpp
10 #define anna_statistics_Accumulator_hpp
12 #include <anna/core/RuntimeException.hpp>
13 #include <anna/core/functions.hpp>
27 namespace statistics {
34 //Sum = 0, // is not very useful
37 BesselStandardDeviation, //This correction (the use of N - 1 instead of N) is known as Bessel's correction
45 unsigned long long Size; // Sample size
46 double Sum; // Accumulated sum
47 double SquareSum; // Accumulated sum of square values
48 double Average; // Current average
49 //double SquareSumSampleDeviation; // Current total sum for square deviation items;
50 double MinimumProcessed; // Minimum value processed
51 double MaximumProcessed; // Maximum value processed
52 anna::Millisecond MinimumEventTimestampMs; // Timestamp (ms) for minimum event
53 anna::Millisecond MaximumEventTimestampMs; // Timestamp (ms) for maximum event
54 anna::Millisecond LastResetEventTimestampMs; // Timestamp (ms) for last statistics reset
58 Size = (unsigned long long)0;
60 SquareSum = (double)0;
62 //SquareSumSampleDeviation = (double)0;
63 MinimumProcessed = (double)0;
64 MaximumProcessed = (double)0;
65 MinimumEventTimestampMs = (anna::Millisecond)0;
66 MaximumEventTimestampMs = (anna::Millisecond)0;
67 LastResetEventTimestampMs = anna::Millisecond::getTime();
72 typedef std::map <int, _concept_data_t> _concept_data_map_t;
73 typedef std::map <int, _concept_data_t>::const_iterator _concept_data_map_iter;
78 //------------------------------------------------------------------------------
79 //------------------------------------------------------------ class Accumulator
80 //------------------------------------------------------------------------------
82 * Accumulated data information for statistic sample.
83 * It can manage a set of concepts at the same time.
85 * @short Contains statistical sample information
91 void initialize(const int & conceptId) throw();
92 _concept_data_t * getConcept(const int & conceptId) const throw(anna::RuntimeException);
93 // Gets the data structure for a existing concept Id.
94 // Initializes and returns the new data structure for a new stored concept Id which is valid (exists for the engine).
95 // Launch exception if concept id is not a valid concept registered at Engine.
97 /*mutable */_concept_data_map_t a_concept_data_map;
99 std::string floatFormat(const int & numberOfDecimals) const throw();
101 double getStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);
102 double getBesselStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);
110 * @param name Accumulator name
112 Accumulator(const std::string &name) : a_name(name) {};
121 * Sets the accumulator name
123 * @param name Name provided
125 void setName(const std::string &name) throw() { a_name = name; }
128 * Adds a new statistic concept through the accumulator, to ease the concept name creation,
129 * which will be a string defined by: concept name + ': ' + accumulator name.
131 * @param description Concept description; e.g.: processing time, messages size, etc.
132 * @param unit Concept unit description
133 * @param integerNatureSample Most of cases we will measure 'time' with the unit which force integer values
134 * (is more intuitive 850 msecs than 0,850 secs). Then, it is @em true by default.
135 * This is useful to advice better representation for some indicators like minimum/maximum
136 * within integer samples.
137 * @param conceptNameFormat Format to register the complete concept name. Will be parsed using (in order) the provided
138 * description, and the accumulator name: '<concept description> [<accumulator name>]' by default.
140 * @return Assigned concept identification number (sequence)
142 int addConcept(const std::string & description, const std::string & unit, const bool & integerNatureSample = true, const char *conceptNameFormat = "%s [%s]") throw();
145 * Process new value for the sample regarding a concept identifier previously registered through the engine.
147 * @param conceptId statistical concept processed
148 * @param value Value for processed item
152 void process(const int & conceptId, const double & value) throw(anna::RuntimeException);
156 * Reset sample for all concepts
160 void reset(void) throw();
164 * Reset sample for provided concept
166 * @param conceptId Concept identification to be reset
170 void reset(const int & conceptId) throw(anna::RuntimeException);
176 * @param accumulator Class instance source of data
178 * @return Returns copied reference
180 const Accumulator & operator = (const Accumulator & accumulator);
187 * Gets the accumulator name
189 * @result Accumulator name
191 const std::string &getName() const throw() { return a_name; }
194 * Gets current sample size for any concept id
196 * @param conceptId Concept identification
198 * @return Sample size
203 unsigned long long int sampleSize(const int & conceptId) const throw(anna::RuntimeException);
207 * Get statistical information
209 * @param conceptId Statistical concept
210 * @param operation Solicited operation
212 * @return Value for solicited conceptId/operation
217 double getValue(const int & conceptId, const Operation::Type & operation) const throw(anna::RuntimeException);
221 * Class string representation
223 * @param numberOfDecimals Number of float decimals at representation string. Default is 2.
225 * @return String with class content
227 std::string asString(const int & numberOfDecimals = 2) const throw();
231 * Class XML representation
233 * @param numberOfDecimals Number of float decimals at XML representation. Default is 2.
235 * @return XML with class content
237 anna::xml::Node* asXML(anna::xml::Node* parent, const int & numberOfDecimals = 2) const throw();