Fix local server for multiple applications
[anna.git] / source / diameter.comm / MessageStatistics.cpp
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 // Standard
9 #include <string>
10
11 // Project
12 #include <anna/diameter.comm/MessageStatistics.hpp>
13 #include <anna/statistics/Engine.hpp>
14 #include <anna/diameter/functions.hpp>
15 #include <anna/diameter/helpers/defines.hpp>
16 #include <anna/diameter/codec/Message.hpp>
17 #include <anna/diameter/codec/Engine.hpp>
18 #include <anna/diameter/codec/EngineManager.hpp>
19
20
21 void anna::diameter::comm::MessageStatistics::initialize(const std::string &name) noexcept(false) {
22
23   // Avoid exception if already created (happens for 2 origin hosts with same local server)
24   a_accumulator = anna::statistics::Engine::instantiate().getAccumulator(name);
25
26   if (!a_accumulator)
27     a_accumulator = anna::statistics::Engine::instantiate().createAccumulator(name);
28 }
29
30
31 void anna::diameter::comm::MessageStatistics::process(const ConceptType::_v &conceptType, const anna::diameter::CommandId &cid, const double & value) noexcept(false) {
32
33   // Development issue:
34 //  if (!a_accumulator) {
35 //    LOGWARNING(anna::Logger::warning("Cannot process uninitialized 'MessageStatistics' instance", ANNA_FILE_LOCATION));
36 //    return;
37 //  }
38
39   // retrieve the concept id for the pair:
40   ConceptId conceptId(cid, conceptType);
41   int engine_concept_id = 0;
42   concepts_map_it it = a_concepts.find(conceptId);
43   if (it != a_concepts.end()) { // found
44     engine_concept_id = it->second;
45   }
46   else { // register a new concept id
47     std::string description;
48     if(conceptType == ConceptType::SentRequestProcessingTime) {
49       description = anna::functions::asString("Processing time for outgoing requests (code = %d)", cid.first);
50     }
51     else if (conceptType == ConceptType::ReceivedMessageSize) {
52       description = "Message size for received messages ";
53       description += anna::diameter::functions::commandIdAsPairString(cid);
54     }
55
56     engine_concept_id = a_accumulator->addConcept(description, "ms", true/* integer values */);
57     a_concepts[conceptId] = engine_concept_id;
58   }
59
60   a_accumulator->process(engine_concept_id, value);
61 }
62