From c8784d1f669b75dbbe8e09cbe1b2a0856ce8f197 Mon Sep 17 00:00:00 2001 From: aignatov-bio <47317017+aignatov-bio@users.noreply.github.com> Date: Mon, 31 Jan 2022 09:21:08 +0100 Subject: [PATCH] Add advanced filters for date/datetime/time range [SCI-6462] (#3806) Co-authored-by: Anton --- .../vue/repository_filter/filter.vue | 6 ++ .../filters/repositoryDateRangeValue.vue | 63 ++++++++++++++++++ .../filters/repositoryDateTimeRangeValue.vue | 65 +++++++++++++++++++ .../filters/repositoryTimeRangeValue.vue | 63 ++++++++++++++++++ app/models/repository_date_range_value.rb | 23 +++++++ .../repository_date_time_range_value.rb | 23 +++++++ app/models/repository_time_range_value.rb | 25 +++++++ config/initializers/extends.rb | 6 ++ 8 files changed, 274 insertions(+) create mode 100644 app/javascript/vue/repository_filter/filters/repositoryDateRangeValue.vue create mode 100644 app/javascript/vue/repository_filter/filters/repositoryDateTimeRangeValue.vue create mode 100644 app/javascript/vue/repository_filter/filters/repositoryTimeRangeValue.vue diff --git a/app/javascript/vue/repository_filter/filter.vue b/app/javascript/vue/repository_filter/filter.vue index 1747bf8d9..1d56acced 100644 --- a/app/javascript/vue/repository_filter/filter.vue +++ b/app/javascript/vue/repository_filter/filter.vue @@ -25,8 +25,11 @@ import RepositoryNumberValue from 'vue/repository_filter/filters/repositoryNumberValue.vue' import RepositoryMyModuleValue from 'vue/repository_filter/filters/repositoryMyModuleValue.vue' import RepositoryDateValue from 'vue/repository_filter/filters/repositoryDateValue.vue' + import RepositoryDateRangeValue from 'vue/repository_filter/filters/repositoryDateRangeValue.vue' import RepositoryDateTimeValue from 'vue/repository_filter/filters/repositoryDateTimeValue.vue' + import RepositoryDateTimeRangeValue from 'vue/repository_filter/filters/repositoryDateTimeRangeValue.vue' import RepositoryTimeValue from 'vue/repository_filter/filters/repositoryTimeValue.vue' + import RepositoryTimeRangeValue from 'vue/repository_filter/filters/repositoryTimeRangeValue.vue' import RepositoryListValue from 'vue/repository_filter/filters/repositoryListValue.vue' import RepositoryStatusValue from 'vue/repository_filter/filters/repositoryStatusValue.vue' import RepositoryChecklistValue from 'vue/repository_filter/filters/repositoryChecklistValue.vue' @@ -47,8 +50,11 @@ RepositoryNumberValue, RepositoryMyModuleValue, RepositoryDateValue, + RepositoryDateRangeValue, RepositoryTimeValue, + RepositoryTimeRangeValue, RepositoryDateTimeValue, + RepositoryDateTimeRangeValue, RepositoryListValue, RepositoryStatusValue, RepositoryChecklistValue, diff --git a/app/javascript/vue/repository_filter/filters/repositoryDateRangeValue.vue b/app/javascript/vue/repository_filter/filters/repositoryDateRangeValue.vue new file mode 100644 index 000000000..8b785035f --- /dev/null +++ b/app/javascript/vue/repository_filter/filters/repositoryDateRangeValue.vue @@ -0,0 +1,63 @@ + + + diff --git a/app/javascript/vue/repository_filter/filters/repositoryDateTimeRangeValue.vue b/app/javascript/vue/repository_filter/filters/repositoryDateTimeRangeValue.vue new file mode 100644 index 000000000..c37580ac3 --- /dev/null +++ b/app/javascript/vue/repository_filter/filters/repositoryDateTimeRangeValue.vue @@ -0,0 +1,65 @@ + + + diff --git a/app/javascript/vue/repository_filter/filters/repositoryTimeRangeValue.vue b/app/javascript/vue/repository_filter/filters/repositoryTimeRangeValue.vue new file mode 100644 index 000000000..d005c9502 --- /dev/null +++ b/app/javascript/vue/repository_filter/filters/repositoryTimeRangeValue.vue @@ -0,0 +1,63 @@ + + + diff --git a/app/models/repository_date_range_value.rb b/app/models/repository_date_range_value.rb index 7caafc52d..f45513e40 100644 --- a/app/models/repository_date_range_value.rb +++ b/app/models/repository_date_range_value.rb @@ -12,6 +12,29 @@ class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase super(:full_date) end + def self.add_filter_condition(repository_rows, filter_element) + parameters = filter_element.parameters + case filter_element.operator + when 'equal_to' + repository_rows.where(repository_date_time_range_values: { start_time: parameters['start_date'], end_time: parameters['end_date'] }) + when 'unequal_to' + repository_rows.where.not(repository_date_time_range_values: { start_time: parameters['start_date'], end_time: parameters['end_date'] }) + when 'greater_than' + repository_rows.where('repository_date_time_range_values.start_time > ?', parameters['end_date']) + when 'greater_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.start_time >= ?', parameters['end_date']) + when 'less_than' + repository_rows.where('repository_date_time_range_values.end_time < ?', parameters['start_date']) + when 'less_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.end_time <= ?', parameters['start_date']) + when 'between' + repository_rows.where('repository_date_time_range_values.start_time > ? AND repository_date_time_range_values.end_time < ?', + parameters['start_date'], parameters['end_date']) + else + raise ArgumentError, 'Wrong operator for RepositoryDateRangeValue!' + end + end + def self.new_with_payload(payload, attributes) data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload diff --git a/app/models/repository_date_time_range_value.rb b/app/models/repository_date_time_range_value.rb index 56376fa2e..d74a70e89 100644 --- a/app/models/repository_date_time_range_value.rb +++ b/app/models/repository_date_time_range_value.rb @@ -12,6 +12,29 @@ class RepositoryDateTimeRangeValue < RepositoryDateTimeRangeValueBase super(:full_with_comma) end + def self.add_filter_condition(repository_rows, filter_element) + parameters = filter_element.parameters + case filter_element.operator + when 'equal_to' + repository_rows.where(repository_date_time_range_values: { start_time: parameters['start_datetime'], end_time: parameters['end_datetime'] }) + when 'unequal_to' + repository_rows.where.not(repository_date_time_range_values: { start_time: parameters['start_datetime'], end_time: parameters['end_datetime'] }) + when 'greater_than' + repository_rows.where('repository_date_time_range_values.start_time > ?', parameters['end_datetime']) + when 'greater_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.start_time >= ?', parameters['end_datetime']) + when 'less_than' + repository_rows.where('repository_date_time_range_values.end_time < ?', parameters['start_datetime']) + when 'less_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.end_time <= ?', parameters['start_datetime']) + when 'between' + repository_rows.where('repository_date_time_range_values.start_time > ? AND repository_date_time_range_values.end_time < ?', + parameters['start_datetime'], parameters['end_datetime']) + else + raise ArgumentError, 'Wrong operator for RepositoryDateTimeRangeValue!' + end + end + def self.new_with_payload(payload, attributes) data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload diff --git a/app/models/repository_time_range_value.rb b/app/models/repository_time_range_value.rb index 5b2d903fb..18d1ae05e 100644 --- a/app/models/repository_time_range_value.rb +++ b/app/models/repository_time_range_value.rb @@ -13,6 +13,31 @@ class RepositoryTimeRangeValue < RepositoryDateTimeRangeValueBase super(:time) end + def self.add_filter_condition(repository_rows, filter_element) + parameters = filter_element.parameters + case filter_element.operator + when 'equal_to' + repository_rows.where('repository_date_time_range_values.start_time::time = ? AND repository_date_time_range_values.end_time::time = ?', + parameters['start_time'], parameters['end_time']) + when 'unequal_to' + repository_rows.where.not('repository_date_time_range_values.start_time::time = ? AND repository_date_time_range_values.end_time::time = ?', + parameters['start_time'], parameters['end_time']) + when 'greater_than' + repository_rows.where('repository_date_time_range_values.start_time::time > ?', parameters['end_datetime']) + when 'greater_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.start_time::time >= ?', parameters['end_datetime']) + when 'less_than' + repository_rows.where('repository_date_time_range_values.end_time::time < ?', parameters['start_datetime']) + when 'less_than_or_equal_to' + repository_rows.where('repository_date_time_range_values.end_time::time <= ?', parameters['start_datetime']) + when 'between' + repository_rows.where('repository_date_time_range_values.start_time::time > ? AND repository_date_time_range_values.end_time::time < ?', + parameters['start_datetime'], parameters['end_datetime']) + else + raise ArgumentError, 'Wrong operator for RepositoryTimeRangeValue!' + end + end + def self.new_with_payload(payload, attributes) data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 33c6b2afa..eb13d5827 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -109,10 +109,16 @@ class Extends includes: { repository_asset_values: { asset: { file_attachment: :blob } } } }, RepositoryDateTimeValue: { field: 'repository_date_time_values.data', includes: :repository_date_time_values + }, RepositoryDateTimeRangeValue: { + field: 'repository_date_time_range_values.data', includes: :repository_date_time_range_values }, RepositoryDateValue: { field: 'repository_date_time_values.data', includes: :repository_date_values + }, RepositoryDateRangeValue: { + field: 'repository_date_time_range_values.data', includes: :repository_date_range_values }, RepositoryTimeValue: { field: 'repository_date_time_values.data', includes: :repository_time_values + }, RepositoryTimeRangeValue: { + field: 'repository_date_time_range_values.data', includes: :repository_time_range_values } }