Class: Influxer::Relation
Overview
Relation is used to build queries rubocop:disable Metrics/ClassLength
Constant Summary
collapse
- SUPPORTED_EPOCH_FORMAT =
%i[h m s ms u ns].freeze
- MULTI_VALUE_METHODS =
%i[select where group order].freeze
- MULTI_KEY_METHODS =
%i[fanout].freeze
- SINGLE_VALUE_METHODS =
%i[fill time limit offset slimit soffset from normalized].freeze
- MULTI_VALUE_SIMPLE_METHODS =
%i[select group].freeze
- SINGLE_VALUE_SIMPLE_METHODS =
%i[fill limit offset slimit soffset from].freeze
TimestampQuoting::DEFAULT_PRECISION, TimestampQuoting::TIME_FACTORS
Calculations::CALCULATION_METHODS
Constants included
from TimeQuery
TimeQuery::FILL_RESERVED, TimeQuery::TIME_ALIASES
Instance Attribute Summary collapse
Instance Method Summary
collapse
#factorize_timestamp, #quote_timestamp, #quote_timestamp_with_suffix
#percentile
Methods included from TimeQuery
#past, #since, #time
#none, #not, #where
Constructor Details
#initialize(klass, params = {}) ⇒ Relation
Initialize new Relation for ‘klass’ (Class) metrics.
Available params:
:attributes - hash of attributes to be included to new Metrics object
and where clause of Relation
84
85
86
87
88
89
|
# File 'lib/influxer/metrics/relation.rb', line 84
def initialize(klass, params = {})
@klass = klass
@instance = klass.new params[:attributes]
reset
where(params[:attributes]) if params[:attributes].present?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
292
293
294
295
|
# File 'lib/influxer/metrics/relation.rb', line 292
def method_missing(method, *args, &block)
return super unless @klass.respond_to?(method)
merge!(scoping { @klass.public_send(method, *args, &block) })
end
|
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
18
19
20
|
# File 'lib/influxer/metrics/relation.rb', line 18
def values
@values
end
|
Instance Method Details
#as_json(options = nil) ⇒ Object
191
192
193
|
# File 'lib/influxer/metrics/relation.rb', line 191
def as_json(options = nil)
to_a.as_json(options)
end
|
#build(params = {}) ⇒ Object
Also known as:
new
99
100
101
102
103
104
105
|
# File 'lib/influxer/metrics/relation.rb', line 99
def build(params = {})
point = @instance.dup
params.each do |key, val|
point.send("#{key}=", val) if point.respond_to?(key)
end
point
end
|
#delete_all ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/influxer/metrics/relation.rb', line 207
def delete_all
sql = ["drop series"]
sql << "from #{@instance.series}"
sql << "where #{where_values.join(' and ')}" unless where_values.empty?
sql = sql.join " "
@instance.client.query sql
end
|
#empty? ⇒ Boolean
182
183
184
185
186
187
188
189
|
# File 'lib/influxer/metrics/relation.rb', line 182
def empty?
unless loaded?
select_values.clear
limit(1).load
end
@records.empty?
end
|
#epoch(val) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/influxer/metrics/relation.rb', line 118
def epoch(val)
return self unless SUPPORTED_EPOCH_FORMAT.include? val
@values[:epoch] = val
self
end
|
#inspect ⇒ Object
175
176
177
178
179
180
|
# File 'lib/influxer/metrics/relation.rb', line 175
def inspect
entries = to_a.take(11).map!(&:inspect)
entries[10] = '...' if entries.size == 11
"#<#{self.class.name} [#{entries.join(', ')}]>"
end
|
#load ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/influxer/metrics/relation.rb', line 195
def load
@records = get_points(
@instance.client.query(
to_sql,
denormalize: !normalized?,
epoch: @values[:epoch]
)
)
@loaded = true
@records
end
|
#merge!(rel) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/influxer/metrics/relation.rb', line 229
def merge!(rel)
return self if rel.nil?
MULTI_VALUE_METHODS.each do |method|
(@values[method] ||= []).concat(rel.values[method]).uniq! unless rel.values[method].nil?
end
MULTI_KEY_METHODS.each do |method|
(@values[method] ||= {}).merge!(rel.values[method]) unless rel.values[method].nil?
end
SINGLE_VALUE_METHODS.each do |method|
@values[method] = rel.values[method] unless rel.values[method].nil?
end
self
end
|
#normalized ⇒ Object
109
110
111
112
|
# File 'lib/influxer/metrics/relation.rb', line 109
def normalized
@values[:normalized] = true
self
end
|
#normalized? ⇒ Boolean
114
115
116
|
# File 'lib/influxer/metrics/relation.rb', line 114
def normalized?
@values[:normalized] == true
end
|
#order(val) ⇒ Object
125
126
127
128
129
130
131
132
133
|
# File 'lib/influxer/metrics/relation.rb', line 125
def order(val)
case val
when Hash
val.each { |k, v| order_values << "#{k} #{v}" }
when String
order_values << val
end
self
end
|
#scoping ⇒ Object
219
220
221
222
223
224
225
|
# File 'lib/influxer/metrics/relation.rb', line 219
def scoping
previous = @klass.current_scope
@klass.current_scope = self
yield
ensure
@klass.current_scope = previous
end
|
#to_a ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength rubocop:enable Metrics/PerceivedComplexity
170
171
172
173
|
# File 'lib/influxer/metrics/relation.rb', line 170
def to_a
return @records if loaded?
load
end
|
#to_sql ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity
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
|
# File 'lib/influxer/metrics/relation.rb', line 139
def to_sql
sql = ["select"]
select_values << "*" if select_values.empty?
sql << select_values.uniq.join(", ")
sql << "from #{build_series_name}"
sql << "where #{where_values.join(' and ')}" unless where_values.empty?
unless group_values.empty? && time_value.nil?
group_fields = (time_value.nil? ? [] : ['time(' + @values[:time] + ')']) + group_values
group_fields.uniq!
sql << "group by #{group_fields.join(', ')}"
end
sql << "fill(#{fill_value})" unless fill_value.nil?
sql << "order by #{order_values.uniq.join(',')}" unless order_values.empty?
sql << "limit #{limit_value}" unless limit_value.nil?
sql << "offset #{offset_value}" unless offset_value.nil?
sql << "slimit #{slimit_value}" unless slimit_value.nil?
sql << "soffset #{soffset_value}" unless soffset_value.nil?
sql.join " "
end
|
#write(params = {}) ⇒ Object
91
92
93
|
# File 'lib/influxer/metrics/relation.rb', line 91
def write(params = {})
build(params).write
end
|
#write!(params = {}) ⇒ Object
95
96
97
|
# File 'lib/influxer/metrics/relation.rb', line 95
def write!(params = {})
build(params).write!
end
|