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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/influxdb/client.rb', line 43

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
  @retry = opts.fetch(:retry, nil)
  @retry = case @retry
  when Integer
    @retry
  when true, nil
    -1
  when false
    0
  end

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

  at_exit { stop! }
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



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

def database
  @database
end

#hostsObject

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

#stoppedObject

Returns the value of attribute stopped.



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

def stopped
  @stopped
end

#time_precisionObject

Returns the value of attribute time_precision.



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

def time_precision
  @time_precision
end

#use_sslObject

Returns the value of attribute use_ssl.



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

def use_ssl
  @use_ssl
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#worker=(value) ⇒ Object

Sets the attribute worker

Parameters:

  • value

    the value to set the attribute worker to.



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

def worker=(value)
  @worker = value
end

Instance Method Details

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



167
168
169
170
171
# File 'lib/influxdb/client.rb', line 167

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



132
133
134
# File 'lib/influxdb/client.rb', line 132

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

#continuous_queries(database) ⇒ Object



136
137
138
# File 'lib/influxdb/client.rb', line 136

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

#create_cluster_admin(username, password) ⇒ Object



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

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)



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

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



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

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



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

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

#delete_database(name) ⇒ Object



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

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

#delete_database_user(database, username) ⇒ Object



120
121
122
# File 'lib/influxdb/client.rb', line 120

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

#delete_series(series) ⇒ Object



189
190
191
# File 'lib/influxdb/client.rb', line 189

def delete_series(series)
  delete full_url("/db/#{@database}/series/#{series}")
end

#delete_shard(shard_id, server_ids, username, password) ⇒ Object



144
145
146
147
# File 'lib/influxdb/client.rb', line 144

def delete_shard(shard_id, server_ids, username, password)
  data = JSON.generate({"serverIds" => server_ids})
  delete full_url("/cluster/shards/#{shard_id}", :u => username, :p => password), data
end

#get_cluster_admin_listObject



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

def get_cluster_admin_list
  get full_url("/cluster_admins")
end

#get_database_listObject



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

def get_database_list
  get full_url("/db")
end

#get_database_user_info(database, username) ⇒ Object



128
129
130
# File 'lib/influxdb/client.rb', line 128

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

#get_database_user_list(database) ⇒ Object



124
125
126
# File 'lib/influxdb/client.rb', line 124

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

#get_shard_listObject



140
141
142
# File 'lib/influxdb/client.rb', line 140

def get_shard_list()
  get full_url("/cluster/shards")
end

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/influxdb/client.rb', line 173

def query(query, time_precision=@time_precision)
  url = 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



193
194
195
# File 'lib/influxdb/client.rb', line 193

def stop!
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/influxdb/client.rb', line 197

def stopped?
  @stopped
end

#update_cluster_admin(username, password) ⇒ Object



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

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



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

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/influxdb/client.rb', line 149

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