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



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

def initialize *args
  @database = args.first if args.first.is_a? String
  opts = args.last.is_a?(Hash) ? args.last : {}
  @hosts = 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

  unless @hosts.is_a? Array
    @hosts = [@hosts]
  end
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.



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

def queue
  @queue
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.



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

def worker=(value)
  @worker = value
end

Instance Method Details

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



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

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



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

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

#continuous_queries(database) ⇒ Object



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

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

#create_cluster_admin(username, password) ⇒ Object



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

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)



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

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



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

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



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

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

#delete_database(name) ⇒ Object



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

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

#delete_database_user(database, username) ⇒ Object



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

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

#get_cluster_admin_listObject



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

def get_cluster_admin_list
  get full_url("cluster_admins")
end

#get_database_listObject



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

def get_database_list
  get full_url("db")
end

#get_database_user_info(database, username) ⇒ Object



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

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

#get_database_user_list(database) ⇒ Object



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

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

#query(query) ⇒ Object



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

def query(query)
  url = URI.encode full_url("db/#{@database}/series", "q=#{query}")
  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

#update_cluster_admin(username, password) ⇒ Object



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

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



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

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



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

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