Permalink
Browse files

Refactor index

  • Loading branch information...
1 parent 3a02b49 commit e59c55cdf822dd1740f3787409fbafb74cf029e5 @thejonanshow committed Mar 28, 2012
View
@@ -34,9 +34,9 @@ def self.load_directory(directory, options = {})
if options[:index]
Thread.new do
t = Time.now
- puts "indexing everything!"
SalesEngine::Persistence.instance.index(:id)
- puts "Indexing completed in #{Time.now - t} seconds!"
+ SalesEngine::Persistence.instance.insert_index(:merchant_id, SalesEngine::Invoice)
+ puts "\nIndexing completed in #{Time.now - t} seconds!"
end
end
end
@@ -25,5 +25,9 @@ def customer
def merchant
@merchant ||= Merchant.find(@merchant_id)
end
+
+ def revenue
+ InvoiceItem.find_all_by_invoice_id(id).map(&:revenue).inject(:+)
+ end
end
end
@@ -16,6 +16,10 @@ def initialize(attributes)
validate_attributes
end
+ def revenue
+ unit_price * quantity
+ end
+
private
def validate_attributes
@@ -27,9 +27,23 @@ def invoices
@invoices ||= Invoice.find_all_by_merchant_id(self.id)
end
- def revenue(date=nil)
-
+ def revenue
+ @revenue ||= Invoice.find_all_by_merchant_id(id).map(&:revenue).inject(:+)
end
+ def self.revenue(date=nil)
+ Invoice.find_all_by_created_at_date(date).map(&:revenue).inject(:+)
+ end
+
+ def self.most_revenue(n)
+ merchants_and_revenues = []
+
+ merchants = Merchant.find_all
+ merchants.each do |merchant|
+ merchants_and_revenues.push [merchant.revenue, merchant]
+ end
+
+ # merchants_and_revenues.sort_by { |rev_mer| rev_mer[0] }.map(&:last)[0..n]
+ end
end
end
View
@@ -49,6 +49,10 @@ def self.included(target)
target.extend ClassMethods
end
+ def created_at_date
+ @created_at.to_date
+ end
+
module ClassMethods
def find(id)
find_by_id(id)
@@ -94,7 +98,7 @@ def find_by(attribute, *values)
indices = SalesEngine::Persistence.instance.fetch_indices(self)
unless indices.empty?
- model = indices[attribute][value] if indices[attribute]
+ model = indices[attribute][value].first if indices[attribute]
end
unless model
@@ -109,8 +113,16 @@ def find_all_by(attribute, *values)
attribute = attribute.to_sym
value = values[0]
- models = SalesEngine::Persistence.instance.fetch(self)
- model = models.find_all { |m| m.send(attribute) == value }
+ indices = SalesEngine::Persistence.instance.fetch_indices(self)
+
+ unless indices.empty?
+ models = indices[attribute][value] if indices[attribute]
+ end
+
+ unless models
+ models = SalesEngine::Persistence.instance.fetch(self)
+ model = models.find_all { |m| m.send(attribute) == value }
+ end
end
end
end
@@ -17,6 +17,7 @@ def persist(model)
else
@data[name] = [model]
end
+
true
end
@@ -59,6 +60,24 @@ def insert_index(attribute, class_name)
@indices[class_name][attribute] = result
end
+ def index_attribute_by_class(attribute, class_name)
+ result = {}
+
+ data = @data[class_name]
+
+ data.each do |model|
+ value = model.send(attribute)
+
+ if result[value]
+ result[value].push model
+ else
+ result[value] = [model]
+ end
+ end
+
+ result
+ end
+
def index_all
@data.keys.each do |class_name|
datum = @data[class_name]
@@ -84,20 +103,5 @@ def dump_indices
def import(data)
@data = data
end
-
- private
-
- def index_attribute_by_class(attribute, class_name)
- result = {}
-
- data = @data[class_name]
-
- data.each do |model|
- value = model.send(attribute)
- result[value] = model
- end
-
- result
- end
end
end
View
@@ -3,6 +3,8 @@
describe SalesEngine::Merchant do
let(:merchant) { Fabricate(:merchant) }
+ before(:all) { SalesEngine.startup('data/evaluation') }
+
it 'creates a merchant with valid attributes' do
merchant.nil?.should be_false
end
@@ -100,4 +102,22 @@
end
end
end
+
+ context "Business Intelligence" do
+ describe ".revenue" do
+ it "returns all revenue for a given date" do
+ date = Date.parse "Tue, 20 Mar 2012"
+
+ SalesEngine::Merchant.revenue(date).to_f.should be_within(0.001).of(263902466.0)
+ end
+ end
+
+ describe ".most_revenue" do
+ it "returns the top n revenue-earners" do
+ most = SalesEngine::Merchant.most_revenue(3)
+ most.first.name.should == "Dicki-Bednar"
+ most.last.name.should == "Okuneva, Prohaska and Rolfson"
+ end
+ end
+ end
end
View
@@ -45,7 +45,7 @@ def initialize(attributes)
attribute = :id
persistence.index(attribute)
indices = persistence.dump_indices
- indices[model.class][attribute][model.id].should be model
+ indices[model.class][attribute][model.id].first.should be model
end
end
@@ -37,8 +37,8 @@
end
it "creates models" do
- #SalesEngine.load_directory(directory)
- #SalesEngine::Persistence.instance.dump_data.should_not be_empty
+ SalesEngine.load_directory(directory)
+ SalesEngine::Persistence.instance.dump_data.should_not be_empty
end
end
end

0 comments on commit e59c55c

Please sign in to comment.