Skip to content

Commit

Permalink
Adds distinct param and removes percentiles to Availability get_data
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio committed Jun 30, 2016
1 parent d098085 commit 9a3cb2e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/hawkular/metrics/metric_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, perc
order: nil)
params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets,
percentiles: percentiles, limit: limit, order: order }
resp = @client.http_get("/#{@resource}/#{ERB::Util.url_encode(id)}/data/?" +
encode_params(params))
resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
get_data_helper(id, params)
end

# Retrieve metric datapoints by tags
Expand All @@ -150,6 +148,14 @@ def tags_param(tags)
def encode_params(params)
URI.encode_www_form(params.select { |_k, v| !v.nil? })
end

private

def get_data_helper(id, params)
resp = @client.http_get("/#{@resource}/#{ERB::Util.url_encode(id)}/data/?" +
encode_params(params))
resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
end
end

# Class that interacts with "gauge" metric types
Expand Down Expand Up @@ -204,6 +210,24 @@ class Availability < Metrics
def initialize(client)
super(client, 'availability', 'availability')
end

# Retrieve metric datapoints
# @param id [String] metric definition id
# @param starts [Integer] optional timestamp (default now - 8h)
# @param ends [Integer] optional timestamp (default now)
# @param buckets [Integer] optional desired number of buckets over the specified timerange
# @param bucketDuration [String] optional interval (default no aggregation)
# @param distinct [String] optional set to true to return only distinct, contiguous values
# @param limit [Integer] optional limit the number of data points returned
# @param order [String] optional Data point sort order, based on timestamp (ASC, DESC)
# @return [Array[Hash]] datapoints
# @see #push_data #push_data for datapoint detail
def get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, distinct: nil, limit: nil,
order: nil)
params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets,
distinct: distinct, limit: limit, order: order }
get_data_helper(id, params)
end
end
end
end
27 changes: 27 additions & 0 deletions spec/integration/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,33 @@
update_metric_by_tags @client.avail, id
end
end

it 'Should raise ArgumentError, availability does not accept percentiles param' do
expect { @client.avail.get_data(SecureRandom.uuid, percentiles: 50) }.to raise_error(ArgumentError)
end

it 'Should group contiguous values' do
id = SecureRandom.uuid
now = @client.now
setup_client(username: 'jdoe', password: 'password', tenant: 'vcr-test')
VCR.use_cassette('Availability_metrics/Should group contiguous values',
erb: { id: id, now: now }, record: :none
) do
@client.avail.push_data(id, [
{ timestamp: now - 50, value: 'up' },
{ timestamp: now - 40, value: 'up' },
{ timestamp: now - 30, value: 'down' },
{ timestamp: now - 20, value: 'down' },
{ timestamp: now - 10, value: 'down' },
{ timestamp: now, value: 'up' }
])
expect(@client.avail.get_data(id, distinct: true, order: 'ASC')).to eq([
{ 'timestamp' => now - 50, 'value' => 'up' },
{ 'timestamp' => now - 30, 'value' => 'down' },
{ 'timestamp' => now, 'value' => 'up' }
])
end
end
end

describe 'Gauge metrics' do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a3cb2e

Please sign in to comment.