The Gaudi Framework  v36r6 (b1ee9983)
JSONSink.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2021 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
12 #include "Gaudi/MonitoringHub.h"
13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/Service.h"
15 
16 #include <boost/algorithm/string.hpp>
17 
18 #include <fmt/core.h>
19 #include <fmt/format.h>
20 
21 #include <Gaudi/Property.h>
22 #include <deque>
23 #include <fstream>
24 #include <map>
25 #include <nlohmann/json.hpp>
26 #include <string_view>
27 
28 namespace {
29  void writeFileJson( std::string fileName, bool fulloutput,
31 
32  // perpareJsonFile
33  using json = nlohmann::json;
34  json jsonfile;
35 
36  // dump all counters
37  for ( auto& [algoName, entityMap] : sEntities ) {
38  for ( auto& [entityName, entity] : entityMap ) {
39  if ( !fulloutput ) {
40  if ( !entity.at( "empty" ).template get<bool>() ) {
41  jsonfile[algoName][entityName] = entity;
42  jsonfile[algoName][entityName].erase( "empty" );
43  // jsonfile[algoName][entityName].erase( "type" );
44  jsonfile[algoName][entityName].erase( "sum2" );
45  }
46  } else {
47  jsonfile[algoName][entityName] = entity;
48  }
49  }
50  }
51  std::ofstream os( fileName, std::ios::out );
52  os << jsonfile.dump( 4 );
53  os.close();
54  }
55 } // namespace
56 
57 namespace Gaudi::Monitoring {
58 
59  class JSONSink : public Service, public Hub::Sink {
60 
61  public:
62  using Service::Service;
63 
65  StatusCode initialize() override {
66  return Service::initialize().andThen( [&] {
67  // declare ourself as a monitoding sink
68  serviceLocator()->monitoringHub().addSink( this );
69  } );
70  }
71 
73  StatusCode stop() override;
74 
75  // Gaudi::Monitoring::Hub::Sink implementation
76  void registerEntity( Hub::Entity ent ) override {
77  if ( std::string_view( ent.type ).substr( 0, 8 ) == "counter:" || ent.type == "statentity" ||
78  ent.type == "histogram" ) {
79  m_monitoringEntities.emplace_back( std::move( ent ) );
80  }
81  }
82 
83  // Gaudi::Monitoring::Hub::Sink implementation
84  void removeEntity( Hub::Entity const& ent ) override {
86  if ( it != m_monitoringEntities.end() ) { m_monitoringEntities.erase( it ); }
87  }
88 
89  private:
91  Gaudi::Property<std::string> m_jsonOutputFileName{ this, "FileName", "counters.json",
92  "Name of JSON file for counters dump" };
93  Gaudi::Property<bool> m_jsonOutputFullInfo{ this, "DumpFullInfo", false,
94  "Dump full Counter info in the JSON output file" };
95  };
96 
97  DECLARE_COMPONENT( JSONSink )
98 } // namespace Gaudi::Monitoring
99 
101  // We will try to mimic the old monitoring of counters, so we need to split
102  // them per Algo. The algo name can be extracted form the id of the entity
103  // as its format is "algoName/counterName"
104  // This map groups entities per algoName. For each name, the submap gives
105  // the counter name of each subentity and the associated json
107  // fill the sorted map
108  for ( auto& entity : m_monitoringEntities ) { sortedEntities[entity.component][entity.name] = entity.toJSON(); }
109 
110  if ( !m_jsonOutputFileName.empty() ) {
111  info() << "Writing counters to JSON file " << m_jsonOutputFileName.value() << endmsg;
112  writeFileJson( m_jsonOutputFileName, m_jsonOutputFullInfo, sortedEntities );
113  }
114 
115  return Service::stop();
116 }
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:129
Gaudi::Monitoring::JSONSink::removeEntity
void removeEntity(Hub::Entity const &ent) override
Definition: JSONSink.cpp:84
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
Gaudi::Monitoring::JSONSink
Definition: JSONSink.cpp:59
std::string
STL class.
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
std::move
T move(T... args)
MonitoringHub.h
std::find
T find(T... args)
Gaudi::Monitoring
Definition: JSONSink.cpp:57
jsonFromLHCbLog.json
dictionary json
Definition: jsonFromLHCbLog.py:87
Gaudi::Monitoring::JSONSink::m_jsonOutputFullInfo
Gaudi::Property< bool > m_jsonOutputFullInfo
Definition: JSONSink.cpp:93
Gaudi::Monitoring::JSONSink::m_monitoringEntities
std::deque< Hub::Entity > m_monitoringEntities
Definition: JSONSink.cpp:90
Gaudi::Monitoring::JSONSink::registerEntity
void registerEntity(Hub::Entity ent) override
Definition: JSONSink.cpp:76
Service
Definition: Service.h:46
Gaudi::Monitoring::JSONSink::initialize
StatusCode initialize() override
initialization, registers to Monitoring::Hub
Definition: JSONSink.cpp:65
StatusCode
Definition: StatusCode.h:65
std::ofstream
STL class.
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:240
std::deque
STL class.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Gaudi::Monitoring::JSONSink::stop
StatusCode stop() override
stop method, handles the printing
Definition: JSONSink.cpp:100
std::map
STL class.
IOTest.end
def end
Definition: IOTest.py:128
Service.h
Gaudi::Monitoring::JSONSink::m_jsonOutputFileName
Gaudi::Property< std::string > m_jsonOutputFileName
Definition: JSONSink.cpp:91
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor
Definition: Service.cpp:339
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:71
Gaudi::Property< std::string >
Property.h
MsgStream.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:90
PrepareBase.out
out
Definition: PrepareBase.py:20