influxdb-client 1.0.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.circleci/config.yml +157 -0
- data/.circleci/setup-rubygems.sh +3 -0
- data/.github/PULL_REQUEST_TEMPLATE +8 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +34 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +24 -0
- data/LICENSE +21 -0
- data/README.md +157 -0
- data/Rakefile +37 -0
- data/bin/influxdb-onboarding.sh +39 -0
- data/bin/influxdb-restart.sh +60 -0
- data/influxdb-client.gemspec +53 -0
- data/lib/influxdb2/client.rb +25 -0
- data/lib/influxdb2/client/client.rb +71 -0
- data/lib/influxdb2/client/influx_error.rb +27 -0
- data/lib/influxdb2/client/point.rb +214 -0
- data/lib/influxdb2/client/version.rb +23 -0
- data/lib/influxdb2/client/write_api.rb +154 -0
- data/test/influxdb/client_test.rb +70 -0
- data/test/influxdb/point_test.rb +221 -0
- data/test/influxdb/write_api_integration_test.rb +75 -0
- data/test/influxdb/write_api_test.rb +235 -0
- data/test/test_helper.rb +36 -0
- metadata +187 -0
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'bundler/gem_tasks'
|
22
|
+
require 'rake/testtask'
|
23
|
+
require 'rubocop/rake_task'
|
24
|
+
|
25
|
+
Rake::TestTask.new(:test) do |t|
|
26
|
+
t.libs << 'test'
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.test_files = FileList['test/**/*_test.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
task default: :test
|
32
|
+
|
33
|
+
# Rubocop
|
34
|
+
desc 'Run Rubocop lint checks'
|
35
|
+
task :rubocop do
|
36
|
+
RuboCop::RakeTask.new
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
#
|
3
|
+
# The MIT License
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
#
|
23
|
+
|
24
|
+
set -e
|
25
|
+
|
26
|
+
echo "Wait to start InfluxDB 2.0"
|
27
|
+
wget -S --spider --tries=20 --retry-connrefused --waitretry=5 http://localhost:9999/metrics
|
28
|
+
|
29
|
+
echo
|
30
|
+
echo "Post onBoarding request, to setup initial user (my-user@my-password), org (my-org) and bucketSetup (my-bucket)"
|
31
|
+
echo
|
32
|
+
curl -i -X POST http://localhost:9999/api/v2/setup -H 'accept: application/json' \
|
33
|
+
-d '{
|
34
|
+
"username": "my-user",
|
35
|
+
"password": "my-password",
|
36
|
+
"org": "my-org",
|
37
|
+
"bucket": "my-bucket",
|
38
|
+
"token": "my-token"
|
39
|
+
}'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
#
|
3
|
+
# The MIT License
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
#
|
23
|
+
|
24
|
+
set -e
|
25
|
+
|
26
|
+
DEFAULT_DOCKER_REGISTRY="quay.io/influxdb/"
|
27
|
+
DOCKER_REGISTRY="${DOCKER_REGISTRY:-$DEFAULT_DOCKER_REGISTRY}"
|
28
|
+
|
29
|
+
DEFAULT_INFLUXDB_V2_REPOSITORY="influxdb"
|
30
|
+
DEFAULT_INFLUXDB_V2_VERSION="2.0.0-beta"
|
31
|
+
INFLUXDB_V2_REPOSITORY="${INFLUXDB_V2_REPOSITORY:-$DEFAULT_INFLUXDB_V2_REPOSITORY}"
|
32
|
+
INFLUXDB_V2_VERSION="${INFLUXDB_V2_VERSION:-$DEFAULT_INFLUXDB_V2_VERSION}"
|
33
|
+
INFLUXDB_V2_IMAGE=${DOCKER_REGISTRY}${INFLUXDB_V2_REPOSITORY}:${INFLUXDB_V2_VERSION}
|
34
|
+
|
35
|
+
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
|
36
|
+
|
37
|
+
docker kill influxdb_v2 || true
|
38
|
+
docker rm influxdb_v2 || true
|
39
|
+
docker network rm influx_network || true
|
40
|
+
docker network create -d bridge influx_network --subnet 192.168.0.0/24 --gateway 192.168.0.1
|
41
|
+
|
42
|
+
#
|
43
|
+
# InfluxDB 2.0
|
44
|
+
#
|
45
|
+
echo
|
46
|
+
echo "Restarting InfluxDB 2.0 [${INFLUXDB_V2_IMAGE}] ... "
|
47
|
+
echo
|
48
|
+
|
49
|
+
docker pull "${INFLUXDB_V2_IMAGE}" || true
|
50
|
+
docker run \
|
51
|
+
--detach \
|
52
|
+
--name influxdb_v2 \
|
53
|
+
--network influx_network \
|
54
|
+
--publish 9999:9999 \
|
55
|
+
"${INFLUXDB_V2_IMAGE}"
|
56
|
+
|
57
|
+
#
|
58
|
+
# Post onBoarding request to InfluxDB 2
|
59
|
+
#
|
60
|
+
"${SCRIPT_PATH}"/influxdb-onboarding.sh
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
lib = File.expand_path('lib', __dir__)
|
22
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
23
|
+
require 'influxdb2/client/version'
|
24
|
+
|
25
|
+
Gem::Specification.new do |spec|
|
26
|
+
spec.name = 'influxdb-client'
|
27
|
+
spec.version = ENV['CIRCLE_BUILD_NUM'] ? "#{InfluxDB2::VERSION}-#{ENV['CIRCLE_BUILD_NUM']}" : InfluxDB2::VERSION
|
28
|
+
spec.authors = ['Jakub Bednar']
|
29
|
+
spec.email = ['jakub.bednar@gmail.com']
|
30
|
+
|
31
|
+
spec.summary = 'Ruby library for InfluxDB 2.'
|
32
|
+
spec.description = 'This is the official Ruby library for InfluxDB 2.'
|
33
|
+
spec.homepage = 'https://github.com/influxdata/influxdb-client-ruby'
|
34
|
+
spec.license = 'MIT'
|
35
|
+
|
36
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
37
|
+
spec.metadata['source_code_uri'] = 'https://github.com/influxdata/influxdb-client-ruby'
|
38
|
+
spec.metadata['changelog_uri'] = 'https://raw.githubusercontent.com/influxdata/influxdb-client-ruby/master/CHANGELOG.md'
|
39
|
+
|
40
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
41
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features|smoke)/})
|
42
|
+
spec.require_paths = ['lib']
|
43
|
+
spec.required_ruby_version = '>= 2.2.0'
|
44
|
+
|
45
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
46
|
+
spec.add_development_dependency 'codecov', '~> 0.1.16'
|
47
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
48
|
+
spec.add_development_dependency 'minitest-reporters', '~> 1.4'
|
49
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
50
|
+
spec.add_development_dependency 'rubocop', '~> 0.66.0'
|
51
|
+
spec.add_development_dependency 'simplecov', '~> 0.17.1'
|
52
|
+
spec.add_development_dependency 'webmock', '~> 3.7'
|
53
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'influxdb2/client/version'
|
22
|
+
require 'influxdb2/client/client'
|
23
|
+
require 'influxdb2/client/influx_error'
|
24
|
+
require 'influxdb2/client/write_api'
|
25
|
+
require 'influxdb2/client/point'
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
require 'net/http'
|
21
|
+
|
22
|
+
module InfluxDB2
|
23
|
+
# The client is the entry point to HTTP API defined
|
24
|
+
# in https://github.com/influxdata/influxdb/blob/master/http/swagger.yml.
|
25
|
+
class Client
|
26
|
+
# @return [ Hash ] options The configuration options.
|
27
|
+
attr_reader :options
|
28
|
+
|
29
|
+
# Instantiate a new InfluxDB client.
|
30
|
+
#
|
31
|
+
# @example Instantiate a client.
|
32
|
+
# InfluxDBClient::Client.new(url: 'https://localhost:9999', token: 'my-token')
|
33
|
+
#
|
34
|
+
# @param [Hash] options The options to be used by the client.
|
35
|
+
# @param [String] url InfluxDB URL to connect to (ex. https://localhost:9999).
|
36
|
+
# @param [String] token Access Token used for authenticating/authorizing the InfluxDB request sent by client.
|
37
|
+
#
|
38
|
+
# @option options [String] :bucket the default destination bucket for writes
|
39
|
+
# @option options [String] :org the default organization bucket for writes
|
40
|
+
# @option options [WritePrecision] :precision the default precision for the unix timestamps within
|
41
|
+
# @option options [Integer] :open_timeout Number of seconds to wait for the connection to open
|
42
|
+
# @option options [Integer] :write_timeout Number of seconds to wait for one block of data to be written
|
43
|
+
# @option options [Integer] :read_timeout Number of seconds to wait for one block of data to be read
|
44
|
+
# @option options [Integer] :max_redirect_count Maximal number of followed HTTP redirects
|
45
|
+
# @option options [bool] :use_ssl Turn on/off SSL for HTTP communication
|
46
|
+
# the body line-protocol
|
47
|
+
def initialize(url, token, options = nil)
|
48
|
+
@options = options ? options.dup : {}
|
49
|
+
@options[:url] = url if url.is_a? String
|
50
|
+
@options[:token] = token if token.is_a? String
|
51
|
+
@closed = false
|
52
|
+
|
53
|
+
at_exit { close! }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Write time series data into InfluxDB thought WriteApi.
|
57
|
+
#
|
58
|
+
# @return [WriteApi] New instance of WriteApi.
|
59
|
+
def create_write_api
|
60
|
+
WriteApi.new(options: @options)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Close all connections into InfluxDB 2.
|
64
|
+
#
|
65
|
+
# @return [ true ] Always true.
|
66
|
+
def close!
|
67
|
+
@closed = true
|
68
|
+
true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module InfluxDB2
|
2
|
+
# InfluxError that is raised during HTTP communication.
|
3
|
+
class InfluxError < StandardError
|
4
|
+
# HTTP status code
|
5
|
+
attr_reader :code
|
6
|
+
# Reference code unique to the error type
|
7
|
+
attr_reader :reference
|
8
|
+
|
9
|
+
def initialize(message:, code:, reference:)
|
10
|
+
super(message)
|
11
|
+
|
12
|
+
@code = code
|
13
|
+
@reference = reference
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_response(response)
|
17
|
+
json = JSON.parse(response.body)
|
18
|
+
obj = new(message: json['message'] || '', code: response.code, reference: json['code'] || '')
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.from_message(message)
|
23
|
+
obj = new(message: message, code: '', reference: '')
|
24
|
+
obj
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
# The MIT License
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module InfluxDB2
|
22
|
+
DEFAULT_WRITE_PRECISION = WritePrecision::NANOSECOND
|
23
|
+
ESCAPE_KEY_LIST = ['\\'.freeze, ','.freeze, ' '.freeze, '='.freeze].freeze
|
24
|
+
ESCAPE_VALUE_LIST = ['\\'.freeze, '"'.freeze].freeze
|
25
|
+
|
26
|
+
# Point defines the values that will be written to the database.
|
27
|
+
# Ref: http://bit.ly/influxdata-point
|
28
|
+
class Point
|
29
|
+
# Create DataPoint instance for specified measurement name.
|
30
|
+
#
|
31
|
+
# @example InfluxDB::Point.new(name: "h2o",
|
32
|
+
# tags: {host: 'aws', region: 'us'},
|
33
|
+
# fields: {level: 5, saturation: "99%"},
|
34
|
+
# time: 123)
|
35
|
+
#
|
36
|
+
# @param [String] name the measurement name for the point.
|
37
|
+
# @param [Hash] tags the tag set for the point
|
38
|
+
# @param [Hash] fields the fields for the point
|
39
|
+
# @param [Integer] time the timestamp for the point
|
40
|
+
# @param [WritePrecision] precision the precision for the unix timestamps within the body line-protocol
|
41
|
+
def initialize(name:, tags: nil, fields: nil, time: nil, precision: DEFAULT_WRITE_PRECISION)
|
42
|
+
@name = name
|
43
|
+
@tags = tags || {}
|
44
|
+
@fields = fields || {}
|
45
|
+
@time = time
|
46
|
+
@precision = precision
|
47
|
+
end
|
48
|
+
|
49
|
+
# Create DataPoint instance from specified data.
|
50
|
+
#
|
51
|
+
# @example Point.fromHash({
|
52
|
+
# name: 'cpu',
|
53
|
+
# tags: { host: 'server_nl', regios: 'us' },
|
54
|
+
# fields: {internal: 5, external: 6},
|
55
|
+
# time: 1422568543702900257
|
56
|
+
# })
|
57
|
+
#
|
58
|
+
# @param [Hash] data
|
59
|
+
def self.from_hash(data)
|
60
|
+
obj = new(name: data[:name], tags: data[:tags], fields: data[:fields], time: data[:time])
|
61
|
+
obj
|
62
|
+
end
|
63
|
+
|
64
|
+
# Adds or replaces a tag value for a point.
|
65
|
+
#
|
66
|
+
# @example InfluxDB::Point.new(name: "h2o")
|
67
|
+
# .add_tag("location", "europe")
|
68
|
+
# .add_field("level", 2)
|
69
|
+
#
|
70
|
+
# @param [Object] key the tag name
|
71
|
+
# @param [Object] value the tag value
|
72
|
+
def add_tag(key, value)
|
73
|
+
@tags[key] = value
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
# Adds or replaces a field value for a point.
|
78
|
+
#
|
79
|
+
# @example InfluxDB::Point.new(name: "h2o")
|
80
|
+
# .add_tag("location", "europe")
|
81
|
+
# .add_field("level", 2)
|
82
|
+
#
|
83
|
+
# @param [Object] key the field name
|
84
|
+
# @param [Object] value the field value
|
85
|
+
def add_field(key, value)
|
86
|
+
@fields[key] = value
|
87
|
+
self
|
88
|
+
end
|
89
|
+
|
90
|
+
# Updates the timestamp for the point.
|
91
|
+
#
|
92
|
+
# @example InfluxDB::Point.new(name: "h2o")
|
93
|
+
# .add_tag("location", "europe")
|
94
|
+
# .add_field("level", 2)
|
95
|
+
# .time(Time.new(2015, 10, 15, 8, 20, 15), InfluxDB::WritePrecision::MILLISECOND)
|
96
|
+
#
|
97
|
+
# @example InfluxDB::Point.new(name: "h2o")
|
98
|
+
# .add_tag("location", "europe")
|
99
|
+
# .add_field("level", 2)
|
100
|
+
# .time(123, InfluxDB::WritePrecision::NANOSECOND)
|
101
|
+
#
|
102
|
+
# @param [Object] time the timestamp
|
103
|
+
# @param [WritePrecision] precision the timestamp precision
|
104
|
+
def time(time, precision)
|
105
|
+
@time = time
|
106
|
+
@precision = precision
|
107
|
+
self
|
108
|
+
end
|
109
|
+
|
110
|
+
# If there is no field then return nil.
|
111
|
+
#
|
112
|
+
# @return a string representation of the point
|
113
|
+
def to_line_protocol
|
114
|
+
line_protocol = ''
|
115
|
+
measurement = _escape_key(@name || '')
|
116
|
+
|
117
|
+
line_protocol << measurement
|
118
|
+
|
119
|
+
tags = _escape_tags
|
120
|
+
line_protocol << ",#{tags}" unless tags.empty?
|
121
|
+
line_protocol << ' '.freeze if line_protocol[-1] == '\\'
|
122
|
+
|
123
|
+
fields = _escape_fields
|
124
|
+
return nil if fields.empty?
|
125
|
+
|
126
|
+
line_protocol << " #{fields}" if fields
|
127
|
+
timestamp = _escape_time
|
128
|
+
line_protocol << " #{timestamp}" if timestamp
|
129
|
+
|
130
|
+
line_protocol
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def _escape_tags
|
136
|
+
return if @tags.nil?
|
137
|
+
|
138
|
+
@tags.sort.to_h.map do |k, v|
|
139
|
+
key = _escape_key(k.to_s)
|
140
|
+
value = _escape_key(v.to_s)
|
141
|
+
if key.empty? || value.empty?
|
142
|
+
nil
|
143
|
+
else
|
144
|
+
"#{key}=#{value}"
|
145
|
+
end
|
146
|
+
end.reject(&:nil?).join(','.freeze)
|
147
|
+
end
|
148
|
+
|
149
|
+
def _escape_fields
|
150
|
+
return if @fields.nil?
|
151
|
+
|
152
|
+
@fields.sort.to_h.map do |k, v|
|
153
|
+
key = _escape_key(k.to_s)
|
154
|
+
value = _escape_value(v)
|
155
|
+
if key.empty? || value.empty?
|
156
|
+
nil
|
157
|
+
else
|
158
|
+
"#{key}=#{value}"
|
159
|
+
end
|
160
|
+
end.reject(&:nil?).join(','.freeze)
|
161
|
+
end
|
162
|
+
|
163
|
+
def _escape_key(value)
|
164
|
+
result = value.dup
|
165
|
+
ESCAPE_KEY_LIST.each do |ch|
|
166
|
+
result = result.gsub(ch) { "\\#{ch}" }
|
167
|
+
end
|
168
|
+
result
|
169
|
+
end
|
170
|
+
|
171
|
+
def _escape_value(value)
|
172
|
+
if value.nil?
|
173
|
+
''
|
174
|
+
elsif value.is_a?(String)
|
175
|
+
result = value.dup
|
176
|
+
ESCAPE_VALUE_LIST.each do |ch|
|
177
|
+
result = result.gsub(ch) { "\\#{ch}" }
|
178
|
+
end
|
179
|
+
'"'.freeze + result + '"'.freeze
|
180
|
+
elsif value.is_a?(Integer)
|
181
|
+
"#{value}i"
|
182
|
+
elsif [Float::INFINITY, -Float::INFINITY].include?(value)
|
183
|
+
''
|
184
|
+
else
|
185
|
+
value.to_s
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def _escape_time
|
190
|
+
if @time.nil?
|
191
|
+
nil
|
192
|
+
elsif @time.is_a?(Integer)
|
193
|
+
@time.to_s
|
194
|
+
elsif @time.is_a?(Float)
|
195
|
+
@time.round.to_s
|
196
|
+
elsif @time.is_a?(Time)
|
197
|
+
nano_seconds = @time.to_i * 1e9
|
198
|
+
nano_seconds += @time.tv_nsec
|
199
|
+
case @precision || DEFAULT_WRITE_PRECISION
|
200
|
+
when InfluxDB2::WritePrecision::MILLISECOND then
|
201
|
+
(nano_seconds / 1e6).round
|
202
|
+
when InfluxDB2::WritePrecision::SECOND then
|
203
|
+
(nano_seconds / 1e9).round
|
204
|
+
when InfluxDB2::WritePrecision::MICROSECOND then
|
205
|
+
(nano_seconds / 1e3).round
|
206
|
+
when InfluxDB2::WritePrecision::NANOSECOND then
|
207
|
+
nano_seconds.round
|
208
|
+
end
|
209
|
+
else
|
210
|
+
@time.to_s
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|