Class: RubyLens::ArtModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylens/art_model_builder.rb

Constant Summary collapse

SIGNAL_FIELDS =
%w[ancestorDepth definitionSites reopenings descendants references members].freeze

Instance Method Summary collapse

Constructor Details

#initialize(seed: 0x51A7_E11A) ⇒ ArtModelBuilder

Returns a new instance of ArtModelBuilder.



7
8
9
# File 'lib/rubylens/art_model_builder.rb', line 7

def initialize(seed: 0x51A7_E11A)
  @seed = seed
end

Instance Method Details

#build(snapshot) ⇒ Object



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
# File 'lib/rubylens/art_model_builder.rb', line 11

def build(snapshot)
  random = Random.new(@seed)
  namespace_order = (0...snapshot.fetch("namespaces").length).to_a.shuffle(random: random)
  namespaces = namespace_order.map do |index|
    row = snapshot.fetch("namespaces").fetch(index)
    [random.rand(0..0xffff_ffff), *row]
  end
  namespace_names = namespace_order.map { |index| snapshot.fetch("namespace_names").fetch(index) }
  package_order = (0...snapshot.fetch("packages").length).to_a.shuffle(random: random)
  package_index = package_order.each_with_index.to_h
  packages = package_order.map do |old_index|
    package = snapshot.fetch("packages").fetch(old_index)
    declaration_count = package.fetch("declaration_count") { package.fetch("declarations").length }
    [
      random.rand(0..0xffff_ffff),
      package.fetch("role"),
      package.fetch("location"),
      declaration_count,
      *package.fetch("ruby_counts"),
    ]
  end
  package_names = package_order.map { |old_index| snapshot.fetch("packages").fetch(old_index).fetch("name") }
  indexed_dependency_count = snapshot.fetch("packages").sum do |package|
    package.fetch("declaration_count") { package.fetch("declarations").length }
  end
  render_target = [18_000, indexed_dependency_count].min
  dependencies = []
  package_order.each do |old_index|
    package = snapshot.fetch("packages").fetch(old_index)
    declarations = package.fetch("declarations").shuffle(random: random)
    quota = if package.key?("declaration_count")
      declarations.length
    elsif declarations.empty?
      0
    else
      [declarations.length, [1, declarations.length * render_target / [indexed_dependency_count, 1].max].max].min
    end
    declarations.first(quota).each do |declaration|
      dependencies << [
        random.rand(0..0xffff_ffff),
        package_index.fetch(old_index),
        *declaration.drop(1),
      ]
    end
  end
  {
    "schema" => "rubylens.art.v7",
    "projectName" => snapshot.fetch("project_name"),
    "totals" => {
      "namespaces" => namespaces.length,
      "packages" => packages.length,
      "dependencyStars" => indexed_dependency_count,
      "renderedDependencyStars" => dependencies.length,
    },
    "domains" => signal_domains(namespaces, dependencies, snapshot["dependency_signal_maxima"]),
    "componentCounts" => snapshot.fetch("components"),
    "categoryStats" => snapshot.fetch("category_stats"),
    "namespaceNames" => namespace_names,
    "namespaces" => namespaces,
    "packageNames" => package_names,
    "packages" => packages,
    "dependencyStars" => dependencies,
    "warningCounts" => snapshot.fetch("warning_counts"),
  }
end