Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions app/controllers/redis_web_manager/keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions app/helpers/redis_web_manager/keys_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/views/redis_web_manager/keys/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<%= label_tag :type, 'Type' %>
<%= select_tag :type, options_for_select(types, params[:type]), class: 'form-control custom-select mx-2' %>
</div>
<div class='form-group'>
<%= label_tag :expiry_date, 'Expiration Date' %>
<%= select_tag :expiry_date, options_for_select(expiry_date_filters.collect(&:reverse), params[:expiry_date]), class: 'form-control' %>
</div>
<%= submit_tag 'Search', class: 'btn btn-primary ml-4', name: nil %>
<% end %>
<div class="table-responsive">
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DURATION = {
less_than_one_hour: 3600,
less_than_one_week: 604800,
less_than_one_month: 2592000
}.freeze
10 changes: 10 additions & 0 deletions spec/helpers/keys_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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