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 231075 : ComponentHeightTracer &ComponentHeightTracer::instance()
11 : {
12 231075 : static ComponentHeightTracer tracer;
13 231075 : 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 231075 : bool ComponentHeightTracer::shouldTrace(const std::string &compName, unsigned int pkgRef) const
58 : {
59 : // Check if component is in trace list
60 231075 : 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 231075 : return shouldTraceFailure();
66 : }
67 :
68 231075 : bool ComponentHeightTracer::shouldTraceFailure() const
69 : {
70 462150 : return m_failureCount.load() < m_maxFailuresToTrace;
71 : }
72 :
73 1049 : void ComponentHeightTracer::markFailure() const
74 : {
75 1049 : m_failureCount.fetch_add(1);
76 1049 : }
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 74384 : void ComponentHeightTracer::logParseStart(const std::string &compName, unsigned int pkgRef, const std::string &attrIdString) const
116 : {
117 74384 : if (!shouldTrace(compName, pkgRef))
118 12236 : return;
119 :
120 62148 : std::stringstream ss;
121 : ss << "[CompHeightTrace][PARSE_START] Component=" << compName
122 62148 : << ", Package=" << pkgRef
123 62148 : << ", attrIdString=\"" << attrIdString << "\"";
124 124296 : loginfo(ss.str());
125 62148 : }
126 :
127 74384 : 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 74384 : if (!shouldTrace(compName, pkgRef))
132 12236 : return;
133 :
134 62148 : std::stringstream ss;
135 : ss << "[CompHeightTrace][PARSE_RESULT] Component=" << compName
136 62148 : << ", Package=" << pkgRef
137 0 : << ", AttributeNames=" << formatAttributeNames(attributeNames)
138 62148 : << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
139 :
140 : // Check if .comp_height is at index 0 and if key "0" exists
141 62148 : bool hasCompHeightAttr = !attributeNames.empty() && attributeNames[0] == ".comp_height";
142 124296 : bool hasKey0 = lookupTable.find("0") != lookupTable.end();
143 :
144 : ss << ", .comp_height@index0=" << (hasCompHeightAttr ? "YES" : "NO")
145 62148 : << ", key\"0\"_exists=" << (hasKey0 ? "YES" : "NO");
146 :
147 62148 : if (hasCompHeightAttr && !hasKey0)
148 : {
149 1049 : ss << " ⚠️ MISSING_KEY_0";
150 1049 : markFailure();
151 : }
152 61099 : else if (hasKey0)
153 : {
154 183297 : ss << ", height_value=\"" << lookupTable.at("0") << "\"";
155 : }
156 :
157 124296 : loginfo(ss.str());
158 62148 : }
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 64939 : return;
166 :
167 7612 : std::stringstream ss;
168 : ss << "[CompHeightTrace][SERIALIZE] Component=" << compName
169 7612 : << ", Package=" << pkgRef
170 0 : << ", AttributeNames=" << formatAttributeNames(attributeNames)
171 7612 : << ", AttributeLookupTable=" << formatLookupTable(lookupTable);
172 :
173 15224 : bool hasKey0 = lookupTable.find("0") != lookupTable.end();
174 7612 : if (hasKey0)
175 : {
176 22803 : ss << ", height_value=\"" << lookupTable.at("0") << "\"";
177 : }
178 : else
179 : {
180 11 : ss << ", key\"0\"_missing";
181 : }
182 :
183 15224 : loginfo(ss.str());
184 7612 : }
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 : }
|