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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/influxdb/client.rb', line 34

def initialize *args
  @database = args.first if args.first.is_a? String
  opts = args.last.is_a?(Hash) ? args.last : {}
  @host = opts[:host] || "localhost"
  @port = opts[:port] || 8086
  @username = opts[:username] || "root"
  @password = opts[:password] || "root"
  @http = Net::HTTP.new(@host, @port)
  @http.use_ssl = opts[:use_ssl]
  @time_precision = opts[:time_precision] || "m"
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

#hostObject

Returns the value of attribute host.



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

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



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

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

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



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

def worker
  @worker
end

Instance Method Details

#_write(payload, time_precision = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/influxdb/client.rb', line 125

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

  headers = {"Content-Type" => "application/json"}
  response = @http.request(Net::HTTP::Post.new(url, headers), data)
  raise "Write failed with '#{response.message}'" unless (200...300).include?(response.code.to_i)
  response
end

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



102
103
104
# File 'lib/influxdb/client.rb', line 102

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

#create_cluster_admin(username, password) ⇒ Object



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

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)



47
48
49
50
51
52
# File 'lib/influxdb/client.rb', line 47

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



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

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



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

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

#delete_database(name) ⇒ Object



54
55
56
# File 'lib/influxdb/client.rb', line 54

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

#delete_database_user(database, username) ⇒ Object



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

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

#get_cluster_admin_listObject



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

def get_cluster_admin_list
  get full_url("cluster_admins")
end

#get_database_listObject



58
59
60
# File 'lib/influxdb/client.rb', line 58

def get_database_list
  get full_url("db")
end

#get_database_user_list(database) ⇒ Object



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

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

#query(query) ⇒ Object



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

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



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

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



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

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 = false, time_precision = @time_precision) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/influxdb/client.rb', line 106

def write_point(name, data, async=false, 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 if @worker.nil?
    @worker.queue.push(payload)
  else
    _write([payload], time_precision)
  end
end