DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1
9#include "opmonlib/Utils.hpp"
10
11
12dunedaq::opmon::OpMonId dunedaq::opmonlib::make_origin( const std::string & session, const std::string & app ) {
13 opmon::OpMonId origin;
14 origin.set_session( session );
15 origin.set_application( app );
16 return origin;
17}
18
19
20dunedaq::opmon::OpMonEntry dunedaq::opmonlib::to_entry(const google::protobuf::Message & m,
21 const CustomOrigin & co) {
22
24 entry.set_measurement( m.GetTypeName() );
25
27
28 for ( const auto & [tag, value] : co ) {
29 (*entry.mutable_custom_origin())[tag] = value;
30 }
31
32 return entry;
33}
34
35
36dunedaq::opmonlib::map_type dunedaq::opmonlib::to_map( const google::protobuf::Message & m,
37 std::string top_block) {
38
40
41 const auto * descriptor_p = m.GetDescriptor();
42 const auto & des = *descriptor_p;
43
44 const auto * reflection_p = m.GetReflection();
45 const auto & ref = *reflection_p;
46
47 using namespace google::protobuf;
48
49 auto count = des.field_count();
50 for ( int i = 0; i < count; ++i ) {
51 const auto * field_p = des.field(i);
52 if ( field_p -> is_repeated() ) continue;
53
54 auto name = top_block.empty() ? field_p -> name() : top_block + '.' + field_p -> name();
55
56 auto type = field_p -> cpp_type();
58 bool success = true;
59 switch (type) {
60 case FieldDescriptor::CppType::CPPTYPE_INT32:
61 value.set_int4_value( ref.GetInt32(m, field_p) );
62 break;
63 case FieldDescriptor::CppType::CPPTYPE_INT64:
64 value.set_int8_value( ref.GetInt64(m, field_p) );
65 break;
66 case FieldDescriptor::CppType::CPPTYPE_UINT32:
67 value.set_uint4_value( ref.GetUInt32(m, field_p) );
68 break;
69 case FieldDescriptor::CppType::CPPTYPE_UINT64:
70 value.set_uint8_value( ref.GetUInt64(m, field_p) );
71 break;
72 case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
73 value.set_double_value( ref.GetDouble(m, field_p) );
74 break;
75 case FieldDescriptor::CppType::CPPTYPE_FLOAT:
76 value.set_float_value( ref.GetFloat(m, field_p) );
77 break;
78 case FieldDescriptor::CppType::CPPTYPE_BOOL:
79 value.set_boolean_value( ref.GetBool(m, field_p) );
80 break;
81 case FieldDescriptor::CppType::CPPTYPE_STRING:
82 value.set_string_value( ref.GetString(m, field_p) );
83 break;
84 case FieldDescriptor::CppType::CPPTYPE_MESSAGE:
85 {
86 auto data = dunedaq::opmonlib::to_map(ref.GetMessage(m, field_p), name);
87 ret.insert(data.begin(), data.end());
88 success = false;
89 break;
90 }
91 default:
92 success = false;
93 break;
94 } // switch on the cpp type
95
96
97 if ( success )
98 ret[name] = value;
99 }
100
101 return ret;
102
103}
104
105
106void dunedaq::opmonlib::from_entry( google::protobuf::Message & m,
108 std::string top_block) {
109
110 const auto * descriptor_p = m.GetDescriptor();
111 const auto & des = *descriptor_p;
112
113 const auto * reflection_p = m.GetReflection();
114 const auto & ref = *reflection_p;
115
116 using namespace google::protobuf;
117
118 auto count = des.field_count();
119 for ( int i = 0; i < count; ++i ) {
120 const auto * field_p = des.field(i);
121 if ( field_p -> is_repeated() ) continue;
122
123 auto name = top_block.empty() ? field_p -> name() : top_block + '.' + field_p -> name();
124
125 auto type = field_p -> cpp_type();
126
127 if ( type == FieldDescriptor::CppType::CPPTYPE_MESSAGE ) {
128 auto message = ref.MutableMessage(&m, field_p);
129 from_entry( *message, e, name );
130 } else {
131
132 auto value = e.data().find(name)->second;
133
134 switch (type) {
135 case FieldDescriptor::CppType::CPPTYPE_INT32:
136 ref.SetInt32(&m, field_p, value.int4_value());
137 break;
138 case FieldDescriptor::CppType::CPPTYPE_INT64:
139 ref.SetInt64(&m, field_p, value.int8_value());
140 break;
141 case FieldDescriptor::CppType::CPPTYPE_UINT32:
142 ref.SetUInt32(&m, field_p, value.uint4_value());
143 break;
144 case FieldDescriptor::CppType::CPPTYPE_UINT64:
145 ref.SetUInt64(&m, field_p, value.uint8_value());
146 break;
147 case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
148 ref.SetDouble(&m, field_p, value.double_value());
149 break;
150 case FieldDescriptor::CppType::CPPTYPE_FLOAT:
151 ref.SetFloat(&m, field_p, value.float_value());
152 break;
153 case FieldDescriptor::CppType::CPPTYPE_BOOL:
154 ref.SetBool(&m, field_p, value.boolean_value());
155 break;
156 case FieldDescriptor::CppType::CPPTYPE_STRING:
157 ref.SetString(&m, field_p, value.string_value());
158 break;
159 default:
160 break;
161 } // switch on the cpp type
162 } // else if it's a message
163 } // loop over the fields
164
165}
166
168
169 std::string ret(id.session());
170
171 if ( ! id.application().empty() ) {
172 ret += '.';
173 ret += id.application();
174 }
175
176 for( int i = 0; i < id.substructure().size(); ++i ) {
177 ret += '.';
178 ret += id.substructure()[i];
179 }
180
181 return ret;
182}
183
184
186 const std::string & element) {
187
188 if ( element.empty() ) return id;
189
190 if ( id.session().empty() ) {
191 id.set_session(element);
192 return id;
193 }
194
195 if ( id.application().empty() ) {
196 id.set_application(element);
197 return id;
198 }
199
200 id.add_substructure(element);
201 return id;
202}
203
204
206 const std::string & element ) {
207
208 auto ret = id;
209 ret += element;
210 return ret;
211}
212
213
214
215//---------------------------------------------------
216// template specifications for setters by name
217//---------------------------------------------------
218template<>
219void dunedaq::opmonlib::set_value<int32_t>( const google::protobuf::Reflection & r,
220 google::protobuf::Message & m,
221 const google::protobuf::FieldDescriptor* f_p, int32_t value) {
222
223 r.SetInt32(&m, f_p, value);
224}
225
226template<>
227void dunedaq::opmonlib::set_value<int64_t>( const google::protobuf::Reflection & r,
228 google::protobuf::Message & m,
229 const google::protobuf::FieldDescriptor* f_p, int64_t value) {
230
231 r.SetInt64(&m, f_p, value);
232}
233
234template<>
235void dunedaq::opmonlib::set_value<uint32_t>( const google::protobuf::Reflection & r,
236 google::protobuf::Message & m,
237 const google::protobuf::FieldDescriptor* f_p, uint32_t value) {
238
239 r.SetUInt32(&m, f_p, value);
240}
241
242template<>
243void dunedaq::opmonlib::set_value<uint64_t>( const google::protobuf::Reflection & r,
244 google::protobuf::Message & m,
245 const google::protobuf::FieldDescriptor* f_p, uint64_t value) {
246
247 r.SetUInt64(&m, f_p, value);
248}
249
250template<>
251void dunedaq::opmonlib::set_value<double>( const google::protobuf::Reflection & r,
252 google::protobuf::Message & m,
253 const google::protobuf::FieldDescriptor* f_p, double value) {
254
255 r.SetDouble(&m, f_p, value);
256}
257
258template<>
259void dunedaq::opmonlib::set_value<float>( const google::protobuf::Reflection & r,
260 google::protobuf::Message & m,
261 const google::protobuf::FieldDescriptor* f_p, float value) {
262
263 r.SetFloat(&m, f_p, value);
264}
265
266template<>
267void dunedaq::opmonlib::set_value<bool>( const google::protobuf::Reflection & r,
268 google::protobuf::Message & m,
269 const google::protobuf::FieldDescriptor* f_p, bool value) {
270
271 r.SetBool(&m, f_p, value);
272}
273
274template<>
275void dunedaq::opmonlib::set_value<std::string>( const google::protobuf::Reflection & r,
276 google::protobuf::Message & m,
277 const google::protobuf::FieldDescriptor* f_p, std::string value) {
278
279 r.SetString(&m, f_p, value);
280}
::google::protobuf::Map< std::string, ::dunedaq::opmon::OpMonValue > * mutable_data()
const ::google::protobuf::Map< std::string, ::dunedaq::opmon::OpMonValue > & data() const
void set_measurement(Arg_ &&arg, Args_... args)
::google::protobuf::Map< std::string, std::string > * mutable_custom_origin()
void set_session(Arg_ &&arg, Args_... args)
void set_application(Arg_ &&arg, Args_... args)
std::string * add_substructure()
void set_int4_value(::int32_t value)
void set_uint4_value(::uint32_t value)
void set_boolean_value(bool value)
void set_string_value(Arg_ &&arg, Args_... args)
void set_uint8_value(::uint64_t value)
void set_int8_value(::int64_t value)
void set_float_value(float value)
void set_double_value(double value)
const dunedaq::opmon::OpMonId & operator+=(dunedaq::opmon::OpMonId &, const std::string &element)
Definition Utils.cpp:185
dunedaq::opmon::OpMonId operator+(const dunedaq::opmon::OpMonId &, const std::string &element)
Definition Utils.cpp:205
void set_value(google::protobuf::Message &m, const std::string &name, T value)
Definition Utils.hxx:17
std::remove_const< std::remove_reference< cr_map_type >::type >::type map_type
Definition Utils.hpp:56
M from_entry(const dunedaq::opmon::OpMonEntry &)
Definition Utils.hxx:2
dunedaq::opmon::OpMonEntry to_entry(const google::protobuf::Message &m, const CustomOrigin &co)
Definition Utils.cpp:20
opmon::OpMonId make_origin(const std::string &session, const std::string &app)
Definition Utils.cpp:12
map_type to_map(const google::protobuf::Message &m, std::string top_block="")
Definition Utils.cpp:36
std::string to_string(const dunedaq::opmon::OpMonId &)
Definition Utils.cpp:167
std::map< std::string, std::string > CustomOrigin
Definition Utils.hpp:53
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size