diff options
| author | mo khan <mo@mokhan.ca> | 2015-10-18 21:58:09 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-10-18 21:58:09 -0600 |
| commit | b1601e92e0d7d2f0993df79078ccded7d820a4e2 (patch) | |
| tree | 1bfe3827fdf96bf7a6d4fe1607a509e32cd70517 | |
| parent | 5fbea49f99605e79a63bad0ab8800c0766424e3e (diff) | |
create active record style query interface.
| -rw-r--r-- | lib/killjoy.rb | 2 | ||||
| -rw-r--r-- | lib/killjoy/log_line.rb | 4 | ||||
| -rw-r--r-- | lib/killjoy/query_builder.rb | 19 | ||||
| -rw-r--r-- | lib/killjoy/queryable.rb | 16 | ||||
| -rw-r--r-- | lib/killjoy/web.rb | 23 |
5 files changed, 40 insertions, 24 deletions
diff --git a/lib/killjoy.rb b/lib/killjoy.rb index 4329cab..567449c 100644 --- a/lib/killjoy.rb +++ b/lib/killjoy.rb @@ -1,7 +1,9 @@ +require "active_support/core_ext/string" require "cassandra" require "facter" require "json" require "killjoy/nullable" +require "killjoy/queryable" require "sneakers" require "spank" require "virtus" diff --git a/lib/killjoy/log_line.rb b/lib/killjoy/log_line.rb index f9d6db9..d65f793 100644 --- a/lib/killjoy/log_line.rb +++ b/lib/killjoy/log_line.rb @@ -1,8 +1,10 @@ module Killjoy class LogLine + include Virtus.model + include Queryable + NULL = Nullable.new - include Virtus.model attribute :http_status, Integer attribute :http_verb, String attribute :http_version, String diff --git a/lib/killjoy/query_builder.rb b/lib/killjoy/query_builder.rb index 413f455..2b94fdf 100644 --- a/lib/killjoy/query_builder.rb +++ b/lib/killjoy/query_builder.rb @@ -8,6 +8,11 @@ module Killjoy @criteria = [] end + def map_using(mapper) + @mapper = mapper + self + end + def where(options) options.each do |(column, value)| @criteria << { @@ -21,9 +26,9 @@ module Killjoy def contains(options) options.each do |(column, value)| @criteria << { - cql: "AND #{column} CONTAINS :#{column}", - binding: [column.to_sym, value] - } + cql: "AND #{column} CONTAINS :#{column}", + binding: [column.to_sym, value] + } end self end @@ -44,16 +49,10 @@ module Killjoy end def each(&block) - @results ||= run + @results ||= @mapper ? run.map { |x| @mapper.new(x) } : run @results.each(&block) end - def map_as(model) - map do |row| - model.new(row) - end - end - private def run diff --git a/lib/killjoy/queryable.rb b/lib/killjoy/queryable.rb new file mode 100644 index 0000000..7d35bdc --- /dev/null +++ b/lib/killjoy/queryable.rb @@ -0,0 +1,16 @@ +require 'active_support/concern' + +module Killjoy + module Queryable + extend ActiveSupport::Concern + class_methods do + def all + CassandraDb.from(table_name).map_using(self) + end + + def table_name + @table_name ||= self.name.tableize.split('/').last.to_sym + end + end + end +end diff --git a/lib/killjoy/web.rb b/lib/killjoy/web.rb index da16a35..4c59657 100644 --- a/lib/killjoy/web.rb +++ b/lib/killjoy/web.rb @@ -1,11 +1,6 @@ require 'killjoy' require 'sinatra' - -helpers do - def h(text) - Rack::Utils.escape_html(text) - end -end +require 'tilt/erb' set :bind, '0.0.0.0' set :port, 9292 @@ -16,18 +11,20 @@ Killjoy::Startup.new(Spank::Container.new).run do |container| Spank::IOC.resolve(:session).execute("select * from system.hints;") end +helpers do + def h(text) + Rack::Utils.escape_html(text) + end +end + get '/' do - @logs = Killjoy::CassandraDb - .from(:log_lines) - .map_as(Killjoy::LogLine) + @logs = Killjoy::LogLine.all erb :index end get '/ip/:ipaddress' do - @logs = Killjoy::CassandraDb - .from(:log_lines) - .where(ipaddress: IPAddr.new(params['ipaddress'])) - .map_as(Killjoy::LogLine) + @ipaddress = IPAddr.new(params['ipaddress']) + @logs = Killjoy::LogLine.all.where(ipaddress: @ipaddress) erb :index end |
