Skip to content

Commit

Permalink
[#160332453] modify DateRangeTopNFieldQuery to allow queries without …
Browse files Browse the repository at this point in the history
…an affiliate (#146)

- refactor some logstash query code
  • Loading branch information
MothOnMars authored Sep 28, 2018
1 parent cc927cc commit 0368ee4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
15 changes: 10 additions & 5 deletions app/models/logstash_queries/date_range_top_n_field_query.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
class DateRangeTopNFieldQuery < DateRangeTopNQuery
attr_reader :filters

def initialize(affiliate_name, start_date, end_date, filter_field, filter_value, agg_options = {})
super(affiliate_name, start_date, end_date, agg_options)
@filter_field, @filter_value = filter_field, filter_value
@filters = { 'affiliate' => @affiliate_name, filter_field => filter_value }.compact
end

def booleans(json)
filters.each do |field, value|
json.must do
json.child! { json.term { json.set! field, value } }
end
end

json.must do
json.child! { json.term { json.affiliate @affiliate_name } }
json.child! { json.term { json.set! @filter_field, @filter_value } }
json.child! { date_range(json, @start_date, @end_date) }
end
end

end
end
4 changes: 1 addition & 3 deletions app/models/logstash_queries/date_range_top_n_query.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class DateRangeTopNQuery < TopNQuery
include DateRangeFilter

def initialize(affiliate_name, start_date, end_date, agg_options = {})
super(affiliate_name, agg_options)
Expand All @@ -8,7 +7,6 @@ def initialize(affiliate_name, start_date, end_date, agg_options = {})

def booleans(json)
must_affiliate_date_range(json, @affiliate_name, @start_date, @end_date)
must_not_spider(json)
end

end
end
2 changes: 0 additions & 2 deletions app/models/logstash_queries/top_query_match_query.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class TopQueryMatchQuery
include AnalyticsDSL
include DateRangeFilter

def initialize(affiliate_name, raw_query, start_date, end_date, agg_options = {})
@affiliate_name, @raw_query, @start_date, @end_date = affiliate_name, raw_query, start_date, end_date
Expand Down Expand Up @@ -37,7 +36,6 @@ def query(json)

def booleans(json)
must_affiliate_date_range(json, @affiliate_name, @start_date, @end_date)
must_not_spider(json)
end

def terms_type_agg(json)
Expand Down
17 changes: 17 additions & 0 deletions lib/analytics_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@ def type_terms_agg(json, field_name, size)
end
end

def must_affiliate(json, site_name)
json.must do
json.child! { json.term { json.affiliate site_name } }
end
end

def must_date_range(json, start_date, end_date)
json.must do
json.child! { date_range(json, start_date, end_date) }
end
end

def must_affiliate_date_range(json, site_name, start_date, end_date)
must_affiliate(json, site_name)
must_date_range(json, start_date, end_date)
must_not_spider(json)
end
end
12 changes: 0 additions & 12 deletions lib/date_range_filter.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@

it { is_expected.to eq(%q({"query":{"filtered":{"filter":{"bool":{"must":[{"term":{"affiliate":"foo"}},{"term":{"params.url":"some_url"}},{"range":{"@timestamp":{"gte":"2014-06-28","lte":"2014-06-29"}}}]}}}},"aggs":{"agg":{"terms":{"field":"raw","size":0}}}}))}

end
context 'when the affiliate is nil' do
let(:query) do
DateRangeTopNFieldQuery.new(nil,
Date.parse("2014-06-28"),
Date.parse("2014-06-29"),
'some_field',
'some_value',
{ field: 'raw', size: 0 })
end

it 'filters by the field' do
expect(body).to eq(
%q({"query":{"filtered":{"filter":{"bool":{"must":[{"term":{"some_field":"some_value"}},{"range":{"@timestamp":{"gte":"2014-06-28","lte":"2014-06-29"}}}]}}}},"aggs":{"agg":{"terms":{"field":"raw","size":0}}}})
)
end
end
end

0 comments on commit 0368ee4

Please sign in to comment.