LCOV - code coverage report
Current view: top level - opmonlib/src - Utils.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 82.1 % 145 119
Test Date: 2025-12-21 13:07:08 Functions: 66.7 % 15 10

            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              : 
      12           11 : dunedaq::opmon::OpMonId dunedaq::opmonlib::make_origin( const std::string & session, const std::string & app ) {
      13           11 :   opmon::OpMonId origin;
      14           11 :   origin.set_session( session );
      15           11 :   origin.set_application( app );
      16           11 :   return origin;
      17            0 : }
      18              : 
      19              : 
      20         1618 : dunedaq::opmon::OpMonEntry dunedaq::opmonlib::to_entry(const google::protobuf::Message & m,
      21              :                                                        const CustomOrigin & co) {
      22              : 
      23         1618 :   dunedaq::opmon::OpMonEntry entry;
      24         3236 :   entry.set_measurement( m.GetTypeName() );
      25              : 
      26         1618 :   *entry.mutable_data() = dunedaq::opmonlib::to_map(m);
      27              :   
      28         1622 :   for ( const auto & [tag, value] : co ) {
      29            4 :     (*entry.mutable_custom_origin())[tag] = value;
      30              :   }
      31              :   
      32         1618 :   return entry;
      33            0 : }
      34              : 
      35              : 
      36         1619 : dunedaq::opmonlib::map_type dunedaq::opmonlib::to_map( const google::protobuf::Message & m,
      37              :                                                        std::string top_block) {
      38              : 
      39         1619 :   dunedaq::opmonlib::map_type ret;
      40              : 
      41         1619 :   const auto * descriptor_p = m.GetDescriptor();
      42         1619 :   const auto & des = *descriptor_p;
      43              :   
      44         1619 :   const auto * reflection_p = m.GetReflection();
      45         1620 :   const auto & ref = *reflection_p;
      46              :   
      47         1620 :   using namespace google::protobuf;
      48              : 
      49         1620 :   auto count = des.field_count();
      50         7979 :   for ( int i = 0; i < count; ++i ) {
      51         6359 :     const auto * field_p = des.field(i);
      52         6359 :     if ( field_p -> is_repeated() ) continue;
      53              :     
      54         6357 :     auto name = top_block.empty() ? field_p -> name() : top_block + '.' + field_p -> name(); 
      55              : 
      56         6355 :     auto type = field_p -> cpp_type();
      57         6355 :     dunedaq::opmon::OpMonValue value;
      58         6359 :     bool success = true;
      59         6359 :     switch (type) {
      60            0 :     case FieldDescriptor::CppType::CPPTYPE_INT32:
      61            0 :       value.set_int4_value( ref.GetInt32(m, field_p) );
      62              :       break;
      63         1511 :     case FieldDescriptor::CppType::CPPTYPE_INT64:
      64         1511 :       value.set_int8_value( ref.GetInt64(m, field_p) );
      65              :       break;
      66            8 :     case FieldDescriptor::CppType::CPPTYPE_UINT32:
      67            8 :       value.set_uint4_value( ref.GetUInt32(m, field_p) );
      68              :       break;
      69          294 :     case FieldDescriptor::CppType::CPPTYPE_UINT64:
      70          294 :       value.set_uint8_value( ref.GetUInt64(m, field_p) );
      71              :       break;
      72         1510 :     case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
      73         1510 :       value.set_double_value( ref.GetDouble(m, field_p) );
      74              :       break;
      75            2 :     case FieldDescriptor::CppType::CPPTYPE_FLOAT:
      76            2 :       value.set_float_value( ref.GetFloat(m, field_p) );
      77              :       break;
      78         1518 :     case FieldDescriptor::CppType::CPPTYPE_BOOL:
      79         1518 :       value.set_boolean_value( ref.GetBool(m, field_p) );
      80              :       break;
      81         1514 :     case FieldDescriptor::CppType::CPPTYPE_STRING:
      82         3028 :       value.set_string_value( ref.GetString(m, field_p) );
      83         1514 :       break;
      84            2 :     case FieldDescriptor::CppType::CPPTYPE_MESSAGE:
      85            2 :       {
      86            2 :         auto data = dunedaq::opmonlib::to_map(ref.GetMessage(m, field_p), name);
      87            2 :         ret.insert(data.begin(), data.end());
      88            2 :         success = false;
      89            2 :         break;
      90            2 :       }
      91              :     default:
      92              :       success = false;
      93              :       break;
      94              :     } // switch on the cpp type                                                                         
      95              : 
      96              :       
      97         1516 :     if ( success )
      98         6357 :       ret[name] = value;
      99         6359 :   }
     100              : 
     101         1620 :   return ret;
     102              :   
     103            0 : }
     104              : 
     105              : 
     106            9 : void dunedaq::opmonlib::from_entry( google::protobuf::Message & m,
     107              :                                     const dunedaq::opmon::OpMonEntry & e,
     108              :                                     std::string top_block) {
     109              : 
     110            9 :   const auto * descriptor_p = m.GetDescriptor();
     111            9 :   const auto & des = *descriptor_p;
     112              :   
     113            9 :   const auto * reflection_p = m.GetReflection();
     114            9 :   const auto & ref = *reflection_p;
     115              :   
     116            9 :   using namespace google::protobuf;
     117              : 
     118            9 :   auto count = des.field_count();
     119           68 :   for ( int i = 0; i < count; ++i ) {
     120           59 :     const auto * field_p = des.field(i);
     121           59 :     if ( field_p -> is_repeated() ) continue;
     122              :     
     123           58 :     auto name = top_block.empty() ? field_p -> name() : top_block + '.' + field_p -> name(); 
     124              : 
     125           58 :     auto type = field_p -> cpp_type();
     126              : 
     127           58 :     if ( type == FieldDescriptor::CppType::CPPTYPE_MESSAGE ) {
     128            1 :       auto message = ref.MutableMessage(&m, field_p);
     129            1 :       from_entry( *message, e, name );
     130              :     } else {
     131              : 
     132           57 :       auto value = e.data().find(name)->second;
     133              : 
     134           57 :       switch (type) {
     135            0 :       case FieldDescriptor::CppType::CPPTYPE_INT32:
     136            0 :         ref.SetInt32(&m, field_p, value.int4_value());
     137              :         break;
     138            2 :       case FieldDescriptor::CppType::CPPTYPE_INT64:
     139            2 :         ref.SetInt64(&m, field_p, value.int8_value());
     140              :         break;
     141            0 :       case FieldDescriptor::CppType::CPPTYPE_UINT32:
     142            0 :         ref.SetUInt32(&m, field_p, value.uint4_value());
     143              :         break;
     144           48 :       case FieldDescriptor::CppType::CPPTYPE_UINT64:
     145           48 :         ref.SetUInt64(&m, field_p, value.uint8_value());
     146              :         break;
     147            2 :       case FieldDescriptor::CppType::CPPTYPE_DOUBLE:
     148            2 :         ref.SetDouble(&m, field_p, value.double_value());
     149              :         break;
     150            1 :       case FieldDescriptor::CppType::CPPTYPE_FLOAT:
     151            1 :         ref.SetFloat(&m, field_p, value.float_value());
     152              :         break;
     153            2 :       case FieldDescriptor::CppType::CPPTYPE_BOOL:
     154            2 :         ref.SetBool(&m, field_p, value.boolean_value());
     155              :         break;
     156            2 :       case FieldDescriptor::CppType::CPPTYPE_STRING:
     157            2 :         ref.SetString(&m, field_p, value.string_value());
     158            2 :         break;
     159              :       default:
     160              :         break;
     161              :       } // switch on the cpp type                                                                         
     162           57 :     } // else if it's a message
     163           58 :   } // loop over the fields
     164              : 
     165            9 : }
     166              : 
     167          322 : std::string dunedaq::opmonlib::to_string( const dunedaq::opmon::OpMonId & id ) {
     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              : 
     185          879 : const dunedaq::opmon::OpMonId & dunedaq::opmonlib::operator += (dunedaq::opmon::OpMonId & id,
     186              :                                                                 const  std::string & element) {
     187              :   
     188          879 :   if ( element.empty() ) return id;
     189              :   
     190          878 :   if ( id.session().empty() ) {
     191           50 :     id.set_session(element);
     192           50 :     return id;
     193              :   }
     194              : 
     195          828 :   if ( id.application().empty() ) {
     196          467 :     id.set_application(element);
     197          467 :     return id;
     198              :   }
     199              : 
     200          361 :   id.add_substructure(element);
     201          361 :   return id;
     202              : }
     203              : 
     204              : 
     205          729 : dunedaq::opmon::OpMonId dunedaq::opmonlib::operator + (const dunedaq::opmon::OpMonId & id,
     206              :                                                        const  std::string & element ) {
     207              : 
     208          729 :   auto ret = id;
     209          729 :   ret += element;
     210          729 :   return ret;
     211            0 : }
     212              : 
     213              : 
     214              : 
     215              : //---------------------------------------------------
     216              : // template specifications for setters by name
     217              : //---------------------------------------------------
     218              : template<>
     219            0 : void 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            0 :   r.SetInt32(&m, f_p, value);
     224            0 : }
     225              : 
     226              : template<>
     227            1 : void 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            1 :   r.SetInt64(&m, f_p, value);
     232            1 : }
     233              : 
     234              : template<>
     235            0 : void 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            0 :   r.SetUInt32(&m, f_p, value);
     240            0 : }
     241              : 
     242              : template<>
     243            0 : void 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            0 :   r.SetUInt64(&m, f_p, value);
     248            0 : }
     249              : 
     250              : template<>
     251            1 : void 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            1 :   r.SetDouble(&m, f_p, value);
     256            1 : }
     257              : 
     258              : template<>
     259            0 : void 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            0 :   r.SetFloat(&m, f_p, value);
     264            0 : }
     265              : 
     266              : template<>
     267            0 : void 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            0 :   r.SetBool(&m, f_p, value);
     272            0 : }
     273              : 
     274              : template<>
     275            1 : void 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            1 :   r.SetString(&m, f_p, value);
     280            1 : }
        

Generated by: LCOV version 2.0-1