Skip to content

Instantly share code, notes, and snippets.

@jbrook
Created January 15, 2013 23:33
Show Gist options
  • Save jbrook/4543167 to your computer and use it in GitHub Desktop.
Save jbrook/4543167 to your computer and use it in GitHub Desktop.
Send a batch of test logstash json events to a Redis queue
require "redis"
require "logstash/event"
require "stud/try"
require "time"
event_count = 10
def time_rand from = 0.0, to = Time.now
Time.at(from + rand * (to.to_f - from.to_f))
end
redis = Redis.new(:host => "testr.spaaza.com", :port => "6379")
event_count.times do |value|
scanEvent = LogStash::Event.new
# Fixed values (at least for this script)
source = "api://my.api.server.x/endPoint"
type = "redis-input"
theClass = "spaaza\\stats\\VarPriceEvent"
# Random stuff
scan_timestamp = (time_rand Time.local(2012, 11, 5).utc)
gender = ["M", "F"].sample
age = rand(7..77)
didLikeOnFacebook = [true, false].sample
product_id = ["18905", "53535", "64647"].sample
product_name = ["Chateau Lafite 2005", "Plain T-Shirt", "Lego Mindstorms nxt 3.0"].sample
# Goal 1 - Scan
# TODO: Some sort of TX_ID to tie it all together
scanEvent.timestamp = scan_timestamp.iso8601
scanEvent.source = source
scanEvent.type = type
scanEvent.message = "Variable Price event: scanned"
scanEvent["gender"] = gender
scanEvent["age"] = age
scanEvent["didLikeOnFacebook"] = didLikeOnFacebook
scanEvent["varprice"] = {
"status" => 0,
"status_str" => "scanned",
"product_id" => product_id,
"product_name" => product_name
}
scanEvent["class"] = theClass
scanEvent.tags << "scan"
Stud::try(10.times) do
puts "Scan event #{scanEvent.to_json}"
redis.rpush("logstash", scanEvent.to_json)
end
if [true, false].sample
# Goal 2 - Generate a price
priceGenEvent = LogStash::Event.new
# Let's say a price is generated within an hour
iElapsedSinceScan = rand(5..3600)
priceGenTimestamp = scan_timestamp + iElapsedSinceScan
priceGenEvent.timestamp = priceGenTimestamp.iso8601
iOriginalPrice = [999, 32000, 2500].sample # Using an integer
discountPercent = [0.8, 0.85, 0.9].sample
iMyPrice = (iOriginalPrice * discountPercent).floor
claim_key = "%06d" % rand(0..999999)
priceGenEvent.source = source
priceGenEvent.type = type
priceGenEvent.message = "Variable Price event: generated"
priceGenEvent["gender"] = gender
priceGenEvent["age"] = age
priceGenEvent["didLikeOnFacebook"] = didLikeOnFacebook
priceGenEvent["varprice"] = {
"status" => 1,
"status_str" => "generated",
"product_id" => product_id,
"product_name" => product_name,
"claim_key" => claim_key,
"iOriginalPrice"=> iOriginalPrice,
"iMyPrice" => iMyPrice,
"iLastScanTime" => scan_timestamp,
"iElapsedSincePreviousGoal" => iElapsedSinceScan # Think of a better name
# TODO: Work out what price stuff we should really send, threshold, etc
}
priceGenEvent["class"] = theClass
priceGenEvent.tags << "price generated"
Stud::try(10.times) do
puts "Price generation event #{priceGenEvent.to_json}"
redis.rpush("logstash", priceGenEvent.to_json)
end
# TODO: Goal 3 - verify some prices/purchases
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment