Skip to content

Commit f965492

Browse files
committed
Reduce duplication in metric hotspots, move toward explicit strategies
1 parent 08f8b2e commit f965492

File tree

11 files changed

+119
-67
lines changed

11 files changed

+119
-67
lines changed

lib/metric_fu/metrics/churn/churn_hotspot.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ def name
1010
:churn
1111
end
1212

13-
def map(row)
14-
MetricFu::HotspotScoringStrategies.present(row)
13+
def map_strategy
14+
:present
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.sum(scores)
17+
def reduce_strategy
18+
:sum
1919
end
2020

21-
def score(metric_ranking, item)
21+
def score_strategy
22+
:calculate_score
23+
end
24+
25+
def calculate_score(metric_ranking, item)
2226
flat_churn_score = 0.50
2327
metric_ranking.scored?(item) ? flat_churn_score : 0
2428
end

lib/metric_fu/metrics/flay/flay_hotspot.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ def name
1010
:flay
1111
end
1212

13-
def map(row)
14-
MetricFu::HotspotScoringStrategies.present(row)
13+
def map_strategy
14+
:present
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.sum(scores)
17+
def reduce_strategy
18+
:sum
1919
end
2020

21-
def score(metric_ranking, item)
22-
MetricFu::HotspotScoringStrategies.percentile(metric_ranking, item)
21+
def score_strategy
22+
:percentile
2323
end
2424

2525
def generate_records(data, table)

lib/metric_fu/metrics/flog/flog_hotspot.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ def name
1010
:flog
1111
end
1212

13-
def map(row)
14-
row.score
13+
def map_strategy
14+
:score
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.average(scores)
17+
def reduce_strategy
18+
:average
1919
end
2020

21-
def score(metric_ranking, item)
22-
MetricFu::HotspotScoringStrategies.identity(metric_ranking, item)
21+
def score_strategy
22+
:identity
2323
end
2424

2525
def generate_records(data, table)

lib/metric_fu/metrics/hotspots/analysis/ranking.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def percentile(item)
1818
end
1919

2020
def_delegator :@items_to_score, :has_key?, :scored?
21-
def_delegators :@items_to_score, :[], :[]=, :length, :each, :delete
21+
def_delegators :@items_to_score, :[], :[]=, :length, :each, :delete, :fetch
2222

2323
private
2424

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
module MetricFu
22
module HotspotScoringStrategies
33

4+
module_function
5+
6+
# per project score percentile
47
def percentile(ranking, item)
5-
ranking.percentile(item) # per project score percentile
8+
ranking.percentile(item)
69
end
710

11+
# Use the score you got
12+
# (ex flog score of 20 is not bad even if it is the top one in project)
813
def identity(ranking, item)
9-
ranking[item] # Use the score you got (ex flog score of 20 is not bad even if it is the top one in project)
10-
end
11-
12-
def present(row)
13-
1 # If present it's a one, not present it's a zero - For things like Reek that don't have a number
14+
ranking.fetch(item)
1415
end
1516

1617
def sum(scores)
17-
scores.inject(0) {|s,x| s+x}
18+
scores.inject(&:+)
1819
end
1920

2021
def average(scores)
21-
# remove dependency on statarray
22-
# scores.to_statarray.mean
23-
score_length = scores.length
24-
total = 0
25-
total= scores.inject( nil ) { |sum,x| sum ? sum+x : x }
26-
(total.to_f / score_length.to_f)
22+
sum(scores).to_f / scores.size.to_f
2723
end
2824

29-
extend self
3025
end
3126
end

lib/metric_fu/metrics/hotspots/hotspot.rb

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,65 @@ def self.inherited(subclass)
1818
@analyzers[subclass.metric] = subclass.new
1919
end
2020

21-
# TODO simplify calculation
22-
# DUPLICATES CODE IN ScoringStrategies
2321
def get_mean(collection)
24-
collection_length = collection.length
25-
total = 0
26-
total = collection.inject( nil ) { |sum,x| sum ? sum+x : x }
27-
(total.to_f / collection_length.to_f)
22+
MetricFu::HotspotScoringStrategies.average(collection)
23+
end
24+
25+
def map(row)
26+
mapping_strategies.fetch(map_strategy) { row.public_send(map_strategy) }
27+
end
28+
29+
def mapping_strategies
30+
{
31+
:present => 1,
32+
:absent => 0,
33+
}
34+
end
35+
36+
def map_strategy
37+
not_implemented
38+
end
39+
40+
def reduce(scores)
41+
{
42+
:average => MetricFu::HotspotScoringStrategies.average(scores),
43+
:sum => MetricFu::HotspotScoringStrategies.sum(scores),
44+
:absent => 0,
45+
}.fetch(reduce_strategy) { raise "#{reduce_strategy} not a know reduce strategy" }
46+
end
47+
48+
def reduce_strategy
49+
not_implemented
50+
end
51+
52+
# @return [Integer]
53+
def score(metric_ranking, item)
54+
{
55+
:identity => MetricFu::HotspotScoringStrategies.identity(metric_ranking, item),
56+
:percentile => MetricFu::HotspotScoringStrategies.percentile(metric_ranking, item),
57+
:absent => 0,
58+
}.fetch(score_strategy) { method(score_strategy).call(metric_ranking, item) }
59+
end
60+
61+
def score_strategy
62+
not_implemented
63+
end
64+
65+
# Transforms the data param, if non-nil, into a hash with keys:
66+
# 'metric', etc.
67+
# and appends the hash to the table param
68+
# Has no return value
69+
def generate_records(data, table)
70+
not_implemented
71+
end
72+
73+
# @return [String] description result
74+
def present_group(group)
75+
not_implemented
76+
end
77+
78+
def not_implemented
79+
raise "#{caller[0]} not implemented"
2880
end
2981
end
3082
end

lib/metric_fu/metrics/rcov/rcov_hotspot.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def name
1111
end
1212

1313
def map(row)
14-
row.percentage_uncovered
14+
:percentage_uncovered
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.average(scores)
17+
def reduce_strategy
18+
:average
1919
end
2020

21-
def score(metric_ranking, item)
22-
MetricFu::HotspotScoringStrategies.identity(metric_ranking, item)
21+
def score_strategy
22+
:identity
2323
end
2424

2525
def generate_records(data, table)

lib/metric_fu/metrics/reek/reek_hotspot.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ def name
1515
:reek
1616
end
1717

18-
def map(row)
19-
MetricFu::HotspotScoringStrategies.present(row)
18+
def map_strategy
19+
:present
2020
end
2121

22-
def reduce(scores)
23-
MetricFu::HotspotScoringStrategies.sum(scores)
22+
def reduce_strategy
23+
:sum
2424
end
2525

26-
def score(metric_ranking, item)
27-
MetricFu::HotspotScoringStrategies.percentile(metric_ranking, item)
26+
def score_strategy
27+
:percentile
2828
end
2929

3030
def generate_records(data, table)
@@ -60,6 +60,7 @@ def present_group(group)
6060
occurences = group.size
6161
"found #{occurences} code smells"
6262
end
63+
6364
private
6465

6566
def comparable_message(type_name, message)

lib/metric_fu/metrics/roodi/roodi_hotspot.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ def name
1010
:roodi
1111
end
1212

13-
def map(row)
14-
MetricFu::HotspotScoringStrategies.present(row)
13+
def map_strategy
14+
:present
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.sum(scores)
17+
def reduce_strategy
18+
:sum
1919
end
2020

21-
def score(metric_ranking, item)
22-
MetricFu::HotspotScoringStrategies.percentile(metric_ranking, item)
21+
def score_strategy
22+
:percentile
2323
end
2424

2525
def generate_records(data, table)

lib/metric_fu/metrics/saikuro/saikuro_hotspot.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ def name
1010
:saikuro
1111
end
1212

13-
def map(row)
14-
row.complexity
13+
def map_strategy
14+
:complexity
1515
end
1616

17-
def reduce(scores)
18-
MetricFu::HotspotScoringStrategies.average(scores)
17+
def reduce_strategy
18+
:average
1919
end
2020

21-
def score(metric_ranking, item)
22-
MetricFu::HotspotScoringStrategies.identity(metric_ranking, item)
21+
def score_strategy
22+
:identity
2323
end
2424

2525
def generate_records(data, table)

0 commit comments

Comments
 (0)