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_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
.database ⇒ Object (readonly)
Returns the value of attribute database.
44 45 46 |
# File 'lib/influxer/metrics/metrics.rb', line 44 def database @database end |
.precision ⇒ Object (readonly)
Returns the value of attribute precision.
44 45 46 |
# File 'lib/influxer/metrics/metrics.rb', line 44 def precision @precision end |
.retention_policy ⇒ Object (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 |
.series ⇒ Object (readonly)
Returns the value of attribute series.
44 45 46 |
# File 'lib/influxer/metrics/metrics.rb', line 44 def series @series end |
.tag_names ⇒ Object
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
#timestamp ⇒ Object
Returns the value of attribute timestamp.
151 152 153 |
# File 'lib/influxer/metrics/metrics.rb', line 151 def @timestamp end |
Class Method Details
.all ⇒ Object
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
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 (*attrs) attributes(*attrs) self.tag_names ||= [] self.tag_names += attrs.map(&:to_s) end |
Instance Method Details
#client ⇒ Object
196 197 198 |
# File 'lib/influxer/metrics/metrics.rb', line 196 def client Influxer.client end |
#dup ⇒ Object
200 201 202 |
# File 'lib/influxer/metrics/metrics.rb', line 200 def dup self.class.new(@attributes) end |
#persisted? ⇒ 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(**) self.class.quoted_series(self.class.series, self, **) end |
#tag_names ⇒ Object
214 215 216 |
# File 'lib/influxer/metrics/metrics.rb', line 214 def tag_names self.class.tag_names end |
#tags ⇒ Object
Returns hash with metrics tags
210 211 212 |
# File 'lib/influxer/metrics/metrics.rb', line 210 def @attributes.select { |k, _| tag_names.include?(k.to_s) } end |
#values ⇒ Object
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 |
#write ⇒ Object
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
170 171 172 173 |
# File 'lib/influxer/metrics/metrics.rb', line 170 def write! raise MetricsInvalid if invalid? write end |
#write_point ⇒ Object
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: , timestamp: } write_with_config( unquote(series(write: true)), data ) @persisted = true end |