Skip to content

Commit 196c6f0

Browse files
authored
Merge pull request #807 from mcserep/order_metrics_results
Properly order the metrics results
2 parents 79e4425 + 29edeac commit 196c6f0

File tree

4 files changed

+111
-59
lines changed

4 files changed

+111
-59
lines changed

plugins/cpp_metrics/service/cxxmetrics.thrift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,22 @@ struct CppMetricsModuleSingle
6161
3:double value
6262
}
6363

64-
struct CppMetricsModuleAll
64+
struct CppMetricsAstNodeEntry
6565
{
66-
1:common.FileId id,
67-
2:list<CppMetricsModuleSingle> metrics
66+
1: common.AstNodeId astNodeId,
67+
2: list<CppMetricsAstNodeSingle> metrics
68+
}
69+
70+
struct CppMetricsAstNodeDetailedEntry
71+
{
72+
1: common.AstNodeId astNodeId,
73+
2: CppMetricsAstNodeDetailed details
74+
}
75+
76+
struct CppMetricsModuleEntry
77+
{
78+
1: common.FileId fileId,
79+
2: list<CppMetricsModuleSingle> metrics
6880
}
6981

7082
service CppMetricsService
@@ -97,7 +109,7 @@ service CppMetricsService
97109
*
98110
* The given path is a handled as a prefix.
99111
*/
100-
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getCppAstNodeMetricsForPath(
112+
list<CppMetricsAstNodeEntry> getCppAstNodeMetricsForPath(
101113
1:string path)
102114

103115
/**
@@ -106,7 +118,7 @@ service CppMetricsService
106118
*
107119
* The given path is a handled as a prefix.
108120
*/
109-
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getPagedCppAstNodeMetricsForPath(
121+
list<CppMetricsAstNodeEntry> getPagedCppAstNodeMetricsForPath(
110122
1:string path
111123
2:i32 pageSize,
112124
3:common.AstNodeId previousId)
@@ -117,7 +129,7 @@ service CppMetricsService
117129
*
118130
* The given path is a handled as a prefix.
119131
*/
120-
map<common.AstNodeId, CppMetricsAstNodeDetailed> getCppAstNodeMetricsDetailedForPath(
132+
list<CppMetricsAstNodeDetailedEntry> getCppAstNodeMetricsDetailedForPath(
121133
1:string path)
122134

123135
/**
@@ -126,7 +138,7 @@ service CppMetricsService
126138
*
127139
* The given path is a handled as a prefix.
128140
*/
129-
map<common.AstNodeId, CppMetricsAstNodeDetailed> getPagedCppAstNodeMetricsDetailedForPath(
141+
list<CppMetricsAstNodeDetailedEntry> getPagedCppAstNodeMetricsDetailedForPath(
130142
1:string path,
131143
2:i32 pageSize,
132144
3:common.AstNodeId previousId)
@@ -137,7 +149,7 @@ service CppMetricsService
137149
*
138150
* The given path is a handled as a prefix.
139151
*/
140-
map<common.FileId, list<CppMetricsModuleSingle>> getCppFileMetricsForPath(
152+
list<CppMetricsModuleEntry> getCppFileMetricsForPath(
141153
1:string path)
142154

143155
/**
@@ -146,7 +158,7 @@ service CppMetricsService
146158
*
147159
* The given path is a handled as a prefix.
148160
*/
149-
map<common.FileId, list<CppMetricsModuleSingle>> getPagedCppFileMetricsForPath(
161+
list<CppMetricsModuleEntry> getPagedCppFileMetricsForPath(
150162
1:string path,
151163
2:i32 pageSize,
152164
3:common.FileId previousId)

plugins/cpp_metrics/service/include/service/cppmetricsservice.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,31 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
5050
const core::FileId& fileId_) override;
5151

5252
void getCppAstNodeMetricsForPath(
53-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
53+
std::vector<CppMetricsAstNodeEntry>& _return,
5454
const std::string& path_) override;
5555

5656
void getPagedCppAstNodeMetricsForPath(
57-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
57+
std::vector<CppMetricsAstNodeEntry>& _return,
5858
const std::string& path_,
5959
const std::int32_t pageSize_,
6060
const core::AstNodeId& previousId_) override;
6161

6262
void getCppAstNodeMetricsDetailedForPath(
63-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
63+
std::vector<CppMetricsAstNodeDetailedEntry>& _return,
6464
const std::string& path_) override;
6565

6666
void getPagedCppAstNodeMetricsDetailedForPath(
67-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
67+
std::vector<CppMetricsAstNodeDetailedEntry>& _return,
6868
const std::string& path_,
6969
const std::int32_t pageSize_,
7070
const core::AstNodeId& previousId_) override;
7171

7272
void getCppFileMetricsForPath(
73-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
73+
std::vector<CppMetricsModuleEntry>& _return,
7474
const std::string& path_) override;
7575

7676
void getPagedCppFileMetricsForPath(
77-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
77+
std::vector<CppMetricsModuleEntry>& _return,
7878
const std::string& path_,
7979
const std::int32_t pageSize_,
8080
const core::FileId& previousId_) override;
@@ -104,15 +104,15 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
104104
const model::FileId previousId_);
105105

106106
void queryCppAstNodeMetricsForPath(
107-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
107+
std::vector<CppMetricsAstNodeEntry>& result_,
108108
const odb::query<model::CppAstNodeMetricsForPathView>& query_);
109109

110110
void queryCppAstNodeMetricsDetailedForPath(
111-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
111+
std::vector<CppMetricsAstNodeDetailedEntry>& result_,
112112
const odb::query<model::CppAstNodeMetricsAndDataForPathView>& query_);
113113

114114
void queryCppFileMetricsForPath(
115-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
115+
std::vector<CppMetricsModuleEntry>& result_,
116116
const odb::query<model::CppModuleMetricsForPathView>& query_);
117117
};
118118

plugins/cpp_metrics/service/src/cppmetricsservice.cpp

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void CppMetricsServiceHandler::getCppMetricsForModule(
148148
}
149149

150150
void CppMetricsServiceHandler::queryCppAstNodeMetricsForPath(
151-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
151+
std::vector<CppMetricsAstNodeEntry>& result_,
152152
const odb::query<model::CppAstNodeMetricsForPathView>& query_)
153153
{
154154
_transaction([&, this](){
@@ -161,30 +161,40 @@ void CppMetricsServiceHandler::queryCppAstNodeMetricsForPath(
161161
metric.type = static_cast<CppAstNodeMetricsType::type>(node.type);
162162
metric.value = node.value;
163163

164-
if (_return.count(std::to_string(node.astNodeId)))
164+
// Try to find existing entry with same astNodeId
165+
auto it = std::find_if(result_.begin(), result_.end(),
166+
[&](const CppMetricsAstNodeEntry& entry) {
167+
return std::stoull(entry.astNodeId) == node.astNodeId;
168+
});
169+
170+
if (it != result_.end())
165171
{
166-
_return[std::to_string(node.astNodeId)].push_back(metric);
172+
// Found existing entry → append metric
173+
it->metrics.push_back(metric);
167174
}
175+
168176
else
169177
{
170-
std::vector<CppMetricsAstNodeSingle> metricsList;
171-
metricsList.push_back(metric);
172-
_return.insert(std::make_pair(std::to_string(node.astNodeId), metricsList));
178+
// No entry for this astNodeId → create new one
179+
CppMetricsAstNodeEntry entry;
180+
entry.astNodeId = std::to_string(node.astNodeId);
181+
entry.metrics.push_back(metric);
182+
result_.push_back(std::move(entry));
173183
}
174184
}
175185
});
176186
}
177187

178188
void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
179-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
189+
std::vector<CppMetricsAstNodeEntry>& _return,
180190
const std::string& path_)
181191
{
182192
queryCppAstNodeMetricsForPath(_return,
183193
CppNodeMetricsQuery::LocFile::path.like(path_ + '%'));
184194
}
185195

186196
void CppMetricsServiceHandler::getPagedCppAstNodeMetricsForPath(
187-
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
197+
std::vector<CppMetricsAstNodeEntry>& _return,
188198
const std::string& path_,
189199
const std::int32_t pageSize_,
190200
const core::AstNodeId& previousId_)
@@ -197,7 +207,7 @@ void CppMetricsServiceHandler::getPagedCppAstNodeMetricsForPath(
197207
}
198208

199209
void CppMetricsServiceHandler::queryCppAstNodeMetricsDetailedForPath(
200-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
210+
std::vector<CppMetricsAstNodeDetailedEntry>& result_,
201211
const odb::query<model::CppAstNodeMetricsAndDataForPathView>& query_)
202212
{
203213
_transaction([&, this](){
@@ -206,12 +216,21 @@ void CppMetricsServiceHandler::queryCppAstNodeMetricsDetailedForPath(
206216
for (const auto& node : nodes)
207217
{
208218
auto pair = std::make_pair(static_cast<CppAstNodeMetricsType::type>(node.type), node.value);
209-
if (_return.count(std::to_string(node.astNodeId)))
219+
220+
// Try to find existing entry with same astNodeId
221+
auto it = std::find_if(result_.begin(), result_.end(),
222+
[&](const CppMetricsAstNodeDetailedEntry& entry) {
223+
return std::stoull(entry.astNodeId) == node.astNodeId;
224+
});
225+
226+
if (it != result_.end())
210227
{
211-
_return[std::to_string(node.astNodeId)].metrics.insert(pair);
228+
// Found existing entry → append metric
229+
it->details.metrics.insert(pair);
212230
}
213231
else
214232
{
233+
// No entry for this astNodeId → create new one
215234
CppMetricsAstNodeDetailed metric;
216235
std::size_t pos = node.path.find_last_of('/');
217236
metric.path = node.path.substr(0, pos + 1);
@@ -223,22 +242,26 @@ void CppMetricsServiceHandler::queryCppAstNodeMetricsDetailedForPath(
223242
metric.astType = cc::model::astTypeToString(node.astType);
224243
metric.metrics.insert(pair);
225244

226-
_return.insert(std::make_pair(std::to_string(node.astNodeId), metric));
245+
CppMetricsAstNodeDetailedEntry entry;
246+
entry.astNodeId = std::to_string(node.astNodeId);
247+
entry.details = metric;
248+
249+
result_.push_back(std::move(entry));
227250
}
228251
}
229252
});
230253
}
231254

232255
void CppMetricsServiceHandler::getCppAstNodeMetricsDetailedForPath(
233-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
256+
std::vector<CppMetricsAstNodeDetailedEntry>& _return,
234257
const std::string& path_)
235258
{
236259
queryCppAstNodeMetricsDetailedForPath(_return,
237260
CppNodeMetricsQuery::LocFile::path.like(path_ + '%'));
238261
}
239262

240263
void CppMetricsServiceHandler::getPagedCppAstNodeMetricsDetailedForPath(
241-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
264+
std::vector<CppMetricsAstNodeDetailedEntry>& _return,
242265
const std::string& path_,
243266
const std::int32_t pageSize_,
244267
const core::AstNodeId& previousId_)
@@ -247,11 +270,12 @@ void CppMetricsServiceHandler::getPagedCppAstNodeMetricsDetailedForPath(
247270
path_, pageSize_, previousId_.empty() ? 0 : std::stoull(previousId_));
248271

249272
queryCppAstNodeMetricsDetailedForPath(_return,
250-
CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range(paged_nodes.begin(), paged_nodes.end()));
273+
CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range(paged_nodes.begin(), paged_nodes.end())
274+
+ ("ORDER BY" + odb::query<model::CppAstNodeMetrics>::astNodeId));
251275
}
252276

253277
void CppMetricsServiceHandler::queryCppFileMetricsForPath(
254-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
278+
std::vector<CppMetricsModuleEntry>& result_,
255279
const odb::query<model::CppModuleMetricsForPathView>& query_)
256280
{
257281
_transaction([&, this](){
@@ -264,38 +288,50 @@ void CppMetricsServiceHandler::queryCppFileMetricsForPath(
264288
metric.type = static_cast<CppModuleMetricsType::type>(file.type);
265289
metric.value = file.value;
266290

267-
if (_return.count(std::to_string(file.fileId)))
291+
// Try to find existing entry with same fileId
292+
auto it = std::find_if(result_.begin(), result_.end(),
293+
[&](const CppMetricsModuleEntry& entry) {
294+
return std::stoull(entry.fileId) == file.fileId;
295+
});
296+
297+
if (it != result_.end())
268298
{
269-
_return[std::to_string(file.fileId)].push_back(metric);
299+
// Found existing entry → append metric
300+
it->metrics.push_back(metric);
270301
}
302+
271303
else
272304
{
273-
std::vector<CppMetricsModuleSingle> metricsList;
274-
metricsList.push_back(metric);
275-
_return.insert(std::make_pair(std::to_string(file.fileId), metricsList));
305+
// No entry for this fileId → create new one
306+
CppMetricsModuleEntry entry;
307+
entry.fileId = std::to_string(file.fileId);
308+
entry.metrics.push_back(metric);
309+
result_.push_back(std::move(entry));
276310
}
277311
}
278312
});
279313
}
280314

281315
void CppMetricsServiceHandler::getCppFileMetricsForPath(
282-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
316+
std::vector<CppMetricsModuleEntry>& _return,
283317
const std::string& path_)
284318
{
285319
queryCppFileMetricsForPath(_return,
286320
CppModuleMetricsQuery::File::path.like(path_ + '%'));
287321
}
288322

289323
void CppMetricsServiceHandler::getPagedCppFileMetricsForPath(
290-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
324+
std::vector<CppMetricsModuleEntry>& _return,
291325
const std::string& path_,
292326
const std::int32_t pageSize_,
293327
const core::FileId& previousId_)
294328
{
295329
std::vector<model::FileId> paged_files = pageFileMetrics(
296330
path_, pageSize_, previousId_.empty() ? 0 : std::stoull(previousId_));
331+
297332
queryCppFileMetricsForPath(_return,
298-
CppModuleMetricsQuery::CppFileMetrics::file.in_range(paged_files.begin(), paged_files.end()));
333+
CppModuleMetricsQuery::CppFileMetrics::file.in_range(paged_files.begin(), paged_files.end())
334+
+ ("ORDER BY" + odb::query<model::CppFileMetrics>::file));
299335
}
300336

301337
std::string CppMetricsServiceHandler::getLimitQuery(

0 commit comments

Comments
 (0)