Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export csv #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Search keys
* Can be mounted to Rails applications as engine
* Can connect to multiple databases
* Export keys as CSV

## Installation

Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
connections:
localhost:
url: redis://127.0.0.1:6379/0
exclude_pattern: "^.+/total/.+$"
other:
host: 127.0.0.1
port: 6379
Expand Down
6 changes: 5 additions & 1 deletion lib/redis-browser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'csv'
require 'sinatra/base'
require 'multi_json'
require 'sinatra/json'
Expand All @@ -11,7 +12,10 @@
module RedisBrowser
DEFAULTS = {
'connections' => {
'default' => 'redis://127.0.0.1:6379/0'
'default' => {
'url' => 'redis://127.0.0.1:6379/0',
'exclude-pattern' => ''
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions lib/redis-browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ def get(key, opts = {})
}.merge(data)
end

def exportCSV(include_pattern, exclude_pattern)
key = include_pattern
key << "*" unless key.end_with?("*")
exclude_rx = Regexp.new(exclude_pattern)
keys = redis.keys(key).select { |k| exclude_rx.match(k).nil? }
values = redis.multi do |multi|
keys.each { |k| multi.get(k) }
end
kv = keys.zip(values)

csv_string = CSV.generate do |csv|
kv.each do |k, v|
line = k.split(/[:\/]/) << v
csv << line
end
end
csv_string
rescue => ex
{:error => ex.message}
end

def ping
redis.ping == "PONG"
{:ok => 1}
Expand Down
7 changes: 6 additions & 1 deletion lib/redis-browser/public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ body {
margin-right: 5px;
}

.help-block {
font-size: 0.9em;
color: gray;
}


.value-list-index { width: 30px; }
.value-zset-score { width: 30px; }
.per-page { margin-top: 25px; }
.connection-info { padding-right: 20px; }
.header-item { padding-right: 20px; }
.keys-search { width: 100px; }

#http-loader {
Expand Down
43 changes: 43 additions & 0 deletions lib/redis-browser/templates/coffee/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ app.factory 'API', ['$http', ($http) ->
(connection) ->
ps = {connection: connection}
{
exportCSV: (params) -> $http.get("#{jsEnv.root_path}export-csv", {
params: angular.extend({}, ps, params)
}).then (e) -> e,

ping: () -> $http.get("#{jsEnv.root_path}ping.json", {
params: ps
}).then (e) -> e.data,
Expand Down Expand Up @@ -97,6 +101,45 @@ app.factory 'API', ['$http', ($http) ->
backdropFade: true
dialogFade: true

$scope.export =
filename: "redis-dump.csv"

open: ->
$scope.export.show = true
$scope.export.include = $scope.key.full || "*"
$scope.export.exclude = $scope.connections[$scope.config.connection].exclude_pattern

close: ->
$scope.export.show = false
$scope.export.error = null

run: ->
$scope.api.exportCSV(
include: $scope.export.include,
exclude: $scope.export.exclude
).then (response) ->
$scope.export.error = null

content = response.data
if response.headers('Content-Type').includes("text/csv")
hiddenElement = document.createElement('a')

hiddenElement.href = 'data:attachment/csv,' + encodeURI(content)
hiddenElement.target = '_blank'
hiddenElement.download = $scope.export.filename
hiddenElement.click()

$scope.config.close()

destroyA = setInterval( ->
hiddenElement.remove()
clearInterval(destroyA)
, 5000
)

else
$scope.export.error = content.error

$scope.api = API($scope.config.connection)


Expand Down
41 changes: 40 additions & 1 deletion lib/redis-browser/templates/index.slim
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,43 @@ html ng-app="browser"
' {{ config.error }}
button.btn.btn-success type="submit" ng-click="config.save()" Save

div modal="export.show" close="export.close()" options="config.modalOpts"
.form-horizontal
.modal-header
button.close type="button" ng-click="export.close()" &times;
h4 Export CSV
.modal-body
.control-group
label.control-label for="export-include" Include keys
.controls
input.form-control id="export-include" ng-model="export.include" ng-required="required" type="text"

.control-group
label.control-label for="export-exclude" Exclude keys pattern
.controls
input.form-control id="export-exclude" ng-model="export.exclude" type="text"

.control-group
label.control-label for="export-filename" File name
.controls
input.form-control id="export-filename" ng-model="export.filename" ng-required="required" type="text"

span.help-block
b Include keys
| is keys to include in the output as per
a href="http://redis.io/commands/KEYS" target="_blank" KEYS
| .
br
b Exclude keys pattern
| is a
a href="https://en.wikipedia.org/wiki/Regular_expression" target="_blank" regex
| which excludes the keys from the output.

.modal-footer
span.alert.alert-error.pull-left ng-show="export.error"
' {{ export.error }}
button.btn.btn-success type="submit" ng-click="export.run()" Export

.navbar.navbar-inverse.navbar-fixed-top
.navbar-inner
.container-fluid
Expand All @@ -58,8 +95,10 @@ html ng-app="browser"

.nav-collapse.collapse
p.pull-right
button.btn.btn-default ng-click="export.open()" Export CSV
p.pull-right.header-item
button.btn.btn-success ng-click="config.open()" Configure
p.navbar-text.pull-right.connection-info
p.navbar-text.pull-right.header-item
' Connected to
strong
' {{ config.connection }}
Expand Down
11 changes: 11 additions & 0 deletions lib/redis-browser/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class Web < Sinatra::Base
slim :index
end

get '/export-csv' do
result = browser.exportCSV(params[:include], params[:exclude])
if result.is_a? String
content_type 'text/csv'
attachment "redis-dump.csv"
result
else
json result
end
end

get '/ping.json' do
json browser.ping
end
Expand Down