<commit_before>class TreeBuilderPolicy < TreeBuilder
  has_kids_for MiqPolicy, [:x_get_tree_po_kids]
  has_kids_for MiqEventDefinition, [:x_get_tree_ev_kids, :parents]

  private

  def tree_init_options(_tree_name)
    {:full_ids => true}
  end

  def set_locals_for_render
    locals = super
    locals.merge!(
      :id_prefix => "po_",
      :autoload  => true,
    )
  end

  def compliance_control_kids(mode)
    text_i18n = {:compliance => {:Host                => N_("Host Compliance Policies"),
                                 :Vm                  => N_("Vm Compliance Policies"),
                                 :ContainerReplicator => N_("Replicator Compliance Policies"),
                                 :ContainerGroup      => N_("Pod Compliance Policies"),
                                 :ContainerNode       => N_("Container Node Compliance Policies"),
                                 :ContainerImage      => N_("Container Image Compliance Policies")},
                 :control    => {:Host                => N_("Host Control Policies"),
                                 :Vm                  => N_("Vm Control Policies"),
                                 :ContainerReplicator => N_("Replicator Control Policies"),
                                 :ContainerGroup      => N_("Pod Control Policies"),
                                 :ContainerNode       => N_("Container Node Control Policies"),
                                 :ContainerImage      => N_("Container Image Control Policies")}}

    MiqPolicyController::UI_FOLDERS.collect do |model|
      text = text_i18n[mode.to_sym][model.name.to_sym]
      {:id    => "#{mode}-#{model.name.camelize(:lower)}",
       :image => model.name.underscore,
       :text  => text,
       :tip   => text}
    end
  end

  # level 0 - root
  def root_options
    [t = _("All Policies"), t]
  end

  # level 1 - compliance & control
  def x_get_tree_roots(count_only, _options)
    objects = []
    objects << {:id => "compliance", :text => N_("Compliance Policies"), :image => "compliance", :tip => N_("Compliance Policies")}
    objects << {:id => "control", :text => N_("Control Policies"), :image => "control", :tip => N_("Control Policies")}

    # Push folder node ids onto open_nodes array
    objects.each { |o| open_node("xx-#{o[:id]}") }

    count_only_or_objects(count_only, objects)
  end

  # level 2 & 3...
  def x_get_tree_custom_kids(parent, count_only, options)
    assert_type(options[:type], :policy)

    # level 2 - host, vm, etc. under compliance/control
    if %w(compliance control).include?(parent[:id])
      mode = parent[:id]
      objects = compliance_control_kids(mode)

      # Push folder node ids onto open_nodes array
      objects.each { |o| open_node("xx-#{mode}_xx-#{o[:id]}") }

      return count_only_or_objects(count_only, objects)
    end

    # level 3 - actual policies
    mode, towhat = parent[:id].split('-')
    towhat = towhat.camelize
    if MiqPolicyController::UI_FOLDERS.collect(&:name).include?(towhat)
      objects = MiqPolicy.where(:mode => mode, :towhat => towhat)

      return count_only_or_objects(count_only, objects, :description)
    end

    # error checking
    super
  end

  # level 4 - conditions & events for policy
  def x_get_tree_po_kids(parent, count_only)
    conditions = count_only_or_objects(count_only, parent.conditions, :description)
    miq_events = count_only_or_objects(count_only, parent.miq_event_definitions, :description)
    conditions + miq_events
  end

  # level 5 - actions under events
  def x_get_tree_ev_kids(parent, count_only, parents)
    # the policy from level 3
    pol_rec = node_by_tree_id(parents.last)

    success = count_only_or_objects(count_only, pol_rec ? pol_rec.actions_for_event(parent, :success) : [])
    failure = count_only_or_objects(count_only, pol_rec ? pol_rec.actions_for_event(parent, :failure) : [])<commit_msg>TreeBuilderPolicy - only expand compliance & control, not hosts<commit_after>class TreeBuilderPolicy < TreeBuilder
  has_kids_for MiqPolicy, [:x_get_tree_po_kids]
  has_kids_for MiqEventDefinition, [:x_get_tree_ev_kids, :parents]

  private

  def tree_init_options(_tree_name)
    {:full_ids => true}
  end

  def set_locals_for_render
    locals = super
    locals.merge!(
      :id_prefix => "po_",
      :autoload  => true,
    )
  end

  def compliance_control_kids(mode)
    text_i18n = {:compliance => {:Host                => N_("Host Compliance Policies"),
                                 :Vm                  => N_("Vm Compliance Policies"),
                                 :ContainerReplicator => N_("Replicator Compliance Policies"),
                                 :ContainerGroup      => N_("Pod Compliance Policies"),
                                 :ContainerNode       => N_("Container Node Compliance Policies"),
                                 :ContainerImage      => N_("Container Image Compliance Policies")},
                 :control    => {:Host                => N_("Host Control Policies"),
                                 :Vm                  => N_("Vm Control Policies"),
                                 :ContainerReplicator => N_("Replicator Control Policies"),
                                 :ContainerGroup      => N_("Pod Control Policies"),
                                 :ContainerNode       => N_("Container Node Control Policies"),
                                 :ContainerImage      => N_("Container Image Control Policies")}}

    MiqPolicyController::UI_FOLDERS.collect do |model|
      text = text_i18n[mode.to_sym][model.name.to_sym]
      {:id    => "#{mode}-#{model.name.camelize(:lower)}",
       :image => model.name.underscore,
       :text  => text,
       :tip   => text}
    end
  end

  # level 0 - root
  def root_options
    [t = _("All Policies"), t]
  end

  # level 1 - compliance & control
  def x_get_tree_roots(count_only, _options)
    objects = []
    objects << {:id => "compliance", :text => N_("Compliance Policies"), :image => "compliance", :tip => N_("Compliance Policies")}
    objects << {:id => "control", :text => N_("Control Policies"), :image => "control", :tip => N_("Control Policies")}

    # Push folder node ids onto open_nodes array
    objects.each { |o| open_node("xx-#{o[:id]}") }

    count_only_or_objects(count_only, objects)
  end

  # level 2 & 3...
  def x_get_tree_custom_kids(parent, count_only, options)
    assert_type(options[:type], :policy)

    # level 2 - host, vm, etc. under compliance/control
    if %w(compliance control).include?(parent[:id])
      mode = parent[:id]
      objects = compliance_control_kids(mode)

      return count_only_or_objects(count_only, objects)
    end

    # level 3 - actual policies
    mode, towhat = parent[:id].split('-')
    towhat = towhat.camelize
    if MiqPolicyController::UI_FOLDERS.collect(&:name).include?(towhat)
      objects = MiqPolicy.where(:mode => mode, :towhat => towhat)

      return count_only_or_objects(count_only, objects, :description)
    end

    # error checking
    super
  end

  # level 4 - conditions & events for policy
  def x_get_tree_po_kids(parent, count_only)
    conditions = count_only_or_objects(count_only, parent.conditions, :description)
    miq_events = count_only_or_objects(count_only, parent.miq_event_definitions, :description)
    conditions + miq_events
  end

  # level 5 - actions under events
  def x_get_tree_ev_kids(parent, count_only, parents)
    # the policy from level 3
    pol_rec = node_by_tree_id(parents.last)

    success = count_only_or_objects(count_only, pol_rec ? pol_rec.actions_for_event(parent, :success) : [])
    failure = count_only_or_objects(count_only, pol_rec ? pol_rec.actions_for_event(parent, :failure) : [])
{'fraction_non_alphanumeric': 0.11582213029989659, 'fraction_numerical': 0.003360910031023785, 'mean_word_length': 2.5120290512936903, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4316172', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2300, 'n_words': 514}