Improved monitoring amount_check, no state if no limit params are used.

This commit is contained in:
Martin Edenhofer 2019-01-21 08:01:51 +01:00
parent d28086af95
commit 50d85a1429
2 changed files with 32 additions and 10 deletions

View file

@ -322,10 +322,13 @@ curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h
{ param: :min_warning, notice: 'warning', type: 'lt' }, { param: :min_warning, notice: 'warning', type: 'lt' },
] ]
result = {} result = {}
state_param = false
map.each do |row| map.each do |row|
next if params[row[:param]].blank? next if params[row[:param]].blank?
raise Exceptions::UnprocessableEntity, "#{row[:param]} need to be an integer!" if params[row[:param]].to_i.zero? raise Exceptions::UnprocessableEntity, "#{row[:param]} need to be an integer!" if params[row[:param]].to_i.zero?
state_param = true
count = Ticket.where('created_at >= ?', created_at).count count = Ticket.where('created_at >= ?', created_at).count
if row[:type] == 'gt' if row[:type] == 'gt'
@ -351,12 +354,15 @@ curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h
if result.blank? if result.blank?
result = { result = {
state: 'ok', state: 'ok',
message: '', count: Ticket.where('created_at >= ?', created_at).count,
count: Ticket.where('created_at >= ?', created_at).count,
} }
end end
if state_param == false
result.delete(:state)
end
render json: result render json: result
end end

View file

@ -664,8 +664,8 @@ RSpec.describe 'Monitoring', type: :request do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash) expect(json_response).to be_a_kind_of(Hash)
expect(json_response['state']).to eq('ok') expect(json_response.key?('state')).to eq(false)
expect(json_response['message']).to eq('') expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(0) expect(json_response['count']).to eq(0)
Ticket.destroy_all Ticket.destroy_all
@ -695,7 +695,7 @@ RSpec.describe 'Monitoring', type: :request do
expect(json_response).to be_a_kind_of(Hash) expect(json_response).to be_a_kind_of(Hash)
expect(json_response['state']).to eq('ok') expect(json_response['state']).to eq('ok')
expect(json_response['message']).to eq('') expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(6) expect(json_response['count']).to eq(6)
(1..6).each do |i| (1..6).each do |i|
@ -724,22 +724,38 @@ RSpec.describe 'Monitoring', type: :request do
expect(json_response['message']).to eq('The limit of 20 was exceeded with 22 in the last 1h') expect(json_response['message']).to eq('The limit of 20 was exceeded with 22 in the last 1h')
expect(json_response['count']).to eq(22) expect(json_response['count']).to eq(22)
get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash) expect(json_response).to be_a_kind_of(Hash)
expect(json_response['state']).to eq('ok') expect(json_response['state']).to eq('ok')
expect(json_response['message']).to eq('') expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(22)
get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash)
expect(json_response.key?('state')).to eq(false)
expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(22) expect(json_response['count']).to eq(22)
travel 2.hours travel 2.hours
get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash) expect(json_response).to be_a_kind_of(Hash)
expect(json_response['state']).to eq('ok') expect(json_response['state']).to eq('ok')
expect(json_response['message']).to eq('') expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(0)
get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash)
expect(json_response.key?('state')).to eq(false)
expect(json_response.key?('message')).to eq(false)
expect(json_response['count']).to eq(0) expect(json_response['count']).to eq(0)
end end
end end