diff --git a/app/controllers/monitoring_controller.rb b/app/controllers/monitoring_controller.rb index 8f2ee12b4..e1d4a838b 100644 --- a/app/controllers/monitoring_controller.rb +++ b/app/controllers/monitoring_controller.rb @@ -322,10 +322,13 @@ curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h { param: :min_warning, notice: 'warning', type: 'lt' }, ] result = {} + state_param = false map.each do |row| next if params[row[:param]].blank? 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 if row[:type] == 'gt' @@ -351,12 +354,15 @@ curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h if result.blank? result = { - state: 'ok', - message: '', - count: Ticket.where('created_at >= ?', created_at).count, + state: 'ok', + count: Ticket.where('created_at >= ?', created_at).count, } end + if state_param == false + result.delete(:state) + end + render json: result end diff --git a/spec/requests/integration/monitoring_spec.rb b/spec/requests/integration/monitoring_spec.rb index 513351901..93c269f52 100644 --- a/spec/requests/integration/monitoring_spec.rb +++ b/spec/requests/integration/monitoring_spec.rb @@ -664,8 +664,8 @@ RSpec.describe 'Monitoring', type: :request do expect(response).to have_http_status(200) expect(json_response).to be_a_kind_of(Hash) - expect(json_response['state']).to eq('ok') - expect(json_response['message']).to eq('') + expect(json_response.key?('state')).to eq(false) + expect(json_response.key?('message')).to eq(false) expect(json_response['count']).to eq(0) 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['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) (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['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(json_response).to be_a_kind_of(Hash) 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) 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(json_response).to be_a_kind_of(Hash) 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) end end