LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - ComponentHeightTracer.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 60.0 % 130 78
Test Date: 2026-11-21 03:51:50 Functions: 73.3 % 15 11
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "ComponentHeightTracer.h"
       2              : #include <Logger.h>
       3              : #include <sstream>
       4              : #include <algorithm>
       5              : #include <cstdlib>
       6              : #include "str_utils.h"
       7              : 
       8              : namespace Odb::Lib::FileModel::Design
       9              : {
      10       226439 :     ComponentHeightTracer &ComponentHeightTracer::instance()
      11              :     {
      12       226439 :         static ComponentHeightTracer tracer;
      13       226439 :         return tracer;
      14              :     }
      15              : 
      16           79 :     ComponentHeightTracer::ComponentHeightTracer()
      17              :     {
      18              :         // Default: empty trace set. Use addComponentToTrace() or set
      19              :         // ODBDESIGN_TRACE_COMPONENTS env var (comma-separated) to enable.
      20           79 :         const char* envComponents = std::getenv("ODBDESIGN_TRACE_COMPONENTS");
      21           79 :         if (envComponents != nullptr && envComponents[0] != '\0')
      22              :         {
      23            0 :             std::istringstream stream(envComponents);
      24            0 :             std::string name;
      25            0 :             while (std::getline(stream, name, ','))
      26              :             {
      27              :                 // Trim whitespace
      28            0 :                 Utils::str_trim(name);
      29            0 :                 if (!name.empty())
      30              :                 {
      31            0 :                     m_componentsToTrace.insert(name);
      32              :                 }
      33              :             }
      34            0 :         }
      35              : 
      36           79 :         const char* envMaxFailures = std::getenv("ODBDESIGN_TRACE_MAX_FAILURES");
      37           79 :         if (envMaxFailures != nullptr)
      38              :         {
      39              :             try
      40              :             {
      41            0 :                 m_maxFailuresToTrace = std::stoi(envMaxFailures);
      42              :                 // Validate bounds
      43            0 :                 if (m_maxFailuresToTrace <= 0)
      44              :                 {
      45            0 :                     logwarn("[ComponentHeightTracer] ODBDESIGN_TRACE_MAX_FAILURES must be positive, using default value 20");
      46            0 :                     m_maxFailuresToTrace = 20;
      47              :                 }
      48              :             }
      49            0 :             catch (...)
      50              :             {
      51            0 :                 logwarn("[ComponentHeightTracer] Failed to parse ODBDESIGN_TRACE_MAX_FAILURES env var, using default value 20");
      52            0 :                 m_maxFailuresToTrace = 20;
      53            0 :             }
      54              :         }
      55           79 :     }
      56              : 
      57       226439 :     bool ComponentHeightTracer::shouldTrace(const std::string &compName, unsigned int pkgRef) const
      58              :     {
      59              :         // Check if component is in trace list
      60       226439 :         if (m_componentsToTrace.find(compName) != m_componentsToTrace.end())
      61              :         {
      62            0 :             return true;
      63              :         }
      64              :         // Check if we should trace failures (first N failures)
      65       226439 :         return shouldTraceFailure();
      66              :     }
      67              : 
      68       226439 :     bool ComponentHeightTracer::shouldTraceFailure() const
      69              :     {
      70       452878 :         return m_failureCount.load() < m_maxFailuresToTrace;
      71              :     }
      72              : 
      73         1048 :     void ComponentHeightTracer::markFailure() const
      74              :     {
      75         1048 :         m_failureCount.fetch_add(1);
      76         1048 :     }
      77              : 
      78            0 :     std::string ComponentHeightTracer::getCompKey(const std::string &compName, unsigned int pkgRef) const
      79              :     {
      80            0 :         std::stringstream ss;
      81            0 :         ss << compName << "(Package=" << pkgRef << ")";
      82            0 :         return ss.str();
      83            0 :     }
      84              : 
      85        69760 :     std::string ComponentHeightTracer::formatLookupTable(const std::map<std::string, std::string> &lookupTable) const
      86              :     {
      87        69760 :         std::stringstream ss;
      88        69760 :         ss << "{";
      89        69760 :         bool first = true;
      90       259460 :         for (const auto &kv : lookupTable)
      91              :         {
      92       189700 :             if (!first)
      93       119940 :                 ss << ", ";
      94       189700 :             ss << "\"" << kv.first << "\":\"" << kv.second << "\"";
      95       189700 :             first = false;
      96              :         }
      97        69760 :         ss << "}";
      98       139520 :         return ss.str();
      99        69760 :     }
     100              : 
     101        69760 :     std::string ComponentHeightTracer::formatAttributeNames(const std::vector<std::string> &attributeNames) const
     102              :     {
     103        69760 :         std::stringstream ss;
     104        69760 :         ss << "[";
     105       267408 :         for (size_t i = 0; i < attributeNames.size(); ++i)
     106              :         {
     107       197648 :             if (i > 0)
     108       127888 :                 ss << ", ";
     109       197648 :             ss << i << ":\"" << attributeNames[i] << "\"";
     110              :         }
     111        69760 :         ss << "]";
     112       139520 :         return ss.str();
     113        69760 :     }
     114              : 
     115        72066 :     void ComponentHeightTracer::logParseStart(const std::string &compName, unsigned int pkgRef, const std::string &attrIdString) const
     116              :     {
     117        72066 :         if (!shouldTrace(compName, pkgRef))
     118        10610 :             return;
     119              : 
     120        61456 :         std::stringstream ss;
     121              :         ss << "[CompHeightTrace][PARSE_START] Component=" << compName
     122        61456 :            << ", Package=" << pkgRef
     123        61456 :            << ", attrIdString=\"" << attrIdString << "\"";
     124       122912 :         loginfo(ss.str());
     125        61456 :     }
     126              : 
     127        72066 :     void ComponentHeightTracer::logParseResult(const std::string &compName, unsigned int pkgRef,
     128              :                                                const std::map<std::string, std::string> &lookupTable,
     129              :                                                const std::vector<std::string> &attributeNames) const
     130              :     {
     131        72066 :         if (!shouldTrace(compName, pkgRef))
     132        10610 :             return;
     133              : 
     134        61456 :         std::stringstream ss;
     135              :         ss << "[CompHeightTrace][PARSE_RESULT] Component=" << compName
     136        61456 :            << ", Package=" << pkgRef
     137            0 :            << ", AttributeNames=" << formatAttributeNames(attributeNames)
     138        61456 :            << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
     139              : 
     140              :         // Check if .comp_height is at index 0 and if key "0" exists
     141        61456 :         bool hasCompHeightAttr = !attributeNames.empty() && attributeNames[0] == ".comp_height";
     142       122912 :         bool hasKey0 = lookupTable.find("0") != lookupTable.end();
     143              : 
     144              :         ss << ", .comp_height@index0=" << (hasCompHeightAttr ? "YES" : "NO")
     145        61456 :            << ", key\"0\"_exists=" << (hasKey0 ? "YES" : "NO");
     146              : 
     147        61456 :         if (hasCompHeightAttr && !hasKey0)
     148              :         {
     149         1048 :             ss << " ⚠️ MISSING_KEY_0";
     150         1048 :             markFailure();
     151              :         }
     152        60408 :         else if (hasKey0)
     153              :         {
     154       181224 :             ss << ", height_value=\"" << lookupTable.at("0") << "\"";
     155              :         }
     156              : 
     157       122912 :         loginfo(ss.str());
     158        61456 :     }
     159              : 
     160        72551 :     void ComponentHeightTracer::logSerialization(const std::string &compName, unsigned int pkgRef,
     161              :                                                  const std::map<std::string, std::string> &lookupTable,
     162              :                                                  const std::vector<std::string> &attributeNames) const
     163              :     {
     164        72551 :         if (!shouldTrace(compName, pkgRef))
     165        64247 :             return;
     166              : 
     167         8304 :         std::stringstream ss;
     168              :         ss << "[CompHeightTrace][SERIALIZE] Component=" << compName
     169         8304 :            << ", Package=" << pkgRef
     170            0 :            << ", AttributeNames=" << formatAttributeNames(attributeNames)
     171         8304 :            << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
     172              : 
     173        16608 :         bool hasKey0 = lookupTable.find("0") != lookupTable.end();
     174         8304 :         if (hasKey0)
     175              :         {
     176        24876 :             ss << ", height_value=\"" << lookupTable.at("0") << "\"";
     177              :         }
     178              :         else
     179              :         {
     180           12 :             ss << ", key\"0\"_missing";
     181              :         }
     182              : 
     183        16608 :         loginfo(ss.str());
     184         8304 :     }
     185              : 
     186         9756 :     void ComponentHeightTracer::logGrpcResponse(const std::string &compName, unsigned int pkgRef,
     187              :                                                 const std::map<std::string, std::string> &lookupTable,
     188              :                                                 const std::vector<std::string> &attributeNames) const
     189              :     {
     190         9756 :         if (!shouldTrace(compName, pkgRef))
     191         9756 :             return;
     192              : 
     193            0 :         std::stringstream ss;
     194              :         ss << "[CompHeightTrace][GRPC_RESPONSE] Component=" << compName
     195            0 :            << ", Package=" << pkgRef
     196            0 :            << ", AttributeNames=" << formatAttributeNames(attributeNames)
     197            0 :            << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
     198              : 
     199            0 :         bool hasKey0 = lookupTable.find("0") != lookupTable.end();
     200            0 :         if (hasKey0)
     201              :         {
     202            0 :             ss << ", height_value=\"" << lookupTable.at("0") << "\"";
     203              :         }
     204              :         else
     205              :         {
     206            0 :             ss << ", key\"0\"_missing";
     207              :         }
     208              : 
     209            0 :         loginfo(ss.str());
     210            0 :     }
     211              : 
     212            0 :     void ComponentHeightTracer::logRestResponse(const std::string &compName, unsigned int pkgRef,
     213              :                                                 const std::map<std::string, std::string> &lookupTable,
     214              :                                                 const std::vector<std::string> &attributeNames) const
     215              :     {
     216            0 :         if (!shouldTrace(compName, pkgRef))
     217            0 :             return;
     218              : 
     219            0 :         std::stringstream ss;
     220              :         ss << "[CompHeightTrace][REST_RESPONSE] Component=" << compName
     221            0 :            << ", Package=" << pkgRef
     222            0 :            << ", AttributeNames=" << formatAttributeNames(attributeNames)
     223            0 :            << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
     224              : 
     225            0 :         bool hasKey0 = lookupTable.find("0") != lookupTable.end();
     226            0 :         if (hasKey0)
     227              :         {
     228            0 :             ss << ", height_value=\"" << lookupTable.at("0") << "\"";
     229              :         }
     230              :         else
     231              :         {
     232            0 :             ss << ", key\"0\"_missing";
     233              :         }
     234              : 
     235            0 :         loginfo(ss.str());
     236            0 :     }
     237              : 
     238            0 :     void ComponentHeightTracer::addComponentToTrace(const std::string &compName)
     239              :     {
     240            0 :         m_componentsToTrace.insert(compName);
     241            0 :     }
     242              : 
     243            0 :     void ComponentHeightTracer::setMaxFailuresToTrace(int maxFailures)
     244              :     {
     245            0 :         m_maxFailuresToTrace = maxFailures;
     246            0 :     }
     247              : }
        

Generated by: LCOV version 2.0-1