Permalink
Browse files

Fixed issue #2391 - Wrong file size calculation in monitoring control…

…ler (postgres only)
  • Loading branch information...
rolfschmidt authored and thorsteneckel committed Dec 6, 2018
1 parent ffb64d6 commit 079c4c9593ae69153558c7dd451919974d7af419
Showing with 70 additions and 2 deletions.
  1. +1 −1 app/controllers/monitoring_controller.rb
  2. +69 −1 spec/requests/integration/monitoring_spec.rb
@@ -225,7 +225,7 @@ def status
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)'
sql = 'SELECT SUM(CAST(coalesce(size, \'0\') AS INTEGER)) FROM stores WHERE id IN (SELECT MAX(id) FROM stores GROUP BY store_file_id)'
records_array = ActiveRecord::Base.connection.exec_query(sql)
if records_array[0] && records_array[0]['sum']
sum = records_array[0]['sum']
@@ -110,9 +110,10 @@

# test storage usage
string = ''
10.times do
1000.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,
@@ -141,9 +142,76 @@
expect(json_response.key?('counts')).to be_truthy
expect(json_response.key?('last_created_at')).to be_truthy

first_json_response_kb = 0
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
expect(json_response['storage']).to be_truthy
expect(json_response['storage'].key?('kB')).to be_truthy
expect(json_response['storage']['kB']).to be > 0
expect(json_response['storage'].key?('MB')).to be_truthy
expect(json_response['storage'].key?('GB')).to be_truthy

first_json_response_kb = json_response['storage']['kB']
else
expect(json_response['storage']).to be_falsey
end

# save same file again
Store.add(
object: 'User',
o_id: 1,
data: string,
filename: 'filename.txt',
created_by_id: 1,
)

# status
get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
expect(response).to have_http_status(200)

expect(json_response).to be_a_kind_of(Hash)
expect(json_response['error']).to be_falsey
expect(json_response.key?('agents')).to be_truthy
expect(json_response.key?('last_login')).to be_truthy
expect(json_response.key?('counts')).to be_truthy
expect(json_response.key?('last_created_at')).to be_truthy

if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
expect(json_response['storage']).to be_truthy
expect(json_response['storage'].key?('kB')).to be_truthy

# check if the stores got summarized. value should be the same because the file has the same fingerprint
expect(json_response['storage']['kB']).to eq(first_json_response_kb)
expect(json_response['storage'].key?('MB')).to be_truthy
expect(json_response['storage'].key?('GB')).to be_truthy
else
expect(json_response['storage']).to be_falsey
end

Store.add(
object: 'User',
o_id: 1,
data: string + '123',
filename: 'filename2.txt',
created_by_id: 1,
)

# status
get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
expect(response).to have_http_status(200)

expect(json_response).to be_a_kind_of(Hash)
expect(json_response['error']).to be_falsey
expect(json_response.key?('agents')).to be_truthy
expect(json_response.key?('last_login')).to be_truthy
expect(json_response.key?('counts')).to be_truthy
expect(json_response.key?('last_created_at')).to be_truthy

if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
expect(json_response['storage']).to be_truthy
expect(json_response['storage'].key?('kB')).to be_truthy

# check if the stores got summarized. value should be greather than the size of just one file (saved 2 times)
expect(json_response['storage']['kB']).to be > first_json_response_kb
expect(json_response['storage'].key?('MB')).to be_truthy
expect(json_response['storage'].key?('GB')).to be_truthy
else

0 comments on commit 079c4c9

Please sign in to comment.