Class: Influxer::Metrics

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Model, ActiveModel::Validations, ActiveModel3::Model, Scoping, TimestampQuoting
Defined in:
lib/influxer/metrics/metrics.rb

Overview

Base class for InfluxDB querying and writing rubocop:disable Metrics/ClassLength

Constant Summary collapse

TIME_FACTOR =
1_000_000_000

Constants included from TimestampQuoting

TimestampQuoting::DEFAULT_PRECISION, TimestampQuoting::TIME_FACTORS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimestampQuoting

#factorize_timestamp, #quote_timestamp, #quote_timestamp_with_suffix

Constructor Details

#initialize(attributes = {}) ⇒ Metrics

Returns a new instance of Metrics.



153
154
155
156
157
# File 'lib/influxer/metrics/metrics.rb', line 153

def initialize(attributes = {})
  @attributes = {}
  @persisted = false
  super
end

Class Attribute Details

.databaseObject (readonly)

Returns the value of attribute database.



44
45
46
# File 'lib/influxer/metrics/metrics.rb', line 44

def database
  @database
end

.precisionObject (readonly)

Returns the value of attribute precision.



44
45
46
# File 'lib/influxer/metrics/metrics.rb', line 44

def precision
  @precision
end

.retention_policyObject (readonly)

Returns the value of attribute retention_policy.



44
45
46
# File 'lib/influxer/metrics/metrics.rb', line 44

def retention_policy
  @retention_policy
end

.seriesObject (readonly)

Returns the value of attribute series.



44
45
46
# File 'lib/influxer/metrics/metrics.rb', line 44

def series
  @series
end

.tag_namesObject

Returns the value of attribute tag_names.



45
46
47
# File 'lib/influxer/metrics/metrics.rb', line 45

def tag_names
  @tag_names
end

Instance Attribute Details

#timestampObject

Returns the value of attribute timestamp.



151
152
153
# File 'lib/influxer/metrics/metrics.rb', line 151

def timestamp
  @timestamp
end

Class Method Details

.allObject

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



109
110
111
112
113
114
115
# File 'lib/influxer/metrics/metrics.rb', line 109

def all
  if current_scope
    current_scope.clone
  else
    default_scoped
  end
end

.attributes(*attrs) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/influxer/metrics/metrics.rb', line 47

def attributes(*attrs)
  attrs.each do |name|
    define_method("#{name}=") do |val|
      @attributes[name] = val
    end

    define_method(name.to_s) do
      @attributes[name]
    end
  end
end

.inherited(subclass) ⇒ Object



69
70
71
72
# File 'lib/influxer/metrics/metrics.rb', line 69

def inherited(subclass)
  subclass.set_series
  subclass.tag_names = tag_names.nil? ? [] : tag_names.dup
end

.quote(name) ⇒ Object



143
144
145
# File 'lib/influxer/metrics/metrics.rb', line 143

def quote(name)
  '"' + name.to_s.gsub(/\"/) { '\"' } + '"'
end

.quoted_series(val = @series, instance = nil, write: false) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/influxer/metrics/metrics.rb', line 120

def quoted_series(val = @series, instance = nil, write: false)
  case val
  when Regexp
    val.inspect
  when Proc
    quoted_series(val.call(instance), write: write)
  when Array
    if val.length > 1
      "merge(#{val.map { |s| quoted_series(s, write: write) }.join(',')})"
    else
      quoted_series(val.first, write: write)
    end
  else
    # Only add retention policy when selecting points
    # and not writing
    if !write && retention_policy.present?
      [quote(@retention_policy), quote(val)].join('.')
    else
      quote(val)
    end
  end
end

.set_database(database) ⇒ Object

Use custom database for this metrics



80
81
82
# File 'lib/influxer/metrics/metrics.rb', line 80

def set_database(database)
  @database = database
end

.set_precision(precision) ⇒ Object

Use custom precision for this metrics



85
86
87
# File 'lib/influxer/metrics/metrics.rb', line 85

def set_precision(precision)
  @precision = precision
end

.set_retention_policy(policy_name) ⇒ Object

Use custom retention policy



75
76
77
# File 'lib/influxer/metrics/metrics.rb', line 75

def set_retention_policy(policy_name)
  @retention_policy = policy_name
end

.set_series(*args) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/influxer/metrics/metrics.rb', line 91

def set_series(*args)
  if args.empty?
    matches = to_s.match(/^(.*)Metrics$/)
    @series =
      if matches.nil?
        superclass.respond_to?(:series) ? superclass.series : to_s.underscore
      else
        matches[1].split("::").join("_").underscore
      end
  elsif args.first.is_a?(Proc)
    @series = args.first
  else
    @series = args
  end
end

.tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/influxer/metrics/metrics.rb', line 65

def tag?(name)
  tag_names.include?(name.to_s)
end

.tags(*attrs) ⇒ Object



59
60
61
62
63
# File 'lib/influxer/metrics/metrics.rb', line 59

def tags(*attrs)
  attributes(*attrs)
  self.tag_names ||= []
  self.tag_names += attrs.map(&:to_s)
end

Instance Method Details

#clientObject



196
197
198
# File 'lib/influxer/metrics/metrics.rb', line 196

def client
  Influxer.client
end

#dupObject



200
201
202
# File 'lib/influxer/metrics/metrics.rb', line 200

def dup
  self.class.new(@attributes)
end

#persisted?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/influxer/metrics/metrics.rb', line 188

def persisted?
  @persisted
end

#series(**options) ⇒ Object



192
193
194
# File 'lib/influxer/metrics/metrics.rb', line 192

def series(**options)
  self.class.quoted_series(self.class.series, self, **options)
end

#tag_namesObject



214
215
216
# File 'lib/influxer/metrics/metrics.rb', line 214

def tag_names
  self.class.tag_names
end

#tagsObject

Returns hash with metrics tags



210
211
212
# File 'lib/influxer/metrics/metrics.rb', line 210

def tags
  @attributes.select { |k, _| tag_names.include?(k.to_s) }
end

#valuesObject

Returns hash with metrics values



205
206
207
# File 'lib/influxer/metrics/metrics.rb', line 205

def values
  @attributes.reject { |k, _| tag_names.include?(k.to_s) }
end

#writeObject

Raises:



159
160
161
162
163
164
165
166
167
168
# File 'lib/influxer/metrics/metrics.rb', line 159

def write
  raise MetricsError if persisted?

  return false if invalid?

  run_callbacks :write do
    write_point
  end
  self
end

#write!Object

Raises:



170
171
172
173
# File 'lib/influxer/metrics/metrics.rb', line 170

def write!
  raise MetricsInvalid if invalid?
  write
end

#write_pointObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/influxer/metrics/metrics.rb', line 175

def write_point
  data = {
    values: values,
    tags: tags,
    timestamp: parsed_timestamp
  }
  write_with_config(
    unquote(series(write: true)),
    data
  )
  @persisted = true
end