Add per-category object counter
This commit is contained in:
parent
1a9be076fa
commit
27e5dfc1b5
@ -87,6 +87,7 @@ module RemoteStorage
|
|||||||
def put_data(user, directory, key, data, content_type=nil)
|
def put_data(user, directory, key, data, content_type=nil)
|
||||||
object = build_data_object(user, directory, key, data, content_type)
|
object = build_data_object(user, directory, key, data, content_type)
|
||||||
|
|
||||||
|
object_exists = !object.data.nil?
|
||||||
existing_object_size = object_size(object)
|
existing_object_size = object_size(object)
|
||||||
|
|
||||||
timestamp = (Time.now.to_f * 1000).to_i
|
timestamp = (Time.now.to_f * 1000).to_i
|
||||||
@ -102,6 +103,7 @@ module RemoteStorage
|
|||||||
|
|
||||||
object.store
|
object.store
|
||||||
|
|
||||||
|
log_object_count(user, directory, 1) unless object_exists
|
||||||
log_object_size(user, directory, new_object_size, existing_object_size)
|
log_object_size(user, directory, new_object_size, existing_object_size)
|
||||||
update_all_directory_objects(user, directory, timestamp)
|
update_all_directory_objects(user, directory, timestamp)
|
||||||
|
|
||||||
@ -120,6 +122,7 @@ module RemoteStorage
|
|||||||
|
|
||||||
riak_response = data_bucket.delete("#{user}:#{directory}:#{key}")
|
riak_response = data_bucket.delete("#{user}:#{directory}:#{key}")
|
||||||
|
|
||||||
|
log_object_count(user, directory, -1)
|
||||||
log_object_size(user, directory, 0, existing_object_size)
|
log_object_size(user, directory, 0, existing_object_size)
|
||||||
|
|
||||||
timestamp = (Time.now.to_f * 1000).to_i
|
timestamp = (Time.now.to_f * 1000).to_i
|
||||||
@ -166,6 +169,20 @@ module RemoteStorage
|
|||||||
info.store
|
info.store
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def log_object_count(user, directory, change)
|
||||||
|
category = extract_category(directory)
|
||||||
|
|
||||||
|
info = info_bucket.get_or_new("usage:count:#{user}:#{category}")
|
||||||
|
info.content_type = "text/plain"
|
||||||
|
|
||||||
|
count = change.to_i
|
||||||
|
count += info.data.to_i
|
||||||
|
|
||||||
|
info.data = count.to_s
|
||||||
|
info.indexes.merge!({:user_id_bin => [user]})
|
||||||
|
info.store
|
||||||
|
end
|
||||||
|
|
||||||
def object_size(object)
|
def object_size(object)
|
||||||
if binary_link = object.links.select {|l| l.tag == "binary"}.first
|
if binary_link = object.links.select {|l| l.tag == "binary"}.first
|
||||||
response = head(LiquorCabinet.config['buckets']['binaries'], escape(binary_link.key))
|
response = head(LiquorCabinet.config['buckets']['binaries'], escape(binary_link.key))
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
require_relative "spec_helper"
|
require_relative "spec_helper"
|
||||||
|
|
||||||
def set_usage_size_info(user, category, size)
|
def set_usage_info(user, category, type, value)
|
||||||
object = info_bucket.get_or_new("usage:size:#{user}:#{category}")
|
object = info_bucket.get_or_new("usage:#{type}:#{user}:#{category}")
|
||||||
object.content_type = "text/plain"
|
object.content_type = "text/plain"
|
||||||
object.data = size.to_s
|
object.data = value.to_s
|
||||||
object.store
|
object.store
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ describe "App with Riak backend" do
|
|||||||
describe "PUT" do
|
describe "PUT" do
|
||||||
before do
|
before do
|
||||||
header "Authorization", "Bearer 123"
|
header "Authorization", "Bearer 123"
|
||||||
set_usage_size_info "jimmy", "documents", "23"
|
set_usage_info "jimmy", "documents", "size", "23"
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with implicit content type" do
|
describe "with implicit content type" do
|
||||||
@ -206,7 +206,7 @@ describe "App with Riak backend" do
|
|||||||
|
|
||||||
describe "with existing content" do
|
describe "with existing content" do
|
||||||
before do
|
before do
|
||||||
set_usage_size_info "jimmy", "documents", "10"
|
set_usage_info "jimmy", "documents", "size", "10"
|
||||||
put "/jimmy/documents/archive/foo", "lorem ipsum"
|
put "/jimmy/documents/archive/foo", "lorem ipsum"
|
||||||
put "/jimmy/documents/archive/foo", "some awesome content"
|
put "/jimmy/documents/archive/foo", "some awesome content"
|
||||||
end
|
end
|
||||||
@ -223,18 +223,27 @@ describe "App with Riak backend" do
|
|||||||
|
|
||||||
describe "public data" do
|
describe "public data" do
|
||||||
before do
|
before do
|
||||||
set_usage_size_info "jimmy", "public/documents", "10"
|
set_usage_info "jimmy", "public/documents", "size", "10"
|
||||||
|
set_usage_info "jimmy", "public/documents", "count", "100"
|
||||||
put "/jimmy/public/documents/notes/foo", "note to self"
|
put "/jimmy/public/documents/notes/foo", "note to self"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# after do
|
||||||
|
# info_bucket.delete "usage:size:jimmy:public/documents"
|
||||||
|
# end
|
||||||
|
|
||||||
it "saves the value" do
|
it "saves the value" do
|
||||||
last_response.status.must_equal 200
|
last_response.status.must_equal 200
|
||||||
data_bucket.get("jimmy:public/documents/notes:foo").data.must_equal "note to self"
|
data_bucket.get("jimmy:public/documents/notes:foo").data.must_equal "note to self"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the overall category size" do
|
it "increases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:public/documents").data.must_equal "22"
|
info_bucket.get("usage:size:jimmy:public/documents").data.must_equal "22"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "increases the category object counter" do
|
||||||
|
info_bucket.get("usage:count:jimmy:public/documents").data.must_equal "101"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with binary data" do
|
context "with binary data" do
|
||||||
@ -348,7 +357,8 @@ describe "App with Riak backend" do
|
|||||||
describe "DELETE" do
|
describe "DELETE" do
|
||||||
before do
|
before do
|
||||||
header "Authorization", "Bearer 123"
|
header "Authorization", "Bearer 123"
|
||||||
set_usage_size_info "jimmy", "documents", "123"
|
set_usage_info "jimmy", "documents", "size", "123"
|
||||||
|
set_usage_info "jimmy", "documents", "count", "1000"
|
||||||
delete "/jimmy/documents/foo"
|
delete "/jimmy/documents/foo"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -359,17 +369,21 @@ describe "App with Riak backend" do
|
|||||||
}.must_raise Riak::HTTPFailedRequest
|
}.must_raise Riak::HTTPFailedRequest
|
||||||
end
|
end
|
||||||
|
|
||||||
it "decreases the overall category size" do
|
it "decreases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "101"
|
info_bucket.get("usage:size:jimmy:documents").data.must_equal "101"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "decreases the category object counter" do
|
||||||
|
info_bucket.get("usage:count:jimmy:documents").data.must_equal "999"
|
||||||
|
end
|
||||||
|
|
||||||
context "binary data" do
|
context "binary data" do
|
||||||
before do
|
before do
|
||||||
header "Content-Type", "image/jpeg; charset=binary"
|
header "Content-Type", "image/jpeg; charset=binary"
|
||||||
filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg")
|
filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg")
|
||||||
@image = File.open(filename, "r").read
|
@image = File.open(filename, "r").read
|
||||||
put "/jimmy/documents/jaypeg", @image
|
put "/jimmy/documents/jaypeg", @image
|
||||||
set_usage_size_info "jimmy", "documents", "100000"
|
set_usage_info "jimmy", "documents", "size", "100000"
|
||||||
|
|
||||||
delete "/jimmy/documents/jaypeg"
|
delete "/jimmy/documents/jaypeg"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user