<commit_before>class Operations::InstancesController < ApplicationController
  include ::RfcHistory

  NOTIFICATION_RANGES       = %w(day week month)
  NOTIFICATION_RANGE_LENGTH = HashWithIndifferentAccess.new(:day   => 60 * 60 * 24,
                                                            :week  => 60 * 60 * 24 * 7,
                                                            :month => 60 * 60 * 24 * 31)

  before_filter :find_parents, :except => [:state]
  before_filter :find_instance, :except => [:index, :state, :destroy]

  CustomAction = Struct.new(:actionId, :actionName, :description)

  def self.load_custom_actions(component)
    Cms::Relation.all(:params => {:ciId              => component.ciId,
                                  :direction         => 'from',
                                  :relationShortName => 'EscortedBy'}).map(&:toCi).inject([]) do |m, a|
      m << CustomAction.new(a.ciId, 'user-custom-attachment', a.ciName) if a.ciAttributes.run_on.include?('on-demand')
      m
    end
  end

  def index
    component_scope = params[:component_id].present?
    @instances      = []
    @clouds         = {}
    @state          = params[:instances_state]
    deployed_to     = []
    if @state.present?
      if component_scope
        @instances = Cms::DjRelation.all(:params => {:ciId              => @component.ciId,
                                                     :direction         => 'from',
                                                     :relationShortName => 'RealizedAs'}).map(&:toCi)
        deployed_to = Cms::DjRelation.all(:params => {:nsPath            => @instances.first.nsPath,
                                                      :relationShortName => 'DeployedTo'}) if @instances.present?
      else
        deployed_to = Cms::DjRelation.all(:params => {:nsPath            => scope_ns_path,
                                                      :relationShortName => 'DeployedTo',
                                                      :recursive         => true,
                                                      :includeFromCi     => true})
        @instances = deployed_to.map(&:fromCi)
      end

      @clouds = Cms::Ci.all(:params => {:nsPath      => clouds_ns_path,
                                        :ciClassName => 'account.Cloud'}).inject({}) do |h, c|
        h[c.ciId] = c
        h
      end
    end

    if @instances.present?
      if @platform
        pack_ns_path = platform_pack_ns_path(@platform)
        @instances.each { |i| i.add_policy_locations(pack_ns_path) }
      end

      deployed_to_map = deployed_to.inject({}) do |h, rel|
        rel.attributes.delete(:fromCi)   # This is very important not only to reduce the payload size but more importantly to prevent potential cyclic references during json generation in json responder.
        rel.toCi = @clouds[rel.toCiId]
        h[rel.fromCiId] = rel
        h
      end

      @ops_states = @instances.blank? ? {} : Operations::Sensor.states(@instances)
      @instances = @instances.select do |i|
        i.opsState   = @ops_states[i.ciId]
        i.cloud      = deployed_to_map[i.ciId]
        i.deployedTo = deployed_to_map[i.ciId].try(:toCi)

        @state == 'all' || (i.opsState && @state.include?(i.opsState))
      end
    end

    respond_to do |format|
      format.js do
        if @instances.present?
          unless component_scope
            @managed_via_health = Cms::Relation.all(:params => {:nsPath       => scope_ns_path,<commit_msg>Performance improvements.<commit_after>class Operations::InstancesController < ApplicationController
  include ::RfcHistory

  NOTIFICATION_RANGES       = %w(day week month)
  NOTIFICATION_RANGE_LENGTH = HashWithIndifferentAccess.new(:day   => 60 * 60 * 24,
                                                            :week  => 60 * 60 * 24 * 7,
                                                            :month => 60 * 60 * 24 * 31)

  before_filter :find_parents, :except => [:state]
  before_filter :find_instance, :except => [:index, :state, :destroy]

  CustomAction = Struct.new(:actionId, :actionName, :description)

  def self.load_custom_actions(component)
    Cms::Relation.all(:params => {:ciId              => component.ciId,
                                  :direction         => 'from',
                                  :relationShortName => 'EscortedBy'}).map(&:toCi).inject([]) do |m, a|
      m << CustomAction.new(a.ciId, 'user-custom-attachment', a.ciName) if a.ciAttributes.run_on.include?('on-demand')
      m
    end
  end

  def index
    component_scope = params[:component_id].present?
    @instances      = []
    @ops_states     = {}
    @clouds         = {}
    @state          = params[:instances_state]
    deployed_to     = []
    instance_ids    = []

    if @state.present?
      if component_scope
        deployed_to = Cms::Relation.all(:params => {:nsPath            => @component.nsPath.sub('/manifest/', '/bom/'),
                                                    :relationShortName => 'DeployedTo'})
        instance_ids = Cms::Relation.all(:params => {:ciId              => @component.ciId,
                                                     :direction         => 'from',
                                                     :includeToCi       => false,
                                                     :relationShortName => 'RealizedAs'}).map(&:toCiId)
      else
        deployed_to = Cms::Relation.all(:params => {:nsPath            => scope_ns_path,
                                                    :relationShortName => 'DeployedTo',
                                                    :recursive         => true})
        instance_ids = deployed_to.map(&:fromCiId)
      end

      @clouds = Cms::Ci.all(:params => {:nsPath      => clouds_ns_path,
                                        :ciClassName => 'account.Cloud'}).to_map(&:ciId)
    end

    if instance_ids.present?
      deployed_to_map = deployed_to.inject({}) do |h, rel|
        # rel.attributes.delete(:fromCi)   # This is very important not only to reduce the payload size but more importantly to prevent potential cyclic references during json generation in json responder.
        rel.toCi = @clouds[rel.toCiId]
        h[rel.fromCiId] = rel
        h
      end

      @ops_states = Operations::Sensor.states(instance_ids)
      unless @state == 'all'
        instance_ids = instance_ids.select do |i|
          state = @ops_states[i]
          state && @state.include?(state)
        end
      end
      @instances = Cms::Ci.list(instance_ids).map {|i| Cms::Ci.new(i, true)}

      if @platform
        pack_ns_path = platform_pack_ns_path(@platform)
        @instances.each {|i| i.add_policy_locations(pack_ns_path)}
      end

      @instances.each do |i|
        i.opsState   = @ops_states[i.ciId]
        i.cloud      = deployed_to_map[i.ciId]
        i.deployedTo = deployed_to_map[i.ciId].try(:toCi)
      end
    end

    respond_to do |format|
      format.js do
        if @instances.present?
          unless component_scope
            @managed_via_health = Cms::Relation.all(:params => {:nsPath       => scope_ns_path,
{'fraction_non_alphanumeric': 0.1152073732718894, 'fraction_numerical': 0.005865102639296188, 'mean_word_length': 1.6476894639556376, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7385290', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1956, 'n_words': 396}