Skip to content

Instantly share code, notes, and snippets.

@kazjote
Created January 8, 2012 18:31
Show Gist options
  • Save kazjote/1579235 to your computer and use it in GitHub Desktop.
Save kazjote/1579235 to your computer and use it in GitHub Desktop.
FnordMetric data generator
# Copy it to fnordmetric directory and run with
#
# bundle exec ruby -Ilib -I. populate.rb live
#
# to generate fake current traffic or
#
# bundle exec ruby -Ilib -I. populate.rb past
#
# to generate fake stats for last month.
#
require "json"
require "socket"
class User < Struct.new :username, :avatar
def session_id
"session_#{username}"
end
end
class EventGenerator
USERS = [
User.new("Alice", "http://www.avatarsdb.com/avatars/draco_malfoy.jpg"),
User.new("Jack", "http://www.avatarsdb.com/avatars/draco_malfoy_eyes.gif"),
User.new("Bob", "http://www.avatarsdb.com/avatars/german_shepherd.jpg"),
User.new("Tom", "http://www.avatarsdb.com/avatars/funny_elite_machine_gun.gif"),
User.new("James", "http://www.avatarsdb.com/avatars/ugly_face.gif"),
User.new("Kate", nil), User.new("Julia", nil) ]
URLS = [[0.1, "/family"], [0.3, "/gadgets"], [0.6, "/cars"], [1.0, "/games"]]
EVENTS = %w{skip_vote action_skip yes_vote action_yes maybe_vote action_maybe} +
%w{_pageview campaign_install app_request_sent abtest_sidebar_btn_click} +
%w{user_demography competition_data action_wink winks_sent message_sent} +
%w{message_read mail_sent mail_clicked app_request_sent app_request_click} +
%w{app_invite_sent app_invite_click}
def initialize
@users_waiting_introduction = USERS.clone
end
def introduction_events user
events = [{_type: :_set_name, name: user.username, _session: user.session_id}]
return events unless user.avatar
events << {_type: :_set_picture, url: user.avatar, _session: user.session_id}
end
def random_event
event = EVENTS.sample
if event == "_pageview"
user = USERS.sample
random_number = rand
url = "#{URLS.detect {|(n, _)| rand <= n }[1]}/#{rand(10)}"
{_type: :_pageview, url: url, _session: user.session_id}
elsif event == "user_demography"
{_type: event, gender: %w{male female}.sample, age: rand(50) + 10}
elsif event == "competition_data"
competitor = %w{badoo areyouinterested zoosk onetwolike kizzle}.sample
stat = "#{competitor}_#{%w{mau dau}.sample}"
{:_type => event, stat => rand(10000)}
elsif event == "abtest_sidebar_btn_click"
variant = %w{leute_treffen jetzt_losflirten dates_finden}.sample
{_type: event, variant: variant}
else
campaign_key = %w(ry201112a ry201112b ry201112_ref).sample
{_type: event, campaign_key: campaign_key}
end
end
def next_events
if user = @users_waiting_introduction.pop
introduction_events user
else
[random_event]
end
end
end
@socket = TCPSocket.new 'localhost', 1337
trap "TERM", proc { @socket.close }
trap "INT", proc { @socket.close }
def send_event event
@socket.puts event.to_json
end
event_generator = EventGenerator.new
if ARGV[0] == "live"
while true
event_generator.next_events.each {|e| send_event e }
sleep rand
end
elsif ARGV[0] == "past"
MONTH = 60 * 60 * 24 * 31
time = Time.now - MONTH
while time < Time.now
event_generator.next_events.each do |event|
event[:_time] = time.to_i
send_event event
end
time += rand * 100
end
else
@socket.close
puts "ruby -Ilib -I. populate.rb (live|past)"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment