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 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
-
.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.
152 153 154 155 156 |
# File 'lib/influxer/metrics/metrics.rb', line 152 def initialize(attributes = {}) @attributes = {} @persisted = false super end |
Class Attribute Details
.database ⇒ Object (readonly)
Returns the value of attribute database.
43 44 45 |
# File 'lib/influxer/metrics/metrics.rb', line 43 def database @database end |
.precision ⇒ Object (readonly)
Returns the value of attribute precision.
43 44 45 |
# File 'lib/influxer/metrics/metrics.rb', line 43 def precision @precision end |
.retention_policy ⇒ Object (readonly)
Returns the value of attribute retention_policy.
43 44 45 |
# File 'lib/influxer/metrics/metrics.rb', line 43 def retention_policy @retention_policy end |
.series ⇒ Object (readonly)
Returns the value of attribute series.
43 44 45 |
# File 'lib/influxer/metrics/metrics.rb', line 43 def series @series end |
.tag_names ⇒ Object
Returns the value of attribute tag_names.
44 45 46 |
# File 'lib/influxer/metrics/metrics.rb', line 44 def tag_names @tag_names end |
Instance Attribute Details
#timestamp ⇒ Object
Returns the value of attribute timestamp.
150 151 152 |
# File 'lib/influxer/metrics/metrics.rb', line 150 def end |
Class Method Details
.all ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
108 109 110 111 112 113 114 |
# File 'lib/influxer/metrics/metrics.rb', line 108 def all if current_scope current_scope.clone else default_scoped end end |
.attributes(*attrs) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/influxer/metrics/metrics.rb', line 46 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
68 69 70 71 |
# File 'lib/influxer/metrics/metrics.rb', line 68 def inherited(subclass) subclass.set_series subclass.tag_names = tag_names.nil? ? [] : tag_names.dup end |
.quote(name) ⇒ Object
142 143 144 |
# File 'lib/influxer/metrics/metrics.rb', line 142 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/influxer/metrics/metrics.rb', line 119 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
79 80 81 |
# File 'lib/influxer/metrics/metrics.rb', line 79 def set_database(database) @database = database end |
.set_precision(precision) ⇒ Object
Use custom precision for this metrics
84 85 86 |
# File 'lib/influxer/metrics/metrics.rb', line 84 def set_precision(precision) @precision = precision end |
.set_retention_policy(policy_name) ⇒ Object
Use custom retention policy
74 75 76 |
# File 'lib/influxer/metrics/metrics.rb', line 74 def set_retention_policy(policy_name) @retention_policy = policy_name end |
.set_series(*args) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/influxer/metrics/metrics.rb', line 90 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
64 65 66 |
# File 'lib/influxer/metrics/metrics.rb', line 64 def tag?(name) tag_names.include?(name.to_s) end |
.tags(*attrs) ⇒ Object
58 59 60 61 62 |
# File 'lib/influxer/metrics/metrics.rb', line 58 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
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/influxer/metrics/metrics.rb', line 158 def write raise MetricsError if persisted? return false if invalid? run_callbacks :write do write_point end self end |
#write! ⇒ Object
169 170 171 172 |
# File 'lib/influxer/metrics/metrics.rb', line 169 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 |