Business intelligence made simple
Added very basic Elasticsearch adapter
Andrew Kanea2b06eaea4ce
diff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erbindex d111da7f..4bcda230 100644--- a/app/views/blazer/queries/show.html.erb+++ b/app/views/blazer/queries/show.html.erb@@ -172,13 +172,17 @@</script><% end %>-<script>- // do not highlight really long queries- // this can lead to performance issues- if ($("code").text().length < 10000) {- hljs.initHighlightingOnLoad();- }+<% if Blazer.data_sources[@query.data_source].adapter == "activerecord" %>+ <script>+ // do not highlight really long queries+ // this can lead to performance issues+ if ($("code").text().length < 10000) {+ hljs.initHighlightingOnLoad();+ }+ </script>+<% end %>+<script>$(".form-inline input, .form-inline select").change( function () {submitIfCompleted($(this).closest("form"));});diff --git a/lib/blazer.rb b/lib/blazer.rbindex 30cc79bf..fbc4fc32 100644--- a/lib/blazer.rb+++ b/lib/blazer.rb@@ -8,6 +8,7 @@ require "blazer/result"require "blazer/run_statement"require "blazer/adapters/base_adapter"require "blazer/adapters/active_record_adapter"+require "blazer/adapters/elasticsearch_adapter"require "blazer/engine"module Blazerdiff --git a/lib/blazer/adapters/active_record_adapter.rb b/lib/blazer/adapters/active_record_adapter.rbindex d4ca52c9..aed8d7b6 100644--- a/lib/blazer/adapters/active_record_adapter.rb+++ b/lib/blazer/adapters/active_record_adapter.rb@@ -62,7 +62,7 @@ module Blazernilend- private+ protecteddef postgresql?["PostgreSQL", "PostGIS"].include?(adapter_name)@@ -100,19 +100,17 @@ module Blazerenddef in_transaction- if use_transaction?- connection_model.transaction do+ connection_model.connection_pool.with_connection do+ if use_transaction?+ connection_model.transaction do+ yield+ raise ActiveRecord::Rollback+ end+ elseyield- raise ActiveRecord::Rollbackend- else- yieldendend-- def settings- @data_source.settings- endendendenddiff --git a/lib/blazer/adapters/base_adapter.rb b/lib/blazer/adapters/base_adapter.rbindex 7ac1ac58..607bc6a8 100644--- a/lib/blazer/adapters/base_adapter.rb+++ b/lib/blazer/adapters/base_adapter.rb@@ -26,6 +26,12 @@ module Blazerdef explain(statement)# optionalend++ protected++ def settings+ @data_source.settings+ endendendenddiff --git a/lib/blazer/adapters/elasticsearch_adapter.rb b/lib/blazer/adapters/elasticsearch_adapter.rbnew file mode 100644index 00000000..84b6c9e4--- /dev/null+++ b/lib/blazer/adapters/elasticsearch_adapter.rb@@ -0,0 +1,31 @@+module Blazer+ module Adapters+ class ElasticsearchAdapter < BaseAdapter+ def run_statement(statement, comment)+ columns = []+ rows = []+ error = nil++ begin+ response = client.search(body: JSON.parse(statement))+ columns = ["response"]+ rows = [[response]]+ rescue => e+ error = e.message+ end++ [columns, rows, error]+ end++ def tables+ client.indices.get_aliases.map { |k, v| [k, v["aliases"].keys] }.flatten.uniq+ end++ protected++ def client+ @client ||= Elasticsearch::Client.new(url: settings["url"])+ end+ end+ end+enddiff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rbindex c8de1f6c..6b3993c6 100644--- a/lib/blazer/data_source.rb+++ b/lib/blazer/data_source.rb@@ -4,9 +4,9 @@ module Blazerclass DataSourceextend Forwardable- attr_reader :id, :settings, :adapter+ attr_reader :id, :settings, :adapter, :adapter_instance- def_delegators :adapter, :schema, :tables, :reconnect, :cost, :explain+ def_delegators :adapter_instance, :schema, :tables, :reconnect, :cost, :explaindef initialize(id, settings)@id = id@@ -16,7 +16,19 @@ module Blazerraise Blazer::Error, "Empty url"end- @adapter = Blazer::Adapters::ActiveRecordAdapter.new(self)+ @adapter_instance =+ case adapter+ when "activerecord"+ Blazer::Adapters::ActiveRecordAdapter.new(self)+ when "elasticsearch"+ Blazer::Adapters::ElasticsearchAdapter.new(self)+ else+ raise Blazer::Error, "Unknown adapter"+ end+ end++ def adapter+ settings["adapter"] || "activerecord"enddef name@@ -139,7 +151,7 @@ module Blazerdef run_statement_helper(statement, comment, run_id)start_time = Time.now- columns, rows, error = @adapter.run_statement(statement, comment)+ columns, rows, error = @adapter_instance.run_statement(statement, comment)duration = Time.now - start_timecache_data = nildiff --git a/lib/blazer/run_statement_job.rb b/lib/blazer/run_statement_job.rbindex 8f41c18a..15d50c16 100644--- a/lib/blazer/run_statement_job.rb+++ b/lib/blazer/run_statement_job.rb@@ -8,9 +8,7 @@ module Blazerdef perform(result, data_source, statement, options)beginActiveRecord::Base.connection_pool.with_connection do- data_source.adapter.connection_model.connection_pool.with_connection do- result << RunStatement.new.perform(data_source, statement, options)- end+ result << RunStatement.new.perform(data_source, statement, options)endrescue Exception => eresult.clear