Jelajahi Sumber

Better handling of checks with variables

Andrew Kane 7 tahun lalu
induk
melakukan
767faefe9d

+ 1 - 8
app/controllers/blazer/base_controller.rb

@@ -25,7 +25,7 @@ module Blazer
     private
 
       def process_vars(statement, data_source)
-        (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
+        (@bind_vars ||= []).concat(Blazer.extract_vars(statement)).uniq!
         @bind_vars.each do |var|
           params[var] ||= Blazer.data_sources[data_source].variable_defaults[var]
         end
@@ -79,13 +79,6 @@ module Blazer
         [smart_var, error]
       end
 
-      def extract_vars(statement)
-        # strip commented out lines
-        # and regex {1} or {1,2}
-        statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
-      end
-      helper_method :extract_vars
-
       def variable_params
         params.except(:controller, :action, :id, :host, :query, :dashboard, :query_id, :query_ids, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement, :data_source, :name, :fork_query_id, :blazer, :run_id).permit!
       end

+ 1 - 1
app/controllers/blazer/queries_controller.rb

@@ -279,7 +279,7 @@ module Blazer
               id: q.id,
               name: q.name,
               creator: blazer_user && q.try(:creator) == blazer_user ? "You" : q.try(:creator).try(Blazer.user_name),
-              vars: extract_vars(q.statement).join(", "),
+              vars: q.variables.join(", "),
               to_param: q.to_param
             }
           end

+ 7 - 0
app/models/blazer/check.rb

@@ -5,6 +5,7 @@ module Blazer
 
     validates :query_id, presence: true
     validate :validate_emails
+    validate :validate_variables, if: -> { query_id_changed? }
 
     before_validation :set_state
     before_validation :fix_emails
@@ -84,5 +85,11 @@ module Blazer
           errors.add(:base, "Invalid emails")
         end
       end
+
+      def validate_variables
+        if query.variables.any?
+          errors.add(:base, "Query can't have variables")
+        end
+      end
   end
 end

+ 4 - 0
app/models/blazer/query.rb

@@ -23,5 +23,9 @@ module Blazer
       editable &&= Blazer.query_editable.call(self, user) if Blazer.query_editable
       editable
     end
+
+    def variables
+      Blazer.extract_vars(statement)
+    end
   end
 end

+ 6 - 0
lib/blazer.rb

@@ -88,6 +88,12 @@ module Blazer
     end
   end
 
+  def self.extract_vars(statement)
+    # strip commented out lines
+    # and regex {1} or {1,2}
+    statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
+  end
+
   def self.run_checks(schedule: nil)
     checks = Blazer::Check.includes(:query)
     checks = checks.where(schedule: schedule) if schedule

+ 1 - 1
lib/blazer/run_statement.rb

@@ -28,7 +28,7 @@ module Blazer
         audit.save! if audit.changed?
       end
 
-      if query && !result.timed_out?
+      if query && !result.timed_out? && !query.variables.any?
         query.checks.each do |check|
           check.update_state(result)
         end