1010#include < model/cppinheritance-odb.hxx>
1111#include < model/cpprecord.h>
1212#include < model/cpprecord-odb.hxx>
13-
13+ #include < model/cpptypedependencymetrics.h>
14+ #include < model/cpptypedependencymetrics-odb.hxx>
1415#include < model/cppastnode.h>
1516#include < model/cppastnode-odb.hxx>
17+ #include < model/file.h>
18+ #include < model/file-odb.hxx>
1619
1720#include < boost/filesystem.hpp>
1821
@@ -390,7 +393,7 @@ void CppMetricsParser::efferentTypeLevel()
390393 dependentTypes.clear ();
391394
392395 // Count parent types
393- auto inheritanceView = _ctx.db ->query <model::CppInheritanceCount >(
396+ auto inheritanceView = _ctx.db ->query <model::CppInheritance >(
394397 InheritanceQuery::derived == type.entityHash );
395398
396399 // Count unique attribute types
@@ -423,8 +426,26 @@ void CppMetricsParser::efferentTypeLevel()
423426 model::CppAstNodeMetrics metric;
424427 metric.astNodeId = type.astNodeId ;
425428 metric.type = model::CppAstNodeMetrics::Type::EFFERENT_TYPE ;
426- metric.value = inheritanceView.begin ()-> count + dependentTypes.size ();
429+ metric.value = inheritanceView.size () + dependentTypes.size ();
427430 _ctx.db ->persist (metric);
431+
432+ auto typeRelationInserter = [this ](const std::uint64_t & entityHash, const std::uint64_t & dependencyHash)
433+ {
434+ model::CppTypeDependencyMetrics relation;
435+ relation.entityHash = entityHash;
436+ relation.dependencyHash = dependencyHash;
437+ _ctx.db ->persist (relation);
438+ };
439+
440+ // Insert type dependency relations
441+ for (const std::uint64_t & d : dependentTypes) {
442+ typeRelationInserter (type.entityHash , d);
443+ }
444+
445+ // Insert inheritance relations
446+ for (const model::CppInheritance& d : inheritanceView) {
447+ typeRelationInserter (type.entityHash , d.base );
448+ }
428449 }
429450 });
430451 });
@@ -513,6 +534,46 @@ void CppMetricsParser::afferentTypeLevel()
513534 });
514535}
515536
537+ odb::query<model::File> CppMetricsParser::getModulePathsQuery ()
538+ {
539+ if (_ctx.moduleDirectories .empty ()) {
540+ // No module directories specified, compute for all directories
541+ return odb::query<model::File>::type == cc::model::File::DIRECTORY_TYPE && getFilterPathsQuery<model::File>();
542+ } else {
543+ // Compute for module directories
544+ return odb::query<model::File>::path.in_range (_ctx.moduleDirectories .begin (), _ctx.moduleDirectories .end ());
545+ }
546+ }
547+
548+ void CppMetricsParser::efferentModuleLevel ()
549+ {
550+ parallelCalcMetric<model::File>(
551+ " Efferent coupling at module level" ,
552+ _threadCount * efferentCouplingModulesPartitionMultiplier,// number of jobs; adjust for granularity
553+ getModulePathsQuery (),
554+ [&, this ](const MetricsTasks<model::File>& tasks)
555+ {
556+ util::OdbTransaction{_ctx.db }([&, this ]
557+ {
558+ typedef odb::query<cc::model::CppDistinctTypeDependencyMetricsPathView> TypeDependencyQuery;
559+ typedef odb::result<cc::model::CppDistinctTypeDependencyMetricsPathView> TypeDependencyResult;
560+
561+ for (const model::File& file : tasks)
562+ {
563+ TypeDependencyResult types = _ctx.db ->query <model::CppDistinctTypeDependencyMetricsPathView>(
564+ TypeDependencyQuery::EntityFile::path.like (file.path + ' %' ) &&
565+ !TypeDependencyQuery::DependencyFile::path.like (file.path + ' %' ));
566+
567+ model::CppFileMetrics metric;
568+ metric.file = file.id ;
569+ metric.type = model::CppFileMetrics::Type::EFFERENT_MODULE ;
570+ metric.value = types.size ();
571+ _ctx.db ->persist (metric);
572+ }
573+ });
574+ });
575+ }
576+
516577bool CppMetricsParser::parse ()
517578{
518579 LOG (info) << " [cppmetricsparser] Computing function parameter count metric." ;
@@ -529,6 +590,8 @@ bool CppMetricsParser::parse()
529590 efferentTypeLevel ();
530591 LOG (info) << " [cppmetricsparser] Computing afferent coupling metric for types." ;
531592 afferentTypeLevel ();
593+ LOG (info) << " [cppmetricsparser] Computing efferent coupling metric at module level." ;
594+ efferentModuleLevel (); // This metric needs to be calculated after efferentTypeLevel
532595 return true ;
533596}
534597
0 commit comments