Class: InfluxDB2::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/influxdb2/client/point.rb

Overview

Point defines the values that will be written to the database. Ref: bit.ly/influxdata-point

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, tags: nil, fields: nil, time: nil, precision: DEFAULT_WRITE_PRECISION) ⇒ Point

Create DataPoint instance for specified measurement name.

Examples:

InfluxDB2::Point.new(name: “h2o”,

tags: {host: 'aws', region: 'us'},
fields: {level: 5, saturation: "99%"},
time: 123)

Parameters:

  • name (String)

    the measurement name for the point.

  • tags (Hash) (defaults to: nil)

    the tag set for the point

  • fields (Hash) (defaults to: nil)

    the fields for the point

  • time (Integer) (defaults to: nil)

    the timestamp for the point

  • precision (WritePrecision) (defaults to: DEFAULT_WRITE_PRECISION)

    the precision for the unix timestamps within the body line-protocol



43
44
45
46
47
48
49
# 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

Instance Attribute Details

#precisionObject (readonly)

Returns the value of attribute precision.



50
51
52
# File 'lib/influxdb2/client/point.rb', line 50

def precision
  @precision
end

Class Method Details

.from_hash(data) ⇒ Object

Create DataPoint instance from specified data.

})

Examples:

Point.fromHash({

name: 'cpu',
tags: { host: 'server_nl', regios: 'us' },
fields: {internal: 5, external: 6},
time: 1422568543702900257

Parameters:

  • data (Hash)


62
63
64
65
# 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

Instance Method Details

#add_field(key, value) ⇒ Object

Adds or replaces a field value for a point.

Examples:

InfluxDB2::Point.new(name: “h2o”)

.add_tag("location", "europe")
.add_field("level", 2)

Parameters:

  • key (Object)

    the field name

  • value (Object)

    the field value



88
89
90
91
# File 'lib/influxdb2/client/point.rb', line 88

def add_field(key, value)
  @fields[key] = value
  self
end

#add_tag(key, value) ⇒ Object

Adds or replaces a tag value for a point.

Examples:

InfluxDB2::Point.new(name: “h2o”)

.add_tag("location", "europe")
.add_field("level", 2)

Parameters:

  • key (Object)

    the tag name

  • value (Object)

    the tag value



75
76
77
78
# File 'lib/influxdb2/client/point.rb', line 75

def add_tag(key, value)
  @tags[key] = value
  self
end

#time(time, precision) ⇒ Object

Updates the timestamp for the point.

Examples:

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)

InfluxDB2::Point.new(name: “h2o”)

.add_tag("location", "europe")
.add_field("level", 2)
.time(123, InfluxDB2::WritePrecision::NANOSECOND)

Parameters:

  • time (Object)

    the timestamp

  • precision (WritePrecision)

    the timestamp precision



107
108
109
110
111
# File 'lib/influxdb2/client/point.rb', line 107

def time(time, precision)
  @time = time
  @precision = precision
  self
end

#to_line_protocolObject

If there is no field then return nil.

Returns:

  • a string representation of the point



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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(@time)
  line_protocol << " #{timestamp}" if timestamp

  line_protocol
end