class InfluxDB2::Point
Point
defines the values that will be written to the database. Ref: bit.ly/influxdata-point
Attributes
Public Class Methods
Create DataPoint instance from specified data.
@example Point.fromHash({
name: 'cpu', tags: { host: 'server_nl', regios: 'us' }, fields: {internal: 5, external: 6}, time: 1422568543702900257
})
@param [Hash] data
# File lib/influxdb2/client/point.rb, line 62 def self.from_hash(data) obj = new(name: data[:name], tags: data[:tags], fields: data[:fields], time: data[:time]) obj end
Create DataPoint instance for specified measurement name.
@example InfluxDB2::Point.new
(name: “h2o”,
tags: {host: 'aws', region: 'us'}, fields: {level: 5, saturation: "99%"}, time: 123)
@param [String] name the measurement name for the point. @param [Hash] tags the tag set for the point @param [Hash] fields the fields for the point @param [Integer] time the timestamp for the point @param [WritePrecision] precision the precision for the unix timestamps within the body line-protocol
# File lib/influxdb2/client/point.rb, line 43 def initialize(name:, tags: nil, fields: nil, time: nil, precision: DEFAULT_WRITE_PRECISION) @name = name @tags = tags || {} @fields = fields || {} @time = time @precision = precision end
Public Instance Methods
Adds or replaces a field value for a point.
@example InfluxDB2::Point.new
(name: “h2o”)
.add_tag("location", "europe") .add_field("level", 2)
@param [Object] key the field name @param [Object] value the field value
# File lib/influxdb2/client/point.rb, line 88 def add_field(key, value) @fields[key] = value self end
Adds or replaces a tag value for a point.
@example InfluxDB2::Point.new
(name: “h2o”)
.add_tag("location", "europe") .add_field("level", 2)
@param [Object] key the tag name @param [Object] value the tag value
# File lib/influxdb2/client/point.rb, line 75 def add_tag(key, value) @tags[key] = value self end
Updates the timestamp for the point.
@example InfluxDB2::Point.new
(name: “h2o”)
.add_tag("location", "europe") .add_field("level", 2) .time(Time.new(2015, 10, 15, 8, 20, 15), InfluxDB2::WritePrecision::MILLISECOND)
@example InfluxDB2::Point.new
(name: “h2o”)
.add_tag("location", "europe") .add_field("level", 2) .time(123, InfluxDB2::WritePrecision::NANOSECOND)
@param [Object] time the timestamp @param [WritePrecision] precision the timestamp precision
# File lib/influxdb2/client/point.rb, line 107 def time(time, precision) @time = time @precision = precision self end
If there is no field then return nil.
@return a string representation of the point
# File lib/influxdb2/client/point.rb, line 116 def to_line_protocol line_protocol = '' measurement = _escape_key(@name || '', ESCAPE_MEASUREMENT_LIST) line_protocol << measurement tags = _escape_tags line_protocol << ",#{tags}" unless tags.empty? line_protocol << ' '.freeze if line_protocol[-1] == '\\' fields = _escape_fields return nil if fields.empty? line_protocol << " #{fields}" if fields timestamp = _escape_time line_protocol << " #{timestamp}" if timestamp line_protocol end
Private Instance Methods
# File lib/influxdb2/client/point.rb, line 152 def _escape_fields return if @fields.nil? @fields.sort.to_h.map do |k, v| key = _escape_key(k.to_s) value = _escape_value(v) if key.empty? || value.empty? nil else "#{key}=#{value}" end end.reject(&:nil?).join(','.freeze) end
# File lib/influxdb2/client/point.rb, line 166 def _escape_key(value, escape_list = ESCAPE_KEY_LIST) result = value.dup escape_list.each do |ch| result = result.gsub(ch) { "\\#{ch}" } end REPLACE_KEY_LIST.keys.each do |ch| result = result.gsub(ch) { REPLACE_KEY_LIST[ch] } end result end
# File lib/influxdb2/client/point.rb, line 195 def _escape_time if @time.nil? nil elsif @time.is_a?(Integer) @time.to_s elsif @time.is_a?(Float) @time.round.to_s elsif @time.is_a?(Time) nano_seconds = @time.to_i * 1e9 nano_seconds += @time.tv_nsec case @precision || DEFAULT_WRITE_PRECISION when InfluxDB2::WritePrecision::MILLISECOND then (nano_seconds / 1e6).round when InfluxDB2::WritePrecision::SECOND then (nano_seconds / 1e9).round when InfluxDB2::WritePrecision::MICROSECOND then (nano_seconds / 1e3).round when InfluxDB2::WritePrecision::NANOSECOND then nano_seconds.round end else @time.to_s end end
# File lib/influxdb2/client/point.rb, line 177 def _escape_value(value) if value.nil? '' elsif value.is_a?(String) result = value.dup ESCAPE_VALUE_LIST.each do |ch| result = result.gsub(ch) { "\\#{ch}" } end '"'.freeze + result + '"'.freeze elsif value.is_a?(Integer) "#{value}i" elsif [Float::INFINITY, -Float::INFINITY].include?(value) '' else value.to_s end end