diff --git a/lib/logstash/web/public/js/logstash.js b/lib/logstash/web/public/js/logstash.js index eabba8548..738a328e4 100644 --- a/lib/logstash/web/public/js/logstash.js +++ b/lib/logstash/web/public/js/logstash.js @@ -81,6 +81,21 @@ } }); + $("a.pager").live("click", function() { + var href = $(this).attr("href"); + var params = href.replace(/^[^?]*\?/, "").split("&") + for (var p in params) { + var a = params[p].split("="); + console.log(a) + var key = a[0] + var value = a[1] + logstash.params[key] = value + console.log("Setting " + key + " => " + value) + } + logstash.search(logstash.params.q) + return false; + }); + $("a.querychanger").live("click", function() { var href = $(this).attr("href"); var re = new RegExp("[&?]q=([^&]+)"); diff --git a/lib/logstash/web/server.rb b/lib/logstash/web/server.rb index be162b535..69e19a1ef 100755 --- a/lib/logstash/web/server.rb +++ b/lib/logstash/web/server.rb @@ -46,8 +46,8 @@ class LogStash::Web::Server < Sinatra::Base apost '/search/ajax' do headers({"Content-Type" => "text/html" }) - params[:count] = (params[:count] or 50).to_i - params[:offset] = (params[:offset] or 0).to_i + count = params["count"] = (params["count"] or 50).to_i + offset = params["offset"] = (params["offset"] or 0).to_i elasticsearch.search(params) do |@results| @hits = (@results["hits"]["hits"] rescue []) @total = (@results["hits"]["total"] rescue 0) @@ -56,17 +56,30 @@ class LogStash::Web::Server < Sinatra::Base @graphpoints << [entry["key"], entry["count"]] end - if params[:count] and params[:offset] - if @total > (params[:count] + params[:offset]) - @result_end = params[:count] + params[:offset] + if count and offset + if @total > (count + offset) + @result_end = (count + offset) else @result_end = @total end - @result_start = params[:offset] + @result_start = offset end + + if count + offset < @total + next_params = params.clone + next_params["offset"] = [offset + count, @total - count].min + @next_href = "?" + next_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&") + end + + if offset - count > 0 + prev_params = params.clone + prev_params["offset"] = [offset - count, 0].max + @prev_href = "?" + prev_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&") + end + body haml :"search/ajax", :layout => !request.xhr? - end - end + end # elasticsearch.search + end # apost '/search/ajax' end # class LogStashWeb diff --git a/lib/logstash/web/views/search/ajax.haml b/lib/logstash/web/views/search/ajax.haml index 712a86482..a108ce700 100644 --- a/lib/logstash/web/views/search/ajax.haml +++ b/lib/logstash/web/views/search/ajax.haml @@ -14,10 +14,14 @@ %strong Results #{@result_start} - #{@result_end} of #{@total} | - %a - prev - %a - next + - if @prev_href + %a.pager{ :href => @prev_href } + prev + - if @next_href + | + - if @next_href + %a.pager{ :href => @next_href } + next %ul.results - @hits.reverse.each do |hit| %li.event{ :"data-full" => hit.to_json }&= hit["_source"]["@message"]