Class: Kapacitor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kapacitor/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
# File 'lib/kapacitor/client.rb', line 14

def initialize(opts = {})
  host = opts['host'] || 'localhost'
  version = opts['version'] || 'v1'

  @uri = URI.parse("http://#{opts['host']}/kapacitor/#{opts['version']}")
  @http = Net::HTTP.new(@uri.host, @uri.port)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



12
13
14
# File 'lib/kapacitor/client.rb', line 12

def http
  @http
end

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/kapacitor/client.rb', line 12

def uri
  @uri
end

Instance Method Details

#define_task(id, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kapacitor/client.rb', line 59

def define_task(id, opts = {})
  raise ArgumentError, "Kapacitor task dbrps is required"

  if (opts['template_id'].nil? and opts['type'].nil? and opts['script'].nil?) or (opts['template_id'] and (opts['type'] or opts['script']))
    raise ArgumentError, "Must specify either a Template ID or a script and type"
  elsif opts['template_id'].nil? and (opts['type'].nil? or opts['script'].nil?)
    raise ArgumentError, "Must specify both task type and script when not using a Template ID"
  end

  if opts['status']
    raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless (opts['status'] == 'enabled' or opts['status'] == 'disabled')
  end

  if opts['type']
    raise ArgumentError, "Kapacitor task type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
  end

  req = {
    'id' => id,
    'dbrps' => opts['dbrps'],
    'status' => opts['status'] || 'enabled'
  }

  if opts['template_id']
    req['template-id'] = opts['template_id']
  else
    req['type'] = opts['type']
    req['script'] = opts['script']
  end

  req['vars'] = opts['vars'] if opts['vars']

  api_post('/tasks', req)
end

#define_template(id, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kapacitor/client.rb', line 22

def define_template(id, opts = {})
  raise ArgumentError, "Kapacitor template type is required" unless opts['type']
  raise ArgumentError, "Kapacitor template tickscript required" unless opts['script']

  if opts['type']
    raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
  end

  req = {
    'id' => id,
    'type' => opts['type'],
    'script' => opts['script']
  }

  api_post('/templates', req)
end

#delete_task(id) ⇒ Object



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

def delete_task(id)
  api_delete("/tasks/#{id}")
end

#delete_template(id) ⇒ Object



51
52
53
# File 'lib/kapacitor/client.rb', line 51

def delete_template(id)
  api_delete("/templates/#{id}")
end

#tasksObject



118
119
120
121
122
123
124
125
126
# File 'lib/kapacitor/client.rb', line 118

def tasks
  tasks = []

  api_get('/tasks?fields=id')['tasks'].each do |task|
    tasks << api_get("/tasks/#{task['id']}")
  end

  tasks
end

#templatesObject



55
56
57
# File 'lib/kapacitor/client.rb', line 55

def templates
  api_get('/templates')['templates']
end

#update_task(id, opts = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kapacitor/client.rb', line 94

def update_task(id, opts = {})
  req = {}
  req['template-id'] = opts['template_id'] if opts['template_id']
  req['type'] = opts['type'] if opts['type']
  req['dbrps'] = opts['dbrps'] if opts['dbrps']
  req['script'] = opts['script'] if opts['script']
  req['status'] = opts['status'] if opts['status']
  req['vars'] = opts['vars'] if opts['vars']

  if opts['type']
    raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
  end

  if opts['status']
    raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless (opts['status'] == 'enabled' or opts['status'] == 'disabled')
  end

  api_patch("/tasks/#{id}", req) unless req.empty?
end

#update_template(id, opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kapacitor/client.rb', line 39

def update_template(id, opts = {})
  req = {}
  req['type'] = type if opts['type']
  req['script'] = script if opts['script']

  if opts['type']
    raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
  end

  api_patch("/templates/#{id}", req) unless req.empty?
end