Class: Dropsonde::Metrics::Modules

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde/metrics/modules.rb

Overview

modules plugin

Class Method Summary collapse

Class Method Details

.cleanupObject



223
224
225
# File 'lib/dropsonde/metrics/modules.rb', line 223

def self.cleanup
  # run just after generating this metric
end

.descriptionObject



12
13
14
15
16
17
# File 'lib/dropsonde/metrics/modules.rb', line 12

def self.description
  <<~DESCRIPTION
    This group of metrics exports name & version information about the public
    modules installed in all environments, ignoring private modules.
  DESCRIPTION
end

.exampleObject



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
# File 'lib/dropsonde/metrics/modules.rb', line 189

def self.example
  # this method is used to generate a table filled with randomized data to
  # make it easier to write data aggregation queries without access to the
  # actual private data that users have submitted.

  versions = ['1.3.2', '0.0.1', '0.1.2', '1.0.0', '3.0.2', '7.10', '6.1.0', '2.1.0', '1.4.0']
  classes = ['', '::Config', '::Service', '::Server', '::Client', '::Packages']
  [
    modules: Dropsonde::Cache.modules
                             .sample(rand(100))
                             .map do |item|
               {
                 name: item.split('-').last,
                 slug: item,
                 version: versions.sample,
               }
             end,
    classes: Dropsonde::Cache.modules
                             .sample(rand(500))
                             .map do |item|
               {
                 name: item.split('-').last.capitalize + classes.sample,
                 count: rand(750),
               }
             end,
    unused_modules: Dropsonde::Cache.modules
                                    .sample(rand(500))
                                    .map { |item| item.split('-').last },
    unused_classes: Dropsonde::Cache.modules
                                    .sample(rand(500))
                                    .map { |item| item.split('-').last.capitalize + classes.sample },
  ]
end

.initialize_modulesObject



5
6
7
8
9
10
# File 'lib/dropsonde/metrics/modules.rb', line 5

def self.initialize_modules
  # require any libraries needed here -- no need to load puppet; it's already initialized
  # All plugins are initialized before any metrics are generated.
  require 'puppet/info_service'
  require 'puppet/info_service/class_information_service'
end

.run(puppetdb_session = nil) ⇒ Object



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
# File 'lib/dropsonde/metrics/modules.rb', line 116

def self.run(puppetdb_session = nil)
  # return an array of hashes representing the data to be merged into the combined checkin
  environments = Puppet.lookup(:environments).list.map { |e| e.name }
  modules = environments.map { |env|
    Puppet.lookup(:environments).get(env).modules.map do |mod|
      next unless mod.forge_module?

      {
        name: mod.name,
        slug: mod.forge_slug,
        version: mod.version,
      }
    end
  }.flatten.compact.uniq

  if puppetdb_session
    # classes and how many nodes they're enforced on
    results = puppetdb_session.puppet_db.request('', 'resources[type, title] { type = "Class" }').data

    # select only classes from public modules.
    # Use uniq to reduce the iteration over very large datasets
    classes = results.uniq.map { |klass|
      title   = klass['title']
      modname = title.split('::').first.downcase
      next unless modules.find { |mod| mod[:name] == modname }

      {
        name: title,
        count: results.count { |row| row['title'] == title },
      }
    }.compact

    # now lets get a list of all classes so we can identify which are unused
    infoservice = Puppet::InfoService::ClassInformationService.new
    env_hash = {}
    environments.each do |env|
      manifests = Puppet.lookup(:environments).get(env).modules.reduce([]) do |acc, mod|
        next acc unless mod.forge_module?

        acc.concat mod.all_manifests
      end
      env_hash[env] = manifests
    end

    klasses_per_env = infoservice.classes_per_environment(env_hash)

    installed_classes = klasses_per_env.reduce([]) do |klasses, (_key, env)|
      names = env.reduce([]) do |acc, (_file, contents)|
        acc.concat(contents[:classes].map { |c| c[:name] })
      end

      klasses.concat names
    end

    unused_modules = installed_classes.map { |c| c.split('::').first }.sort.uniq
    classes.each { |c| unused_modules.delete(c[:name].split('::').first.downcase) }

    unused_classes = installed_classes.dup
    classes.each { |c| unused_classes.delete(c[:name].downcase) }
  else
    classes = []
    unused_modules  = []
    unused_classes  = []
  end

  [
    { modules: modules },
    { classes: classes },
    { unused_modules: unused_modules },
    { unused_classes: unused_classes },
  ]
end

.schemaObject



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
# File 'lib/dropsonde/metrics/modules.rb', line 19

def self.schema
  # return an array of hashes of a partial schema to be merged into the complete schema
  # See https://cloud.google.com/bigquery/docs/schemas#specifying_a_json_schema_file
  [
    {
      "fields": [
        {
          "description": 'The module name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
        {
          "description": 'The module slug (author-name)',
          "mode": 'NULLABLE',
          "name": 'slug',
          "type": 'STRING',
        },
        {
          "description": 'The module version',
          "mode": 'NULLABLE',
          "name": 'version',
          "type": 'STRING',
        },
      ],
      "description": 'List of modules in all environments.',
      "mode": 'REPEATED',
      "name": 'modules',
      "type": 'RECORD',
    },
    {
      "fields": [
        {
          "description": 'The class name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
        {
          "description": 'How many nodes it is declared on',
          "mode": 'NULLABLE',
          "name": 'count',
          "type": 'INTEGER',
        },
      ],
      "description": 'List of classes and counts in all environments.',
      "mode": 'REPEATED',
      "name": 'classes',
      "type": 'RECORD',
    },
    {
      "fields": [
        {
          "description": 'The module name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
        {
          "description": 'The module slug (author-name)',
          "mode": 'NULLABLE',
          "name": 'slug',
          "type": 'STRING',
        },
        {
          "description": 'The module version',
          "mode": 'NULLABLE',
          "name": 'version',
          "type": 'STRING',
        },
      ],
      "description": 'List of modules whose classes are not declared in any environments.',
      "mode": 'REPEATED',
      "name": 'unused_modules',
      "type": 'RECORD',
    },
    {
      "fields": [
        {
          "description": 'The class name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
      ],
      "description": 'List of unused classes in all environments.',
      "mode": 'REPEATED',
      "name": 'unused_classes',
      "type": 'RECORD',
    },
  ]
end

.setupObject



112
113
114
# File 'lib/dropsonde/metrics/modules.rb', line 112

def self.setup
  # run just before generating this metric
end