Class: InfluxDB::Client

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

Constant Summary

Constants included from Logger

Logger::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# 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

  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

#workerObject

Returns the value of attribute worker.



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

def worker
  @worker
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



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

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

#continuous_queriesObject



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

def continuous_queries
  get full_url("continuous_queries")
end

#create_cluster_admin(username, password) ⇒ Object



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

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)



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

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



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

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



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

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

#delete_database(name) ⇒ Object



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

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

#delete_database_user(database, username) ⇒ Object



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

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

#get_cluster_admin_listObject



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

def get_cluster_admin_list
  get full_url("cluster_admins")
end

#get_database_listObject



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

def get_database_list
  get full_url("db")
end

#get_database_user_info(database, username) ⇒ Object



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

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

#get_database_user_list(database) ⇒ Object



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

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

#query(query) ⇒ 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)
  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



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

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



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

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



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

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 = InfluxDB::Worker.new(self) if @worker.nil?
    @worker.queue.push(payload)
  else
    _write([payload], time_precision)
  end
end