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