Skip to content

Commit

Permalink
Fixes: #5015 - IllegalArgumentException on reports with time range in…
Browse files Browse the repository at this point in the history
… report profile and time selection in the report UI
  • Loading branch information
mgruner committed May 2, 2024
1 parent 9221c48 commit 5662043
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/search_index_backend.rb
Expand Up @@ -501,7 +501,16 @@ def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)

response = make_request(url, data: data, method: :post)

with_interval = aggs_interval.present? && aggs_interval[:interval].present?

if !response.success?
# Work around a bug with ES versions <= 8.5.0, where invalid date range conditions caused an error response from the server.
# https://github.com/zammad/zammad/issues/5105, https://github.com/elastic/elasticsearch/issues/88131
# This can probably be removed when the required minimum ES version is >= 8.5.0.
if with_interval && response.code.to_i == 400 && response.body&.include?('illegal_argument_exception')
return fake_empty_es_aggregation_response
end

raise humanized_error(
verb: 'GET',
url: url,
Expand All @@ -511,7 +520,7 @@ def self.selectors(index, selectors = nil, options = {}, aggs_interval = nil)
end
Rails.logger.debug { response.data.to_json }

if aggs_interval.blank? || aggs_interval[:interval].blank?
if !with_interval
object_ids = response.data['hits']['hits'].pluck('_id')

# in lower ES 6 versions, we get total count directly, in higher
Expand Down Expand Up @@ -1001,4 +1010,12 @@ def self.drop_pipeline
]
)
end

# Simulate an empty response from ES.
def self.fake_empty_es_aggregation_response
{
'hits' => { 'total' => { 'value' => 0, 'relation' => 'eq' }, 'max_score' => nil, 'hits' => [] },
'aggregations' => { 'time_buckets' => { 'buckets' => [] } }
}
end
end
24 changes: 24 additions & 0 deletions spec/lib/search_index_backend_spec.rb
Expand Up @@ -349,6 +349,30 @@
expect(result).to eq({ count: 7, object_ids: [ticket7.id.to_s, ticket6.id.to_s, ticket5.id.to_s, ticket4.id.to_s, ticket3.id.to_s, ticket2.id.to_s, ticket1.id.to_s] })
end

# https://github.com/zammad/zammad/issues/5105
context 'with non-overlapping aggs_interval' do
before do
travel_to 18.months.from_now
create(:ticket)
searchindex_model_reload([Ticket])
travel_back
end

it 'finds no records' do
result = described_class.selectors('Ticket',
{ 'ticket.created_at'=>{ 'operator' => 'till (relative)', 'value' => '30', 'range' => 'minute' } },
{},
{
from: 1.year.from_now,
to: 2.years.from_now,
interval: 'month', # year, quarter, month, week, day, hour, minute, second
field: 'created_at',
})

expect(result['hits']['total']['value']).to eq(0)
end
end

it 'finds records with from (relative)' do
result = described_class.selectors('Ticket',
{ 'ticket.created_at'=>{ 'operator' => 'from (relative)', 'value' => '30', 'range' => 'minute' } },
Expand Down

0 comments on commit 5662043

Please sign in to comment.