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

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_for_write, #quote_timestamp_with_suffix

Constructor Details

#initialize(attributes = {}) ⇒ Metrics

Returns a new instance of Metrics.



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

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

Class Attribute Details

.databaseObject (readonly)

Returns the value of attribute database.



42
43
44
# File 'lib/influxer/metrics/metrics.rb', line 42

def database
  @database
end

.precisionObject (readonly)

Returns the value of attribute precision.



42
43
44
# File 'lib/influxer/metrics/metrics.rb', line 42

def precision
  @precision
end

.retention_policyObject (readonly)

Returns the value of attribute retention_policy.



42
43
44
# File 'lib/influxer/metrics/metrics.rb', line 42

def retention_policy
  @retention_policy
end

.seriesObject (readonly)

Returns the value of attribute series.



42
43
44
# File 'lib/influxer/metrics/metrics.rb', line 42

def series
  @series
end

.tag_namesObject

Returns the value of attribute tag_names.



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

def tag_names
  @tag_names
end

Instance Attribute Details

#timestampObject

Returns the value of attribute timestamp.



149
150
151
# File 'lib/influxer/metrics/metrics.rb', line 149

def timestamp
  @timestamp
end

Class Method Details

.allObject

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



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

def all
  if current_scope
    current_scope.clone
  else
    default_scoped
  end
end

.attributes(*attrs) ⇒ Object



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

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



67
68
69
70
# File 'lib/influxer/metrics/metrics.rb', line 67

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

.quote(name) ⇒ Object



141
142
143
# File 'lib/influxer/metrics/metrics.rb', line 141

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



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

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



78
79
80
# File 'lib/influxer/metrics/metrics.rb', line 78

def set_database(database)
  @database = database
end

.set_precision(precision) ⇒ Object

Use custom precision for this metrics



83
84
85
# File 'lib/influxer/metrics/metrics.rb', line 83

def set_precision(precision)
  @precision = precision
end

.set_retention_policy(policy_name) ⇒ Object

Use custom retention policy



73
74
75
# File 'lib/influxer/metrics/metrics.rb', line 73

def set_retention_policy(policy_name)
  @retention_policy = policy_name
end

.set_series(*args) ⇒ Object

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



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

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)


63
64
65
# File 'lib/influxer/metrics/metrics.rb', line 63

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

.tags(*attrs) ⇒ Object



57
58
59
60
61
# File 'lib/influxer/metrics/metrics.rb', line 57

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

Instance Method Details

#clientObject



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

def client
  Influxer.client
end

#dupObject



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted
end

#series(**options) ⇒ Object



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

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

#tag_namesObject



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

def tag_names
  self.class.tag_names
end

#tagsObject

Returns hash with metrics tags



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

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

#valuesObject

Returns hash with metrics values



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

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

#writeObject

Raises:



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

def write
  raise MetricsError if persisted?

  return false if invalid?

  run_callbacks :write do
    write_point
  end
  self
end

#write!Object

Raises:



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

def write!
  raise MetricsInvalid if invalid?

  write
end

#write_pointObject



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

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