This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
catalog-analyzer.rb
executable file
·131 lines (92 loc) · 2.71 KB
/
catalog-analyzer.rb
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
#!/usr/bin/env ruby
require 'json'
require 'terminal-table'
require 'colored'
require 'set'
class Catalog
attr_reader :name, :environment, :version
def initialize(hash)
@data = hash['data']
@metadata = hash['metadata']
@name = @data['name']
@version = @data['version']
@environment = @data['environment']
end
def resources
@data['resources']
end
def edges
@data['edges']
end
def classes
@data['classes']
end
def resource_type_count
type_map = Hash.new { |h, k| h[k] = 0 }
@data['resources'].each do |resource|
resource_type = resource['type']
type_map[resource_type] += 1
end
type_map
end
def source_edge_count
source_map = Hash.new { |h, k| h[k] = 0 }
@data['edges'].each do |edge|
source_map[edge['source']] += 1
end
source_map
end
def target_edge_count
target_map = Hash.new { |h, k| h[k] = 0 }
@data['edges'].each do |edge|
target_map[edge['target']] += 1
end
target_map
end
end
def tableflip(headings, hash, take = nil)
rows = hash.to_a.sort { |a, b| b[1] <=> a[1] }
if take
rows = rows.take(take)
end
table = Terminal::Table.new(:headings => headings, :rows => rows)
puts table
end
hash = JSON.parse(File.read(ARGV[0]))
catalog = Catalog.new(hash)
puts "--- Catalog ---"
name = catalog.name
environment = catalog.environment
version = catalog.version
puts "Name: #{name.inspect}"
puts "Environment: #{environment.inspect}"
puts "Catalog version: #{version.inspect}"
puts "--- Statistics ---"
puts "Edges: #{catalog.edges.count}"
puts "Resources: #{catalog.resources.count}"
puts "Classes: #{catalog.classes.count}"
puts "--- Resource types: ---"
tableflip(['Resource Type', 'Count'], catalog.resource_type_count)
puts "--- Edges ---"
source_map = catalog.source_edge_count
target_map = catalog.target_edge_count
deleter = lambda { |k, v| v <= 1 }
source_map.delete_if(&deleter)
target_map.delete_if(&deleter)
tableflip(['Heavily depended resources', 'Count'], source_map, 20)
tableflip(['Heavily dependent resources', 'Count'], target_map, 20)
file_map = Hash.new { |h, k| h[k] = [] }
catalog.resources.each do |resource|
file_map[resource['file']] << "#{resource['type']}[#{resource['title']}]" if resource['file']
end
puts "--- Files with the most defined resources ---"
file_rows = file_map.to_a.sort { |a, b| b[1].count <=> a[1].count }
tableflip(['File', 'Resource count'], file_rows.map { |(file, resources)| [file, resources.count] }, 20)
puts "--- Resources per file ---"
file_rows.take(20).map do |(file, resources)|
puts "#{file} " + "(#{resources.count} resources)".yellow
resources.sort.each do |resource|
puts " -- #{resource}"
end
puts "-" * 20
end