Skip to content

Commit c2acf2c

Browse files
authored
Endpoint to include more data with AST node metrics (#755)
1 parent 19e6bd8 commit c2acf2c

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <model/cppastnode.h>
55
#include <model/cppentity.h>
66
#include <model/cpprecord.h>
7+
#include <model/position.h>
78

89
namespace cc
910
{
@@ -78,6 +79,42 @@ struct CppAstNodeMetricsForPathView
7879
double value;
7980
};
8081

82+
#pragma db view \
83+
object(CppAstNodeMetrics) \
84+
object(CppAstNode : CppAstNodeMetrics::astNodeId == CppAstNode::id) \
85+
object(File = LocFile : CppAstNode::location.file)
86+
struct CppAstNodeMetricsAndDataForPathView
87+
{
88+
typedef cc::model::Position::PosType PosType;
89+
90+
#pragma db column(CppAstNodeMetrics::astNodeId)
91+
CppAstNodeId astNodeId;
92+
93+
#pragma db column(LocFile::path)
94+
std::string path;
95+
96+
#pragma db column(CppAstNode::location.range.start.line)
97+
PosType startLine;
98+
99+
#pragma db column(CppAstNode::location.range.end.line)
100+
PosType endLine;
101+
102+
#pragma db column(CppAstNode::astValue)
103+
std::string astValue;
104+
105+
#pragma db column(CppAstNode::symbolType)
106+
CppAstNode::SymbolType symbolType;
107+
108+
#pragma db column(CppAstNode::astType)
109+
CppAstNode::AstType astType;
110+
111+
#pragma db column(CppAstNodeMetrics::type)
112+
CppAstNodeMetrics::Type type;
113+
114+
#pragma db column(CppAstNodeMetrics::value)
115+
double value;
116+
};
117+
81118
} //model
82119
} //cc
83120

plugins/cpp_metrics/service/cxxmetrics.thrift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ struct CppMetricsAstNodeSingle
3838
3:double value
3939
}
4040

41-
struct CppMetricsAstNodeAll
41+
struct CppMetricsAstNodeDetailed
4242
{
43-
1:common.AstNodeId id,
44-
2:list<CppMetricsAstNodeSingle> metrics
43+
1:string path,
44+
2:string file,
45+
3:string symbolType,
46+
4:string astType,
47+
5:i32 startLine,
48+
6:i32 endLine,
49+
7:string astValue,
50+
8:map<CppAstNodeMetricsType, double> metrics
4551
}
4652

4753
struct CppMetricsModuleSingle
@@ -90,6 +96,15 @@ service CppMetricsService
9096
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getCppAstNodeMetricsForPath(
9197
1:string path)
9298

99+
/**
100+
* This function returns all available C++ metrics
101+
* (AST node-level) and miscellaneous data for a particular path.
102+
*
103+
* The given path is a handled as a prefix.
104+
*/
105+
map<common.AstNodeId, CppMetricsAstNodeDetailed> getCppAstNodeMetricsDetailedForPath(
106+
1:string path)
107+
93108
/**
94109
* This function returns all available C++ metrics
95110
* (file-level) for a particular path.

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

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

56+
void getCppAstNodeMetricsDetailedForPath(
57+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
58+
const std::string& path_) override;
59+
5660
void getCppFileMetricsForPath(
5761
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
5862
const std::string& path_) override;

plugins/cpp_metrics/service/src/cppmetricsservice.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <odb/query.hxx>
55
#include <model/file.h>
66
#include <model/file-odb.hxx>
7+
#include <model/cppastnode.h>
8+
#include <model/cppastnode-odb.hxx>
79

810
namespace cc
911
{
@@ -161,6 +163,46 @@ void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
161163
});
162164
}
163165

166+
void CppMetricsServiceHandler::getCppAstNodeMetricsDetailedForPath(
167+
std::map<core::AstNodeId, CppMetricsAstNodeDetailed>& _return,
168+
const std::string& path_)
169+
{
170+
_transaction([&, this](){
171+
typedef odb::query<model::CppAstNodeFilePath> CppAstNodeFilePathQuery;
172+
typedef odb::result<model::CppAstNodeFilePath> CppAstNodeFilePathResult;
173+
typedef odb::query<model::CppAstNodeMetricsAndDataForPathView> CppAstNodeMetricsAndDataForPathViewQuery;
174+
typedef odb::result<model::CppAstNodeMetricsAndDataForPathView> CppAstNodeMetricsAndDataForPathViewResult;
175+
176+
auto nodes = _db->query<model::CppAstNodeMetricsAndDataForPathView>(
177+
CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%'));
178+
179+
for (const auto& node : nodes)
180+
{
181+
auto pair = std::make_pair(static_cast<CppAstNodeMetricsType::type>(node.type), node.value);
182+
if (_return.count(std::to_string(node.astNodeId)))
183+
{
184+
_return[std::to_string(node.astNodeId)].metrics.insert(pair);
185+
}
186+
else
187+
{
188+
CppMetricsAstNodeDetailed metric;
189+
std::size_t pos = node.path.find_last_of('/');
190+
metric.path = node.path.substr(0, pos + 1);
191+
metric.file = node.path.substr(pos + 1);
192+
metric.startLine = node.startLine;
193+
metric.endLine = node.endLine;
194+
metric.astValue = node.astValue;
195+
metric.symbolType = cc::model::symbolTypeToString(node.symbolType);
196+
metric.astType = cc::model::astTypeToString(node.astType);
197+
198+
std::map<CppAstNodeMetricsType::type, double> metricsList;
199+
metricsList.insert(pair);
200+
_return.insert(std::make_pair(std::to_string(node.astNodeId), metric));
201+
}
202+
}
203+
});
204+
}
205+
164206
void CppMetricsServiceHandler::getCppFileMetricsForPath(
165207
std::map<core::FileId, std::vector<CppMetricsModuleSingle>>& _return,
166208
const std::string& path_)

0 commit comments

Comments
 (0)