Line data Source code
1 : /**
2 : * @file Utils.cpp
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "opmonlib/Utils.hpp"
10 :
11 : dunedaq::opmon::OpMonId
12 11 : dunedaq::opmonlib::make_origin(const std::string& session, const std::string& app)
13 : {
14 11 : opmon::OpMonId origin;
15 11 : origin.set_session(session);
16 11 : origin.set_application(app);
17 11 : return origin;
18 0 : }
19 :
20 : dunedaq::opmon::OpMonEntry
21 1618 : dunedaq::opmonlib::to_entry(const google::protobuf::Message& m, const CustomOrigin& co)
22 : {
23 :
24 1618 : dunedaq::opmon::OpMonEntry entry;
25 3236 : entry.set_measurement(m.GetTypeName());
26 :
27 1618 : *entry.mutable_data() = dunedaq::opmonlib::to_map(m);
28 :
29 1620 : for (const auto& [tag, value] : co) {
30 4 : (*entry.mutable_custom_origin())[tag] = value;
31 : }
32 :
33 1617 : return entry;
34 0 : }
35 :
36 : dunedaq::opmonlib::map_type
37 1620 : dunedaq::opmonlib::to_map(const google::protobuf::Message& m, std::string top_block)
38 : {
39 :
40 1620 : dunedaq::opmonlib::map_type ret;
41 :
42 1620 : const auto* descriptor_p = m.GetDescriptor();
43 1620 : const auto& des = *descriptor_p;
44 :
45 1620 : const auto* reflection_p = m.GetReflection();
46 1620 : const auto& ref = *reflection_p;
47 :
48 1620 : using namespace google::protobuf;
49 :
50 1620 : auto count = des.field_count();
51 7982 : for (int i = 0; i < count; ++i) {
52 6363 : const auto* field_p = des.field(i);
53 6363 : if (field_p->is_repeated())
54 2 : continue;
55 :
56 6361 : auto name = top_block.empty() ? field_p->name() : top_block + '.' + field_p->name();
57 :
58 6361 : auto type = field_p->cpp_type();
59 6359 : dunedaq::opmon::OpMonValue value;
60 6360 : bool success = true;
61 6360 : switch (type) {
62 0 : case FieldDescriptor::CppType::CPPTYPE_INT32:
63 0 : value.set_int4_value(ref.GetInt32(m, field_p));
64 : break;
65 1513 : case FieldDescriptor::CppType::CPPTYPE_INT64:
66 1513 : value.set_int8_value(ref.GetInt64(m, field_p));
67 : break;
68 8 : case FieldDescriptor::CppType::CPPTYPE_UINT32:
69 8 : value.set_uint4_value(ref.GetUInt32(m, field_p));
70 : break;
71 294 : case FieldDescriptor::CppType::CPPTYPE_UINT64:
72 294 : value.set_uint8_value(ref.GetUInt64(m, field_p));
73 : break;
74 1510 : case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
75 1510 : value.set_double_value(ref.GetDouble(m, field_p));
76 : break;
77 2 : case FieldDescriptor::CppType::CPPTYPE_FLOAT:
78 2 : value.set_float_value(ref.GetFloat(m, field_p));
79 : break;
80 1517 : case FieldDescriptor::CppType::CPPTYPE_BOOL:
81 1517 : value.set_boolean_value(ref.GetBool(m, field_p));
82 : break;
83 1514 : case FieldDescriptor::CppType::CPPTYPE_STRING:
84 3028 : value.set_string_value(ref.GetString(m, field_p));
85 1514 : break;
86 2 : case FieldDescriptor::CppType::CPPTYPE_MESSAGE: {
87 2 : auto data = dunedaq::opmonlib::to_map(ref.GetMessage(m, field_p), name);
88 2 : ret.insert(data.begin(), data.end());
89 2 : success = false;
90 2 : break;
91 2 : }
92 : default:
93 : success = false;
94 : break;
95 : } // switch on the cpp type
96 :
97 1516 : if (success)
98 6359 : ret[name] = value;
99 6359 : }
100 :
101 1619 : return ret;
102 0 : }
103 :
104 : void
105 9 : dunedaq::opmonlib::from_entry(google::protobuf::Message& m, const dunedaq::opmon::OpMonEntry& e, std::string top_block)
106 : {
107 :
108 9 : const auto* descriptor_p = m.GetDescriptor();
109 9 : const auto& des = *descriptor_p;
110 :
111 9 : const auto* reflection_p = m.GetReflection();
112 9 : const auto& ref = *reflection_p;
113 :
114 9 : using namespace google::protobuf;
115 :
116 9 : auto count = des.field_count();
117 68 : for (int i = 0; i < count; ++i) {
118 59 : const auto* field_p = des.field(i);
119 59 : if (field_p->is_repeated())
120 1 : continue;
121 :
122 58 : auto name = top_block.empty() ? field_p->name() : top_block + '.' + field_p->name();
123 :
124 58 : auto type = field_p->cpp_type();
125 :
126 58 : if (type == FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
127 1 : auto message = ref.MutableMessage(&m, field_p);
128 1 : from_entry(*message, e, name);
129 : } else {
130 :
131 57 : auto value = e.data().find(name)->second;
132 :
133 57 : switch (type) {
134 0 : case FieldDescriptor::CppType::CPPTYPE_INT32:
135 0 : ref.SetInt32(&m, field_p, value.int4_value());
136 : break;
137 2 : case FieldDescriptor::CppType::CPPTYPE_INT64:
138 2 : ref.SetInt64(&m, field_p, value.int8_value());
139 : break;
140 0 : case FieldDescriptor::CppType::CPPTYPE_UINT32:
141 0 : ref.SetUInt32(&m, field_p, value.uint4_value());
142 : break;
143 48 : case FieldDescriptor::CppType::CPPTYPE_UINT64:
144 48 : ref.SetUInt64(&m, field_p, value.uint8_value());
145 : break;
146 2 : case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
147 2 : ref.SetDouble(&m, field_p, value.double_value());
148 : break;
149 1 : case FieldDescriptor::CppType::CPPTYPE_FLOAT:
150 1 : ref.SetFloat(&m, field_p, value.float_value());
151 : break;
152 2 : case FieldDescriptor::CppType::CPPTYPE_BOOL:
153 2 : ref.SetBool(&m, field_p, value.boolean_value());
154 : break;
155 2 : case FieldDescriptor::CppType::CPPTYPE_STRING:
156 2 : ref.SetString(&m, field_p, value.string_value());
157 2 : break;
158 : default:
159 : break;
160 : } // switch on the cpp type
161 57 : } // else if it's a message
162 58 : } // loop over the fields
163 9 : }
164 :
165 : std::string
166 322 : dunedaq::opmonlib::to_string(const dunedaq::opmon::OpMonId& id)
167 : {
168 :
169 322 : std::string ret(id.session());
170 :
171 322 : if (!id.application().empty()) {
172 320 : ret += '.';
173 320 : ret += id.application();
174 : }
175 :
176 426 : for (int i = 0; i < id.substructure().size(); ++i) {
177 104 : ret += '.';
178 104 : ret += id.substructure()[i];
179 : }
180 :
181 322 : return ret;
182 0 : }
183 :
184 : const dunedaq::opmon::OpMonId&
185 879 : dunedaq::opmonlib::operator+=(dunedaq::opmon::OpMonId& id, const std::string& element)
186 : {
187 :
188 879 : if (element.empty())
189 : return id;
190 :
191 878 : if (id.session().empty()) {
192 50 : id.set_session(element);
193 50 : return id;
194 : }
195 :
196 828 : if (id.application().empty()) {
197 467 : id.set_application(element);
198 467 : return id;
199 : }
200 :
201 361 : id.add_substructure(element);
202 361 : return id;
203 : }
204 :
205 : dunedaq::opmon::OpMonId
206 729 : dunedaq::opmonlib::operator+(const dunedaq::opmon::OpMonId& id, const std::string& element)
207 : {
208 :
209 729 : auto ret = id;
210 729 : ret += element;
211 729 : return ret;
212 0 : }
213 :
214 : //---------------------------------------------------
215 : // template specifications for setters by name
216 : //---------------------------------------------------
217 : template<>
218 : void
219 0 : dunedaq::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 0 : r.SetInt32(&m, f_p, value);
226 0 : }
227 :
228 : template<>
229 : void
230 1 : dunedaq::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 1 : r.SetInt64(&m, f_p, value);
237 1 : }
238 :
239 : template<>
240 : void
241 0 : dunedaq::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 0 : r.SetUInt32(&m, f_p, value);
248 0 : }
249 :
250 : template<>
251 : void
252 0 : dunedaq::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 0 : r.SetUInt64(&m, f_p, value);
259 0 : }
260 :
261 : template<>
262 : void
263 1 : dunedaq::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 1 : r.SetDouble(&m, f_p, value);
270 1 : }
271 :
272 : template<>
273 : void
274 0 : dunedaq::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 0 : r.SetFloat(&m, f_p, value);
281 0 : }
282 :
283 : template<>
284 : void
285 0 : dunedaq::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 0 : r.SetBool(&m, f_p, value);
292 0 : }
293 :
294 : template<>
295 : void
296 1 : dunedaq::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 1 : r.SetString(&m, f_p, value);
303 1 : }
|