Sha256: 1eafc51f99d8302e0588716719e9570cc87e3c853f386fed7704a30984e93f23

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module ForemanStatistics
  module Statistics
    class CountHosts < Base
      def calculate
        count_distribution(Host.authorized(:view_hosts, Host), count_by)
      end

      private

      def count_distribution(scope, association)
        case association.to_sym
        when :environment
          klass = ForemanPuppet::Environment
          scope = scope.joins(:puppet)
          grouping = ForemanPuppet::HostPuppetFacet.arel_table[:environment_id]
        else
          klass = association.to_s.camelize.constantize
          grouping = Host::Managed.arel_table["#{association}_id"]
        end

        output = []
        data = scope.reorder('').group(grouping).count
        associations = klass.where(id: data.keys).to_a
        data.each do |k, v|
          output << { label: associations.detect { |a| a.id == k }.to_label, data: v } unless v.zero?
        rescue StandardError => e
          Rails.logger.info "skipped '#{association} - #{k}' as it has has no label"
          Rails.logger.debug e
        end
        output
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_statistics-2.1.0 app/services/foreman_statistics/statistics/count_hosts.rb
foreman_statistics-2.0.1 app/services/foreman_statistics/statistics/count_hosts.rb