Created
March 17, 2012 19:03
-
-
Save buger/2064211 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'mongo' | |
r = Random.new | |
$db = Mongo::Connection.new.db("test") | |
def create_collection site_id | |
coll = $db["rules_#{site_id}"] | |
coll.drop() | |
coll.create_index( | |
[ | |
["rule_location", Mongo::GEO2D] | |
] | |
) | |
coll.create_index( | |
[ | |
["rule_location", Mongo::GEO2D], | |
["rule_order_sum_start", Mongo::ASCENDING], | |
["rule_order_sum_end", Mongo::ASCENDING], | |
["rule_page_visit_count", Mongo::ASCENDING], | |
["rule_site_visit_count", Mongo::ASCENDING], | |
["rule_url_last_visit", Mongo::ASCENDING], | |
["rule_order_state", Mongo::ASCENDING], | |
["rule_product_price", Mongo::ASCENDING] | |
], | |
{ name: "rule_index" } | |
) | |
coll | |
end | |
=begin | |
Example of generated rule: | |
{ | |
"campaign_id" : 1, | |
"site_id" : 1, | |
"offer_value" : 11, | |
"offer_type" : "discount", | |
"rule_location" : [ 2, 2 ], | |
"rule_order_sum_start" : [41,42,43,45,46,47], | |
"rule_site_visit_count" : 6, | |
"rule_order_state" : 1, | |
"rule_product_price" : 2 | |
}, | |
{ | |
"campaign_id" : 2, | |
"site_id" : 1, | |
"offer_value" : 10, | |
"offer_type" : "discount", | |
"rule_site_visit_count" : 5 | |
} | |
=end | |
puts "filling mongo with rules:", | |
Benchmark.measure { | |
# Creating 3 000 000 records in mongo | |
1.times do |site_id| # Assume that we have 1000 sites | |
rules = create_collection site_id | |
records = [] | |
10.times do |campaign_id| # each client has 10 campaigns | |
300.times do |rule_id| # each campaign have 300 rules | |
record = { | |
campaign_id: campaign_id, | |
site_id: site_id, | |
offer_value: r.rand(10..20), | |
offer_type: "discount" | |
} | |
if r.rand(3) == 1 # insert this rule only in every third record | |
record[:rule_location] = [r.rand(10), r.rand(20)] | |
end | |
if r.rand(3) == 1 | |
record[:rule_order_sum_start] = r.rand(30..60) | |
record[:rule_order_sum_end] = r.rand(60..80) | |
end | |
if r.rand(3) == 1 | |
record[:rule_page_visit_count] = 1 + rand(10) | |
end | |
if r.rand(3) == 1 | |
record[:rule_site_visit_count] = 1 + rand(10) | |
end | |
if r.rand(3) == 1 | |
record[:rule_order_state] = rand(3) | |
end | |
if r.rand(3) == 1 | |
record[:rule_product_price] = rand(3) | |
end | |
records.push record | |
end | |
end | |
rules.insert records | |
end | |
} | |
user = { | |
location: [10, 5], | |
order_sum: 50, | |
site_visit_count: 20, | |
page_visit_count: 10, | |
order_state: 2, | |
site_id: 0 | |
} | |
puts $db["rules_#{user[:site_id]}"].find( | |
{ | |
"$and" => [ | |
{ "$or" => [{ rule_location: user[:location] }, {rule_location: {"$exists" => false} } ] }, | |
{ | |
"$or" => [ | |
{ rule_order_sum_start: { "$lte" => user[:order_sum] } }, | |
{ rule_order_sum_start: { "$exists" => false } } | |
] | |
}, | |
{ | |
"$or" => [ | |
{ rule_order_sum_end: { "$gte" => user[:order_sum] } }, | |
{ rule_order_sum_end: { "$exists" => false } } | |
] | |
}, | |
{ rule_location: {"$in" => [user[:location], nil]} }, | |
{ rule_order_sum: {"$in" => [user[:order_sum], nil]} }, | |
{ | |
"$or" => [ | |
{ rule_page_visit_count: {"$gte" => user[:page_visit_count]}}, | |
{ rule_page_visit_count: nil | |
}] | |
} | |
] | |
} | |
).count() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment