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

Commit

Permalink
Update history collection
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 29, 2024
1 parent d1c0442 commit e12e917
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions lib/mosaik/collectors/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,63 @@ def call
# Open the git repository
git = Git.open(MOSAIK.options.directory, log: ::Logger.new(File::NULL))

# Fetch commits since a specific date or last 1000 commits
commits = options[:since] ? git.log.since(options[:since]) : git.log(1000)
# Fetch commits, limited to the last 1000 commits
commits = git.log(10)

# Create a 2D matrix for each pair of nodes in the graph
matrix = graph.nodes.map { |_| graph.nodes.map { |_| 0 } }
# Limit commits to the load paths
commits = commits.path(MOSAIK.configuration.load_paths.map { |l| File.join(MOSAIK.options.directory, l) })

# Limit commits to a specific date
commits = commits.since(options[:since]) if options[:since]

info "Analyzing #{commits.count} commits"

# Create a nested mapping for each pair of nodes in the graph
matrix = Hash.new { |h, k| h[k] = Hash.new(0) }

# Calculate the aggregated local coupling
commits.each do |commit|
# Get the files for the commit
constants = commit.diff_parent.stats[:files].map do |file|
# Resolve file path to class name
resolver.resolve(file)
files = commit.diff_parent.stats[:files]

# Reject files not in the load paths
files = files
.map { |file, _| File.join(MOSAIK.options.directory, file) }
.select { |file| file.in? MOSAIK.configuration.files }

# Resolve file paths to class name
constants = files.map do |file|
resolver.resolve!(file)
end

debug "Commit #{commit.sha} (#{constants.count} constants: #{constants.join(', ')})"

# Calculate the local coupling
constants
.permutation(2)
.each { |a, b| matrix[graph.nodes.index(a)][graph.nodes.index(b)] += 1 }
.each { |(a, b)| matrix[a][b] += 1 }
end

# For each non-zero element in the matrix, add an edge to the graph
matrix.each_with_index do |row, i|
row.each_with_index do |value, j|
matrix.each do |a, row|
row.each do |b, value|
next if value.zero?

# Add an edge from the node to the receiver
graph.add_edge(graph.nodes[i], graph.nodes[j], label: value)
graph.add_edge(graph.find_node(a), graph.find_node(b), label: value)
end
end
end

private

def resolver
@resolver ||= Resolver.new(
MOSAIK.options.directory,
MOSAIK.configuration.load_paths,
MOSAIK.configuration.overrides,
)
end
end
end
end

0 comments on commit e12e917

Please sign in to comment.