This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.rb
More file actions
275 lines (225 loc) · 9.6 KB
/
Copy pathstats.rb
File metadata and controls
275 lines (225 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Stats
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.first(name: branch_name)
if @options[:by_author]
output_by_author
else
data = collect_stats(branch, branch_name)
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate(data)
else
paginate { output_text(data) }
end
end
end
def collect_stats(branch, branch_name)
ecosystem = @options[:ecosystem]
since_time = @options[:since] ? parse_time(@options[:since]) : nil
until_time = @options[:until] ? parse_time(@options[:until]) : nil
commits = branch ? branch.commits_dataset : Models::Commit.where(false)
commits = commits.where { committed_at >= since_time } if since_time
commits = commits.where { committed_at <= until_time } if until_time
data = {
branch: branch_name,
ecosystem: ecosystem,
since: @options[:since],
until: @options[:until],
commits_analyzed: commits.count,
commits_with_changes: commits.where(has_dependency_changes: true).count,
current_dependencies: {},
changes: {},
most_changed: [],
manifests: []
}
if branch&.last_analyzed_sha
current_commit = Models::Commit.first(sha: branch.last_analyzed_sha)
snapshots = current_commit ? current_commit.dependency_snapshots_dataset : Models::DependencySnapshot.where(false)
snapshots = snapshots.where(ecosystem: ecosystem) if ecosystem
data[:current_dependencies] = {
total: snapshots.count,
by_platform: snapshots.group_and_count(:ecosystem).as_hash(:ecosystem, :count),
by_type: snapshots.group_and_count(:dependency_type).as_hash(:dependency_type, :count)
}
end
changes = Models::DependencyChange.join(:commits, id: :commit_id)
changes = changes.where(Sequel[:dependency_changes][:ecosystem] => ecosystem) if ecosystem
changes = changes.where { Sequel[:commits][:committed_at] >= since_time } if since_time
changes = changes.where { Sequel[:commits][:committed_at] <= until_time } if until_time
data[:changes] = {
total: changes.count,
by_type: changes.group_and_count(Sequel[:dependency_changes][:change_type]).as_hash(:change_type, :count)
}
most_changed = changes
.select(Sequel[:dependency_changes][:name], Sequel[:dependency_changes][:ecosystem])
.select_append { count.function.*.as(:change_count) }
.group(Sequel[:dependency_changes][:name], Sequel[:dependency_changes][:ecosystem])
.order(Sequel.desc(:change_count))
.limit(10)
.all
data[:most_changed] = most_changed.map do |row|
{ name: row[:name], ecosystem: row[:ecosystem], changes: row[:change_count] }
end
manifests = Models::Manifest.dataset
manifests = manifests.where(ecosystem: ecosystem) if ecosystem
manifest_ids = manifests.select_map(:id)
change_counts_query = Models::DependencyChange
.join(:commits, id: :commit_id)
.where(Sequel[:dependency_changes][:manifest_id] => manifest_ids)
change_counts_query = change_counts_query.where { Sequel[:commits][:committed_at] >= since_time } if since_time
change_counts_query = change_counts_query.where { Sequel[:commits][:committed_at] <= until_time } if until_time
change_counts = change_counts_query.group_and_count(Sequel[:dependency_changes][:manifest_id]).as_hash(:manifest_id, :count)
data[:manifests] = manifests.all.map do |manifest|
{ path: manifest.path, ecosystem: manifest.ecosystem, changes: change_counts[manifest.id] || 0 }
end
top_authors = changes
.where(Sequel[:dependency_changes][:change_type] => "added")
.group_and_count(Sequel[:commits][:author_name])
.order(Sequel.desc(:count))
.limit(5)
.as_hash(:author_name, :count)
data[:top_authors] = top_authors.map { |name, count| { name: name, added: count } }
data
end
def output_text(data)
puts "Dependency Statistics"
puts "=" * 40
puts
puts "Branch: #{data[:branch]}"
puts "Ecosystem: #{data[:ecosystem]}" if data[:ecosystem]
puts "Since: #{data[:since]}" if data[:since]
puts "Until: #{data[:until]}" if data[:until]
puts "Commits analyzed: #{data[:commits_analyzed]}"
puts "Commits with changes: #{data[:commits_with_changes]}"
puts
if data[:current_dependencies][:total]
puts "Current Dependencies"
puts "-" * 20
puts "Total: #{data[:current_dependencies][:total]}"
data[:current_dependencies][:by_platform].sort_by { |_, c| -c }.each do |ecosystem, count|
puts " #{ecosystem}: #{count}"
end
by_type = data[:current_dependencies][:by_type]
if by_type.keys.compact.any?
puts
puts "By type:"
by_type.sort_by { |_, c| -c }.each do |type, count|
puts " #{type || 'unknown'}: #{count}"
end
end
end
puts
puts "Dependency Changes"
puts "-" * 20
puts "Total changes: #{data[:changes][:total]}"
data[:changes][:by_type].each do |type, count|
puts " #{type}: #{count}"
end
puts
puts "Most Changed Dependencies"
puts "-" * 25
data[:most_changed].each do |dep|
puts " #{dep[:name]} (#{dep[:ecosystem]}): #{dep[:changes]} changes"
end
puts
puts "Manifest Files"
puts "-" * 14
data[:manifests].each do |m|
puts " #{m[:path]} (#{m[:ecosystem]}): #{m[:changes]} changes"
end
if data[:top_authors]&.any?
puts
puts "Top Authors (by deps added)"
puts "-" * 27
data[:top_authors].each do |author|
puts " #{author[:added].to_s.rjust(4)} #{author[:name]}"
end
end
end
def output_by_author
since_time = @options[:since] ? parse_time(@options[:since]) : nil
until_time = @options[:until] ? parse_time(@options[:until]) : nil
changes = Models::DependencyChange
.join(:commits, id: :commit_id)
.where(Sequel[:dependency_changes][:change_type] => "added")
changes = changes.where(Sequel[:dependency_changes][:ecosystem] => @options[:ecosystem]) if @options[:ecosystem]
changes = changes.where { Sequel[:commits][:committed_at] >= since_time } if since_time
changes = changes.where { Sequel[:commits][:committed_at] <= until_time } if until_time
counts = changes
.group_and_count(Sequel[:commits][:author_name])
.order(Sequel.desc(:count))
.limit(@options[:limit] || 20)
.as_hash(:author_name, :count)
if counts.empty?
empty_result "No dependency additions found"
return
end
if @options[:format] == "json"
require "json"
data = counts.map { |name, count| { author: name, added: count } }
puts JSON.pretty_generate(data)
else
paginate { output_by_author_text(counts) }
end
end
def output_by_author_text(counts)
puts "Dependencies Added by Author"
puts "=" * 40
puts
counts.each do |name, count|
puts " #{count.to_s.rjust(4)} #{name}"
end
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs stats [options]"
opts.on("-b", "--branch=NAME", "Branch to analyze") do |v|
options[:branch] = v
end
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v|
options[:ecosystem] = v
end
opts.on("-f", "--format=FORMAT", "Output format (text, json)") do |v|
options[:format] = v
end
opts.on("--since=DATE", "Show changes after date") do |v|
options[:since] = v
end
opts.on("--until=DATE", "Show changes before date") do |v|
options[:until] = v
end
opts.on("--by-author", "Show dependencies added by author") do
options[:by_author] = true
end
opts.on("-n", "--limit=N", Integer, "Limit results (default: 20)") do |v|
options[:limit] = v
end
opts.on("--no-pager", "Do not pipe output into a pager") do
options[:no_pager] = true
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end