Combine category usage counters in a single JSON object
This commit is contained in:
parent
27e5dfc1b5
commit
08661ea148
@ -158,27 +158,22 @@ module RemoteStorage
|
|||||||
|
|
||||||
def log_object_size(user, directory, new_size=0, old_size=0)
|
def log_object_size(user, directory, new_size=0, old_size=0)
|
||||||
category = extract_category(directory)
|
category = extract_category(directory)
|
||||||
info = info_bucket.get_or_new("usage:size:#{user}:#{category}")
|
info = info_bucket.get_or_new("usage:#{user}:#{category}")
|
||||||
info.content_type = "text/plain"
|
info.content_type = "application/json"
|
||||||
|
info.data ||= {}
|
||||||
size = -old_size + new_size
|
info.data["size"] ||= 0
|
||||||
size += info.data.to_i
|
info.data["size"] += (-old_size + new_size)
|
||||||
|
|
||||||
info.data = size.to_s
|
|
||||||
info.indexes.merge!({:user_id_bin => [user]})
|
info.indexes.merge!({:user_id_bin => [user]})
|
||||||
info.store
|
info.store
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_object_count(user, directory, change)
|
def log_object_count(user, directory, change)
|
||||||
category = extract_category(directory)
|
category = extract_category(directory)
|
||||||
|
info = info_bucket.get_or_new("usage:#{user}:#{category}")
|
||||||
info = info_bucket.get_or_new("usage:count:#{user}:#{category}")
|
info.content_type = "application/json"
|
||||||
info.content_type = "text/plain"
|
info.data ||= {}
|
||||||
|
info.data["count"] ||= 0
|
||||||
count = change.to_i
|
info.data["count"] += change
|
||||||
count += info.data.to_i
|
|
||||||
|
|
||||||
info.data = count.to_s
|
|
||||||
info.indexes.merge!({:user_id_bin => [user]})
|
info.indexes.merge!({:user_id_bin => [user]})
|
||||||
info.store
|
info.store
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
require_relative "spec_helper"
|
require_relative "spec_helper"
|
||||||
|
|
||||||
def set_usage_info(user, category, type, value)
|
def set_usage_info(user, category, type, value)
|
||||||
object = info_bucket.get_or_new("usage:#{type}:#{user}:#{category}")
|
object = info_bucket.get_or_new("usage:#{user}:#{category}")
|
||||||
object.content_type = "text/plain"
|
object.content_type = "application/json"
|
||||||
object.data = value.to_s
|
data = object.data || {}
|
||||||
|
data.merge!(type => value)
|
||||||
|
object.data = data
|
||||||
object.store
|
object.store
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -93,7 +95,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_info "jimmy", "documents", "size", "23"
|
set_usage_info "jimmy", "documents", "size", 23
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with implicit content type" do
|
describe "with implicit content type" do
|
||||||
@ -112,10 +114,9 @@ describe "App with Riak backend" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "increases the usage size counter" do
|
it "increases the usage size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "35"
|
usage = info_bucket.get("usage:jimmy:documents")
|
||||||
|
usage.data["size"].must_equal 35
|
||||||
indexes = info_bucket.get("usage:size:jimmy:documents").indexes
|
usage.indexes["user_id_bin"].must_include "jimmy"
|
||||||
indexes["user_id_bin"].must_include "jimmy"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "indexes the data set" do
|
it "indexes the data set" do
|
||||||
@ -143,8 +144,8 @@ describe "App with Riak backend" do
|
|||||||
data_bucket.get("jimmy:documents:jason").content_type.must_equal "application/json"
|
data_bucket.get("jimmy:documents:jason").content_type.must_equal "application/json"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the overall category size" do
|
it "increases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "49"
|
info_bucket.get("usage:jimmy:documents").data["size"].must_equal 49
|
||||||
end
|
end
|
||||||
|
|
||||||
it "delivers the data correctly" do
|
it "delivers the data correctly" do
|
||||||
@ -206,7 +207,7 @@ describe "App with Riak backend" do
|
|||||||
|
|
||||||
describe "with existing content" do
|
describe "with existing content" do
|
||||||
before do
|
before do
|
||||||
set_usage_info "jimmy", "documents", "size", "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
|
||||||
@ -216,33 +217,29 @@ describe "App with Riak backend" do
|
|||||||
data_bucket.get("jimmy:documents/archive:foo").data.must_equal "some awesome content"
|
data_bucket.get("jimmy:documents/archive:foo").data.must_equal "some awesome content"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the overall category size" do
|
it "increases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "30"
|
info_bucket.get("usage:jimmy:documents").data["size"].must_equal 30
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "public data" do
|
describe "public data" do
|
||||||
before do
|
before do
|
||||||
set_usage_info "jimmy", "public/documents", "size", "10"
|
set_usage_info "jimmy", "public/documents", "size", 10
|
||||||
set_usage_info "jimmy", "public/documents", "count", "100"
|
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 category size counter" do
|
it "increases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:public/documents").data.must_equal "22"
|
info_bucket.get("usage:jimmy:public/documents").data["size"].must_equal 22
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the category object counter" do
|
it "increases the category object counter" do
|
||||||
info_bucket.get("usage:count:jimmy:public/documents").data.must_equal "101"
|
info_bucket.get("usage:jimmy:public/documents").data["count"].must_equal 101
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -269,8 +266,8 @@ describe "App with Riak backend" do
|
|||||||
last_response.body.must_equal @image
|
last_response.body.must_equal @image
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the overall category size" do
|
it "increases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "16067"
|
info_bucket.get("usage:jimmy:documents").data["size"].must_equal 16067
|
||||||
end
|
end
|
||||||
|
|
||||||
it "indexes the binary set" do
|
it "indexes the binary set" do
|
||||||
@ -357,8 +354,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_info "jimmy", "documents", "size", "123"
|
set_usage_info "jimmy", "documents", "size", 123
|
||||||
set_usage_info "jimmy", "documents", "count", "1000"
|
set_usage_info "jimmy", "documents", "count", 1000
|
||||||
delete "/jimmy/documents/foo"
|
delete "/jimmy/documents/foo"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -370,11 +367,11 @@ describe "App with Riak backend" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "decreases the category size counter" do
|
it "decreases the category size counter" do
|
||||||
info_bucket.get("usage:size:jimmy:documents").data.must_equal "101"
|
info_bucket.get("usage:jimmy:documents").data["size"].must_equal 101
|
||||||
end
|
end
|
||||||
|
|
||||||
it "decreases the category object counter" do
|
it "decreases the category object counter" do
|
||||||
info_bucket.get("usage:count:jimmy:documents").data.must_equal "999"
|
info_bucket.get("usage:jimmy:documents").data["count"].must_equal 999
|
||||||
end
|
end
|
||||||
|
|
||||||
context "binary data" do
|
context "binary data" do
|
||||||
@ -383,7 +380,8 @@ describe "App with Riak backend" do
|
|||||||
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_info "jimmy", "documents", "size", "100000"
|
set_usage_info "jimmy", "documents", "size", 100000
|
||||||
|
set_usage_info "jimmy", "documents", "count", 10
|
||||||
|
|
||||||
delete "/jimmy/documents/jaypeg"
|
delete "/jimmy/documents/jaypeg"
|
||||||
end
|
end
|
||||||
@ -402,8 +400,12 @@ 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 "83956"
|
info_bucket.get("usage:jimmy:documents").data["size"].must_equal 83956
|
||||||
|
end
|
||||||
|
|
||||||
|
it "decreases the category object counter" do
|
||||||
|
info_bucket.get("usage:jimmy:documents").data["count"].must_equal 9
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user