From f3487658196121420be7a7c3e4a25de42ca2a6c4 Mon Sep 17 00:00:00 2001 From: Lucas Ponce Date: Thu, 18 Aug 2016 00:32:04 +0200 Subject: [PATCH 1/2] Adjust LiveMetric conditions parsing Close #10087 --- app/models/live_metric.rb | 2 +- spec/models/live_metric_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/live_metric.rb b/app/models/live_metric.rb index 98818d54dfe..4f840a4ae83 100644 --- a/app/models/live_metric.rb +++ b/app/models/live_metric.rb @@ -43,7 +43,7 @@ def self.parse_condition(column, op, value) end def self.process_conditions(conditions) - parsed_conditions = parse_conditions(conditions) + parsed_conditions = parse_conditions(conditions[0]) processed = {} parsed_conditions.each do |condition| case condition[:column] diff --git a/spec/models/live_metric_spec.rb b/spec/models/live_metric_spec.rb index 4a093bad57d..0fcdd5642bf 100644 --- a/spec/models/live_metric_spec.rb +++ b/spec/models/live_metric_spec.rb @@ -32,7 +32,7 @@ end it "#process_conditions" do - processed = LiveMetric.process_conditions(conditions) + processed = LiveMetric.process_conditions([conditions]) expect(processed[:resource_type]).to eq("MiddlewareServer") expect(processed[:resource_id]).to eq("6") expect(processed[:start_time]).to eq(Time.parse("2016-04-03 00:00:00 UTC").utc) @@ -42,7 +42,7 @@ it "#process_conditions raises error on incomplete conditions" do expect do - LiveMetric.process_conditions(incomplete_conditions) + LiveMetric.process_conditions([incomplete_conditions]) end.to raise_error(LiveMetric::LiveMetricError, "LiveMetric expression must contain resource_id condition") end end From 8e739929bf604abf28414e904e9426fde80ff99b Mon Sep 17 00:00:00 2001 From: Lucas Ponce Date: Thu, 18 Aug 2016 12:56:19 +0200 Subject: [PATCH 2/2] Filter cols into LiveMetric --- app/models/live_metric.rb | 13 ++++++------- app/models/miq_report/generator.rb | 2 +- spec/models/live_metric_spec.rb | 14 +++++++++----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/models/live_metric.rb b/app/models/live_metric.rb index 4f840a4ae83..a3f4346470e 100644 --- a/app/models/live_metric.rb +++ b/app/models/live_metric.rb @@ -8,8 +8,7 @@ def self.find(*args) validate_raw_query(raw_query) processed = process_conditions(raw_query[:conditions]) resource = fetch_resource(processed[:resource_type], processed[:resource_id]) - ext_options = raw_query[:ext_options] - filtered_cols = raw_query[:select] || ext_options ? ext_options[:only_cols] : nil + filtered_cols = raw_query[:select] || raw_query[:include].keys.map(&:to_s) if resource.nil? [] else @@ -25,12 +24,12 @@ def self.validate_raw_query(raw) end end - def self.parse_conditions(conditions) - if conditions.index('or') - _log.error("LiveMetric expression #{conditions} must not contain 'or' operator.") + def self.parse_conditions(raw_conditions) + if raw_conditions.index('or') + _log.error("LiveMetric expression #{raw_conditions} must not contain 'or' operator.") raise LiveMetricError, "LiveMetric expression doesn't support 'or' operator" end - conditions.split('and').collect do |exp| + raw_conditions.split('and').collect do |exp| parsed = exp.scan(/(.*)\s+(<=|=|>=|<|>|!=)\s+(.*)/) parse_condition(parsed[0][0], parsed[0][1], parsed[0][2]) end @@ -43,7 +42,7 @@ def self.parse_condition(column, op, value) end def self.process_conditions(conditions) - parsed_conditions = parse_conditions(conditions[0]) + parsed_conditions = parse_conditions(conditions.first) processed = {} parsed_conditions.each do |condition| case condition[:column] diff --git a/app/models/miq_report/generator.rb b/app/models/miq_report/generator.rb index a628691921e..021f1c0b623 100644 --- a/app/models/miq_report/generator.rb +++ b/app/models/miq_report/generator.rb @@ -89,7 +89,7 @@ def get_include_for_find(includes, klass = nil) if klass.nil? klass = db_class result = {} - cols.each { |c| result.merge!(c.to_sym => {}) if klass.virtual_attribute?(c) } if cols + cols.each { |c| result.merge!(c.to_sym => {}) if klass.virtual_attribute?(c) || klass == LiveMetric } if cols end if includes.kind_of?(Hash) diff --git a/spec/models/live_metric_spec.rb b/spec/models/live_metric_spec.rb index 0fcdd5642bf..1c3983a8a23 100644 --- a/spec/models/live_metric_spec.rb +++ b/spec/models/live_metric_spec.rb @@ -1,18 +1,22 @@ describe LiveMetric do - let(:conditions) do + let(:raw_conditions) do "resource_type = 'MiddlewareServer' and resource_id = 6 " \ "and timestamp >= '2016-04-03 00:00:00' and timestamp <= '2016-04-05 23:00:00' " \ "and capture_interval_name = 'daily'" end - let(:incomplete_conditions) do + let(:conditions) { [raw_conditions] } + + let(:incomplete_raw_conditions) do "resource_type = 'MiddlewareServer' " \ "and timestamp >= '2016-04-03 00:00:00' and timestamp <= '2016-04-05 23:00:00' " \ "and capture_interval_name = 'daily'" end + let(:incomplete_conditions) { [incomplete_raw_conditions] } + it "#parse_conditions" do - parsed = LiveMetric.parse_conditions(conditions) + parsed = LiveMetric.parse_conditions(raw_conditions) expect(parsed.size).to eq(5) expect(parsed[0][:column]).to eq("resource_type") expect(parsed[0][:op]).to eq("=") @@ -32,7 +36,7 @@ end it "#process_conditions" do - processed = LiveMetric.process_conditions([conditions]) + processed = LiveMetric.process_conditions(conditions) expect(processed[:resource_type]).to eq("MiddlewareServer") expect(processed[:resource_id]).to eq("6") expect(processed[:start_time]).to eq(Time.parse("2016-04-03 00:00:00 UTC").utc) @@ -42,7 +46,7 @@ it "#process_conditions raises error on incomplete conditions" do expect do - LiveMetric.process_conditions([incomplete_conditions]) + LiveMetric.process_conditions(incomplete_conditions) end.to raise_error(LiveMetric::LiveMetricError, "LiveMetric expression must contain resource_id condition") end end