Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Add file system based saved searches. #263

Open
wants to merge 1 commit into
base: kibana-ruby
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

saved
KibanaConfig.rbackup.rb
*.DS_Store
/tmp/*
3 changes: 3 additions & 0 deletions KibanaConfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ module KibanaConfig

# Use this interval as fallback.
Fallback_interval = 900

# directory for hash storage
Storage_dir = 'saved'
end
40 changes: 40 additions & 0 deletions lib/kibana-app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,44 @@ def link_to url_fragment, mode=:full_url
get '/js/timezone.js' do
erb :timezone
end

# Saved Search URL Routes
#
# Get a list of saved searches or a particular saved search
get '/api/saved/:name?' do
if params[:name].nil?
# return a list of all known saved files
data = {
"files" => Dir["#{KibanaConfig::Storage_dir}/*"].map {|f|
File.basename(f)
},
}
JSON.generate(data)
else
# return the contents of a named file
begin
File.read(File.join(KibanaConfig::Storage_dir, params[:name]))
rescue Errno::ENOENT
return 404
end
end
end

# Save a search
put '/api/saved/:name' do
File.open("#{KibanaConfig::Storage_dir}/#{params[:name]}", 'w') do |f|
f.write(request.body.read)
end
return [201, "Created /api/saved/#{params[:name]}"]
end

# Delete a saved search
delete '/api/saved/:name' do
begin
File.delete("#{KibanaConfig::Storage_dir}/#{params[:name]}")
return [200, "Deleted /api/saved/#{params[:name]}"]
rescue Errno::ENOENT
return 404
end
end
end
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<div class="sidebar">
<div id=fields></div>
<div id=analyze></div>
<div id=saved></div>
</div>
</div>

Expand Down
119 changes: 118 additions & 1 deletion public/lib/js/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ function getPage() {
'<center><br><p><img src=images/barload.gif></center>');
getGraph(window.interval);

// buttons to load/save searches
$('#saved').html(
'<div class="btn-group">' +
'<button id="load-search" class="btn btn-mini" title="Load a saved search"><i class="icon-folder-open"></i> Load</button>' +
'<button id="save-search" class="btn btn-mini" title="Save current search"><i class="icon-file"></i> Save</button></div>');
}
}
});
Expand Down Expand Up @@ -751,7 +756,7 @@ function pageLinks() {
// This is very ugly
function blank_page() {
var selectors = ['#graph','#graphheader','#feedlinks','#logs','.pagelinks',
'#fields','#analyze']
'#fields','#analyze','#saved']

for (var selector in selectors) {
$(selectors[selector]).text("");
Expand Down Expand Up @@ -1721,4 +1726,116 @@ function bind_clicks() {
unhighlight_all_events();
});

// save and load
$("body").delegate("#load-search", "click", function () {
window.request = $.ajax({
url: "api/saved/",
type: "GET",
cache: false,
success: function (json) {
var resp = JSON.parse(json)
, elm = [];

for (var idx in resp.files) {
var file = resp.files[idx];
elm.push('<option value="');
elm.push(file);
elm.push('">');
elm.push(file);
elm.push('</option>');
}

// hide any other popovers
if (popover_visible) {
$('.popover').remove();
}

// create and show our popover
$("#load-search").popover({
trigger: 'manual',
classes: 'auto-width',
title: 'Load saved search',
content: '<fieldset><select id="load-select" size="10">' +
elm.join('') + '</select>' +
'<div class="form-actions">' +
'<button id="load-btn-load" class="btn btn-small btn-primary"><i class="icon-ok"></i> Load</button>' +
'<div class="pull-right">' +
'<button id="load-btn-delete" class="btn btn-small btn-danger"><i class="icon-remove"></i> Delete</button> &nbsp; ' +
'<button id="load-btn-cancel" class="btn btn-small">Cancel</button>' +
'</div>' +
'</div>' +
'</fieldset>',
}).popover('show');
} //end ajax success
});
});

$("body").delegate("#load-btn-load", "click", function () {
var $sel = $("#load-select"); // hack!!
if ($sel[0].selectedIndex >= 0) {
var fname = $sel[0].options[$sel[0].selectedIndex].value;
window.request = $.ajax({
url: "api/saved/" + encodeURIComponent(fname),
type: "GET",
cache: false,
success: function (data, status, xhr) {
var hash = data.replace(/^\s+|\s+$/g, '');
$("#load-search").popover('destroy');
window.location.hash = hash;
}, //end ajax success
error: function (xhr, status, err) {
console.log(err);
alert("Load failed for " + fname);
} //end ajax error
});
} else {
// tell them to select something
$sel.wrap('<div class="control-group error"/>');
}
});
$("body").delegate("#load-btn-delete", "click", function () {
var $sel = $("#load-select"); // hack!!
if ($sel[0].selectedIndex >= 0) {
var fname = $sel[0].options[$sel[0].selectedIndex].value;
window.request = $.ajax({
url: "api/saved/" + encodeURIComponent(fname),
type: "DELETE",
cache: false,
success: function (data, status, xhr) {
alert('Deleted ' + fname);
$("#load-search").popover('destroy');
}, //end ajax success
error: function (xhr, status, err) {
console.log(err);
alert("Delete failed for " + fname);
} //end ajax error
});
}
});
$("body").delegate("#load-btn-cancel", "click", function () {
$("#load-search").popover('destroy');
});

$("body").delegate("#save-search", "click", function () {
var fname = prompt("Save search as:", window.hashjson.fname || '');
if (fname) {
window.hashjson.fname = fname;
var payload = Base64.encode(JSON.stringify(window.hashjson, null, ''));
// make the ajax put call
window.request = $.ajax({
url: "api/saved/" + encodeURIComponent(fname),
type: "PUT",
data: payload,
success: function (data, status, xhr) {
alert('Search saved.');
}, //end ajax success
error: function (xhr, status, err) {
console.log(err);
alert("Save failed for " + fname);
} //end ajax error
});
} else {
alert('No save file name specified.');
}
});
}