foreman_statistics 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b95468f6ef7bf5f7cc61869bd23e46bf7a64331aa2d162962178b7c186df6f8a
4
- data.tar.gz: 62baddb120c01c609e1e8a62d30e785a8ee12c3dcdbe4591468822d1fe464d6e
3
+ metadata.gz: ebe6e6bb3aad64251f7d191f3bc2cfcd6adca51bdffcb85901957a9074cff7e3
4
+ data.tar.gz: d4f425260d58b12cc0888a1b20cea07a4294f1a19cdcd0979ca6a7f2b4927797
5
5
  SHA512:
6
- metadata.gz: 455324c4b0fcb964e4db90d36da2c0ccf74323298bac57def3197af5b71e12c80cc7b835e16262cd7fa27d7bd96d15e8ca0f9ac454299835367863c3c5fb30bc
7
- data.tar.gz: 66f4bcf63408b04889e53ed846753bd34e5e3c83ecb17fdd9694181a5aa7322ef12b68ba26378aa6a21d3b6a2ae1195a1a6c52e506e715a4f5d18a3b7db97d96
6
+ metadata.gz: 379d5362616632051dc1e6ac7e9b0919898cf59e5d90ddc4f832876469a6c52ae25a2b4e8aa8f71c409d969b873739342c517b53ad28b286fe6568beeae041af
7
+ data.tar.gz: 255b4e780195c26a59bdf7fff704df68a6ee0bdea94b0859857d91f5147562490eefa97032724bc14a3c6e2ba862574970ae71360573aa7a422e0c1f8c73d26f
@@ -11,10 +11,12 @@ module ForemanStatistics
11
11
 
12
12
  api :GET, '/statistics/', N_('Get statistics')
13
13
  def index
14
- @os_count = Host.authorized(:view_hosts).count_distribution :operatingsystem
15
- @arch_count = Host.authorized(:view_hosts).count_distribution :architecture
16
- @env_count = Host.authorized(:view_hosts).count_distribution :environment
17
- @klass_count = Host.authorized(:view_hosts).count_habtm 'puppetclass'
14
+ @os_count = Statistics::CountHosts.new(count_by: :operatingsystem)
15
+ @arch_count = Statistics::CountHosts.new(count_by: :architecture)
16
+ if defined?(ForemanPuppet)
17
+ @env_count = Statistics::CountHosts.new(count_by: :environment)
18
+ @klass_count = Statistics::CountPuppetClasses.new(id: :puppetclass)
19
+ end
18
20
  @cpu_count = FactValue.authorized(:view_facts).my_facts.count_each 'processorcount'
19
21
  @model_count = FactValue.authorized(:view_facts).my_facts.count_each 'manufacturer'
20
22
  @mem_size = FactValue.authorized(:view_facts).my_facts.mem_average 'memorysize'
@@ -23,8 +25,8 @@ module ForemanStatistics
23
25
  @swap_free = FactValue.authorized(:view_facts).my_facts.mem_average 'swapfree'
24
26
  @mem_totsize = FactValue.authorized(:view_facts).my_facts.mem_sum 'memorysize'
25
27
  @mem_totfree = FactValue.authorized(:view_facts).my_facts.mem_sum 'memoryfree'
26
- render :json => { :os_count => @os_count, :arch_count => @arch_count, :swap_size => @swap_size,
27
- :env_count => @env_count, :klass_count => @klass_count, :cpu_count => @cpu_count,
28
+ render :json => { :os_count => @os_count.calculate, :arch_count => @arch_count.calculate, :swap_size => @swap_size,
29
+ :env_count => @env_count&.calculate, :klass_count => @klass_count&.calculate, :cpu_count => @cpu_count,
28
30
  :model_count => @model_count, :mem_size => @mem_size, :mem_free => @mem_free,
29
31
  :swap_free => @swap_free, :mem_totsize => @mem_totsize, :mem_totfree => @mem_totfree }
30
32
  end
@@ -2,7 +2,31 @@ module ForemanStatistics
2
2
  module Statistics
3
3
  class CountHosts < Base
4
4
  def calculate
5
- Host.authorized(:view_hosts, Host).count_distribution(count_by)
5
+ count_distribution(Host.authorized(:view_hosts, Host), count_by)
6
+ end
7
+
8
+ private
9
+
10
+ def count_distribution(scope, association)
11
+ case association.to_sym
12
+ when :environment
13
+ klass = ForemanPuppet::Environment
14
+ scope = scope.joins(:puppet)
15
+ grouping = ForemanPuppet::HostPuppetFacet.arel_table[:environment_id]
16
+ else
17
+ klass = association.to_s.camelize.constantize
18
+ grouping = Host::Managed.arel_table["#{association}_id"]
19
+ end
20
+
21
+ output = []
22
+ data = scope.reorder('').group(grouping).count
23
+ associations = klass.find_by(id: data.keys)
24
+ data.each do |k, v|
25
+ output << { label: associations.detect { |a| a.id == k }.to_label, data: v } unless v.zero?
26
+ rescue StandardError
27
+ logger.info "skipped #{k} as it has has no label"
28
+ end
29
+ output
6
30
  end
7
31
  end
8
32
  end
@@ -9,7 +9,7 @@ module ForemanStatistics
9
9
  end
10
10
 
11
11
  def calculate
12
- Puppetclass.authorized(:view_puppetclasses).map do |pc|
12
+ ForemanPuppet::Puppetclass.authorized(:view_puppetclasses).map do |pc|
13
13
  count = pc.hosts_count
14
14
  next if count.zero?
15
15
  {
@@ -4,17 +4,19 @@ module ForemanStatistics
4
4
  charts = [
5
5
  CountHosts.new(:count_by => :operatingsystem, :title => 'OS Distribution', :search => 'os_title=~VAL~', :organization_id => org_id, :location_id => loc_id),
6
6
  CountHosts.new(:count_by => :architecture, :title => _('Architecture Distribution'), :search => 'facts.architecture=~VAL~', :organization_id => org_id, :location_id => loc_id),
7
- CountHosts.new(:count_by => :environment, :title => _('Environment Distribution'), :search => 'environment=~VAL~', :organization_id => org_id, :location_id => loc_id),
8
7
  CountHosts.new(:count_by => :hostgroup, :title => _('Host Group Distribution'), :search => 'hostgroup_title=~VAL~', :organization_id => org_id, :location_id => loc_id),
9
8
  CountHosts.new(:count_by => :compute_resource, :title => _('Compute Resource Distribution'), :search => 'compute_resource=~VAL~', :organization_id => org_id, :location_id => loc_id),
10
9
  CountFacts.new(:count_by => :processorcount, :unit => Nn_('%s core', '%s cores'), :title => _('Number of CPUs'), :search => 'facts.processorcount=~VAL1~', :organization_id => org_id, :location_id => loc_id),
11
10
  CountFacts.new(:count_by => :manufacturer, :title => _('Hardware'), :search => 'facts.manufacturer~~VAL~', :organization_id => org_id, :location_id => loc_id),
12
11
  CountNumericalFactPair.new(:count_by => :memory, :title => _('Average Memory Usage'), :organization_id => org_id, :location_id => loc_id),
13
12
  CountNumericalFactPair.new(:count_by => :swap, :title => _('Average Swap Usage'), :organization_id => org_id, :location_id => loc_id),
14
- CountPuppetClasses.new(:id => :puppetclass, :title => _('Class Distribution'), :search => 'class=~VAL1~', :organization_id => org_id, :location_id => loc_id),
15
13
  CountHosts.new(:count_by => :location, :title => _('Location Distribution'), :search => 'location=~VAL~', :organization_id => org_id, :location_id => loc_id),
16
14
  CountHosts.new(:count_by => :organization, :title => _('Organization Distribution'), :search => 'organization=~VAL~', :organization_id => org_id, :location_id => loc_id)
17
15
  ]
16
+ if defined?(ForemanPuppet)
17
+ charts << CountHosts.new(:count_by => :environment, :title => _('Environment Distribution'), :search => 'environment=~VAL~', :organization_id => org_id, :location_id => loc_id)
18
+ charts << CountPuppetClasses.new(:id => :puppetclass, :title => _('Class Distribution'), :search => 'class=~VAL1~', :organization_id => org_id, :location_id => loc_id)
19
+ end
18
20
  charts
19
21
  end
20
22
  end
@@ -19,7 +19,7 @@ module ForemanStatistics
19
19
 
20
20
  initializer 'foreman_statistics.register_plugin', :before => :finisher_hook do |_app|
21
21
  Foreman::Plugin.register :foreman_statistics do
22
- requires_foreman '>= 2.1.0'
22
+ requires_foreman '>= 3.1.0'
23
23
 
24
24
  # ==== Core cleanups
25
25
  # TODO: clean up when this gets removed from core
@@ -70,6 +70,17 @@ module ForemanStatistics
70
70
  :engine => ForemanStatistics::Engine, :parent => :monitor_menu, :after => :trends,
71
71
  :url_hash => { :controller => 'foreman_statistics/statistics', :action => :index }
72
72
  }
73
+
74
+ settings do
75
+ category(:general) do
76
+ setting('max_trend',
77
+ type: :integer,
78
+ default: 30,
79
+ description: N_('Max days for Trends graphs'),
80
+ full_name: N_('Max trends'),
81
+ validate: { numericality: { greater_than: 0 } })
82
+ end
83
+ end
73
84
  end
74
85
  end
75
86
 
@@ -91,15 +102,7 @@ module ForemanStatistics
91
102
  ::Hostgroup.include ForemanStatistics::HasManyTrends
92
103
  ::Model.include ForemanStatistics::HasManyTrends
93
104
  ::Operatingsystem.include ForemanStatistics::HasManyTrends
94
- '::Environment'.safe_constantize&.include ForemanStatistics::HasManyTrends
95
105
  'ForemanPuppet::Environment'.safe_constantize&.include ForemanStatistics::HasManyTrends
96
- ::Setting.include ForemanStatistics::SettingDecorations
97
- ::Setting::General.prepend ForemanStatistics::GeneralSettingDecorations
98
- begin
99
- ::Setting::General.load_defaults
100
- rescue ActiveRecord::NoDatabaseError => e
101
- Rails.logger.warn e
102
- end
103
106
  end
104
107
 
105
108
  rake_tasks do
@@ -1,3 +1,3 @@
1
1
  module ForemanStatistics
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -4,7 +4,6 @@ module ForemanStatistics
4
4
  class TrendsControllerTest < ActionController::TestCase
5
5
  setup do
6
6
  @routes = ForemanStatistics::Engine.routes
7
- Setting::General.create(:name => 'max_trend', :default => 30, :description => 'Max days for Trends graphs')
8
7
  end
9
8
 
10
9
  let(:os_trend) { FactoryBot.create(:foreman_statistics_trend_os) }
@@ -55,14 +55,18 @@ module ForemanStatistics
55
55
  assert_kind_of Array, stat.calculate
56
56
  end
57
57
 
58
- test 'it should fail to calculate a puppet class counter statistics object without an id' do
59
- assert_raise(ArgumentError) { Statistics::CountPuppetClasses.new }
60
- end
58
+ context 'with puppet plugin' do
59
+ test 'it should fail to calculate a puppet class counter statistics object without an id' do
60
+ skip('no puppet plugin') unless Foreman::Plugin.find(:foreman_puppet)
61
+ assert_raise(ArgumentError) { Statistics::CountPuppetClasses.new }
62
+ end
61
63
 
62
- test 'it should initialize a puppet class counter statistics object' do
63
- stat = Statistics::CountPuppetClasses.new(:id => :classes)
64
- assert_equal '/foreman_statistics/statistics/classes', stat.url
65
- assert_kind_of Array, stat.calculate
64
+ test 'it should initialize a puppet class counter statistics object' do
65
+ skip('no puppet plugin') unless Foreman::Plugin.find(:foreman_puppet)
66
+ stat = Statistics::CountPuppetClasses.new(:id => :classes)
67
+ assert_equal '/foreman_statistics/statistics/classes', stat.url
68
+ assert_kind_of Array, stat.calculate
69
+ end
66
70
  end
67
71
 
68
72
  test 'it should fail to calculate a Fact class counter statistics object without count_by' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondrej Ezr
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-16 00:00:00.000000000 Z
11
+ date: 2021-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -42,9 +42,7 @@ files:
42
42
  - app/controllers/foreman_statistics/trends_controller.rb
43
43
  - app/helpers/foreman_statistics/trends_helper.rb
44
44
  - app/jobs/foreman_statistics/trend_counter_job.rb
45
- - app/models/concerns/foreman_statistics/general_setting_decorations.rb
46
45
  - app/models/concerns/foreman_statistics/has_many_trends.rb
47
- - app/models/concerns/foreman_statistics/setting_decorations.rb
48
46
  - app/models/foreman_statistics/fact_trend.rb
49
47
  - app/models/foreman_statistics/foreman_trend.rb
50
48
  - app/models/foreman_statistics/trend.rb
@@ -208,7 +206,7 @@ homepage: https://theforeman.org
208
206
  licenses:
209
207
  - GPL-3.0
210
208
  metadata: {}
211
- post_install_message:
209
+ post_install_message:
212
210
  rdoc_options: []
213
211
  require_paths:
214
212
  - lib
@@ -224,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
222
  version: '0'
225
223
  requirements: []
226
224
  rubygems_version: 3.1.6
227
- signing_key:
225
+ signing_key:
228
226
  specification_version: 4
229
227
  summary: Add Statistics and Trends.
230
228
  test_files:
@@ -1,17 +0,0 @@
1
- module ForemanStatistics
2
- module GeneralSettingDecorations
3
- def self.prepended(base)
4
- class << base
5
- prepend ClassMethodsPrepend
6
- end
7
- end
8
-
9
- module ClassMethodsPrepend
10
- def default_settings
11
- super.concat([
12
- set('max_trend', N_('Max days for Trends graphs'), 30, N_('Max trends'))
13
- ])
14
- end
15
- end
16
- end
17
- end
@@ -1,9 +0,0 @@
1
- module ForemanStatistics
2
- module SettingDecorations
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- Setting::NONZERO_ATTRS.push('max_trend')
7
- end
8
- end
9
- end