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(url: 'http://localhost:9092/kapacitor', version: 'v1') ⇒ Client

Create a new client

Parameters:

  • url (String) (defaults to: 'http://localhost:9092/kapacitor')

    Kapacitor REST API’s URL (defaults to ‘localhost:9092`)

  • version (Integer) (defaults to: 'v1')

    API version (defaults to ‘v1preview`)



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

def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1')
  @http = HTTPClient.new
  @url = [url, version].join('/')
end

Instance Attribute Details

#httpNet::HTTP (readonly)

Returns HTTP client instance.

Returns:

  • (Net::HTTP)

    HTTP client instance



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

def http
  @http
end

#urlURI (readonly)

Returns Kapacitor REST API URL.

Returns:

  • (URI)

    Kapacitor REST API URL



7
8
9
# File 'lib/kapacitor/client.rb', line 7

def url
  @url
end

Instance Method Details

#define_task(id:, dbrps:, **opts) ⇒ Object

Define a Kapacitor task

Parameters:

  • id (String)

    Task ID

  • dbrps (String)

    List of database retention policy pairs the task is allowed to access.

  • **opts (Hash)

    Any number of task parameters to push into the Hash



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kapacitor/client.rb', line 98

def define_task(id:, dbrps:, **opts)
  if (opts[:template_id].nil? && opts[:type].nil? && opts[:script].nil?) || (opts[:template_id] && (opts[:type] || opts[:script]))
    raise ArgumentError, "Must specify either a Template ID or a script and type"
  elsif opts[:template_id].nil? && (opts[:type].nil? || 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' || opts[:status] == 'disabled'
  end

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

  req = {
    'id' => id,
    'dbrps' => 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(endpoint: 'tasks', data: req)
end

#define_template(id:, type:, script:) ⇒ Object

Define a Kapacitor template

Parameters:

  • id (String)

    Template ID

  • type (String)

    Template type. Valid values: ‘batch`, `stream`.

  • script (String)

    Tick script

Raises:

  • (ArgumentError)


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

def define_template(id:, type:, script:)
  raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless type == 'batch' || type == 'stream'

  req = {
    'id' => id,
    'type' => type,
    'script' => script
  }

  api_post(endpoint: 'templates', data: req)
end

#define_topic_handler(id:, topic:, kind:, match: nil, options: {}) ⇒ Object

Define a topic handler

Parameters:

  • id (String)

    Handler ID

  • topic (String)

    Topic name

  • kind (String)

    Kind of handler

  • match (String) (defaults to: nil)

    Lambda expression

  • options (Hash) (defaults to: {})

    Handler options



200
201
202
203
204
205
206
207
208
# File 'lib/kapacitor/client.rb', line 200

def define_topic_handler(id:, topic:, kind:, match: nil, options: {})
  req = {
    'id': id,
    'kind': kind
  }
  req['match'] = match unless match.nil?
  req['options'] = options
  api_post(endpoint: "alerts/topics/#{topic}/handlers", data: req)
end

#delete_task(id:) ⇒ Object

Delete a Kapacitor task

Parameters:

  • id (String)

    Task ID



165
166
167
# File 'lib/kapacitor/client.rb', line 165

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

#delete_template(id:) ⇒ Object

Delete a Kapacitor template

Parameters:

  • id (String)

    Template ID



60
61
62
# File 'lib/kapacitor/client.rb', line 60

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

#delete_topic_handler(id:, topic:) ⇒ Object

Delete a topic handler

Parameters:

  • id (String)

    Handler ID

  • topic (String)

    Topic name



233
234
235
# File 'lib/kapacitor/client.rb', line 233

def delete_topic_handler(id:, topic:)
  api_delete(endpoint: "alerts/topics/#{topic}/handlers/#{id}")
end

#tasks(offset: 0, limit: 100) ⇒ Array[Hash]

Retrieve Kapacitor tasks

Parameters:

  • offset (Integer) (defaults to: 0)

    Offset count for paginating through tasks

  • limit (Integer) (defaults to: 100)

    Maximum number of tasks to return

Returns:

  • (Array[Hash])

    List of tasks



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/kapacitor/client.rb', line 175

def tasks(offset: 0, limit: 100)
  tasks = []

  loop do
    res = api_get(endpoint: "tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
    break unless res.size > 0

    res.each do |task|
      tasks << api_get(endpoint: "tasks/#{task['id']}")
    end

    offset += limit
  end

  tasks
end

#templates(offset: 0, limit: 100) ⇒ Array[Hash]

Retrieve Kapacitor templates

Parameters:

  • offset (Integer) (defaults to: 0)

    Offset count for paginating through templates

  • limit (Integer) (defaults to: 100)

    Maximum number of templates to return

Returns:

  • (Array[Hash])

    List of templates



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kapacitor/client.rb', line 79

def templates(offset: 0, limit: 100)
  ret = []

  loop do
    res = api_get(endpoint: "templates?offset=#{offset}&limit=#{limit}")['templates']
    break unless res.size > 0
    ret += res
    offset += limit
  end

  ret
end

#topic_handlers(topic:) ⇒ Array[Hash]

Retrieve topic’s handlers

Parameters:

  • topic (String)

    Topic name

Returns:

  • (Array[Hash])

    List of handlers



242
243
244
# File 'lib/kapacitor/client.rb', line 242

def topic_handlers(topic:)
  api_get(endpoint: "alerts/topics/#{topic}/handlers")['handlers']
end

#topicsList[String]

Retrieve Kapacitor topic

Returns:

  • (List[String])

    List of topics



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

def topics()
  res = api_get(endpoint: "alerts/topics")
  return res['topics'].map { |v| v['id'] }
end

#update_task(id:, **opts) ⇒ Object

Update a Kapacitor task

Parameters:

  • id (String)

    Task ID

  • **opts (Hash)

    Any number of task parameters to push into the Hash



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kapacitor/client.rb', line 136

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'] = 'disabled'
  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' || opts[:type] == 'stream'
  end

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

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

  if opts[:status] == 'enabled'
    req['status'] = 'enabled'
    api_patch(endpoint: "tasks/#{id}", data: req) unless req.empty?
  end
end

#update_template(id:, **opts) ⇒ Object

Update a Kapacitor template

Parameters:

  • id (String)

    Template ID

  • **opts (Hash)

    Any number of template parameters to push into the Hash



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kapacitor/client.rb', line 44

def update_template(id:, **opts)
  req = {}
  req['type'] = opts[:type] if opts[:type]
  req['script'] = opts[: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(endpoint: "templates/#{id}", data: req) unless req.empty?
end

#update_topic_handler(id:, topic:, kind:, match: nil, options: nil) ⇒ Object

Update a topic handler

Parameters:

  • id (String)

    Handler ID

  • topic (String)

    Topic name

  • kind (String)

    Kind of handler

  • match (String) (defaults to: nil)

    Lambda expression

  • options (Hash) (defaults to: nil)

    Handler options



218
219
220
221
222
223
224
225
226
# File 'lib/kapacitor/client.rb', line 218

def update_topic_handler(id:, topic:, kind:, match: nil, options: nil)
  req = {
    'id': id,
    'kind': kind
  }
  req['match'] = match unless match.nil?
  req['options'] = options unless options.nil?
  api_put(endpoint: "alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
end