Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust LiveMetric conditions parsing #10570

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions app/models/live_metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -43,7 +42,7 @@ def self.parse_condition(column, op, value)
end

def self.process_conditions(conditions)
parsed_conditions = parse_conditions(conditions)
parsed_conditions = parse_conditions(conditions.first)
processed = {}
parsed_conditions.each do |condition|
case condition[:column]
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_report/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions spec/models/live_metric_spec.rb
Original file line number Diff line number Diff line change
@@ -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("=")
Expand Down