From 44409ad215a2d3a601cfa921c441791c4386449b Mon Sep 17 00:00:00 2001 From: Steve Bunlon Date: Mon, 26 Apr 2021 18:22:13 +0200 Subject: [PATCH 1/3] fix(date-filter): filtering only on hours now returns the expected records --- .../operator_date_interval_parser.rb | 4 ++-- .../operator_date_interval_parser_test.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/services/forest_liana/operator_date_interval_parser_test.rb diff --git a/app/services/forest_liana/operator_date_interval_parser.rb b/app/services/forest_liana/operator_date_interval_parser.rb index 6775cfb0..c663ab48 100644 --- a/app/services/forest_liana/operator_date_interval_parser.rb +++ b/app/services/forest_liana/operator_date_interval_parser.rb @@ -93,10 +93,10 @@ def get_date_filter(operator, value) " AND '#{Time.now}'" when OPERATOR_BEFORE_X_HOURS_AGO ensure_integer_value(value) - return "< '#{to_client_timezone((Integer(value)).hour.ago)}'" + return "< '#{(Integer(value)).hour.ago}'" when OPERATOR_AFTER_X_HOURS_AGO ensure_integer_value(value) - return "> '#{to_client_timezone((Integer(value)).hour.ago)}'" + return "> '#{(Integer(value)).hour.ago}'" end duration = PERIODS[operator][:duration] diff --git a/test/services/forest_liana/operator_date_interval_parser_test.rb b/test/services/forest_liana/operator_date_interval_parser_test.rb new file mode 100644 index 00000000..9b66afee --- /dev/null +++ b/test/services/forest_liana/operator_date_interval_parser_test.rb @@ -0,0 +1,16 @@ +module ForestLiana + class OperatorDateIntervalParserTest < ActiveSupport::TestCase + test 'OPERATOR_AFTER_X_HOURS_AGO and OPERATOR_BEFORE_X_HOURS_AGO should not take timezone into account' do + # Setting a big timezone (GMT+10) on purpose, the timezone should not be applied on the result date + operatorDateIntervalParser = OperatorDateIntervalParser.new('Australia/Sydney') + + result = operatorDateIntervalParser.get_date_filter(OperatorDateIntervalParser::OPERATOR_AFTER_X_HOURS_AGO, 2) + hourComputed = result.split('> ')[1].tr('\'', '').to_datetime.hour + assert hourComputed == Time.now.utc.hour - 2 + + result = operatorDateIntervalParser.get_date_filter(OperatorDateIntervalParser::OPERATOR_BEFORE_X_HOURS_AGO, 2) + hourComputed = result.split('< ')[1].tr('\'', '').to_datetime.hour + assert hourComputed == Time.now.utc.hour - 2 + end + end +end \ No newline at end of file From 59f109a47fd9a551a297ac70ed7fcb948d53b30d Mon Sep 17 00:00:00 2001 From: Steve Bunlon Date: Tue, 27 Apr 2021 10:30:29 +0200 Subject: [PATCH 2/3] fix: end of line --- .../services/forest_liana/operator_date_interval_parser_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/services/forest_liana/operator_date_interval_parser_test.rb b/test/services/forest_liana/operator_date_interval_parser_test.rb index 9b66afee..58632baf 100644 --- a/test/services/forest_liana/operator_date_interval_parser_test.rb +++ b/test/services/forest_liana/operator_date_interval_parser_test.rb @@ -13,4 +13,4 @@ class OperatorDateIntervalParserTest < ActiveSupport::TestCase assert hourComputed == Time.now.utc.hour - 2 end end -end \ No newline at end of file +end From af83ce5d4503446e56f34cbd13d20d852bdefc82 Mon Sep 17 00:00:00 2001 From: Steve Bunlon Date: Tue, 27 Apr 2021 11:38:59 +0200 Subject: [PATCH 3/3] refactor: fix codeClimate --- .../operator_date_interval_parser.rb | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/app/services/forest_liana/operator_date_interval_parser.rb b/app/services/forest_liana/operator_date_interval_parser.rb index c663ab48..8ee9ab44 100644 --- a/app/services/forest_liana/operator_date_interval_parser.rb +++ b/app/services/forest_liana/operator_date_interval_parser.rb @@ -73,30 +73,34 @@ def to_client_timezone(date) def get_date_filter(operator, value) return nil unless is_date_operator? operator - case operator - when OPERATOR_FUTURE - return ">= '#{Time.now}'" - when OPERATOR_PAST - return "<= '#{Time.now}'" - when OPERATOR_TODAY - return "BETWEEN '#{to_client_timezone(Time.now.beginning_of_day)}' " + - "AND '#{to_client_timezone(Time.now.end_of_day)}'" - when OPERATOR_PREVIOUS_X_DAYS - ensure_integer_value(value) - return "BETWEEN '" + - "#{to_client_timezone(Integer(value).day.ago.beginning_of_day)}'" + - " AND '#{to_client_timezone(1.day.ago.end_of_day)}'" - when OPERATOR_PREVIOUS_X_DAYS_TO_DATE - ensure_integer_value(value) - return "BETWEEN '" + - "#{to_client_timezone((Integer(value) - 1).day.ago.beginning_of_day)}'" + - " AND '#{Time.now}'" - when OPERATOR_BEFORE_X_HOURS_AGO - ensure_integer_value(value) - return "< '#{(Integer(value)).hour.ago}'" - when OPERATOR_AFTER_X_HOURS_AGO - ensure_integer_value(value) - return "> '#{(Integer(value)).hour.ago}'" + filter = case operator + when OPERATOR_FUTURE + ">= '#{Time.now}'" + when OPERATOR_PAST + "<= '#{Time.now}'" + when OPERATOR_TODAY + "BETWEEN '#{to_client_timezone(Time.now.beginning_of_day)}' " + + "AND '#{to_client_timezone(Time.now.end_of_day)}'" + when OPERATOR_PREVIOUS_X_DAYS + ensure_integer_value(value) + "BETWEEN '" + + "#{to_client_timezone(Integer(value).day.ago.beginning_of_day)}'" + + " AND '#{to_client_timezone(1.day.ago.end_of_day)}'" + when OPERATOR_PREVIOUS_X_DAYS_TO_DATE + ensure_integer_value(value) + "BETWEEN '" + + "#{to_client_timezone((Integer(value) - 1).day.ago.beginning_of_day)}'" + + " AND '#{Time.now}'" + when OPERATOR_BEFORE_X_HOURS_AGO + ensure_integer_value(value) + "< '#{(Integer(value)).hour.ago}'" + when OPERATOR_AFTER_X_HOURS_AGO + ensure_integer_value(value) + "> '#{(Integer(value)).hour.ago}'" + end + + if filter != nil + return filter end duration = PERIODS[operator][:duration]