Skip to content

Instantly share code, notes, and snippets.

@RobinDaugherty
Created November 3, 2014 16:50
Show Gist options
  • Select an option

  • Save RobinDaugherty/91f348f772b1d0e90eda to your computer and use it in GitHub Desktop.

Select an option

Save RobinDaugherty/91f348f772b1d0e90eda to your computer and use it in GitHub Desktop.
require 'time'
class ContentSourcesGenerator
HOUR = 3600
START_TS = Time.parse('2013-10-01 00:00:00 +00').utc
END_TS = Time.parse('2015-01-01 00:00:00 +00').utc
attr_accessor :values
def each_client
(100..5000).lazy
end
def each_timestamp
ts = START_TS
while ts < END_TS
yield ts
ts += 3600
end
end
def reset_values(client_id)
srand client_id
@values = {
your_pins: ValueGenerator.new(0..1000, 300),
partner_pins: ValueGenerator.new(0..1000, 100),
brand_pins: ValueGenerator.new(0..1000, 500),
other_pins: ValueGenerator.new(0..1000, 20),
}
end
def generate
each_client.each do |client_id|
reset_values(client_id)
each_timestamp do |ts|
yield(client_id, ts, values)
end
end
end
end
class ValueGenerator
attr_accessor :value, :value_range, :max_change
def initialize(value_range = (0..99), max_change=nil)
self.value_range = value_range
self.max_change = max_change || value_range.size
end
def value
@value ||= rand(value_range)
end
def value_with_change
# The current value +- the max change, with the value range being the outer boundary.
new_value_floor = [value - max_change, value_range.begin].max
new_value_ceiling = [value + max_change, value_range.end].min
self.value = rand(new_value_floor..new_value_ceiling)
end
def to_i
value_with_change
end
def to_s
to_i.to_s
end
end
ContentSourcesGenerator.new.generate do |client_id, ts, values|
puts "Client #{client_id} ts #{ts}: #{values[:your_pins]}, #{values[:partner_pins]}, #{values[:brand_pins]}, #{values[:other_pins]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment