<commit_before># This is a report from a SQL View
class SystemPackageRelationGrouped < ActiveRecord::Base
  self.table_name = 'system_package_relation_grouped'

  def to_s
    "#{pkg_name}"
  end

  filterrific(
    default_filter_params: { sorted_by: 'name_asc' },
    available_filters: [
      :sorted_by,
      :sys_search_query,
      :pkg_search_query,
      :with_system_group_id,
      :with_package_group_id
    ]
  )

  scope :pkg_search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 2
    where(
      terms.map {
        or_clauses = [
          "LOWER(pkg_name) LIKE ?",
          "LOWER(pkg_section) LIKE ?"
        ].join(' OR ')
        "(#{ or_clauses })"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }

  scope :sys_search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.


    num_or_conditions = 2
    sys_pkg_rel = SystemPackageRelation.where(
        terms.map {
            or_clauses = [
                "LOWER(sys_name) LIKE ?",
                "LOWER(sys_urn) LIKE ?"
            ].join(' OR ')
            "(#{ or_clauses })"
        }.join(' AND '),
        *terms.map { |e| [e] * num_or_conditions }.flatten
    )

    pkg_ids = []
    sys_pkg_rel.each do | pkg |
      pkg_ids.append(pkg.pkg_id)
    end
    where(:pkg_id => pkg_ids)

  }
  scope :sorted_by, lambda { |sort_option|
    # extract the sort direction from the param value.
    direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
    case sort_option.to_s
      when /^id_/
        order("pkg_id #{ direction }")
      when /^name_/
        order("pkg_name #{ direction }")
      when /^section_/
        order("LOWER(pkg_section) #{ direction }")
      when /^nr_of_systems_/
        order("sys_count #{ direction }")
      else
        raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
    end
  }
  scope :with_system_group_id, lambda { |system_group_ids|
    sys_pkg_rel = SystemPackageRelation.where( :sys_grp_id => [*system_group_ids] )
    pkg_ids = []
    sys_pkg_rel.each do | pkg |
        pkg_ids.append(pkg.pkg_id)
    end
    where(:pkg_id => pkg_ids)
  }
  scope :with_package_group_id, lambda { |package_group_ids|
    assignments = GroupAssignment.where( :package_group_id => [*package_group_ids] ) 
    pkg_ids = []
    assignments.each do | pkg |
        pkg_ids.append(pkg.id)
    end
    where(:pkg_id => pkg_ids)
  }

  def self.options_for_sorted_by
    [
      ['Name (a-z)', 'name_asc']
    ]
  end

  self.per_page = Settings.Pagination.NoOfEntriesPerPage

  protected

  # The report_state_popularities relation is a SQL view,
  # so there is no need to try to edit its records ever.
  # Doing otherwise, will result in an exception being thrown
  # by the database connection.
  def readonly?
    true
  end<commit_msg>show correct number of affected systems<commit_after># This is a report from a SQL View
class SystemPackageRelationGrouped < ActiveRecord::Base
  self.table_name = 'system_package_relation_grouped'

  def to_s
    "#{pkg_name}"
  end

  filterrific(
    default_filter_params: { sorted_by: 'name_asc' },
    available_filters: [
      :sorted_by,
      :sys_search_query,
      :pkg_search_query,
      :with_system_group_id,
      :with_package_group_id
    ]
  )

  scope :pkg_search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 2
    where(
      terms.map {
        or_clauses = [
          "LOWER(pkg_name) LIKE ?",
          "LOWER(pkg_section) LIKE ?"
        ].join(' OR ')
        "(#{ or_clauses })"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }

  scope :sys_search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.


    num_or_conditions = 2
    sys_pkg_rel = SystemPackageRelation.where(
        terms.map {
            or_clauses = [
                "LOWER(sys_name) LIKE ?",
                "LOWER(sys_urn) LIKE ?"
            ].join(' OR ')
            "(#{ or_clauses })"
        }.join(' AND '),
        *terms.map { |e| [e] * num_or_conditions }.flatten
    )

    pkg_ids = []
    sys_pkg_rel.each do | pkg |
      pkg_ids.append(pkg.pkg_id)
    end
    where(:pkg_id => pkg_ids)

  }
  scope :sorted_by, lambda { |sort_option|
    # extract the sort direction from the param value.
    direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
    case sort_option.to_s
      when /^id_/
        order("pkg_id #{ direction }")
      when /^name_/
        order("pkg_name #{ direction }")
      when /^section_/
        order("LOWER(pkg_section) #{ direction }")
      when /^nr_of_systems_/
        order("sys_count #{ direction }")
      else
        raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
    end
  }
  scope :with_system_group_id, lambda { |system_group_ids|
    SystemPackageRelation.select("pkg_id, pkg_name, pkg_section, COUNT(*) as sys_count").where( :sys_grp_id => [*system_group_ids] ).group("pkg_id, pkg_name, pkg_section")
  }

  scope :with_package_group_id, lambda { |package_group_ids|
    assignments = GroupAssignment.where( :package_group_id => [*package_group_ids] ) 
    pkg_ids = []
    assignments.each do | pkg |
        pkg_ids.append(pkg.id)
    end
    where(:pkg_id => pkg_ids)
  }

  def self.options_for_sorted_by
    [
      ['Name (a-z)', 'name_asc']
    ]
  end

  self.per_page = Settings.Pagination.NoOfEntriesPerPage

  protected

  # The report_state_popularities relation is a SQL view,
  # so there is no need to try to edit its records ever.
  # Doing otherwise, will result in an exception being thrown
  # by the database connection.
  def readonly?
    true
  end
{'fraction_non_alphanumeric': 0.12535767815778717, 'fraction_numerical': 0.0005450333832947268, 'mean_word_length': 2.559650824442289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5704784', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2394, 'n_words': 591}