Class: InfluxDB::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/influxdb/client.rb

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, logger=

Constructor Details

#initialize(*args) ⇒ Client

Initializes a new InfluxDB client

Examples:

InfluxDB::Client.new                               # connect to localhost using root/root
                                                   # as the credentials and doesn't connect to a db

InfluxDB::Client.new 'db'                          # connect to localhost using root/root
                                                   # as the credentials and 'db' as the db name

InfluxDB::Client.new :username => 'username'       # override username, other defaults remain unchanged

Influxdb::Client.new 'db', :username => 'username' # override username, use 'db' as the db name

Valid options in hash

:host

the hostname to connect to

:port

the port to connect to

:username

the username to use when executing commands

:password

the password associated with the username

:use_ssl

use ssl to connect



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/influxdb/client.rb', line 42

def initialize *args
  @database = args.first if args.first.is_a? String
  opts = args.last.is_a?(Hash) ? args.last : {}
  @hosts = Array(opts[:hosts] || opts[:host] || ["localhost"])
  @port = opts[:port] || 8086
  @username = opts[:username] || "root"
  @password = opts[:password] || "root"
  @use_ssl = opts[:use_ssl] || false
  @time_precision = opts[:time_precision] || "s"
  @initial_delay = opts[:initial_delay] || 0.01
  @max_delay = opts[:max_delay] || 30
  @open_timeout = opts[:write_timeout] || 5
  @read_timeout = opts[:read_timeout] || 300
  @async = opts[:async] || false

  @worker = InfluxDB::Worker.new(self) if @async

  at_exit { stop! }
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def database
  @database
end

#hostsObject

Returns the value of attribute hosts.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def hosts
  @hosts
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def password
  @password
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def port
  @port
end

#queueObject

Returns the value of attribute queue.



17
18
19
# File 'lib/influxdb/client.rb', line 17

def queue
  @queue
end

#stoppedObject

Returns the value of attribute stopped.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def stopped
  @stopped
end

#time_precisionObject

Returns the value of attribute time_precision.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def time_precision
  @time_precision
end

#use_sslObject

Returns the value of attribute use_ssl.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def use_ssl
  @use_ssl
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/influxdb/client.rb', line 8

def username
  @username
end

#worker=(value) ⇒ Object

Sets the attribute worker

Parameters:

  • value

    the value to set the attribute worker to.



17
18
19
# File 'lib/influxdb/client.rb', line 17

def worker=(value)
  @worker = value
end

Instance Method Details

#_write(payload, time_precision = @time_precision) ⇒ Object



148
149
150
151
152
# File 'lib/influxdb/client.rb', line 148

def _write(payload, time_precision=@time_precision)
  url = full_url("db/#{@database}/series", "time_precision=#{time_precision}")
  data = JSON.generate(payload)
  post(url, data)
end

#alter_database_privilege(database, username, admin = true) ⇒ Object



122
123
124
# File 'lib/influxdb/client.rb', line 122

def alter_database_privilege(database, username, admin=true)
  update_database_user(database, username, :admin => admin)
end

#continuous_queries(database) ⇒ Object



126
127
128
# File 'lib/influxdb/client.rb', line 126

def continuous_queries(database)
  get full_url("db/#{database}/continuous_queries")
end

#create_cluster_admin(username, password) ⇒ Object



78
79
80
81
82
# File 'lib/influxdb/client.rb', line 78

def create_cluster_admin(username, password)
  url = full_url("cluster_admins")
  data = JSON.generate({:name => username, :password => password})
  post(url, data)
end

#create_database(name, options = {}) ⇒ Object

allow options, e.g. influxdb.create_database(‘foo’, replicationFactor: 3)



63
64
65
66
67
68
# File 'lib/influxdb/client.rb', line 63

def create_database(name, options = {})
  url = full_url("db")
  options[:name] = name
  data = JSON.generate(options)
  post(url, data)
end

#create_database_user(database, username, password) ⇒ Object



98
99
100
101
102
# File 'lib/influxdb/client.rb', line 98

def create_database_user(database, username, password)
  url = full_url("db/#{database}/users")
  data = JSON.generate({:name => username, :password => password})
  post(url, data)
end

#delete_cluster_admin(username) ⇒ Object



90
91
92
# File 'lib/influxdb/client.rb', line 90

def delete_cluster_admin(username)
  delete full_url("cluster_admins/#{username}")
end

#delete_database(name) ⇒ Object



70
71
72
# File 'lib/influxdb/client.rb', line 70

def delete_database(name)
  delete full_url("db/#{name}")
end

#delete_database_user(database, username) ⇒ Object



110
111
112
# File 'lib/influxdb/client.rb', line 110

def delete_database_user(database, username)
  delete full_url("db/#{database}/users/#{username}")
end

#get_cluster_admin_listObject



94
95
96
# File 'lib/influxdb/client.rb', line 94

def get_cluster_admin_list
  get full_url("cluster_admins")
end

#get_database_listObject



74
75
76
# File 'lib/influxdb/client.rb', line 74

def get_database_list
  get full_url("db")
end

#get_database_user_info(database, username) ⇒ Object



118
119
120
# File 'lib/influxdb/client.rb', line 118

def (database, username)
  get full_url("db/#{database}/users/#{username}")
end

#get_database_user_list(database) ⇒ Object



114
115
116
# File 'lib/influxdb/client.rb', line 114

def get_database_user_list(database)
  get full_url("db/#{database}/users")
end

#query(query, time_precision = @time_precision) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/influxdb/client.rb', line 154

def query(query, time_precision=@time_precision)
  url = URI.encode full_url("db/#{@database}/series", "q=#{query}&time_precision=#{time_precision}")
  series = get(url)

  if block_given?
    series.each { |s| yield s['name'], denormalize_series(s) }
  else
    series.reduce({}) do |col, s|
      name                  = s['name']
      denormalized_series   = denormalize_series s
      col[name]             = denormalized_series
      col
    end
  end
end

#stop!Object



170
171
172
# File 'lib/influxdb/client.rb', line 170

def stop!
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/influxdb/client.rb', line 174

def stopped?
  @stopped
end

#update_cluster_admin(username, password) ⇒ Object



84
85
86
87
88
# File 'lib/influxdb/client.rb', line 84

def update_cluster_admin(username, password)
  url = full_url("cluster_admins/#{username}")
  data = JSON.generate({:password => password})
  post(url, data)
end

#update_database_user(database, username, options = {}) ⇒ Object



104
105
106
107
108
# File 'lib/influxdb/client.rb', line 104

def update_database_user(database, username, options = {})
  url = full_url("db/#{database}/users/#{username}")
  data = JSON.generate(options)
  post(url, data)
end

#write_point(name, data, async = @async, time_precision = @time_precision) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/influxdb/client.rb', line 130

def write_point(name, data, async=@async, time_precision=@time_precision)
  data = data.is_a?(Array) ? data : [data]
  columns = data.reduce(:merge).keys.sort {|a,b| a.to_s <=> b.to_s}
  payload = {:name => name, :points => [], :columns => columns}

  data.each do |point|
    payload[:points] << columns.inject([]) do |array, column|
      array << InfluxDB::PointValue.new(point[column]).dump
    end
  end

  if async
    worker.push(payload)
  else
    _write([payload], time_precision)
  end
end