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

Commit

Permalink
Add evaluate command
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 13, 2024
1 parent 1aed2ba commit 1fac5ef
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lib/mosaik/commands/evaluate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module MOSAIK
module Commands
##
# Evaluate microservice candidates
#
class Evaluate < Command
self.description = "Evaluate microservice candidates"

defaults file: "mosaik-candidates.csv"

argument "--file FILE", "File for the identified microservice candidates graph (default: #{defaults[:file]})"

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

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

# Evaluate modularity
Metrics::Modularity
.new(options, graph)
.evaluate

# Print the clusters
graph.clusters.each_value do |cluster|
info "Cluster #{cluster.id} (modularity: #{cluster.attributes[:modularity]})"

next unless options[:debug]

debug "Components: #{cluster.vertices.map(&:id).join(', ')}"
end

file = "#{File.basename(options[:file], '.*')}-evaluation"

# Write graph to file
File.write("#{file}.csv", graph.to_csv)

info "Dependency graph written to #{options[:file]}"

return unless options[:visualize]

# Write visualization to file
debug graph.to_dot
graph.to_png(file)

info "Evaluated microservice candidate graph written to #{file}.dot and rendered to #{file}.png"
end

private

def graph
@graph ||= Graph::Graph.from_csv(File.read(options[:file]))
end
end
end
end
23 changes: 23 additions & 0 deletions spec/mosaik/commands/evaluate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.describe MOSAIK::Commands::Evaluate do
subject(:command) { build(:evaluate_command, arguments:) }

let(:arguments) { [] }

describe "#validate" do
it "does not raise an error" do
FileUtils.touch("mosaik-candidates.csv")

expect { command.validate }.not_to raise_error
end

describe "--file" do
let(:arguments) { ["--file", "doesnotexist.csv"] }

it "raises an error" do
expect { command.validate }.to raise_error MOSAIK::OptionError, "file not found: doesnotexist.csv"
end
end
end
end
2 changes: 2 additions & 0 deletions spec/support/factories/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

factory :identify_command, parent: :command, class: "MOSAIK::Commands::Identify"

factory :evaluate_command, parent: :command, class: "MOSAIK::Commands::Evaluate"

factory :init_command, parent: :command, class: "MOSAIK::Commands::Init"

factory :version_command, parent: :command, class: "MOSAIK::Commands::Version"
Expand Down

0 comments on commit 1fac5ef

Please sign in to comment.