Class: Influxer::Metrics
- Inherits:
-
Object
- Object
- Influxer::Metrics
- 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
-
.database ⇒ Object
readonly
Returns the value of attribute database.
-
.precision ⇒ Object
readonly
Returns the value of attribute precision.
-
.retention_policy ⇒ Object
readonly
Returns the value of attribute retention_policy.
-
.series ⇒ Object
readonly
Returns the value of attribute series.
-
.tag_names ⇒ Object
Returns the value of attribute tag_names.
Instance Attribute Summary collapse
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
-
.all ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
- .attributes(*attrs) ⇒ Object
- .inherited(subclass) ⇒ Object
- .quote(name) ⇒ Object
-
.quoted_series(val = @series, instance = nil, write: false) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity.
-
.set_database(database) ⇒ Object
Use custom database for this metrics.
-
.set_precision(precision) ⇒ Object
Use custom precision for this metrics.
-
.set_retention_policy(policy_name) ⇒ Object
Use custom retention policy.
-
.set_series(*args) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
- .tag?(name) ⇒ Boolean
- .tags(*attrs) ⇒ Object
Instance Method Summary collapse
- #client ⇒ Object
- #dup ⇒ Object
-
#initialize(attributes = {}) ⇒ Metrics
constructor
A new instance of Metrics.
- #persisted? ⇒ Boolean
- #series(**options) ⇒ Object
- #tag_names ⇒ Object
-
#tags ⇒ Object
Returns hash with metrics tags.
-
#values ⇒ Object
Returns hash with metrics values.
- #write ⇒ Object
- #write! ⇒ Object
- #write_point ⇒ Object
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
.database ⇒ Object (readonly)
Returns the value of attribute database.
42 43 44 |
# File 'lib/influxer/metrics/metrics.rb', line 42 def database @database end |
.precision ⇒ Object (readonly)
Returns the value of attribute precision.
42 43 44 |
# File 'lib/influxer/metrics/metrics.rb', line 42 def precision @precision end |
.retention_policy ⇒ Object (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 |
.series ⇒ Object (readonly)
Returns the value of attribute series.
42 43 44 |
# File 'lib/influxer/metrics/metrics.rb', line 42 def series @series end |
.tag_names ⇒ Object
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
#timestamp ⇒ Object
Returns the value of attribute timestamp.
149 150 151 |
# File 'lib/influxer/metrics/metrics.rb', line 149 def @timestamp end |
Class Method Details
.all ⇒ Object
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
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 (*attrs) attributes(*attrs) self.tag_names ||= [] self.tag_names += attrs.map(&:to_s) end |
Instance Method Details
#client ⇒ Object
195 196 197 |
# File 'lib/influxer/metrics/metrics.rb', line 195 def client Influxer.client end |
#dup ⇒ Object
199 200 201 |
# File 'lib/influxer/metrics/metrics.rb', line 199 def dup self.class.new(@attributes) end |
#persisted? ⇒ 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(**) self.class.quoted_series(self.class.series, self, **) end |
#tag_names ⇒ Object
213 214 215 |
# File 'lib/influxer/metrics/metrics.rb', line 213 def tag_names self.class.tag_names end |
#tags ⇒ Object
Returns hash with metrics tags
209 210 211 |
# File 'lib/influxer/metrics/metrics.rb', line 209 def @attributes.select { |k, _| tag_names.include?(k.to_s) } end |
#values ⇒ Object
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 |
#write ⇒ Object
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
168 169 170 171 172 |
# File 'lib/influxer/metrics/metrics.rb', line 168 def write! raise MetricsInvalid if invalid? write end |
#write_point ⇒ Object
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: , timestamp: } write_with_config( unquote(series(write: true)), data ) @persisted = true end |