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

Fix CodeClimate and MetricFu issues #238

Open
wants to merge 6 commits 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
6 changes: 5 additions & 1 deletion app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ def metric_result_history_of(module_name, metric_name)
module_result = processing.module_results.select { |module_result| module_result.kalibro_module.long_name == module_name }.first
unless module_result.nil?
module_result.tree_metric_results.each do |tree_metric_result|
history << {date: processing.updated_at, tree_metric_result: tree_metric_result, metric_result: tree_metric_result} if tree_metric_result.metric.name == metric_name
check_metric_name(history, metric_name, processing, tree_metric_result)
end
end
end
return history
end

def check_metric_name(history, metric_name, processing, tree_metric_result)
history << {date: processing.updated_at, tree_metric_result: tree_metric_result, metric_result: tree_metric_result} if tree_metric_result.metric.name == metric_name
end

def find_processing_by_date(date, order)
self.processings.where("updated_at #{order} :date", {date: date})
end
Expand Down
39 changes: 29 additions & 10 deletions lib/processor/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ class Collector < ProcessingStep

def self.task(context)
kolekti_collector_names = Set.new(Kolekti.collectors.map(&:name))
kolekti_metrics, unknown_metrics = context.native_metrics.partition do |metric_collector_name, _|
kolekti_collector_names.include?(metric_collector_name)
end

kolekti_metrics, unknown_metrics =
get_metrics(context.native_metrics, kolekti_collector_names)

# Work around the fact that Hash#partition does not return hashes
kolekti_metrics = Hash[kolekti_metrics]
unknown_metrics = Hash[unknown_metrics]

unless unknown_metrics.empty?
unknown_collector_names = unknown_metrics.keys.join(', ')
raise Errors::NotFoundError.new("Metric collectors #{unknown_collector_names} not found")
end
raise_unknown_metrics_exception(unknown_metrics)

collect_kolekti_metrics(context, kolekti_metrics)

context.processing.reload
raise Errors::EmptyModuleResultsError if context.processing.module_results.empty?

raise_empty_module_results_exception(context.processing.module_results)
end

def self.collect_kolekti_metrics(context, wanted_metrics)
persistence_strategy = MetricCollector::PersistenceStrategy.new(context.processing)
runner = Kolekti::Runner.new(context.repository.code_directory, wanted_metrics.values.flatten, persistence_strategy)
runner = Kolekti::Runner.new(
context.repository.code_directory,
wanted_metrics.values.flatten,
persistence_strategy
)

begin
runner.run_wanted_metrics
Expand All @@ -40,7 +42,24 @@ def self.collect_kolekti_metrics(context, wanted_metrics)
end

def self.state
"COLLECTING"
'COLLECTING'
end

def self.get_metrics(native_metrics, names)
native_metrics.partition do |metric_collector_name, _|
names.include?(metric_collector_name)
end
end

def self.raise_unknown_metrics_exception(unknown_metrics)
unless unknown_metrics.empty?
unknown_collector_names = unknown_metrics.keys.join(', ')
raise Errors::NotFoundError.new("Metric collectors #{unknown_collector_names} not found")
end
end

def self.raise_empty_module_results_exception(module_results)
raise Errors::EmptyModuleResultsError if module_results.empty?
end
end
end
40 changes: 29 additions & 11 deletions lib/processor/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@ class Interpreter < ProcessingStep

def self.task(context)
all_module_results = context.processing.root_module_result.pre_order
all_module_results.each do | module_result_child |
numerator = 0
denominator = 0
module_result_child.tree_metric_results.each do |tree_metric_result|
weight = tree_metric_result.metric_configuration.weight
grade = tree_metric_result.has_grade? ? tree_metric_result.range.reading.grade : 0
numerator += weight*grade
denominator += weight
end
quotient = denominator == 0 ? 0 : numerator/denominator

all_module_results.each do |module_result_child|
factors = get_factors(module_result_child.tree_metric_results)
numerator = factors[0]
denominator = factors[1]
quotient = calculate_quotient(numerator, denominator)

module_result_child.update(grade: quotient)
end
end

def self.state
"INTERPRETING"
'INTERPRETING'
end

def self.get_factors(results)
factors = [0, 0]

results.each do |result|
weight = result.metric_configuration.weight
factors[0] += weight * get_grade(result)
factors[1] += weight
end

factors
end

def self.get_grade(tree_metric_result)
tree_metric_result.has_grade? ? tree_metric_result.range.reading.grade : 0
end

def self.calculate_quotient(numerator, denominator)
denominator.zero? ? 0 : numerator / denominator
end

end
end
2 changes: 1 addition & 1 deletion spec/lib/processor/interpreter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

describe 'state' do
it 'is expected to return "INTERPRETING"' do
expect(Processor::Interpreter.state).to eq("INTERPRETING")
expect(Processor::Interpreter.state).to eq('INTERPRETING')
end
end
end
Expand Down