Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metric extraction utility methods to Context #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/models/processor/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ def initialize(params={})
end
@compound_metrics = []
end

def tree_native_metrics
native_metrics.values.flat_map do |metrics|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think an ||= would help here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary since this method should not be a performance problem at all: we never have more than 2 digits of metrics.

metrics.select { |mc| mc.metric.type == 'NativeMetricSnapshot' }
end
end

def tree_metrics
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are we using this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used right now but it will be used immediately when I make the PR for the interpreting rewrite.

tree_native_metrics + compound_metrics
end
end
end
8 changes: 2 additions & 6 deletions lib/processor/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Aggregator < ProcessingStep
def self.task(context)
# Only bother processing Tree metric configurations
# Do nothing if there are none
metric_configurations = extract_tree_metric_configurations(context)
metric_configurations = context.tree_native_metrics
return if metric_configurations.empty?

# Find all the descendants of the root at once.
Expand Down Expand Up @@ -55,10 +55,6 @@ def self.state
"AGGREGATING"
end

def self.extract_tree_metric_configurations(context)
context.native_metrics.values.flatten.reject { |mc| mc.metric.type != 'NativeMetricSnapshot' }
end

def self.module_results_by_id(descendants_by_level)
results = {}
descendants_by_level.each do |level|
Expand All @@ -75,6 +71,6 @@ def self.aggregate_values(tree_metric_results, metric_configuration)
DescriptiveStatistics::Stats.new(tree_metric_results.map(&:value)).send(aggregation_form)
end

private_class_method :extract_tree_metric_configurations, :module_results_by_id, :aggregate_values
private_class_method :module_results_by_id, :aggregate_values
end
end
12 changes: 3 additions & 9 deletions lib/processor/metric_results_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ class MetricResultsChecker < ProcessingStep
def self.task(context)
wanted_metrics = {}

context.native_metrics.each do |metric_collector_name, metric_configurations|
metric_configurations.each { |metric_configuration|
wanted_metrics[metric_key(metric_configuration.metric)] = metric_configuration
}
context.tree_native_metrics.each do |metric_configuration|
wanted_metrics[metric_key(metric_configuration.metric)] = metric_configuration
end

module_results = context.processing.module_results - [context.processing.root_module_result]
Expand All @@ -19,11 +17,7 @@ def self.task(context)
metrics_check_list.delete(metric_key(tree_metric_result.metric))
end

native_metrics = metrics_check_list.select do |metric_key, metric_configuration|
!metric_configuration.metric.is_a?(KalibroClient::Entities::Miscellaneous::HotspotMetric)
end

native_metrics.each do |metric_key, metric_configuration|
metrics_check_list.each_value do |metric_configuration|
TreeMetricResult.create(value: default_value_from(metric_configuration),
module_result: module_result,
metric_configuration_id: metric_configuration.id)
Expand Down
44 changes: 38 additions & 6 deletions spec/lib/processor/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,46 @@
require 'processor'

describe Processor::Context do
def metrics(*traits)
traits.map { |trait| FactoryGirl.build(:metric_configuration, trait) }
end

let(:processing) { FactoryGirl.build(:processing) }
let(:repository) { FactoryGirl.build(:repository) }
let(:tree_native_metrics) { metrics(:flog, :cyclomatic, :maintainability) }
let(:hotspot_metrics) { metrics(:flay, :phpmd) }
let(:compound_metrics) { metrics(:flog_compound_metric_configuration, :saikuro_compound_metric_configuration) }
let(:native_metrics) { tree_native_metrics + hotspot_metrics }

subject { described_class.new(processing: processing, repository: repository) }

describe 'initialize' do
let(:processing) { FactoryGirl.build(:processing) }
let(:repository) { FactoryGirl.build(:repository) }
it 'is expected to set the attributes' do
context = Processor::Context.new(processing: processing, repository: repository)
expect(context.repository).to eq(repository)
expect(context.processing).to eq(processing)
expect(context.native_metrics).to_not be_nil
expect(subject.repository).to eq(repository)
expect(subject.processing).to eq(processing)
expect(subject.native_metrics).to be_a(Hash).and(be_empty)
expect(subject.compound_metrics).to be_empty
end
end

describe 'metric selection methods' do
before :each do
native_metrics.each do |mc|
subject.native_metrics[mc.metric.metric_collector_name] << mc
end
subject.compound_metrics = compound_metrics
end

describe 'tree_native_metrics' do
it 'is expected to return only tree native metrics' do
expect(subject.tree_native_metrics).to contain_exactly(*tree_native_metrics)
end
end

describe 'tree_metrics' do
it 'is expected to return only tree metrics' do
expect(subject.tree_metrics).to contain_exactly(*tree_native_metrics, *compound_metrics)
end
end
end
end