Last active
February 14, 2016 21:05
-
-
Save manleyhimself/46381d5f5ecf6d600bf1 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
#simulating a worker running every X | |
#a user will ahve to receive a msg every Y interval | |
#user msg is sent sometime between now and now+X | |
#next_message_time will have to be set to current_time += Y | |
#Goal: determine if during a time window of a working processing, if a user is receiving fewer messages than it could because of the offset between 0..X | |
class SendProcess | |
RUN_INTERVAL = 60.0 #1 minute in seconds | |
DAY_INTERVAL = 50400 #14 hours in seconds | |
def initialize days: nil | |
set_day | |
@days = days | |
@user = User.new(@current_time) | |
end | |
def stubbed_current_time | |
@current_time + rand(0.0..RUN_INTERVAL) | |
end | |
def run | |
@days.times do | |
while @current_time < @end_time #Add <= for testing (if desired functionality is for process to run at 12th hour) | |
if @user.next_msg_time <= @current_time | |
@user.send_message(stubbed_current_time) | |
end | |
@current_time += RUN_INTERVAL | |
end | |
set_day | |
@user.next_msg_time = @current_time | |
end | |
report | |
end | |
def set_day | |
@current_time = Time.now | |
@end_time = @current_time + DAY_INTERVAL | |
end | |
def report | |
puts "User HAS received #{@user.message_count} messages" | |
puts "User COULD HAVE received #{(DAY_INTERVAL/User::MSG_INTERVAL).floor * @days} messages" | |
end | |
end | |
class User | |
attr_accessor :next_msg_time, :message_count | |
MSG_INTERVAL = 1800.0 #.5 hour in seconds | |
def initialize init_time | |
@next_msg_time = init_time | |
@message_count = 0 | |
end | |
def send_message current_time | |
@last_msg_time = @next_msg_time | |
@next_msg_time = current_time + MSG_INTERVAL | |
@message_count += 1 | |
# report | |
end | |
def report | |
puts "Current msg time: #{next_msg_time.strftime("%I:%M:%S")}" | |
puts "Time until next message: #{(@next_msg_time - @last_msg_time)/60.0}\n" | |
puts "" | |
end | |
end | |
s = SendProcess.new(days: 1) | |
s.run | |
#Thesis (with worker running every minute): | |
# A user loses one minute every time a message was sent. | |
# If the total number of messages that could be sent, given the user's message inteveral, is greater than the user message interval, in minutes, | |
# then the total time lost is greater than the user message interval. | |
# Therefore, at least one message is lost in a given day. Therefore, given that a user cannot receive more than 28 messages in a given day (day_interval = 14 hours) | |
# and a user message interval is no more than 30 minutes, we will not see loss. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment