Module: RhCloudHost::ClassMethods

Defined in:
app/models/concerns/rh_cloud_host.rb

Instance Method Summary collapse

Instance Method Details

#search_by_insights_recommendations_count(_key, operator, value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/concerns/rh_cloud_host.rb', line 50

def search_by_insights_recommendations_count(_key, operator, value)
  if ForemanRhCloud.with_iop_smart_proxy?
    raise ScopedSearch::QueryNotSupported.new(
      _('Searching by recommendations count is not available in IoP mode.')
    )
  end

  unless ScopedSearch::QueryBuilder::SQL_OPERATORS.value?(operator)
    raise ScopedSearch::QueryNotSupported.new(
      _('Unsupported operator for recommendations count search: %s') % operator
    )
  end

  facet_table = InsightsFacet.table_name
  hits_count_col = "COALESCE(#{facet_table}.hits_count, 0)"

  if ['IN', 'NOT IN'].include?(operator)
    values = value.is_a?(Array) ? value : value.to_s.split(',').map(&:strip)
    placeholders = (['?'] * values.size).join(',')
    condition = sanitize_sql_for_conditions(
      ["#{hits_count_col} #{operator} (#{placeholders})", *values]
    )
  else
    condition = sanitize_sql_for_conditions(
      ["#{hits_count_col} #{operator} ?", value_to_sql(operator, value)]
    )
  end

  {
    joins: "LEFT JOIN #{facet_table} ON #{facet_table}.host_id = #{Host::Managed.table_name}.id",
    conditions: condition,
  }
end

#search_by_insights_uuid(_key, operator, value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/concerns/rh_cloud_host.rb', line 84

def search_by_insights_uuid(_key, operator, value)
  # Determine which facet table to search based on IoP mode
  facet_table = ForemanRhCloud.with_iop_smart_proxy? ? Katello::Host::SubscriptionFacet.table_name : InsightsFacet.table_name

  # Build SQL condition
  if ['IN', 'NOT IN'].include?(operator)
    # For IN/NOT IN, value may be an array or comma-separated string
    # Convert to array and build placeholders for each value
    values = value.is_a?(Array) ? value : value.to_s.split(',').map(&:strip)
    placeholders = (['?'] * values.size).join(',')
    condition = sanitize_sql_for_conditions(
      ["#{facet_table}.uuid #{operator} (#{placeholders})", *values]
    )
  else
    # For other operators (=, !=, LIKE, etc.), use value_to_sql for proper SQL formatting
    condition = sanitize_sql_for_conditions(
      ["#{facet_table}.uuid #{operator} ?", value_to_sql(operator, value)]
    )
  end

  # Return search parameters with LEFT JOIN to include hosts without facets
  {
    joins: "LEFT JOIN #{facet_table} ON #{facet_table}.host_id = #{Host::Managed.table_name}.id",
    conditions: condition,
  }
end