Class: Queries::Extract::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Identifiers, Concerns::Protocols, Concerns::Tags, Concerns::Users, Helpers
Defined in:
lib/queries/extract/filter.rb

Instance Attribute Summary collapse

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, #attribute_exact_facet, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #string_fragments, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • args (Hash)

    are permitted params



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/queries/extract/filter.rb', line 62

def initialize(params)
  @ancestor_id = params[:ancestor_id]
  @collection_object_id = params[:collection_object_id]
  @exact_verbatim_anatomical_origin = params[:exact_verbatim_anatomical_origin]
  @extract_end_date_range = params[:extract_end_date_range]
  @extract_origin = params[:extract_origin]
  @extract_start_date_range = params[:extract_start_date_range]
  @otu_id = params[:otu_id]
  @recent = boolean_param(params, :recent)
  @repository_id = params[:repository_id]
  @sequences = boolean_param(params, :sequences)
  @verbatim_anatomical_origin = params[:verbatim_anatomical_origin]

  set_identifier(params)
  set_tags_params(params)
  set_user_dates(params)
  set_protocols_params(params)
end

Instance Attribute Details

#ancestor_idProtonym.id?

Return all extracts linked to OTUs AND CollectionObject that is self or descendant linked to this TaxonName

Returns:

  • (Protonym.id, nil)

    return all extracts linked to OTUs AND CollectionObject that is self or descendant linked to this TaxonName



31
32
33
# File 'lib/queries/extract/filter.rb', line 31

def ancestor_id
  @ancestor_id
end

#collection_object_idArray of Repository#id

Returns:



26
27
28
# File 'lib/queries/extract/filter.rb', line 26

def collection_object_id
  @collection_object_id
end

#exact_verbatim_anatomical_originBoolean?

Parameters:

  • extract_anatomical_origin (String)

    'true', 'false', nil

Returns:

  • (Boolean, nil)


59
60
61
# File 'lib/queries/extract/filter.rb', line 59

def exact_verbatim_anatomical_origin
  @exact_verbatim_anatomical_origin
end

#extract_end_date_rangeDate?

Parameters:

  • extract_end_date_range (String)

    yyyy-mm-dd

Returns:

  • (Date, nil)


41
42
43
# File 'lib/queries/extract/filter.rb', line 41

def extract_end_date_range
  @extract_end_date_range
end

#extract_originString?

Parameters:

  • see (String)

    originates_from in app/models/extract.rb for legal values

Returns:

  • (String, nil)


50
51
52
# File 'lib/queries/extract/filter.rb', line 50

def extract_origin
  @extract_origin
end

#extract_start_date_rangeDate?

Parameters:

  • extract_start_date_range (String)

    yyyy-mm-dd

Returns:

  • (Date, nil)


36
37
38
# File 'lib/queries/extract/filter.rb', line 36

def extract_start_date_range
  @extract_start_date_range
end

#otu_idArray of Repository#id

Returns:



23
24
25
# File 'lib/queries/extract/filter.rb', line 23

def otu_id
  @otu_id
end

#recentBoolen?

!! Likely doesn't do what we think it does when chained with other params

Parameters:

  • 'true' (String, nil)
    • order by updated_at

    'false', nil - do not apply ordering

Returns:

  • (Boolen, nil)


17
18
19
# File 'lib/queries/extract/filter.rb', line 17

def recent
  @recent
end

#repository_idArray of Repository#id

Returns:



20
21
22
# File 'lib/queries/extract/filter.rb', line 20

def repository_id
  @repository_id
end

#sequencesBoolean

Parameters:

  • (String, Boolean, nil)

Returns:

  • (Boolean)


45
46
47
# File 'lib/queries/extract/filter.rb', line 45

def sequences
  @sequences
end

#verbatim_anatomical_originString?

Parameters:

  • anatomical_origin (String)

Returns:

  • (String, nil)


54
55
56
# File 'lib/queries/extract/filter.rb', line 54

def verbatim_anatomical_origin
  @verbatim_anatomical_origin
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/queries/extract/filter.rb', line 262

def all
  a = and_clauses
  b = merge_clauses
  # q = nil
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::Extract.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::Extract.all
  end

  q = q.order(updated_at: :desc) if recent
  q
end

#ancestors_facetObject

TODO: this is not a join, but an IN x2 UNION, i.e. there is likely room for optimization via a join.



194
195
196
197
198
199
200
201
# File 'lib/queries/extract/filter.rb', line 194

def ancestors_facet
  return nil if ancestor_id.nil?

  a = ::Extract.joins(:origin_otus).where(otus: ::Otu.descendant_of_taxon_name(ancestor_id))
  b = ::Extract.joins(:origin_collection_objects).where(collection_objects: ::CollectionObject.joins(:otus).where(otus: ::Otu.descendant_of_taxon_name(ancestor_id)) )

  ::Extract.from("((#{a.to_sql}) UNION (#{b.to_sql})) as extracts")
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


204
205
206
207
208
209
210
211
212
213
214
# File 'lib/queries/extract/filter.rb', line 204

def and_clauses
  clauses = base_and_clauses

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#base_and_clausesArray

Returns:

  • (Array)


217
218
219
220
221
222
223
224
225
# File 'lib/queries/extract/filter.rb', line 217

def base_and_clauses
  clauses = [
    repository_id_facet,
    date_made_facet,
    attribute_exact_facet(:verbatim_anatomical_origin),
  ]
  clauses.compact!
  clauses
end

#base_merge_clausesObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/queries/extract/filter.rb', line 227

def base_merge_clauses
  clauses = []

  clauses += [
    protocol_id_facet,      # See Queries::Concerns::Protocols
    protocol_facet,
    keyword_id_facet,       # See Queries::Concerns::Tags
    created_updated_facet,  # See Queries::Concerns::Users
    identifiers_facet,      # See Queries::Concerns::Identifiers
    identifier_between_facet,
    identifier_facet,
    identifier_namespace_facet,
    collection_object_id_facet,
    otu_id_facet,
    ancestors_facet,
    sequences_facet,
    extract_origin_facet
  ]

  clauses.compact!
  clauses
end

#base_queryObject



86
87
88
# File 'lib/queries/extract/filter.rb', line 86

def base_query
  ::Extract.select('extracts.*')
end

#collection_object_id_facetObject



187
188
189
190
# File 'lib/queries/extract/filter.rb', line 187

def collection_object_id_facet
  return nil if collection_object_id.empty?
  ::Extract.joins(:origin_collection_objects).where(collection_objects: {id: collection_object_id})
end

#date_made_facetObject

TODO: Abstract to single date store range helper



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/queries/extract/filter.rb', line 152

def date_made_facet
  return nil if extract_start_date_range.nil?
  a = extract_start_date_range
  sy,sm,sd = a.year, a.month, a.day
  b = extract_end_date_range
  ey,em,ed = b.year, b.month, b.day

  # 5 possible ranges to consider

  ranges = []

  if sy < ey
    # b/w years
    ranges.push table[:year_made].gt(sy).and(table[:year_made].lt(ey))

    # b/w months start
    ranges.push table[:year_made].eq(sy).and(table[:month_made].gt(sm))

    # b/w months end
    ranges.push table[:year_made].eq(ey).and(table[:month_made].lt(em))
  end

  # days in start month
  ranges.push table[:year_made].eq(sy).and(table[:month_made].eq(sm)).and(table[:day_made].gteq(sd)).and(table[:day_made].lteq(ed))

  # days in end month
  ranges.push table[:year_made].eq(ey).and(table[:month_made].eq(em)).and(table[:day_made].lteq(ed))

  a = ranges.shift
  ranges.each do |r|
    a = a.or(r)
  end
  a
end

#extract_origin_facetObject



115
116
117
118
# File 'lib/queries/extract/filter.rb', line 115

def extract_origin_facet
  return nil if extract_origin.nil?
  ::Extract.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: extract_origin})
end

#merge_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


251
252
253
254
255
256
257
258
259
# File 'lib/queries/extract/filter.rb', line 251

def merge_clauses
  clauses = base_merge_clauses
  return nil if clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.merge(b)
  end
  a
end

#otu_id_facetObject



107
108
109
110
111
112
113
# File 'lib/queries/extract/filter.rb', line 107

def otu_id_facet
  return nil if otu_id.empty?
  a = ::Extract.joins(:origin_otus).where(otus: {id: otu_id})
  b = ::Extract.joins(origin_collection_objects: [:otus]).where(otus: {id: otu_id})
  
 ::Extract.from("((#{a.to_sql}) UNION (#{b.to_sql})) as extracts")
end

#repository_id_facetObject



102
103
104
105
# File 'lib/queries/extract/filter.rb', line 102

def repository_id_facet
  return nil if repository_id.empty?
  table[:repository_id].eq_any(repository_id)
end

#sequences_facetObject



120
121
122
123
124
125
126
127
128
# File 'lib/queries/extract/filter.rb', line 120

def sequences_facet
  return nil if sequences.nil?

  subquery = ::OriginRelationship.where(
    ::OriginRelationship.arel_table[:old_object_id].eq(table[:id])
    .and(::OriginRelationship.arel_table[:new_object_type].eq('Sequence')))
    .arel.exists
  ::Extract.where(sequences ? subquery : subquery.not)
end

#tableArel::Table

Returns:

  • (Arel::Table)


82
83
84
# File 'lib/queries/extract/filter.rb', line 82

def table
  ::Extract.arel_table
end