Improved monitoring controller to get storage usage.

This commit is contained in:
Martin Edenhofer 2018-06-05 14:57:00 +02:00
parent 9d972f9efd
commit 7338b91a31
4 changed files with 35 additions and 0 deletions

View file

@ -214,6 +214,18 @@ curl http://localhost/api/v1/monitoring/status?token=XXX
status[:last_created_at][key] = last&.created_at
end
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
sql = 'SELECT SUM(CAST(coalesce(size, \'0\') AS INTEGER)) FROM stores WHERE id IN (SELECT DISTINCT(store_file_id) FROM stores)'
records_array = ActiveRecord::Base.connection.exec_query(sql)
if records_array[0] && records_array[0]['sum']
sum = records_array[0]['sum']
status[:storage] = {
kB: sum / 1024,
MB: sum / 1024 / 1024,
GB: sum / 1024 / 1024 / 1024,
}
end
end
render json: status
end

View file

@ -22,6 +22,7 @@ add an attachment to storage
object: 'Ticket::Article',
o_id: 4711,
data: binary_string,
filename: 'filename.txt',
preferences: {
content_type: 'image/png',
content_id: 234,

View file

@ -565,6 +565,7 @@ class TestCase < Test::Unit::TestCase
displayed: false, # true|false
browser: browser1,
css: '.some_class',
displayed: true, # true|false
)
=end

View file

@ -147,6 +147,18 @@ class MonitoringControllerTest < ActionDispatch::IntegrationTest
test '03 monitoring with correct token' do
# test storage usage
string = ''
10.times do
string += 'Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text'
end
Store.add(
object: 'User',
o_id: 1,
data: string,
filename: 'filename.txt',
)
# health_check
get "/api/v1/monitoring/health_check?token=#{@token}", params: {}, headers: @headers
assert_response(200)
@ -169,6 +181,15 @@ class MonitoringControllerTest < ActionDispatch::IntegrationTest
assert(result.key?('counts'))
assert(result.key?('last_created_at'))
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
assert(result['storage'])
assert(result['storage'].key?('kB'))
assert(result['storage'].key?('MB'))
assert(result['storage'].key?('GB'))
else
assert_not(result['storage'])
end
# token
post '/api/v1/monitoring/token', params: { token: @token }.to_json, headers: @headers
assert_response(401)