Skip to content

Commit

Permalink
Fixing all AST node (by path) querying endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
intjftw committed Apr 23, 2024
1 parent eb36b1b commit 0640e5d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
16 changes: 16 additions & 0 deletions plugins/cpp_metrics/model/include/model/cppastnodemetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ struct CppRecordMetricsView
double value;
};

#pragma db view \
object(CppAstNode) \
object(File = LocFile : CppAstNode::location.file) \
object(CppAstNodeMetrics : CppAstNode::id == CppAstNodeMetrics::astNodeId)
struct CppAstNodeMetricsForPathView
{
#pragma db column(CppAstNode::id)
CppAstNodeId astNodeId;

#pragma db column(CppAstNodeMetrics::type)
CppAstNodeMetrics::Type type;

#pragma db column(CppAstNodeMetrics::value)
double value;
};

} //model
} //cc

Expand Down
2 changes: 1 addition & 1 deletion plugins/cpp_metrics/service/cxxmetrics.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ service CppMetricsService
* This function returns all available C++ metrics
* (AST node-level) for a particular path.
*/
list<CppMetricsAstNodeAll> getCppAstNodeMetricsForPath(
map<common.AstNodeId, list<CppMetricsAstNodeSingle>> getCppAstNodeMetricsForPath(
1:string path)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>
#include <vector>
#include <map>

#include <boost/program_options/variables_map.hpp>

Expand Down Expand Up @@ -49,7 +50,7 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
const core::FileId& fileId_) override;

void getCppAstNodeMetricsForPath(
std::vector<CppMetricsAstNodeAll>& _return,
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
const std::string& path_) override;

void getCppFileMetricsForPath(
Expand Down
46 changes: 17 additions & 29 deletions plugins/cpp_metrics/service/src/cppmetricsservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,49 +120,37 @@ void CppMetricsServiceHandler::getCppMetricsForModule(
}

void CppMetricsServiceHandler::getCppAstNodeMetricsForPath(
std::vector<CppMetricsAstNodeAll>& _return,
std::map<core::AstNodeId, std::vector<CppMetricsAstNodeSingle>>& _return,
const std::string& path_)
{
_transaction([&, this](){
typedef odb::query<model::CppAstNodeMetrics> CppAstNodeMetricsQuery;
typedef odb::result<model::CppAstNodeMetrics> CppAstNodeMetricsResult;
typedef odb::query<model::CppAstNodeFilePath> CppAstNodeFilePathQuery;
typedef odb::result<model::CppAstNodeFilePath> CppAstNodeFilePathResult;
typedef odb::query<model::CppAstNodeMetricsForPathView> CppAstNodeMetricsForPathViewQuery;
typedef odb::result<model::CppAstNodeMetricsForPathView> CppAstNodeMetricsForPathViewResult;

auto nodesRes = _db->query<model::CppAstNodeMetrics>();
std::set<model::CppAstNodeId> nodesWithMetrics;
for (const auto& node : nodesRes)
nodesWithMetrics.insert(node.astNodeId);

CppAstNodeFilePathResult nodes = _db->query<model::CppAstNodeFilePath>(
CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%') &&
CppAstNodeFilePathQuery::CppAstNode::id.in_range(
nodesWithMetrics.begin(), nodesWithMetrics.end()));

if (nodes.empty())
return;
auto nodes = _db->query<model::CppAstNodeMetricsForPathView>(
CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%'));

for (const auto& node : nodes)
{
auto metricsQuery = _db->query<model::CppAstNodeMetrics>(
CppAstNodeMetricsQuery::astNodeId == node.id);
std::vector<CppMetricsAstNodeSingle> metrics;
CppMetricsAstNodeSingle metric;
metric.type = static_cast<CppAstNodeMetricsType::type>(node.type);
metric.value = node.value;

CppMetricsAstNodeSingle metricsAstNode;
for (const auto& metric : metricsQuery)
if (_return.count(std::to_string(node.astNodeId)))
{
metricsAstNode.type = static_cast<CppAstNodeMetricsType::type>(metric.type);
metricsAstNode.value = metric.value;
metrics.push_back(metricsAstNode);
_return[std::to_string(node.astNodeId)].push_back(metric);
}
else
{
CppMetricsAstNodeAll nodeMetric;
std::vector<CppMetricsAstNodeSingle> metricsList;
metricsList.push_back(metric);
_return.insert(std::make_pair(std::to_string(node.astNodeId), metricsList));
}

if (metrics.empty())
continue;

CppMetricsAstNodeAll nodeMetric;
nodeMetric.id = std::to_string(node.id);
nodeMetric.metrics = metrics;
_return.push_back(nodeMetric);
}
});
}
Expand Down

0 comments on commit 0640e5d

Please sign in to comment.