- fix controller splitting

This commit is contained in:
Jordan Sissel 2011-04-19 03:54:43 -07:00
parent 90a5e5aa3d
commit acd8ff575c
4 changed files with 224 additions and 218 deletions

View file

@ -3,144 +3,143 @@ require "logstash/search/query"
require "logstash/web/helpers/require_param"
require "sinatra/base" # gem sinatra
# TODO(sissel): facter out the emitter (json pretty print handling, etc)
helpers Sinatra::RequireParam
class LogStash::Web::Server < Sinatra::Base
# TODO(sissel): move this to a lib?
# Or a LogStash::Search::WebHelper or something?
def api_search
count = params["count"] = (params["count"] or 50).to_i
offset = params["offset"] = (params["offset"] or 0).to_i
format = (params[:format] or "json")
# TODO(sissel): move this to a lib?
# Or a LogStash::Search::WebHelper or something?
def api_search
count = params["count"] = (params["count"] or 50).to_i
offset = params["offset"] = (params["offset"] or 0).to_i
format = (params[:format] or "json")
query = LogStash::Search::Query.new(
:query_string => params[:q],
:offset => offset,
:count => count
)
query = LogStash::Search::Query.new(
:query_string => params[:q],
:offset => offset,
:count => count
)
@backend.search(query, async=false) do |results|
@results = results
if @results.error?
status 500
case format
when "html"
content_type :html
body haml :"search/error", :layout => !request.xhr?
when "text"
content_type :txt
body erb :"search/error.txt", :layout => false
when "txt"
content_type :txt
body erb :"search/error.txt", :layout => false
when "json"
content_type :json
# TODO(sissel): issue/30 - needs refactoring here.
body({ "error" => @results.error_message }.to_json)
end # case params[:format]
next
end
@events = @results.events
@total = (@results.total rescue 0)
count = @results.events.size
if count and offset
if @total > (count + offset)
@result_end = (count + offset)
else
@result_end = @total
end
@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("&")
last_params = next_params.clone
last_params["offset"] = @total - count
@last_href = "?" + last_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
end
if offset > 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("&")
#if prev_params["offset"] > 0
first_params = prev_params.clone
first_params["offset"] = 0
@first_href = "?" + first_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
#end
end
# TODO(sissel): make a helper function taht goes hash -> cgi querystring
@refresh_href = "?" + params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
@backend.search(query, async=false) do |results|
@results = results
if @results.error?
status 500
case format
when "html"
content_type :html
body haml :"search/error", :layout => !request.xhr?
body haml :"search/ajax", :layout => !request.xhr?
when "text"
content_type :txt
body erb :"search/error.txt", :layout => false
body erb :"search/results.txt", :layout => false
when "txt"
content_type :txt
body erb :"search/error.txt", :layout => false
body erb :"search/results.txt", :layout => false
when "json"
content_type :json
# TODO(sissel): issue/30 - needs refactoring here.
body({ "error" => @results.error_message }.to_json)
pretty = params.has_key?("pretty")
if pretty
body JSON.pretty_generate(@results.to_hash)
else
body @results.to_json
end
end # case params[:format]
end # @backend.search
end # def api_search
# TODO(sissel): Update these to all be /api/v1
post '/api/search' do
api_search
end # post /api/search
get '/api/search' do
api_search
end # get /api/search
get '/api/histogram' do
missing = require_param(:q)
if !missing.empty?
status 500
body({ "error" => "Missing requiremed parameters",
"missing" => missing }.to_json)
next
end # if !missing.empty?
format = (params[:format] or "json") # default json
field = (params[:field] or "@timestamp") # default @timestamp
interval = (params[:interval] or 3600000).to_i # default 1 hour
results = @backend.histogram(params[:q], field, interval, async=false)
p :results => results
if results.error?
status 500
body({ "error" => results.error_message }.to_json)
next
end
@events = @results.events
@total = (@results.total rescue 0)
count = @results.events.size
if count and offset
if @total > (count + offset)
@result_end = (count + offset)
else
@result_end = @total
end
@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("&")
last_params = next_params.clone
last_params["offset"] = @total - count
@last_href = "?" + last_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
end
if offset > 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("&")
#if prev_params["offset"] > 0
first_params = prev_params.clone
first_params["offset"] = 0
@first_href = "?" + first_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
#end
end
# TODO(sissel): make a helper function taht goes hash -> cgi querystring
@refresh_href = "?" + params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
case format
when "html"
content_type :html
body haml :"search/ajax", :layout => !request.xhr?
when "text"
content_type :txt
body erb :"search/results.txt", :layout => false
when "txt"
content_type :txt
body erb :"search/results.txt", :layout => false
when "json"
begin
json = results.results.to_json
p :json => json
content_type :json
pretty = params.has_key?("pretty")
if pretty
body JSON.pretty_generate(@results.to_hash)
else
body @results.to_json
end
end # case params[:format]
end # @backend.search
end # def api_search
# TODO(sissel): Update these to all be /api/v1
post '/api/search' do
api_search
end # post /api/search
get '/api/search' do
api_search
end # get /api/search
get '/api/histogram' do
missing = require_param(:q)
if !missing.empty?
status 500
body({ "error" => "Missing requiremed parameters",
"missing" => missing }.to_json)
next
end # if !missing.empty?
format = (params[:format] or "json") # default json
field = (params[:field] or "@timestamp") # default @timestamp
interval = (params[:interval] or 3600000).to_i # default 1 hour
results = @backend.histogram(params[:q], field, interval, async=false)
p :results => results
if results.error?
status 500
body({ "error" => results.error_message }.to_json)
next
end
begin
json = results.results.to_json
p :json => json
content_type :json
status 200
body json
rescue => e
p :exception => e
p e
raise e
end
end # get '/api/histogram'
status 200
body json
rescue => e
p :exception => e
p e
raise e
end
end # get '/api/histogram'
end # class LogStash::Web::Server

View file

@ -1,60 +1,60 @@
class LogStash::Web::Server < Sinatra::Base
get '/search' do
# TODO(sissel): Refactor this to use the api_search
result_callback = proc do |results|
status 500 if @error
@results = results
get '/search' do
# TODO(sissel): Refactor this to use the api_search
result_callback = proc do |results|
status 500 if @error
@results = results
p :got => results
params[:format] ||= "html"
case params[:format]
when "html"
headers({"Content-Type" => "text/html" })
body haml :"search/results", :layout => !request.xhr?
when "text"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "txt"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "json"
headers({"Content-Type" => "text/plain" })
# TODO(sissel): issue/30 - needs refactoring here.
hits = @hits.collect { |h| h["_source"] }
response = {
"hits" => hits,
}
response["error"] = @error if @error
body response.to_json
end # case params[:format]
end # proc result_callback
# We'll still do a search query here even though most users
# have javascript enabled, we need to show the results in
# case a user doesn't have javascript.
if params[:q] and params[:q] != ""
query = LogStash::Search::Query.new(
:query_string => params[:q],
:offset => params[:offset],
:count => params[:count]
)
@backend.search(query) do |results|
p :got => results
begin
result_callback.call results
rescue => e
p :exception => e
end
end # @backend.search
else
results = LogStash::Search::Result.new(
:events => [],
:error_message => "No query given"
)
result_callback.call results
end
end # get '/search'
params[:format] ||= "html"
case params[:format]
when "html"
headers({"Content-Type" => "text/html" })
body haml :"search/results", :layout => !request.xhr?
when "text"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "txt"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "json"
headers({"Content-Type" => "text/plain" })
# TODO(sissel): issue/30 - needs refactoring here.
hits = @hits.collect { |h| h["_source"] }
response = {
"hits" => hits,
}
response["error"] = @error if @error
body response.to_json
end # case params[:format]
end # proc result_callback
# We'll still do a search query here even though most users
# have javascript enabled, we need to show the results in
# case a user doesn't have javascript.
if params[:q] and params[:q] != ""
query = LogStash::Search::Query.new(
:query_string => params[:q],
:offset => params[:offset],
:count => params[:count]
)
@backend.search(query) do |results|
p :got => results
begin
result_callback.call results
rescue => e
p :exception => e
end
end # @backend.search
else
results = LogStash::Search::Result.new(
:events => [],
:error_message => "No query given"
)
result_callback.call results
end
end # get '/search'
end # class LogStash::Web::Server

View file

@ -1,36 +1,38 @@
class LogStash::Web::Server < Sinatra::Base
# Mizuno can't serve static files from a jar
# https://github.com/matadon/mizuno/issues/9
#if __FILE__ =~ /^file:.+!.+$/
get '/js/*' do static_file end
get '/css/*' do static_file end
get '/media/*' do static_file end
get '/ws/*' do static_file end
#else
## If here, we aren't running from a jar; safe to serve files
## through the normal public handler.
#set :public, "#{File.dirname(__FILE__)}/public"
#end
# Mizuno can't serve static files from a jar
# https://github.com/matadon/mizuno/issues/9
#if __FILE__ =~ /^file:.+!.+$/
get '/js/*' do static_file end
get '/css/*' do static_file end
get '/media/*' do static_file end
get '/ws/*' do static_file end
#else
## If here, we aren't running from a jar; safe to serve files
## through the normal public handler.
#set :public, "#{File.dirname(__FILE__)}/public"
#end
def static_file
# request.path_info is the full path of the request.
path = File.join(File.dirname(__FILE__), "..", "public", *request.path_info.split("/"))
p :static => path
if File.exists?(path)
ext = path.split(".").last
case ext
when "js"; content_type "application/javascript"
when "css"; content_type "text/css"
when "jpg"; content_type "image/jpeg"
when "jpeg"; content_type "image/jpeg"
when "png"; content_type "image/png"
when "gif"; content_type "image/gif"
end
def static_file
# request.path_info is the full path of the request.
path = File.join(File.dirname(__FILE__), "..", "public", *request.path_info.split("/"))
p :static => path
if File.exists?(path)
ext = path.split(".").last
case ext
when "js"; content_type "application/javascript"
when "css"; content_type "text/css"
when "jpg"; content_type "image/jpeg"
when "jpeg"; content_type "image/jpeg"
when "png"; content_type "image/png"
when "gif"; content_type "image/gif"
body File.new(path, "r").read
else
status 404
content_type "text/plain"
body "File not found: #{path}"
end
end # def static_file
end # class LogStash::Web::Server
body File.new(path, "r").read
else
status 404
content_type "text/plain"
body "File not found: #{path}"
end
end # def static_file

View file

@ -16,6 +16,7 @@ require "rack" # gem rack
require "mizuno" # gem mizuno
require "sinatra/base" # gem sinatra
class LogStash::Web::Server < Sinatra::Base
mime_type :html, "text/html"
mime_type :txt, "text/plain"
@ -25,6 +26,10 @@ class LogStash::Web::Server < Sinatra::Base
mime_type :jpg, "image/jpeg"
mime_type :png, "image/png"
require "logstash/web/controllers/api_v1"
require "logstash/web/controllers/static_files"
require "logstash/web/controllers/search"
#register Sinatra::Async
helpers Sinatra::RequireParam # logstash/web/helpers/require_param
@ -40,11 +45,11 @@ class LogStash::Web::Server < Sinatra::Base
# instances variables like @backend, etc.
#
# Load anything in controllers/
Dir.glob(File.join(File.dirname(__FILE__), "controllers", "**", "*")).each do |path|
puts "Loading #{path}"
# TODO(sissel): This is pretty shitty.
eval(File.new(path).read, binding, path)
end
#Dir.glob(File.join(File.dirname(__FILE__), "controllers", "**", "*")).each do |path|
#puts "Loading #{path}"
## TODO(sissel): This is pretty shitty.
#eval(File.new(path).read, binding, path)
#end
def initialize(settings={})
super