Class: Kapacitor::Client
- Inherits:
-
Object
- Object
- Kapacitor::Client
- Defined in:
- lib/kapacitor/client.rb
Instance Attribute Summary collapse
-
#http ⇒ Net::HTTP
readonly
HTTP client instance.
-
#url ⇒ URI
readonly
Kapacitor REST API URL.
Instance Method Summary collapse
-
#define_task(id:, dbrps:, **opts) ⇒ Object
Define a Kapacitor task.
-
#define_template(id:, type:, script:) ⇒ Object
Define a Kapacitor template.
-
#define_topic_handler(id:, topic:, kind:, match: nil, options: {}) ⇒ Object
Define a topic handler.
-
#delete_task(id:) ⇒ Object
Delete a Kapacitor task.
-
#delete_template(id:) ⇒ Object
Delete a Kapacitor template.
-
#delete_topic_handler(id:, topic:) ⇒ Object
Delete a topic handler.
-
#initialize(url: 'http://localhost:9092/kapacitor', version: 'v1') ⇒ Client
constructor
Create a new client.
-
#tasks(offset: 0, limit: 100) ⇒ Array[Hash]
Retrieve Kapacitor tasks.
-
#templates(offset: 0, limit: 100) ⇒ Array[Hash]
Retrieve Kapacitor templates.
-
#topic_handlers(topic:) ⇒ Array[Hash]
Retrieve topic’s handlers.
-
#topics ⇒ List[String]
Retrieve Kapacitor topic.
-
#update_task(id:, **opts) ⇒ Object
Update a Kapacitor task.
-
#update_template(id:, **opts) ⇒ Object
Update a Kapacitor template.
-
#update_topic_handler(id:, topic:, kind:, match: nil, options: nil) ⇒ Object
Update a topic handler.
Constructor Details
#initialize(url: 'http://localhost:9092/kapacitor', version: 'v1') ⇒ Client
Create a new client
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
#http ⇒ Net::HTTP (readonly)
Returns HTTP client instance.
9 10 11 |
# File 'lib/kapacitor/client.rb', line 9 def http @http end |
#url ⇒ URI (readonly)
Returns 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
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
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
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'] = api_post(endpoint: "alerts/topics/#{topic}/handlers", data: req) end |
#delete_task(id:) ⇒ Object
Delete a Kapacitor task
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
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
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
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
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
242 243 244 |
# File 'lib/kapacitor/client.rb', line 242 def topic_handlers(topic:) api_get(endpoint: "alerts/topics/#{topic}/handlers")['handlers'] end |
#topics ⇒ List[String]
Retrieve Kapacitor topic
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
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
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
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'] = unless .nil? api_put(endpoint: "alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty? end |