diff --git a/app/controllers/redis_web_manager/keys_controller.rb b/app/controllers/redis_web_manager/keys_controller.rb index 11ee459..a997183 100644 --- a/app/controllers/redis_web_manager/keys_controller.rb +++ b/app/controllers/redis_web_manager/keys_controller.rb @@ -125,10 +125,29 @@ def format_key(key) def keys query = params[:query].presence - type = params[:type].presence keys = info.search(query).map { |key| format_key(key) } - valid = type && type != 'all' - valid ? keys.select { |key| key[:type] == type } : keys + + keys = filter_by_type(keys, params[:type]) + filter_by_expiry_date(keys, params[:expiry_date]) + end + + def filter_by_type(keys, type) + return keys if validate_filter(type) + keys.select { |key| key[:type].eql?(type) } + end + + def filter_by_expiry_date(keys, expiry_date) + return keys if validate_filter(expiry_date) + + expired = expiry_date.eql?('no_expiry') + return keys.select { |key| key[:expire].eql?(-1) } if expired + + duration = DURATION[expiry_date.to_sym] + keys.select { |key| key[:expire] != -1 && key[:expire] < duration } + end + + def validate_filter(option) + option.blank? || option.eql?('all') end end end diff --git a/app/helpers/redis_web_manager/keys_helper.rb b/app/helpers/redis_web_manager/keys_helper.rb index e352cab..9b08539 100644 --- a/app/helpers/redis_web_manager/keys_helper.rb +++ b/app/helpers/redis_web_manager/keys_helper.rb @@ -10,5 +10,15 @@ def types %w[Zset zset], %w[List list]] end + + def expiry_date_filters + { + all: 'All', + no_expiry: 'No Expiry', + less_than_one_hour: 'Less Than One Hour', + less_than_one_week: 'Less Than One Week', + less_than_one_month: 'Less Than One Month' + } + end end end diff --git a/app/views/redis_web_manager/keys/index.html.erb b/app/views/redis_web_manager/keys/index.html.erb index cb693d2..ff2d9b4 100644 --- a/app/views/redis_web_manager/keys/index.html.erb +++ b/app/views/redis_web_manager/keys/index.html.erb @@ -18,6 +18,10 @@ <%= label_tag :type, 'Type' %> <%= select_tag :type, options_for_select(types, params[:type]), class: 'form-control custom-select mx-2' %> +
+ <%= label_tag :expiry_date, 'Expiration Date' %> + <%= select_tag :expiry_date, options_for_select(expiry_date_filters.collect(&:reverse), params[:expiry_date]), class: 'form-control' %> +
<%= submit_tag 'Search', class: 'btn btn-primary ml-4', name: nil %> <% end %>
diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb new file mode 100644 index 0000000..ee439fa --- /dev/null +++ b/config/initializers/constants.rb @@ -0,0 +1,5 @@ +DURATION = { + less_than_one_hour: 3600, + less_than_one_week: 604800, + less_than_one_month: 2592000 +}.freeze diff --git a/spec/helpers/keys_helper_spec.rb b/spec/helpers/keys_helper_spec.rb index 217fd89..2db3cf8 100644 --- a/spec/helpers/keys_helper_spec.rb +++ b/spec/helpers/keys_helper_spec.rb @@ -12,5 +12,15 @@ %w[Zset zset], %w[List list]]) end + + it 'returns Hash of duration' do + expect(helper.expiry_date_filters).to eq( + all: 'All', + no_expiry: 'No Expiry', + less_than_one_hour: 'Less Than One Hour', + less_than_one_week: 'Less Than One Week', + less_than_one_month: 'Less Than One Month' + ) + end end end