Class: LogStash::Web::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/logstash/web/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Server

use Rack::ShowExceptions



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/web/server.rb', line 35

def initialize(settings={})
  super
  # TODO(sissel): Support alternate backends
  backend_url = URI.parse(settings.backend_url)

  case backend_url.scheme 
    when "elasticsearch"
      @backend = LogStash::Search::ElasticSearch.new(
        :host => backend_url.host,
        :port => backend_url.port
      )
    when "twitter"
      require "logstash/search/twitter"
      @backend = LogStash::Search::Twitter.new(
        :host => backend_url.host,
        :port => backend_url.port
      )
  end # backend_url.scheme
end

Instance Method Details

#api_searchObject

aget /api/search



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/logstash/web/server.rb', line 130

def api_search

  headers({"Content-Type" => "text/html" })
  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
  )

  @backend.search(query) do |results|
    @results = results
    if @results.error?
      status 500
      case format
      when "html"
        headers({"Content-Type" => "text/html" })
        body haml :"search/error", :layout => !request.xhr?
      when "text"
        headers({"Content-Type" => "text/plain" })
        body erb :"search/error.txt", :layout => false
      when "txt"
        headers({"Content-Type" => "text/plain" })
        body erb :"search/error.txt", :layout => false
      when "json"
        headers({"Content-Type" => "text/plain" })
        # TODO(sissel): issue/30 - needs refactoring here.
        if @results.error?
          body({ "error" => @results.error_message }.to_json)
        else
          body @results.to_json
        end
      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("&")

    case format
    when "html"
      headers({"Content-Type" => "text/html" })
      body haml :"search/ajax", :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.
      response = @results
      body response.to_json
    end # case params[:format]
  end # @backend.search
end