Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Allow specifying metrics in evaluate command
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 14, 2024
1 parent 321c41b commit 4d7adef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/mosaik/commands/evaluate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,37 @@ module Commands
class Evaluate < Command
self.description = "Evaluate microservice candidates"

defaults file: "mosaik-candidates.csv"
defaults file: "mosaik-candidates.csv",
metrics: [:modularity, :coupling]

argument "--file FILE", "File for the identified microservice candidates graph (default: #{defaults[:file]})"
argument("--metrics METRICS", Array, "Metrics to evaluate (default: #{defaults[:metrics].join(',')})") { |arg| arg&.map(&:to_sym) }

def validate
raise OptionError, "file not found: #{options[:file]}" unless File.exist? options[:file]

metrics = options[:metrics] - self.class.defaults[:metrics]

raise OptionError, "unknown metrics: #{metrics.join(', ')}" unless metrics.empty?
end

def call
info "Evaluating microservice candidates (#{options.map { |k, v| "#{k}: #{v}" }.join(', ')})"

# Evaluate modularity
Metrics::Modularity
.new(options, graph)
.evaluate
# Evaluate metrics
options[:metrics].each do |metric|
Metrics
.const_get(metric.to_s.camelize)
.new(options, graph)
.evaluate
end

# Evaluate coupling
Metrics::Coupling
.new(options, graph)
.evaluate
# Print the graph
info "Graph (#{options[:metrics].map { |m| "#{m}: #{graph.attributes[m]}" }.join(', ')})"

# Print the clusters
graph.clusters.each_value do |cluster|
info "Cluster #{cluster.id} (modularity: #{cluster.attributes[:modularity]}, coupling: #{cluster.attributes[:coupling]})"
info "Cluster #{cluster.id} (#{options[:metrics].map { |m| "#{m}: #{cluster.attributes[m]}" }.join(', ')})"

next unless options[:debug]

Expand Down
10 changes: 10 additions & 0 deletions spec/mosaik/commands/evaluate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
expect { command.validate }.to raise_error MOSAIK::OptionError, "file not found: doesnotexist.csv"
end
end

describe "--metrics" do
let(:arguments) { ["--metrics", "modularity,doesnotexist"] }

it "raises an error" do
FileUtils.touch("mosaik-candidates.csv")

expect { command.validate }.to raise_error MOSAIK::OptionError, "unknown metrics: doesnotexist"
end
end
end
end

0 comments on commit 4d7adef

Please sign in to comment.