/
pool.rb
173 lines (152 loc) · 5.32 KB
/
pool.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# Copyright 2012 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
require 'util/search'
class Pool < ActiveRecord::Base
include Glue::Candlepin::Pool
set_table_name "pools"
has_many :key_pools, :foreign_key => "pool_id", :dependent => :destroy
has_many :activation_keys, :through => :key_pools
# ActivationKey includes the Pool's json in its own'
def as_json(*args)
{:cp_id => self.cp_id}
end
# Most ActiveRecord models that need to be indexed do so through including IndexedModel. Since not all Pool
# objects are persisted, this could lead to confusion and unnecessary overhead. (Only Pools referenced by
# ActivationKeys are stored.) The methods below are the infrastructure for indexing the Pool objects.
def self.display_attributes
['name', 'sla', 'start', 'end', 'consumed', 'product', 'account', 'contract', 'virtual']
end
def index_options
{
"_type" => :pool,
"id" => @cp_id,
"name" => @product_name,
"name_sort" => @product_name,
"product_name"=> @product_name,
"start" => @start_date,
"end" => @end_date,
"product" => @product_id,
"product_id"=> @product_id,
"account" => @account_number,
"contract" => @contract_number,
"sla" => @support_level,
"support_level"=> @support_level,
"virtual" => @virt_only,
"org" => @owner["key"],
"consumed" => @consumed,
"quantity" => @quantity,
"pool_derived" => @pool_derived,
"derived" => @pool_derived,
"provider_id"=> provider_id,
"stacking_id" => @stacking_id,
"multi_entitlement" => @multi_entitlement
}
end
def self.index_mapping
{
:pool => {
:properties => {
:name => {:type=>'string', :analyzer=>:kt_name_analyzer},
:name_sort => {:type=>'string', :index=>:not_analyzed},
:product_name => {:type=>'string', :index=>:not_analyzed},
:all => {:type=>'string'},
:begin => {:type=>'date'},
:end => {:type=>'date'},
:sockets => {:type=>'long'},
:ram => {:type=>'long'},
:sla => {:type=>'string'},
:support_level=> {:type=>'string', :index=>:not_analyzed},
:org => {:type=>'string', :index=>:not_analyzed},
:quantity => {:type=>'long'},
:consumed => {:type=>'long'},
:pool_derived => {:type=>'boolean', :index=>:not_analyzed},
:derived => {:type=>'boolean'},
:provider_id => {:type=>'long', :index=>:not_analyzed},
:stacking_id => {:type=>'string'},
:multi_entitlement => {:type=>'boolean'}
}
}
}
end
def self.index_settings
{
"index" => {
"analysis" => {
"filter" => Katello::Search::custom_filters,
"analyzer" => Katello::Search::custom_analyzers
}
}
}
end
def self.index
"#{AppConfig.elastic_index}_pool"
end
# If the pool_json is passed in, then candlepin is not hit again to fetch it. This is for the case where
# prior to this call the pool was already fetched.
def self.find_pool(cp_id, pool_json=nil)
pool_json = Resources::Candlepin::Pool.find(cp_id) if !pool_json
::Pool.new(pool_json) if not pool_json.nil?
end
def self.index_pools(pools, clear_filters=nil)
# Clear previous pools index
if !clear_filters.nil?
results = self.search nil, 0, 0, clear_filters
Tire.index self.index do
results.each do |result|
remove :pool, result.id
end
end
end
json_pools = pools.collect{ |pool|
pool.as_json.merge(pool.index_options)
}
Tire.index self.index do
create :settings => ::Pool.index_settings, :mappings => ::Pool.index_mapping
import json_pools
end if !json_pools.empty?
end
def self.search query, start, page_size, filters={}, sort=[:name_sort, "ASC"]
return [] if !Tire.index(self.index).exists?
all_rows = query.blank? #if blank, get all rows
search = Tire.search self.index do
query do
if all_rows
all
else
# No default_field is specified to let search span all indexed fields
string query, {}
end
end
if page_size > 0
size page_size
from start
end
if filters.has_key?(:org)
filter :term, :org=>filters[:org]
end
if filters.has_key?(:provider_id)
filter :term, :provider_id=>filters[:provider_id]
end
sort { by sort[0], sort[1] } unless !all_rows
end
return search.results
rescue
return []
end
# Some fields are are not native to the Candlepin object but are useful for searching
def provider_id
@cp_provider_id
end
def provider_id= cp_id
@cp_provider_id = cp_id
end
end