Skip to content

Commit be4893b

Browse files
Add paging support for C++ metrics (#784)
1 parent b4b6d16 commit be4893b

File tree

7 files changed

+211
-29
lines changed

7 files changed

+211
-29
lines changed

Config.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ set(ODBFLAGS
8787
--default-pointer "std::shared_ptr")
8888

8989
# Set CXX standard
90-
set(CMAKE_CXX_STANDARD 14)
90+
set(CMAKE_CXX_STANDARD 17)
9191
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9292
set(CMAKE_CXX_EXTENSIONS OFF)
9393

plugins/cpp_metrics/model/include/model/cppastnodemetrics.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ struct CppAstNodeMetricsAndDataForPathView
117117
double value;
118118
};
119119

120+
#pragma db view \
121+
object(CppAstNodeMetrics) \
122+
object(CppAstNode : CppAstNodeMetrics::astNodeId == CppAstNode::id) \
123+
object(File : CppAstNode::location.file) \
124+
query(distinct)
125+
struct CppAstNodeMetricsDistinctView
126+
{
127+
#pragma db column(CppAstNodeMetrics::astNodeId)
128+
CppAstNodeId astNodeId;
129+
};
130+
120131
} //model
121132
} //cc
122133

plugins/cpp_metrics/model/include/model/cppfilemetrics.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ struct CppModuleMetricsForPathView
4747
unsigned value;
4848
};
4949

50+
#pragma db view \
51+
object(CppFileMetrics) \
52+
object(File : CppFileMetrics::file == File::id) \
53+
query(distinct)
54+
struct CppModuleMetricsDistinctView
55+
{
56+
#pragma db column(CppFileMetrics::file)
57+
FileId fileId;
58+
};
59+
5060
} //model
5161
} //cc
5262

plugins/cpp_metrics/service/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ add_custom_command(
1919
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/CppMetricsService.cpp
2020
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/CppMetricsService.h
2121
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp
22+
${CMAKE_CURRENT_BINARY_DIR}/gen-js
2223
COMMAND
23-
${THRIFT_EXECUTABLE} --gen cpp
24+
${THRIFT_EXECUTABLE} --gen cpp --gen js
2425
-o ${CMAKE_CURRENT_BINARY_DIR}
2526
-I ${PROJECT_SOURCE_DIR}/service
2627
${CMAKE_CURRENT_SOURCE_DIR}/cxxmetrics.thrift

plugins/cpp_metrics/service/cxxmetrics.thrift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ service CppMetricsService
9898
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getCppAstNodeMetricsForPath(
9999
1:string path)
100100

101+
/**
102+
* This is a paged function which returns all available C++ metrics
103+
* (AST node-level) for a particular path.
104+
*
105+
* The given path is a handled as a prefix.
106+
*/
107+
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getPagedCppAstNodeMetricsForPath(
108+
1:string path
109+
2:i32 pageSize,
110+
3:i32 pageNumber)
111+
101112
/**
102113
* This function returns all available C++ metrics
103114
* (AST node-level) and miscellaneous data for a particular path.
@@ -107,6 +118,17 @@ service CppMetricsService
107118
map<common.AstNodeId, CppMetricsAstNodeDetailed> getCppAstNodeMetricsDetailedForPath(
108119
1:string path)
109120

121+
/**
122+
* This is a paged function which returns all available C++ metrics
123+
* (AST node-level) and miscellaneous data for a particular path.
124+
*
125+
* The given path is a handled as a prefix.
126+
*/
127+
map<common.AstNodeId, CppMetricsAstNodeDetailed> getPagedCppAstNodeMetricsDetailedForPath(
128+
1:string path,
129+
2:i32 pageSize,
130+
3:i32 pageNumber)
131+
110132
/**
111133
* This function returns all available C++ metrics
112134
* (file-level) for a particular path.
@@ -116,6 +138,17 @@ service CppMetricsService
116138
map<common.FileId, list<CppMetricsModuleSingle>> getCppFileMetricsForPath(
117139
1:string path)
118140

141+
/**
142+
* This is a paged function which returns all available C++ metrics
143+
* (file-level) for a particular path.
144+
*
145+
* The given path is a handled as a prefix.
146+
*/
147+
map<common.FileId, list<CppMetricsModuleSingle>> getPagedCppFileMetricsForPath(
148+
1:string path,
149+
2:i32 pageSize,
150+
3:i32 pageNumber)
151+
119152
/**
120153
* This function returns the names of the AST node-level C++ metrics.
121154
*/

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,32 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
5353
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
5454
const std::string& path_) override;
5555

56+
void getPagedCppAstNodeMetricsForPath(
57+
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
58+
const std::string& path_,
59+
const std::int32_t pageSize_,
60+
const std::int32_t pageNumber_) override;
61+
5662
void getCppAstNodeMetricsDetailedForPath(
5763
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
5864
const std::string& path_) override;
5965

66+
void getPagedCppAstNodeMetricsDetailedForPath(
67+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
68+
const std::string& path_,
69+
const std::int32_t pageSize_,
70+
const std::int32_t pageNumber_) override;
71+
6072
void getCppFileMetricsForPath(
6173
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
6274
const std::string& path_) override;
6375

76+
void getPagedCppFileMetricsForPath(
77+
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
78+
const std::string& path_,
79+
const std::int32_t pageSize_,
80+
const std::int32_t pageNumber_) override;
81+
6482
void getCppAstNodeMetricsTypeNames(
6583
std::vector<CppAstNodeMetricsTypeName>& _return) override;
6684

@@ -72,6 +90,46 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
7290
util::OdbTransaction _transaction;
7391

7492
const boost::program_options::variables_map& _config;
93+
94+
std::string getPagingQuery(
95+
const std::int32_t pageSize_,
96+
const std::int32_t pageNumber_);
97+
98+
template <typename TID, typename TView>
99+
std::vector<TID> pageMetrics(
100+
const std::string& path_,
101+
const std::int32_t pageSize_,
102+
const std::int32_t pageNumber_)
103+
{
104+
return _transaction([&, this](){
105+
odb::result<TView> paged_nodes = _db->query<TView>(
106+
odb::query<TView>::File::path.like(path_ + '%') + getPagingQuery(pageSize_, pageNumber_));
107+
108+
std::vector<TID> paged_ids(paged_nodes.size());
109+
std::transform(paged_nodes.begin(), paged_nodes.end(), paged_ids.begin(),
110+
[](const TView& e){
111+
if constexpr (std::is_same<TView, model::CppAstNodeMetricsDistinctView>::value) {
112+
return e.astNodeId;
113+
} else if constexpr (std::is_same<TView, model::CppModuleMetricsDistinctView>::value) {
114+
return e.fileId;
115+
}
116+
});
117+
118+
return paged_ids;
119+
});
120+
}
121+
122+
void queryCppAstNodeMetricsForPath(
123+
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
124+
const odb::query<model::CppAstNodeMetricsForPathView>& query_);
125+
126+
void queryCppAstNodeMetricsDetailedForPath(
127+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
128+
const odb::query<model::CppAstNodeMetricsAndDataForPathView>& query_);
129+
130+
void queryCppFileMetricsForPath(
131+
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
132+
const odb::query<model::CppModuleMetricsForPathView>& query_);
75133
};
76134

77135
} // cppmetrics

plugins/cpp_metrics/service/src/cppmetricsservice.cpp

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ namespace service
1414
namespace cppmetrics
1515
{
1616

17-
typedef odb::query<cc::model::CppAstNode> AstQuery;
18-
typedef odb::result<cc::model::CppAstNode> AstResult;
17+
typedef odb::query<model::CppAstNodeMetricsForPathView> CppNodeMetricsQuery;
18+
typedef odb::result<model::CppAstNodeMetricsForPathView> CppNodeMetricsResult;
19+
typedef odb::query<model::CppModuleMetricsForPathView> CppModuleMetricsQuery;
20+
typedef odb::result<model::CppModuleMetricsForPathView> CppModuleMetricsResult;
1921

2022
CppMetricsServiceHandler::CppMetricsServiceHandler(
2123
std::shared_ptr<odb::database> db_,
@@ -137,18 +139,12 @@ void CppMetricsServiceHandler::getCppMetricsForModule(
137139
});
138140
}
139141

140-
void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
142+
void CppMetricsServiceHandler::queryCppAstNodeMetricsForPath(
141143
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
142-
const std::string& path_)
144+
const odb::query<model::CppAstNodeMetricsForPathView>& query_)
143145
{
144146
_transaction([&, this](){
145-
typedef odb::query<model::CppAstNodeFilePath> CppAstNodeFilePathQuery;
146-
typedef odb::result<model::CppAstNodeFilePath> CppAstNodeFilePathResult;
147-
typedef odb::query<model::CppAstNodeMetricsForPathView> CppAstNodeMetricsForPathViewQuery;
148-
typedef odb::result<model::CppAstNodeMetricsForPathView> CppAstNodeMetricsForPathViewResult;
149-
150-
auto nodes = _db->query<model::CppAstNodeMetricsForPathView>(
151-
CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%'));
147+
auto nodes = _db->query<model::CppAstNodeMetricsForPathView>(query_);
152148

153149
for (const auto& node : nodes)
154150
{
@@ -171,18 +167,33 @@ void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
171167
});
172168
}
173169

174-
void CppMetricsServiceHandler::getCppAstNodeMetricsDetailedForPath(
175-
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
170+
void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
171+
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
176172
const std::string& path_)
177173
{
178-
_transaction([&, this](){
179-
typedef odb::query<model::CppAstNodeFilePath> CppAstNodeFilePathQuery;
180-
typedef odb::result<model::CppAstNodeFilePath> CppAstNodeFilePathResult;
181-
typedef odb::query<model::CppAstNodeMetricsAndDataForPathView> CppAstNodeMetricsAndDataForPathViewQuery;
182-
typedef odb::result<model::CppAstNodeMetricsAndDataForPathView> CppAstNodeMetricsAndDataForPathViewResult;
174+
queryCppAstNodeMetricsForPath(_return,
175+
CppNodeMetricsQuery::LocFile::path.like(path_ + '%'));
176+
}
177+
178+
void CppMetricsServiceHandler::getPagedCppAstNodeMetricsForPath(
179+
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
180+
const std::string& path_,
181+
const std::int32_t pageSize_,
182+
const std::int32_t pageNumber_)
183+
{
184+
std::vector<model::CppAstNodeId> paged_nodes = pageMetrics<model::CppAstNodeId, model::CppAstNodeMetricsDistinctView>(
185+
path_, pageSize_, pageNumber_);
186+
187+
queryCppAstNodeMetricsForPath(_return,
188+
CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range(paged_nodes.begin(), paged_nodes.end()));
189+
}
183190

184-
auto nodes = _db->query<model::CppAstNodeMetricsAndDataForPathView>(
185-
CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%'));
191+
void CppMetricsServiceHandler::queryCppAstNodeMetricsDetailedForPath(
192+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
193+
const odb::query<model::CppAstNodeMetricsAndDataForPathView>& query_)
194+
{
195+
_transaction([&, this](){
196+
auto nodes = _db->query<model::CppAstNodeMetricsAndDataForPathView>(query_);
186197

187198
for (const auto& node : nodes)
188199
{
@@ -210,16 +221,33 @@ void CppMetricsServiceHandler::getCppAstNodeMetricsDetailedForPath(
210221
});
211222
}
212223

213-
void CppMetricsServiceHandler::getCppFileMetricsForPath(
214-
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
224+
void CppMetricsServiceHandler::getCppAstNodeMetricsDetailedForPath(
225+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
215226
const std::string& path_)
216227
{
217-
_transaction([&, this](){
218-
typedef odb::query<model::CppModuleMetricsForPathView> CppModuleMetricsQuery;
219-
typedef odb::result<model::CppModuleMetricsForPathView> CppModuleMetricsResult;
228+
queryCppAstNodeMetricsDetailedForPath(_return,
229+
CppNodeMetricsQuery::LocFile::path.like(path_ + '%'));
230+
}
231+
232+
void CppMetricsServiceHandler::getPagedCppAstNodeMetricsDetailedForPath(
233+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
234+
const std::string& path_,
235+
const std::int32_t pageSize_,
236+
const std::int32_t pageNumber_)
237+
{
238+
std::vector<model::CppAstNodeId> paged_nodes = pageMetrics<model::CppAstNodeId, model::CppAstNodeMetricsDistinctView>(
239+
path_, pageSize_, pageNumber_);
220240

221-
auto files = _db->query<model::CppModuleMetricsForPathView>(
222-
CppModuleMetricsQuery::File::path.like(path_ + '%'));
241+
queryCppAstNodeMetricsDetailedForPath(_return,
242+
CppNodeMetricsQuery::CppAstNodeMetrics::astNodeId.in_range(paged_nodes.begin(), paged_nodes.end()));
243+
}
244+
245+
void CppMetricsServiceHandler::queryCppFileMetricsForPath(
246+
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
247+
const odb::query<model::CppModuleMetricsForPathView>& query_)
248+
{
249+
_transaction([&, this](){
250+
auto files = _db->query<model::CppModuleMetricsForPathView>(query_);
223251

224252
for (const auto& file : files)
225253
{
@@ -242,6 +270,47 @@ void CppMetricsServiceHandler::getCppFileMetricsForPath(
242270
});
243271
}
244272

273+
void CppMetricsServiceHandler::getCppFileMetricsForPath(
274+
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
275+
const std::string& path_)
276+
{
277+
queryCppFileMetricsForPath(_return,
278+
CppModuleMetricsQuery::File::path.like(path_ + '%'));
279+
}
280+
281+
void CppMetricsServiceHandler::getPagedCppFileMetricsForPath(
282+
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
283+
const std::string& path_,
284+
const std::int32_t pageSize_,
285+
const std::int32_t pageNumber_)
286+
{
287+
std::vector<model::FileId> paged_files = pageMetrics<model::FileId, model::CppModuleMetricsDistinctView>(path_, pageSize_, pageNumber_);
288+
queryCppFileMetricsForPath(_return,
289+
CppModuleMetricsQuery::CppFileMetrics::file.in_range(paged_files.begin(), paged_files.end()));
290+
}
291+
292+
std::string CppMetricsServiceHandler::getPagingQuery(
293+
const std::int32_t pageSize_,
294+
const std::int32_t pageNumber_)
295+
{
296+
if (pageSize_ <= 0)
297+
{
298+
core::InvalidInput ex;
299+
ex.__set_msg("Invalid page size: " + std::to_string(pageSize_));
300+
throw ex;
301+
}
302+
303+
if (pageNumber_ <= 0)
304+
{
305+
core::InvalidInput ex;
306+
ex.__set_msg("Invalid page number: " + std::to_string(pageNumber_));
307+
throw ex;
308+
}
309+
310+
const std::int32_t offset = (pageNumber_ - 1) * pageSize_;
311+
return " LIMIT " + std::to_string(pageSize_) + " OFFSET " + std::to_string(offset);
312+
}
313+
245314
} // cppmetrics
246315
} // service
247316
} // cc

0 commit comments

Comments
 (0)