Register accumulators on stat engine to centralize reports. TODO remove from engine...
[anna.git] / source / statistics / Engine.cpp
index b6e1ee1..b90b609 100644 (file)
@@ -9,7 +9,7 @@
 // Local
 #include <anna/statistics/Engine.hpp>
 #include <anna/statistics/internal/sccs.hpp>
-
+#include <anna/core/mt/Guard.hpp>
 #include <anna/core/functions.hpp>
 #include <anna/xml/xml.hpp>
 
@@ -24,6 +24,10 @@ using namespace anna::statistics;
 //******************************************************************************
 //----------------------------------------------------------------------- Engine
 //******************************************************************************
+
+//------------------------------------------------------------------------------
+//------------------------------------------------------------- Engine::Engine()
+//------------------------------------------------------------------------------
 Engine::Engine() {
   statistics::sccs::activate();
   a_enabled = false;
@@ -31,6 +35,15 @@ Engine::Engine() {
 }
 
 
+//------------------------------------------------------------------------------
+//------------------------------------------------------------ Engine::~Engine()
+//------------------------------------------------------------------------------
+Engine::~Engine() {
+  for (_accumulator_vector_it it = a_accumulators.begin(); it != a_accumulators.end(); it++)
+    delete (*it);
+}
+
+
 //------------------------------------------------------------------------------
 //--------------------------------------------------------- Engine::addConcept()
 //------------------------------------------------------------------------------
@@ -119,6 +132,8 @@ bool Engine::disableSampleLog(const int & id) throw() {
 //--------------------------------------------------- Engine::disableSampleLog()
 //------------------------------------------------------------------------------
 bool Engine::logSample(const int & conceptId, const anna::Millisecond & unixTimestamp, const double & value) const throw() {
+  anna::Guard guard(a_mutex);
+
   _concept_identification_map_iter it = a_concept_identification_map.find(conceptId);
 
   if(it == a_concept_identification_map.end()) return false;
@@ -139,6 +154,19 @@ bool Engine::logSample(const int & conceptId, const anna::Millisecond & unixTime
   return true;
 }
 
+
+//------------------------------------------------------------------------------
+//----------------------------------------------------------- Engine::asString()
+//------------------------------------------------------------------------------
+Accumulator *Engine::createAccumulator() throw() {
+  Accumulator *result;
+
+  result = new Accumulator();
+  a_accumulators.push_back(result);
+  return result;
+}
+
+
 //------------------------------------------------------------------------------
 //----------------------------------------------------------- Engine::asString()
 //------------------------------------------------------------------------------
@@ -201,6 +229,11 @@ anna::xml::Node* Engine::asXML(anna::xml::Node* parent, const int & numberOfDeci
     concept->createAttribute("IntegerNatureSample", (*iter).second.IntegerNatureSample ? "yes" : "no");
   }
 
+  // Accumulators:
+  for (_accumulator_vector_it it = a_accumulators.begin(); it != a_accumulators.end(); it++) {
+    anna::xml::Node* accumulators = result->createChild("anna.statistics.Accumulators");
+    (*it)->asXML(accumulators);
+  }
+
   return result;
 }
-