Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add job count per api key #467

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions api/v01/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions api/v01/entities/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -128,5 +129,6 @@ module OptimizerWrapper
allow_polylines: true
},
redis_count: REDIS_COUNT,
redis_resque: REDIS_RESQUE
}
end
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion test/api/v01/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/api/v01/helpers/count_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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