/
target.rb
191 lines (158 loc) · 4.49 KB
/
target.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class MiqExpression::Target
# TODO: fix to run against a single regular expression
# supports strange syntax like: managed.location, Model.x.y.managed-location
def self.parse(str)
MiqExpression::Field.parse(str) ||
MiqExpression::CountField.parse(str) ||
MiqExpression::Tag.parse(str) ||
MiqExpression::InvalidTarget.new
end
# example .parse('Host.vms-host_name')
# returns hash:
# {:model_name => Host<ApplicationRecord> , :associations => ['vms']<Array>, :column_name => 'host_name' <String>}
def self.parse_params(field)
return unless field.kind_of?(String)
parsed_params = match(field)
return if parsed_params.nil?
begin
parsed_params[:model_name] = parsed_params[:model_name].classify.safe_constantize
rescue LoadError # issues for case sensitivity (e.g.: VM vs vm)
parsed_params[:model_name] = nil
end
parsed_params[:associations] = parsed_params[:associations].to_s.split(".")
parsed_params
end
def self.match(field)
self::REGEX.match(field)&.named_captures&.symbolize_keys
end
attr_reader :column
attr_accessor :model, :associations
def initialize(model, associations, column)
@model = model
@associations = associations
@column = column
end
def column_type
nil
end
def sub_type
column_type
end
def date?
column_type == :date
end
def datetime?
column_type == :datetime
end
def string?
column_type == :string
end
def decimal?
column_type == :decimal
end
def tag?
false
end
def numeric?
%i[fixnum integer decimal float].include?(column_type)
end
def plural?
return false if reflections.empty?
[:has_many, :has_and_belongs_to_many].include?(reflections.last.macro)
end
def attribute_supported_by_sql?
reflection_supported_by_sql?
end
def reflection_supported_by_sql?
model&.follow_associations(associations).present?
rescue ArgumentError
false
end
# @returns AR or virtual reflections
# @raises ArgumentError
def reflections
model.collect_reflections_with_virtual(associations) ||
raise(ArgumentError, "One or more associations are invalid: #{associations.join(", ")}")
end
# @returns only AR reflections
# @raises ArgumentError
def collect_reflections
model.collect_reflections(associations) ||
raise(ArgumentError, "One or more associations are invalid: #{associations.join(", ")}")
end
def includes
ret = {}
model && collect_reflections.map(&:name).inject(ret) { |a, p| a[p] ||= {} }
ret
rescue ArgumentError
# since the reflections are not valid, we don't have any includes
{}
end
# @raises ArgumentError
def target
if associations.none?
model
else
reflections.last.klass
end
end
def tag_path_with(value = nil)
# encode embedded / characters in values since / is used as a tag seperator
"#{tag_path}#{value.nil? ? '' : '/' + value.to_s.gsub("/", "%2f")}"
end
def exclude_col_by_preprocess_options?(options)
if options.kind_of?(Hash) && options[:vim_performance_daily_adhoc]
Metric::Rollup.excluded_col_for_expression?(column.to_sym)
elsif target == Service
Service::AGGREGATE_ALL_VM_ATTRS.include?(column.to_sym)
else
false
end
rescue ArgumentError # reflections are not valid when looking up target
false
end
# this should only be accessed in MiqExpression
# please avoid using it
def arel_table
if associations.none?
model.arel_table
else
# if the target attribute is in the same table as the model (the base table),
# alias the table to avoid conflicting table from clauses
# seems AR should do this for us...
ref = reflections.last
if ref.klass.table_name == model.table_name
ref.klass.arel_table.alias(ref.alias_candidate(model.table_name))
else
ref.klass.arel_table
end
end
end
# this should only be accessed in MiqExpression
# please avoid using it
def arel_attribute
target && target.arel_table[column, arel_table]
end
def virtual_attribute?
false
end
def hash
[column, associations, model].hash
end
def ==(other)
other.kind_of?(MiqExpression::Target) &&
column == other.column &&
model == other.model &&
associations == other.associations
end
def eql?(other)
other.class == self.class && self == other
end
private
def tag_path
"/#{tag_values.join('/')}"
end
def tag_values
['virtual'] + @associations + [@column]
end
end