Latias::Influxdb
Ruby lib for use influxdb version 2 on Ruby language
Installation
Add this line to your application's Gemfile:
gem 'latias-influxdb'
And then execute:
$ bundle
Or install it yourself as:
$ gem install latias-influxdb
Check version
rails latias:influxdb:verion
Configuration & Usage
Configuration
First generating config file
rails g latias:influxdb:install
# config/initializers/latias_influxdb
Latias::Influxdb.configure do |config|
# default
# config.url = 'http://localhost:8086'
# config.token = 'token'
# config.bucket = 'bucket'
# config.org = 'org'
# config.use_ssl = false
# config.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
Usage
You can use
bucket = Latias::Influxdb::Bucket.new
bucket.client_connection
# or bucket = Latias::Influxdb::Bucket.new.client_connection
bucket.range('start: -1m').filter('fn: (r) => r["_measurement"] == "cpu"')
bucket.call
Or use with model
class MdbEnergyBucket < Latias::Influxdb::Bucket
def initialize
@url = 'http://localhost:8086'
@token = 'your token'
@bucket = 'MDBEnergy'
@org = 'MDBEnergy'
@use_ssl = false
client_connection
end
end
mdb_energy_bucket = MdbEnergyBucket.new
mdb_energy_bucket.range('start: -1m').filter('fn: (r) => r["_measurement"] == "cpu"')
mdb_energy_bucket.call
And you can add methods in model
class MdbEnergyBucket < Latias::Influxdb::Bucket
def initialize
@url = 'http://localhost:8086'
@token = 'your token'
@bucket = 'MDBEnergy'
@org = 'MDBEnergy'
@use_ssl = false
client_connection
end
def one_minute_before
range('start: -1m')
end
def find_cpu
filter('fn: (r) => r["_measurement"] == "cpu"')
end
def my_cpu_value
one_minute_before.find_cpu.call
end
end
mdb_energy_bucket = MdbEnergyBucket.new
mdb_energy_bucket.one_minute_before.find_cpu.call
# OR
mdb_energy_bucket.my_cpu_value
Method
def yield(query)
merge_query('yield', query)
end
def range(query)
merge_query('range', query)
end
def filter(query)
merge_query('filter', query)
end
def duplicate(query)
merge_query('duplicate', query)
end
def merge_query(key, query)
raw_function "#{key}(#{query})"
end
def raw_function(function)
raw_query "#{new_command} #{function}"
end
def raw_query(query)
@query += query
self
end
def client_reconnection
client_connection
end
# And
def execute(query)
@query = query
end
License
The gem is available as open source under the terms of the MIT License.
Build and push GEM
gem build latias-influxdb.gemspec
gem push latias-influxdb-xx.xx.xx.gem