diff --git a/api/v01/api.rb b/api/v01/api.rb index c5cbbd483..570f71ec6 100644 --- a/api/v01/api.rb +++ b/api/v01/api.rb @@ -142,6 +142,36 @@ def split_key(key) json end + + def count_current_jobs(api_key) + OptimizerWrapper.config[:redis_resque].keys("resque:status*").count { |key| + JSON.parse(OptimizerWrapper.config[:redis_resque].get(key))['status'] == 'queued' && + JSON.parse(OptimizerWrapper.config[:redis_resque].get(key))['options']['api_key'] == api_key + } + end + + def metric(key) + hkey = split_key(key) + + if redis_count.type(key) == 'hash' + hredis = redis_count.hgetall(key) + hredis['count_current_jobs'] = count_current_jobs(hkey['key']).to_s + + { + count_asset: hkey['asset'], + count_date: hkey['date'], + count_endpoint: hkey['endpoint'], + count_hits: hredis['hits'], + count_ip: hkey['ip'], + count_key: hkey['key'], + count_service: hkey['service'], + count_transactions: hredis['transactions'], + count_current_jobs: hredis['count_current_jobs'], + } + else + log "Metrics: #{key} is not a hash" && {} + end + end end before do @@ -199,21 +229,7 @@ def split_key(key) status 200 present( - redis_count.keys("*#{count_base_key_no_key('optimize').join(':')}*").flat_map{ |key| - hkey = split_key(key) - hredis = redis_count.hgetall(key) - - { - count_asset: hkey['asset'], - count_date: hkey['date'], - count_endpoint: hkey['endpoint'], - count_hits: hredis['hits'], - count_ip: hkey['ip'], - count_key: hkey['key'], - count_service: hkey['service'], - count_transactions: hredis['transactions'], - } - }, with: Metrics + redis_count.keys("*#{count_base_key_no_key('*').join(':')}*").flat_map{ |key| metric(key) }, with: Metrics ) end end diff --git a/api/v01/entities/metrics.rb b/api/v01/entities/metrics.rb index 200003c02..ef100ccb7 100644 --- a/api/v01/entities/metrics.rb +++ b/api/v01/entities/metrics.rb @@ -31,6 +31,8 @@ def self.entity_name expose(:count_key, documentation: {type: String, desc: 'Key used for the request' }) expose(:count_service, documentation: {type: String, desc: 'Service used for the request' }) expose(:count_transactions, documentation: {type: Integer, desc: 'Transactions in the service for the request' }) + expose(:count_current_jobs, documentation: {type: Integer, \ + desc: 'Number of current running jobs for the api key' }) end end end diff --git a/config/environments/test.rb b/config/environments/test.rb index e53a515a9..68b3f3056 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -41,6 +41,7 @@ module OptimizerWrapper PARAMS_LIMIT = { points: 150, vehicles: 10 }.freeze QUOTAS = [{ daily: 100000, monthly: 1000000, yearly: 10000000 }].freeze # Only taken into account if REDIS_COUNT REDIS_COUNT = ENV['REDIS_COUNT_HOST'] && Redis.new(host: ENV['REDIS_COUNT_HOST']) || Redis.new + REDIS_RESQUE = ENV['REDIS_RESQUE_HOST'] && Redis.new(host: ENV['REDIS_RESQUE_HOST']) || Redis.new DUMP_DIR = File.join(Dir.tmpdir, 'optimizer-api', 'test', 'dump') FileUtils.mkdir_p(DUMP_DIR) unless File.directory?(DUMP_DIR) @@ -128,5 +129,6 @@ module OptimizerWrapper allow_polylines: true }, redis_count: REDIS_COUNT, + redis_resque: REDIS_RESQUE } end diff --git a/docker-compose.yml b/docker-compose.yml index 0f31ef09f..2827f8064 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,8 +35,6 @@ x-default-service: &default-service context: . dockerfile: docker/Dockerfile image: dev.example.com/mapotempo/mt-optimizer - depends_on: - - base volumes: - ./:/srv/app/ - ./archives/dump:/tmp/optimizer-api/dump diff --git a/test/api/v01/api_test.rb b/test/api/v01/api_test.rb index 50b50d574..f81fd975c 100644 --- a/test/api/v01/api_test.rb +++ b/test/api/v01/api_test.rb @@ -95,9 +95,16 @@ def test_use_quota def test_metrics clear_optim_redis_count + clear_optim_redis_resque + + old_config_solve_synchronously = OptimizerWrapper.config[:solve][:synchronously] + OptimizerWrapper.config[:solve][:synchronously] = false + old_resque_inline = Resque.inline + Resque.inline = false + post '/0.1/vrp/submit?asset=myAsset', { api_key: 'demo', vrp: VRP.toy }.to_json, \ 'CONTENT_TYPE' => 'application/json' - assert last_response.ok?, last_response.body + assert_equal 201, last_response.status get '0.1/metrics', { api_key: 'demo'} assert_equal 401, last_response.status @@ -114,6 +121,10 @@ def test_metrics assert_equal "myAsset", json["count_asset"] assert_equal "optimizer", json["count_service"] assert_equal "optimize", json["count_endpoint"] + assert_equal "1", json["count_current_jobs"] + ensure + Resque.inline = old_resque_inline + OptimizerWrapper.config[:solve][:synchronously] = old_config_solve_synchronously end def test_use_quota_nil diff --git a/test/api/v01/helpers/count_helper.rb b/test/api/v01/helpers/count_helper.rb index e65e6c2cf..8eed5634e 100644 --- a/test/api/v01/helpers/count_helper.rb +++ b/test/api/v01/helpers/count_helper.rb @@ -23,4 +23,10 @@ def clear_optim_redis_count OptimizerWrapper.config[:redis_count].del(to_remove) end end + + def clear_optim_redis_resque + OptimizerWrapper.config[:redis_resque].each_key do |to_remove| + OptimizerWrapper.config[:redis_resque].del(to_remove) + end + end end