Business intelligence made simple
Added support for multiple data sources
Andrew Kanefc0c26c142c0
diff --git a/README.md b/README.mdindex e04d193b..e378145f 100644--- a/README.md+++ b/README.md@@ -188,7 +188,7 @@ with```rubycreate_table :blazer_checks do |t|- t.references :blazer_query+ t.references :queryt.string :statet.text :emailst.timestampsdiff --git a/app/controllers/blazer/base_controller.rb b/app/controllers/blazer/base_controller.rbindex 7494633b..3598135a 100644--- a/app/controllers/blazer/base_controller.rb+++ b/app/controllers/blazer/base_controller.rb@@ -41,7 +41,7 @@ module Blazerhelper_method :extract_varsdef variable_params- params.except(:controller, :action, :id, :host, :query, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement)+ params.except(:controller, :action, :id, :host, :query, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement, :data_source)endhelper_method :variable_paramsenddiff --git a/app/controllers/blazer/checks_controller.rb b/app/controllers/blazer/checks_controller.rbindex 8766f904..dc3fdf0a 100644--- a/app/controllers/blazer/checks_controller.rb+++ b/app/controllers/blazer/checks_controller.rb@@ -3,8 +3,8 @@ module Blazerbefore_action :set_check, only: [:edit, :update, :destroy, :run]def index- @checks = Blazer::Check.joins(:blazer_query).includes(:blazer_query).order("state, blazer_queries.name, blazer_checks.id").to_a- @checks.select! { |c| "#{c.blazer_query.name} #{c.emails}".downcase.include?(params[:q]) } if params[:q]+ @checks = Blazer::Check.joins(:query).includes(:query).order("state, blazer_queries.name, blazer_checks.id").to_a+ @checks.select! { |c| "#{c.query.name} #{c.emails}".downcase.include?(params[:q]) } if params[:q]enddef new@@ -35,13 +35,13 @@ module Blazerenddef run- @query = @check.blazer_query+ @query = @check.queryendprivatedef check_params- params.require(:check).permit(:blazer_query_id, :emails)+ params.require(:check).permit(:query_id, :emails)enddef set_checkdiff --git a/app/controllers/blazer/dashboard_queries_controller.rb b/app/controllers/blazer/dashboard_queries_controller.rbdeleted file mode 100644index b7e99f27..00000000--- a/app/controllers/blazer/dashboard_queries_controller.rb+++ /dev/null@@ -1,26 +0,0 @@-module Blazer- class DashboardQueriesController < BaseController- def create- @dashboard_query = Blazer::DashboardQuery.new(dashboard_query_params)- @dashboard_query.position = @dashboard_query.blazer_dashboard.blazer_dashboard_queries.maximum(:position).to_i + 1-- if @dashboard_query.save- redirect_to dashboard_path(@dashboard_query.blazer_dashboard_id)- else- raise "boom"- end- end-- def update- end-- def destroy- end-- protected-- def dashboard_query_params- params.require(:dashboard_query).permit(:blazer_dashboard_id, :blazer_query_id)- end- end-enddiff --git a/app/controllers/blazer/dashboards_controller.rb b/app/controllers/blazer/dashboards_controller.rbindex 08ba06e3..0a938aef 100644--- a/app/controllers/blazer/dashboards_controller.rb+++ b/app/controllers/blazer/dashboards_controller.rb@@ -21,7 +21,7 @@ module Blazerenddef show- @queries = @dashboard.blazer_dashboard_queries.order(:position).preload(:blazer_query).map(&:blazer_query)+ @queries = @dashboard.dashboard_queries.order(:position).preload(:query).map(&:query)@queries.each do |query|process_vars(query.statement)end@@ -29,12 +29,15 @@ module Blazer@smart_vars = {}@sql_errors = []+ data_sources = @queries.map { |q| Blazer.data_sources[q.data_source] }.uniq@bind_vars.each do |var|- query = Blazer.smart_variables[var]- if query- rows, error = Blazer.run_statement(query)- @smart_vars[var] = rows.map { |v| v.values.reverse }- @sql_errors << error if error+ data_sources.each do |data_source|+ query = data_source.smart_variables[var]+ if query+ rows, error = data_source.run_statement(query)+ (@smart_vars[var] ||= []).concat rows.map { |v| v.values.reverse }+ @sql_errors << error if error+ endendendend@@ -68,19 +71,19 @@ module Blazerdef update_dashboard(dashboard)dashboard.assign_attributes(dashboard_params)Blazer::Dashboard.transaction do- if params[:blazer_query_ids].is_a?(Array)- query_ids = params[:blazer_query_ids].map(&:to_i)+ if params[:query_ids].is_a?(Array)+ query_ids = params[:query_ids].map(&:to_i)@queries = Blazer::Query.find(query_ids).sort_by { |q| query_ids.index(q.id) }endif dashboard.saveif @queries@queries.each_with_index do |query, i|- dashboard_query = dashboard.blazer_dashboard_queries.where(blazer_query_id: query.id).first_or_initialize+ dashboard_query = dashboard.dashboard_queries.where(query_id: query.id).first_or_initializedashboard_query.position = idashboard_query.save!endif dashboard.persisted?- dashboard.blazer_dashboard_queries.where.not(blazer_query_id: query_ids).destroy_all+ dashboard.dashboard_queries.where.not(query_id: query_ids).destroy_allendendtruediff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rbindex 081de973..b9a6851b 100644--- a/app/controllers/blazer/queries_controller.rb+++ b/app/controllers/blazer/queries_controller.rb@@ -6,11 +6,11 @@ module Blazer@queries = Blazer::Query.order(:name)@queries = @queries.includes(:creator) if Blazer.user_class@trending_queries = Blazer::Audit.group(:query_id).where("created_at > ?", 2.days.ago).having("COUNT(DISTINCT user_id) >= 3").uniq.count(:user_id)- @checks = Blazer::Check.group(:blazer_query_id).count+ @checks = Blazer::Check.group(:query_id).countenddef new- @query = Blazer::Query.new(statement: params[:statement])+ @query = Blazer::Query.new(statement: params[:statement], data_source: params[:data_source])enddef create@@ -30,10 +30,11 @@ module Blazer@smart_vars = {}@sql_errors = []+ data_source = Blazer.data_sources[@query.data_source]@bind_vars.each do |var|- query = Blazer.smart_variables[var]+ query = data_source.smart_variables[var]if query- rows, error = Blazer.run_statement(query)+ rows, error = data_source.run_statement(query)@smart_vars[var] = rows.map { |v| v.values.reverse }@sql_errors << error if errorend@@ -51,18 +52,23 @@ module Blazerif @success@query = Query.find_by(id: params[:query_id]) if params[:query_id]+ data_source = params[:data_source]+ data_source = @query.data_source if @query && @query.data_source+# auditif Blazer.auditaudit = Blazer::Audit.new(statement: @statement)audit.query = @query+ audit.data_source = data_sourceaudit.user = current_user if respond_to?(:current_user) && Blazer.user_classaudit.save!end- @rows, @error = Blazer.run_statement(@statement)+ @data_source = Blazer.data_sources[data_source]+ @rows, @error = @data_source.run_statement(@statement)if @query && [email protected]_s.include?("canceling statement due to statement timeout")- @query.blazer_checks.each do |check|+ @query.checks.each do |check|check.update_state(@rows, @error)endend@@ -84,19 +90,19 @@ module Blazer@filename = @query.name.parameterize if @query- @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || Blazer.smart_columns[k] }.keys+ @min_width_types = (@rows.first || {}).select { |k, v| v.is_a?(Time) || v.is_a?(String) || @data_source.smart_columns[k] }.keys@boom = {}@columns.keys.each do |key|- query = Blazer.smart_columns[key]+ query = @data_source.smart_columns[key]if queryvalues = @rows.map { |r| r[key] }.compact.uniq- rows, error = Blazer.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))+ rows, error = @data_source.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))@boom[key] = Hash[rows.map(&:values)]endend- @linked_columns = Blazer.linked_columns+ @linked_columns = @data_source.linked_columnsendrespond_to do |format|@@ -122,6 +128,11 @@ module Blazerredirect_to root_urlend+ def tables+ @data_source = params[:data_source]+ render layout: false+ end+privatedef set_query@@ -129,7 +140,7 @@ module Blazerenddef query_params- params.require(:query).permit(:name, :description, :statement)+ params.require(:query).permit(:name, :description, :statement, :data_source)enddef csv_data(rows)diff --git a/app/mailers/blazer/check_mailer.rb b/app/mailers/blazer/check_mailer.rbindex e6a31b4e..6f2f71c5 100644--- a/app/mailers/blazer/check_mailer.rb+++ b/app/mailers/blazer/check_mailer.rb@@ -10,7 +10,7 @@ module Blazer@state_was = state_was@rows_count = rows_count@error = error- mail to: check.emails, subject: "Check #{state.titleize}: #{check.blazer_query.name}"+ mail to: check.emails, subject: "Check #{state.titleize}: #{check.query.name}"enddef failing_checks(email, checks)diff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rbindex 2ca3f43e..517fdbda 100644--- a/app/models/blazer/check.rb+++ b/app/models/blazer/check.rb@@ -1,8 +1,8 @@module Blazerclass Check < ActiveRecord::Base- belongs_to :blazer_query, class_name: "Blazer::Query"+ belongs_to :query- validates :blazer_query_id, presence: true+ validates :query_id, presence: truedef split_emailsemails.to_s.split(",").map(&:strip)diff --git a/app/models/blazer/connection.rb b/app/models/blazer/connection.rbindex 4cf2d0d0..cebf906b 100644--- a/app/models/blazer/connection.rb+++ b/app/models/blazer/connection.rb@@ -1,6 +1,5 @@module Blazerclass Connection < ActiveRecord::Base- establish_connection ENV["BLAZER_DATABASE_URL"] if ENV["BLAZER_DATABASE_URL"]self.abstract_class = trueendenddiff --git a/app/models/blazer/dashboard.rb b/app/models/blazer/dashboard.rbindex c5a40377..a7202b77 100644--- a/app/models/blazer/dashboard.rb+++ b/app/models/blazer/dashboard.rb@@ -1,7 +1,7 @@module Blazerclass Dashboard < ActiveRecord::Base- has_many :blazer_dashboard_queries, class_name: "Blazer::DashboardQuery", foreign_key: "blazer_dashboard_id", dependent: :destroy- has_many :blazer_queries, class_name: "Blazer::Query", through: :blazer_dashboard_queries+ has_many :dashboard_queries, dependent: :destroy+ has_many :queries, through: :dashboard_queriesvalidates :name, presence: truediff --git a/app/models/blazer/dashboard_query.rb b/app/models/blazer/dashboard_query.rbindex 22544502..330bc7fc 100644--- a/app/models/blazer/dashboard_query.rb+++ b/app/models/blazer/dashboard_query.rb@@ -1,9 +1,9 @@module Blazerclass DashboardQuery < ActiveRecord::Base- belongs_to :blazer_dashboard, class_name: "Blazer::Dashboard"- belongs_to :blazer_query, class_name: "Blazer::Query"+ belongs_to :dashboard+ belongs_to :query- validates :blazer_dashboard_id, presence: true- validates :blazer_query_id, presence: true+ validates :dashboard_id, presence: true+ validates :query_id, presence: trueendenddiff --git a/app/models/blazer/query.rb b/app/models/blazer/query.rbindex fab87b1b..4aae9cee 100644--- a/app/models/blazer/query.rb+++ b/app/models/blazer/query.rb@@ -1,7 +1,7 @@module Blazerclass Query < ActiveRecord::Basebelongs_to :creator, class_name: Blazer.user_class.to_s if Blazer.user_class- has_many :blazer_checks, class_name: "Blazer::Check", foreign_key: "blazer_query_id", dependent: :destroy+ has_many :checks, dependent: :destroyvalidates :name, presence: truevalidates :statement, presence: truediff --git a/app/views/blazer/check_mailer/failing_checks.html.erb b/app/views/blazer/check_mailer/failing_checks.html.erbindex 05bde140..71264daf 100644--- a/app/views/blazer/check_mailer/failing_checks.html.erb+++ b/app/views/blazer/check_mailer/failing_checks.html.erb@@ -1,6 +1,6 @@<ul><% @checks.each do |check| %>- <li><%= link_to check.blazer_query.name, query_url(check.blazer_query_id) %></li>+ <li><%= link_to check.query.name, query_url(check.query_id) %></li><% end %></ul><p><%= link_to "Manage checks", checks_url %></p>diff --git a/app/views/blazer/check_mailer/state_change.html.erb b/app/views/blazer/check_mailer/state_change.html.erbindex da513626..2422e00c 100644--- a/app/views/blazer/check_mailer/state_change.html.erb+++ b/app/views/blazer/check_mailer/state_change.html.erb@@ -1,4 +1,4 @@-<p><%= link_to "View", query_url(@check.blazer_query_id) %></p>+<p><%= link_to "View", query_url(@check.query_id) %></p><% if @error %><p><%= @error %></p><% elsif @rows_count > 0 %>diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erbindex 27e3864a..d79dbd4e 100644--- a/app/views/blazer/checks/_form.html.erb+++ b/app/views/blazer/checks/_form.html.erb@@ -6,12 +6,12 @@<%= form_for @check do |f| %><div class="form-group">- <%= f.label :blazer_query_id, "Query" %>+ <%= f.label :query_id, "Query" %><div class="hide">- <%= f.select :blazer_query_id, Blazer::Query.order(:name).map { |q| [q.name, q.id] }, {include_blank: true} %>+ <%= f.select :query_id, Blazer::Query.order(:name).map { |q| [q.name, q.id] }, {include_blank: true} %></div><script>- $("#check_blazer_query_id").selectize().parents(".hide").removeClass("hide");+ $("#check_query_id").selectize().parents(".hide").removeClass("hide");</script></div><div class="form-group">diff --git a/app/views/blazer/checks/index.html.erb b/app/views/blazer/checks/index.html.erbindex 9016c8f2..8941d6d7 100644--- a/app/views/blazer/checks/index.html.erb+++ b/app/views/blazer/checks/index.html.erb@@ -18,7 +18,7 @@<tbody><% @checks.each do |check| %><tr>- <td><%= link_to check.blazer_query.name, check.blazer_query %></td>+ <td><%= link_to check.query.name, check.query %></td><td><% if check.state %><small style="font-weight: bold; color: <%= colors[check.state.to_sym] %>;"><%= check.state.upcase %></small>diff --git a/app/views/blazer/dashboards/_form.html.erb b/app/views/blazer/dashboards/_form.html.erbindex a7d2386f..cd9796eb 100644--- a/app/views/blazer/dashboards/_form.html.erb+++ b/app/views/blazer/dashboards/_form.html.erb@@ -19,26 +19,26 @@ li:hover .glyphicon-remove {<%= f.label :name %><%= f.text_field :name, class: "form-control" %></div>- <div class="form-group <%= "hide" if (@queries || @dashboard.blazer_queries).empty? %>">+ <div class="form-group <%= "hide" if (@queries || @dashboard.queries).empty? %>"><%= f.label :charts %><ul class="list-group">- <% (@queries || @dashboard.blazer_dashboard_queries.order(:position).map(&:blazer_query)).each do |query| %>+ <% (@queries || @dashboard.dashboard_queries.order(:position).map(&:query)).each do |query| %><li class="list-group-item"><span class="glyphicon glyphicon-remove" aria-hidden="true" style="float: right; margin-top: 3px;"></span><%= query.name %>- <%= hidden_field_tag "blazer_query_ids[]", query.id %>+ <%= hidden_field_tag "query_ids[]", query.id %></li><% end %></ul></div><div class="form-group">- <%= f.label :blazer_query_id, "Add Chart" %>+ <%= f.label :query_id, "Add Chart" %><div class="hide">- <%= select_tag :blazer_query_id, options_for_select(Blazer::Query.order(:name).map { |q| [q.name, q.id] }, {include_blank: true}) %>+ <%= select_tag :query_id, options_for_select(Blazer::Query.order(:name).map { |q| [q.name, q.id] }, {include_blank: true}) %></div><script>- $("#blazer_query_id").selectize({allowEmptyOption: true}).parents(".hide").removeClass("hide");- $("#blazer_query_id").change( function () {+ $("#query_id").selectize({allowEmptyOption: true}).parents(".hide").removeClass("hide");+ $("#query_id").change( function () {var $option = $(this).find("option:selected");if ($option.val() !== "") {// console.log($option.val());@@ -46,7 +46,7 @@ li:hover .glyphicon-remove {var $li = $("<li></li>");$li.addClass("list-group-item");$li.text($option.text());- $li.prepend('<span class="glyphicon glyphicon-remove" aria-hidden="true" style="float: right; margin-top: 3px;"></span><input type="hidden" name="blazer_query_ids[]" id="blazer_query_ids_" value="' + $option.val() + '">');+ $li.prepend('<span class="glyphicon glyphicon-remove" aria-hidden="true" style="float: right; margin-top: 3px;"></span><input type="hidden" name="query_ids[]" id="query_ids_" value="' + $option.val() + '">');$(".list-group").append($li);$(this).val("");$(".form-group").removeClass("hide");diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erbindex e9884621..66c31c23 100644--- a/app/views/blazer/queries/_form.html.erb+++ b/app/views/blazer/queries/_form.html.erb@@ -14,9 +14,12 @@<div class="form-group text-right"><div class="pull-left" style="margin-top: 6px;"><%= link_to "Back", :back %>- <span class="text-muted" style="margin-left: 20px;"> Use {start_time} and {end_time} for time ranges</span>+ <% if Blazer.data_sources.size == 1 %>+ <span class="text-muted" style="margin-left: 20px;"> Use {start_time} and {end_time} for time ranges</span>+ <% end %></div>- <%= select_tag :table_names, options_for_select([["Preview table", nil]] + Blazer.tables.keys), style: "margin-right: 20px; width: 240px;" %>+ <%= f.select :data_source, Blazer.data_sources.values.map { |ds| [ds.name, ds.id] }, {}, class: ("hide" if Blazer.data_sources.size == 1), style: "width: 140px;" %>+ <div id="tables" style="display: inline-block; width: 260px; margin-right: 10px;"></div><%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top;" %></div></div>@@ -99,7 +102,7 @@if (xhr) {xhr.abort();}- xhr = $.post("<%= run_queries_path %>", $.extend({}, params, {statement: editor.getValue()}), function (data) {+ xhr = $.post("<%= run_queries_path %>", $.extend({}, params, {statement: editor.getValue(), data_source: $("#query_data_source").val()}), function (data) {$("#results").html(data);error_line = /LINE (\d+)/g.exec($("#results").find('.alert-danger').text());@@ -118,8 +121,7 @@$("#run").click();}- $("#table_names").selectize({- }).change( function () {+ $(document).on("change", "#table_names", function () {var val = $(this).val();if (val.length > 0) {editor.setValue("SELECT * FROM " + val + " LIMIT 10");@@ -127,6 +129,14 @@}});+ function updatePreviewSelect() {+ $("#tables").load("<%= tables_queries_path %>?" + $.param({data_source: $("#query_data_source").val()}), function () {+ $("#table_names").selectize({});+ });+ }+ updatePreviewSelect();+ $("#query_data_source").selectize().change(updatePreviewSelect);+$("form.the_form").on("submit", function() {$("#query_statement").val(editor.getValue());return true;diff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erbindex 015cec17..a60e178f 100644--- a/app/views/blazer/queries/show.html.erb+++ b/app/views/blazer/queries/show.html.erb@@ -25,7 +25,7 @@</div><div class="col-sm-3 text-right"><%= link_to "Edit", edit_query_path(@query, variable_params), class: "btn btn-default" %>- <%= link_to "Fork", new_query_path(variable_params.merge(statement: @query.statement)), class: "btn btn-info" %>+ <%= link_to "Fork", new_query_path(variable_params.merge(statement: @query.statement, data_source: @query.data_source)), class: "btn btn-info" %><% if !@error && @success %><%= button_to "Download", run_queries_path(statement: @statement, query_id: @query.id, format: "csv"), class: "btn btn-primary" %>diff --git a/app/views/blazer/queries/tables.html b/app/views/blazer/queries/tables.htmlnew file mode 100644index 00000000..9ee677e2--- /dev/null+++ b/app/views/blazer/queries/tables.html@@ -0,0 +1 @@+<%= select_tag :table_names, options_for_select([["Preview table", nil]] + Blazer.data_sources[@data_source].tables.keys), style: "width: 240px;" %>diff --git a/config/routes.rb b/config/routes.rbindex c7109ea9..d36b4fc4 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1,11 +1,11 @@Blazer::Engine.routes.draw doresources :queries, except: [:index] dopost :run, on: :collection # err on the side of caution+ get :tables, on: :collectionendresources :checks, except: [:show] doget :run, on: :memberendresources :dashboards- resources :dashboard_queries, only: [:create, :destroy]root to: "queries#index"enddiff --git a/lib/blazer.rb b/lib/blazer.rbindex 6cec7686..3d1efdd8 100644--- a/lib/blazer.rb+++ b/lib/blazer.rb@@ -1,6 +1,7 @@require "csv"require "chartkick"require "blazer/version"+require "blazer/data_source"require "blazer/engine"require "blazer/tasks"@@ -10,74 +11,39 @@ module Blazerattr_reader :time_zoneattr_accessor :user_nameattr_accessor :user_class- attr_accessor :timeoutattr_accessor :from_emailendself.audit = trueself.user_name = :name- self.timeout = 15def self.time_zone=(time_zone)@time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]enddef self.settings- @settings ||= YAML.load(File.read(Rails.root.join("config", "blazer.yml")))+ @settings ||= YAML.load(ERB.new(File.read(Rails.root.join("config", "blazer.yml"))).result)end- def self.linked_columns- settings["linked_columns"] || {}- end-- def self.smart_columns- settings["smart_columns"] || {}- end-- def self.smart_variables- settings["smart_variables"] || {}- end-- def self.run_statement(statement)- rows = []- error = nil- begin- Blazer::Connection.transaction do- Blazer::Connection.connection.execute("SET statement_timeout = #{Blazer.timeout * 1000}") if Blazer.timeout && postgresql?- result = Blazer::Connection.connection.select_all(statement)- result.each do |untyped_row|- row = {}- untyped_row.each do |k, v|- row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v)- end- rows << row+ def self.data_sources+ @data_sources ||= begin+ ds = Hash[+ settings["data_sources"].map do |id, s|+ [id, Blazer::DataSource.new(id, s)]end- raise ActiveRecord::Rollback- end- rescue ActiveRecord::StatementInvalid => e- error = e.message.sub(/.+ERROR: /, "")+ ]+ ds.default = ds.values.first+ dsend- [rows, error]- end-- def self.tables- default_schema = postgresql? ? "public" : Blazer::Connection.connection_config[:database]- schema = Blazer::Connection.connection_config[:schema] || default_schema- rows, error = run_statement(Blazer::Connection.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))- Hash[rows.group_by { |r| r["table_name"] }.map { |t, f| [t, f.sort_by { |f| f["ordinal_position"] }.map { |f| f.slice("column_name", "data_type") }] }.sort_by { |t, _f| t }]- end-- def self.postgresql?- ["PostgreSQL", "Redshift"].include?(Blazer::Connection.connection.adapter_name)enddef self.run_checks- Blazer::Check.includes(:blazer_query).find_each do |check|+ Blazer::Check.includes(:query).find_each do |check|rows = nilerror = niltries = 0# try 3 times on timeout errorsbegin- rows, error = run_statement(check.blazer_query.statement)+ rows, error = data_sources[check.query.data_source].run_statement(check.query.statement)tries += 1end while error && error.include?("canceling statement due to statement timeout") && tries < 3check.update_state(rows, error)@@ -86,7 +52,7 @@ module Blazerdef self.send_failing_checksemails = {}- Blazer::Check.includes(:blazer_query).where(state: %w[failing error]).find_each do |check|+ Blazer::Check.includes(:query).where(state: %w[failing error]).find_each do |check|check.split_emails.each do |email|(emails[email] ||= []) << checkenddiff --git a/lib/blazer/data_source.rb b/lib/blazer/data_source.rbnew file mode 100644index 00000000..0cd67973--- /dev/null+++ b/lib/blazer/data_source.rb@@ -0,0 +1,70 @@+module Blazer+ class DataSource+ attr_reader :id, :settings, :connection_model++ def initialize(id, settings)+ @id = id+ @settings = settings+ @connection_model =+ Class.new(Blazer::Connection) do+ def self.name+ "Blazer::Connection::#{object_id}"+ end+ establish_connection(settings["url"]) if settings["url"]+ end+ end++ def name+ settings["name"] || @id+ end++ def linked_columns+ settings["linked_columns"] || {}+ end++ def smart_columns+ settings["smart_columns"] || {}+ end++ def smart_variables+ settings["smart_variables"] || {}+ end++ def timeout+ settings["timeout"]+ end++ def run_statement(statement)+ rows = []+ error = nil+ begin+ connection_model.transaction do+ connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}") if timeout && postgresql?+ result = connection_model.connection.select_all(statement)+ result.each do |untyped_row|+ row = {}+ untyped_row.each do |k, v|+ row[k] = result.column_types.empty? ? v : result.column_types[k].send(:type_cast, v)+ end+ rows << row+ end+ raise ActiveRecord::Rollback+ end+ rescue ActiveRecord::StatementInvalid => e+ error = e.message.sub(/.+ERROR: /, "")+ end+ [rows, error]+ end++ def tables+ default_schema = postgresql? ? "public" : connection_model.connection_config[:database]+ schema = connection_model.connection_config[:schema] || default_schema+ rows, error = run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))+ Hash[rows.group_by { |r| r["table_name"] }.map { |t, f| [t, f.sort_by { |f| f["ordinal_position"] }.map { |f| f.slice("column_name", "data_type") }] }.sort_by { |t, _f| t }]+ end++ def postgresql?+ ["PostgreSQL", "Redshift"].include?(connection_model.connection.adapter_name)+ end+ end+enddiff --git a/lib/blazer/engine.rb b/lib/blazer/engine.rbindex cfbe5f54..b5a27516 100644--- a/lib/blazer/engine.rb+++ b/lib/blazer/engine.rb@@ -6,8 +6,12 @@ module Blazer# use a proc instead of a stringapp.config.assets.precompile << proc { |path| path =~ /\Ablazer\/application\.(js|css)\z/ }- Blazer.time_zone ||= Time.zone- Blazer.user_class ||= User rescue nil+ Blazer.time_zone ||= Blazer.settings["time_zone"] || Time.zone+ Blazer.audit = Blazer.settings.key?("audit") ? Blazer.settings["audit"] : true+ Blazer.user_name = Blazer.settings["user_name"] if Blazer.settings["user_name"]+ Blazer.from_email = Blazer.settings["from_email"] if Blazer.settings["from_email"]++ Blazer.user_class ||= Blazer.settings["user_class"] || User rescue nilBlazer::Query.belongs_to :creator, class_name: Blazer.user_class.to_s if Blazer.user_classendenddiff --git a/lib/generators/blazer/templates/config.yml b/lib/generators/blazer/templates/config.ymlindex b89bf535..662c2c1b 100644--- a/lib/generators/blazer/templates/config.yml+++ b/lib/generators/blazer/templates/config.yml@@ -1,8 +1,29 @@-linked_columns:- # user_id: "/admin/users/{value}"+# see https://github.com/ankane/blazer for more info-smart_columns:- # user_id: "SELECT id, name FROM users WHERE id IN {value}"+# data sources+data_sources:+ main:+ url: <%%= ENV["BLAZER_DATABASE_URL"] %>-smart_variables:- # zone_id: "SELECT id, name FROM zones ORDER BY name ASC"+ timeout: 15++ smart_variables:+ # zone_id: "SELECT id, name FROM zones ORDER BY name ASC"++ linked_columns:+ # user_id: "/admin/users/{value}"++ smart_columns:+ # user_id: "SELECT id, name FROM users WHERE id IN {value}"++# create audits+audit: true++# change the time zone+# time_zone: "Pacific Time (US & Canada)"++# class name of the user model+# user_class: User++# method name for the user model+# user_name: namediff --git a/lib/generators/blazer/templates/install.rb b/lib/generators/blazer/templates/install.rbindex 0ca6eb5b..0c34832d 100644--- a/lib/generators/blazer/templates/install.rb+++ b/lib/generators/blazer/templates/install.rb@@ -5,6 +5,7 @@ class <%= migration_class_name %> < ActiveRecord::Migrationt.string :namet.text :descriptiont.text :statement+ t.string :data_sourcet.timestampsend@@ -12,11 +13,12 @@ class <%= migration_class_name %> < ActiveRecord::Migrationt.references :usert.references :queryt.text :statement+ t.string :data_sourcet.timestamp :created_atendcreate_table :blazer_checks do |t|- t.references :blazer_query+ t.references :queryt.string :statet.text :emailst.timestamps@@ -28,8 +30,8 @@ class <%= migration_class_name %> < ActiveRecord::Migrationendcreate_table :blazer_dashboard_queries do |t|- t.references :blazer_dashboard- t.references :blazer_query+ t.references :dashboard+ t.references :queryt.integer :positiont.timestampsend