From 0640e5da93b56226006ae13a61ebb770c22c58fa Mon Sep 17 00:00:00 2001 From: Anett Fekete Date: Tue, 23 Apr 2024 02:26:21 +0200 Subject: [PATCH] Fixing all AST node (by path) querying endpoint. --- .../model/include/model/cppastnodemetrics.h | 16 +++++++ plugins/cpp_metrics/service/cxxmetrics.thrift | 2 +- .../include/service/cppmetricsservice.h | 3 +- .../service/src/cppmetricsservice.cpp | 46 +++++++------------ 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h b/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h index eb17cdf3d..5d5c35b29 100644 --- a/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h +++ b/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h @@ -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 diff --git a/plugins/cpp_metrics/service/cxxmetrics.thrift b/plugins/cpp_metrics/service/cxxmetrics.thrift index c3789c554..a1b0af840 100644 --- a/plugins/cpp_metrics/service/cxxmetrics.thrift +++ b/plugins/cpp_metrics/service/cxxmetrics.thrift @@ -87,7 +87,7 @@ service CppMetricsService * This function returns all available C++ metrics * (AST node-level) for a particular path. */ - list getCppAstNodeMetricsForPath( + map> getCppAstNodeMetricsForPath( 1:string path) /** diff --git a/plugins/cpp_metrics/service/include/service/cppmetricsservice.h b/plugins/cpp_metrics/service/include/service/cppmetricsservice.h index 24a39f4af..c010fd4b9 100644 --- a/plugins/cpp_metrics/service/include/service/cppmetricsservice.h +++ b/plugins/cpp_metrics/service/include/service/cppmetricsservice.h @@ -3,6 +3,7 @@ #include #include +#include #include @@ -49,7 +50,7 @@ class CppMetricsServiceHandler : virtual public CppMetricsServiceIf const core::FileId& fileId_) override; void getCppAstNodeMetricsForPath( - std::vector& _return, + std::map>& _return, const std::string& path_) override; void getCppFileMetricsForPath( diff --git a/plugins/cpp_metrics/service/src/cppmetricsservice.cpp b/plugins/cpp_metrics/service/src/cppmetricsservice.cpp index 31921625b..932b4a80e 100644 --- a/plugins/cpp_metrics/service/src/cppmetricsservice.cpp +++ b/plugins/cpp_metrics/service/src/cppmetricsservice.cpp @@ -120,7 +120,7 @@ void CppMetricsServiceHandler::getCppMetricsForModule( } void CppMetricsServiceHandler::getCppAstNodeMetricsForPath( - std::vector& _return, + std::map>& _return, const std::string& path_) { _transaction([&, this](){ @@ -128,41 +128,29 @@ void CppMetricsServiceHandler::getCppAstNodeMetricsForPath( typedef odb::result CppAstNodeMetricsResult; typedef odb::query CppAstNodeFilePathQuery; typedef odb::result CppAstNodeFilePathResult; + typedef odb::query CppAstNodeMetricsForPathViewQuery; + typedef odb::result CppAstNodeMetricsForPathViewResult; - auto nodesRes = _db->query(); - std::set nodesWithMetrics; - for (const auto& node : nodesRes) - nodesWithMetrics.insert(node.astNodeId); - - CppAstNodeFilePathResult nodes = _db->query( - CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%') && - CppAstNodeFilePathQuery::CppAstNode::id.in_range( - nodesWithMetrics.begin(), nodesWithMetrics.end())); - - if (nodes.empty()) - return; + auto nodes = _db->query( + CppAstNodeFilePathQuery::LocFile::path.like(path_ + '%')); for (const auto& node : nodes) { - auto metricsQuery = _db->query( - CppAstNodeMetricsQuery::astNodeId == node.id); - std::vector metrics; + CppMetricsAstNodeSingle metric; + metric.type = static_cast(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(metric.type); - metricsAstNode.value = metric.value; - metrics.push_back(metricsAstNode); + _return[std::to_string(node.astNodeId)].push_back(metric); + } + else + { + CppMetricsAstNodeAll nodeMetric; + std::vector 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); } }); }