From 7338b91a315897856008f812705d193346b931ea Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Jun 2018 14:57:00 +0200 Subject: [PATCH] Improved monitoring controller to get storage usage. --- app/controllers/monitoring_controller.rb | 12 +++++++++++ app/models/store.rb | 1 + test/browser_test_helper.rb | 1 + .../integration/monitoring_controller_test.rb | 21 +++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/app/controllers/monitoring_controller.rb b/app/controllers/monitoring_controller.rb index cdc18fec0..aca2d5b66 100644 --- a/app/controllers/monitoring_controller.rb +++ b/app/controllers/monitoring_controller.rb @@ -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 diff --git a/app/models/store.rb b/app/models/store.rb index 2b9e1cca5..41ddb9b17 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -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, diff --git a/test/browser_test_helper.rb b/test/browser_test_helper.rb index b53a5e477..50a77dff4 100644 --- a/test/browser_test_helper.rb +++ b/test/browser_test_helper.rb @@ -565,6 +565,7 @@ class TestCase < Test::Unit::TestCase displayed: false, # true|false browser: browser1, css: '.some_class', + displayed: true, # true|false ) =end diff --git a/test/integration/monitoring_controller_test.rb b/test/integration/monitoring_controller_test.rb index 0778d09b9..68e291aad 100644 --- a/test/integration/monitoring_controller_test.rb +++ b/test/integration/monitoring_controller_test.rb @@ -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)