How to create a new JSON object in every loop iteration - ruby-on-rails

I am working on a feature that given my daily work hours and the start and end date of my contract displays in a calendar my work hours. The problem is when I have 2 or more shifts on the same day. Let's say Julian has a shift from 03:00 to 04:00 and another one on the same day from 5:00 to 6:00 every Monday starting from July 17, 2019 to July 31, 2019. That means that my calendar should display these 2 shifts in July 22 and July 29. I have coded the logic to get me the correct dates. As of right now my JSON response looks like this
{"user":"Julian","start":"2019-07-22","day":"Monday","start_time":["03:00","05:00"],"end_time":["04:00","06:00"]},{"user_":"Julian","start":"2019-07-29","day":"Monday","start_time":["03:00","05:00"],"end_time":["04:00","06:00"]}
which is being generated by the following code in my JSON builder file
json.array! event.each do |inevent|
json.id hr_schedule.id
json.user_id hr_schedule.user_id
json.start inevent.strftime('%Y-%m-%d')
day_of_week = inevent.strftime('%A')
json.day day_of_week
if day_of_week == "Monday"
json.start_time hr_schedule.monday_st
json.end_time hr_schedule.monday_end
elsif day_of_week == "Tuesday"
json.start_time hr_schedule.tuesday_st
json.end_time hr_schedule.tuesday_end
elsif day_of_week == "Wednesday"
json.start_time hr_schedule.wednesday_st
json.end_time hr_schedule.wednesday_end
elsif day_of_week == "Thursday"
json.start_time hr_schedule.thursday_st
json.end_time hr_schedule.thursday_end
elsif day_of_week == "Friday"
json.start_time hr_schedule.friday_st
json.end_time hr_schedule.friday_end
elsif day_of_week == "Saturday"
json.start_time hr_schedule.saturday_st
json.end_time hr_schedule.saturday_end
elsif day_of_week == "Sunday"
json.start_time hr_schedule.sunday_st
json.end_time hr_schedule.sunday_end
end
end
what I want to achieve is to be able to have all my events as a unique JSON object like the following
{"user":"Julian","start":"2019-07-22","day":"Monday","start_time":"03:00","end_time":"04:00"},{"user":"Julian","start":"2019-07-22","day":"Monday","start_time":"05:00","end_time":"06:00"},
{"user":"Julian","start":"2019-07-29","day":"Monday","start_time":"03:00","end_time":"04:00"},{"user":"Julian","start":"2019-07-29","day":"Monday","start_time":"05:00","end_time":"06:00"}

I'm not sure what class json is, nor what array! is doing. But the basic way to do what you want is to use map to turn your events into an Array of Hashes. Then turn the Array into JSON with to_json.
calendar = event.map do |inevent|
json = {}
json[:id] = hr_schedule.id
json[:user] = hr_schedule.user_id
json[:start] = inevent.strftime('%Y-%m-%d')
json[:day] = inevent.strftime('%A')
case inevent.wday
when 0
json[:start_time] = hr_schedule.sunday_st
json[:end_time] = hr_schedule.sunday_end
when 1
json[:start_time] = hr_schedule.monday_st
json[:end_time] = hr_schedule.monday_end
when 2
json[:start_time] = hr_schedule.tuesday_st
json[:end_time] = hr_schedule.tuesday_end
when 3
json[:start_time] = hr_schedule.wednesday_st
json[:end_time] = hr_schedule.wednesday_end
when 4
json[:start_time] = hr_schedule.thursday_st
json[:end_time] = hr_schedule.thursday_end
when 5
json[:start_time] = hr_schedule.friday_st
json[:end_time] = hr_schedule.friday_end
when 6
json[:start_time] = hr_schedule.saturday_st
json[:end_time] = hr_schedule.saturday_end
end
json
end
puts calendar.to_json
I've simplified it a bit by using a case/when and Time#wday. strftime(%A) might change if the language of the application changes, and the day of week numbers are harder to typo.
As a side note, this event structure limits itself to only events which end before midnight. Consider instead using a full ISO8061 datetimes for start and end. In general, it's easier to work with a full datetime than separate date and time. The event day can be inferred from start.
{"user":"Julian","start":"2019-07-22T03:00","end":"2019-07-22T04:00"}
Or if the events are relative, like they happen every Monday, consider a start and a duration. And use an ISO weekday number for the day instead of a string, less parsing for the receiver to do.
{"user":"Julian","day":1,"start":"03:00","duration":"P1H"}

Related

How to make working hours in ruby ​on rails?

I'm creating a chat panel with a bot. Every message sent by the bot will depend on working hours. For example, during business hours the customer sends a message to the bot and the bot will reply to the text message with the sentence: Hello, you sent a message during business hours
and for example when outside working hours the customer sends a text message to the bot and the bot will reply to the message with the sentence: Hello, you sent a message outside of working hours. Where working hours apply every Monday-Saturday at 08: 30-17: 00.
I made a configuration using an array like this:
start_on = [
"Monday, 08:30:00",
"Tuesday, 08:30:00",
"Wednesday, 08:30:00",
"Thursday, 08:30:00",
"Friday, 08:30:00",
"Saturday, 08:30:00"
]
end_on = [
"Monday, 17:00:00",
"Tuesday, 17:00:00",
"Wednesday, 17:00:00",
"Thursday, 17:00:00",
"Friday, 17:00:00",
"Saturday, 17:00:00"
]
And what I want to ask is how to set the current time current_time = (Time.now.to_time) by configuring the working hours in the array start_on and end_on using ruby ​​on rails?
This function returns true if the day is not sunday and the time is between 08:30 and 17:00
def is_working_hour?(time)
!time.sunday? && time.to_s(:time).between?('08:30','17:00')
end
is_working_hour?(Time.now)
I would personally structure the start and end times like this using wday as the day of the week (0 is sunday so 1 == monday):
times = [
{ day: 1, start: "08:30:00", end: "17:00:00" },
{ day: 2, start: "08:30:00", end: "17:00:00" },
...
]
Once you've got a structure like this (you can work it work it out from the above too of course), you can use the following to get today's times.
today = times.detect { |time| time[:day] == Time.now.wday }
Then start and end times are just:
start_time = Time.parse(today[:start])
end_time = Time.parse(today[:end])
# start_time = Time.parse("08:30:00")
# end_time = Time.parse("17:00:00")
Then it's simply a matter of seeing if the current time is inside that range:
Time.now > start && Time.now < end_time
=> true
To convert the values in DateTime, you have to strptime the weekday for Date, and the time for the Time. Then combine the resulting date and time to obtain the DateTime.
curr_index=1 #loop index if necessary
#get start datetime
arr=start_on[curr_index].split(",")
d = Date.strptime(arr[0].strip, '%A')
t = Time.strptime(arr[1].strip, ' %T')
startime = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
#get end datetime
arr=end_on[curr_index].split(",")
d = Date.strptime(arr[0].strip, '%A')
t = Time.strptime(arr[1].strip, ' %T')
endtime = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
return (startime..endtime).cover? Time.now #check if within the range

For a given period, getting the smallest list of dates, using jokers

I use Elasticsearch where I have one index per day, and I want my Ruby on Rails application to query documents in a given period by specifying the smallest and most precise list of indices.
I can't find the code to get that list of indices. Let me explain it:
Consider a date formatted in YYYY-MM-DD.
You can use the joker * at the end of the date string. E.g. 2016-07-2* describes all the dates from 2016-07-20 to 2016-07-29.
Now, consider a period represented by a start date and an end date.
The code must return the smallest possible array of dates representing the period.
Let's use an example. For the following period:
start date: 2014-11-29
end date: 2016-10-13
The code must return an array containing the following strings:
2014-11-29
2014-11-30
2014-12-*
2015-*
2016-0*
2016-10-0*
2016-10-10
2016-10-11
2016-10-12
2016-10-13
It's better (but I'll still take a unoptimized code rather than nothing) if:
The code returns the most precise list of dates (i.e. doesn't return dates with a joker that describes a period starting before the start date, or ending after the end date)
The code returns the smallest list possible (i.e. ["2016-09-*"] is better than ["2016-09-0*", "2016-09-1*", "2016-09-2*", "2016-09-30"]
Any idea?
Okay, after more thinking and the help of a coworker, I may have a solution. Probably not totally optimized, but still...
def get_indices_from_period(start_date_str, end_date_str)
dates = {}
dates_strings = []
start_date = Date.parse(start_date_str)
end_date = Date.parse(end_date_str)
# Create a hash with, for each year and each month of the period: {:YYYY => {:MMMM => [DD1, DD2, DD3...]}}
(start_date..end_date).collect do |date|
year, month, day = date.year, date.month, date.day
dates[year] ||= {}
dates[year][month] ||= []
dates[year][month] << day
end
dates.each do |year, days_in_year|
start_of_year = Date.new(year, 1, 1)
max_number_of_days_in_year = (start_of_year.end_of_year - start_of_year).to_i + 1
number_of_days_in_year = days_in_year.collect{|month, days_in_month| days_in_month}.flatten.size
if max_number_of_days_in_year == number_of_days_in_year
# Return index formatted as YYYY-* if full year
dates_strings << "#{year}-*"
else
days_in_year.each do |month, days_in_month|
formatted_month = format('%02d', month)
if Time.days_in_month(month, year) == days_in_month.size
# Return index formatted as YYYY-MM-* if full month
dates_strings << "#{year}-#{formatted_month}-*"
else
decades_in_month = {}
days_in_month.each do |day|
decade = day / 10
decades_in_month[decade] ||= []
decades_in_month[decade] << day
end
decades_in_month.each do |decade, days_in_decade|
if (decade == 0 && days_in_decade.size == 9) ||
((decade == 1 || decade == 2) && days_in_decade.size == 10)
# Return index formatted as YYYY-MM-D* if full decade
dates_strings << "#{year}-#{formatted_month}-#{decade}*"
else
# Return index formatted as YYYY-MM-DD
dates_strings += days_in_decade.collect{|day| "#{year}-#{formatted_month}-#{format('%02d', day)}"}
end
end
end
end
end
end
return dates_strings
end
Test call:
get_indices_from_period('2014-11-29', '2016-10-13')
=> ["2014-11-29", "2014-11-30", "2014-12-*", "2015-*", "2016-01-*", "2016-02-*", "2016-03-*", "2016-04-*", "2016-05-*", "2016-06-*", "2016-07-*", "2016-08-*", "2016-09-*", "2016-10-0*", "2016-10-10", "2016-10-11", "2016-10-12", "2016-10-13"]

Ruby DateTime: Get next 5:15pm (or similar)

So, given a DateTime object, and a fixed time, I want to get the next occurrence of the fixed time after the given DateTime object.
For example, given the date of 14th March, 2016, 4:00pm, and the time of 5:15pm, I want to return 14th March, 2016 5:15pm.
However, given the date of 14th March, 2016, 6:00pm, and the time of 5:15pm, I want to return 15th March, 2016, 5:15pm, since that's the next occurrence.
So far, I've written this code:
# Given fixed_time and date_time
new_time = date_time
if fixed_time.utc.strftime("%H%M%S%N") >= date_time.utc.strftime("%H%M%S%N")
new_time = DateTime.new(
date_time.year,
date_time.month,
date_time.day,
fixed_time.hour,
fixed_time.min,
fixed_time.sec
)
else
next_day = date_time.beginning_of_day + 1.day
new_time = DateTime.new(
next_day.year,
next_day.month,
next_day.day,
fixed_time.hour,
fixed_time.min,
fixed_time.sec
)
end
# Return new_time
It works, but is there a better way?
I would construct the new date time just once and add 1 day if needed:
# Given fixed_time and date_time
new_date_time = DateTime.new(
date_time.year,
date_time.month,
date_time.day,
fixed_time.hour,
fixed_time.min,
fixed_time.sec
)
# add 1 day if new date prior to the given date
new_date_time += 1.day if new_date_time < date_time
Here's a little stab at refactoring it to remove some of the redundancy:
# Given fixed_time and date_time
base_date = date_time.to_date
if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N")
base_date = base_date.next_day
end
new_time = DateTime.new(
base_date.year,
base_date.month,
base_date.day,
fixed_time.hour,
fixed_time.min,
fixed_time.sec
)
# Return new_time
The biggest changes are that the base_date is determined before the new_time is created, so that it can be used there.
I also used the next_day method on DateTime to get the next day, and used the "%T" format specifier as a shortcut for "%H:%M:%S"
Here's a little test program that to show that it works:
require "date"
def next_schedule(fixed_time, date_time)
# Given fixed_time and date_time
base_date = date_time.to_date
if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N")
base_date = base_date.next_day
end
new_time = DateTime.new(
base_date.year,
base_date.month,
base_date.day,
fixed_time.hour,
fixed_time.min,
fixed_time.sec
)
# Return new_time
end
StartTime = DateTime.strptime("2016-02-14 17:15:00", "%F %T")
Dates = [
"2016-03-14 16:00:00",
"2016-03-14 18:00:00"
]
Dates.each do |current_date|
scheduled = next_schedule(StartTime, DateTime.strptime(current_date, "%F %T"))
puts "Scheduled: #{scheduled.strftime('%F %T')}"
end
The output of this is:
Scheduled: 2016-03-14 17:15:00
Scheduled: 2016-03-15 17:15:00
It's using the test cases described in the question, and it gets the expected answers.

Generate a list of commercial weeks falling between 2 dates in Ruby / Rails

I am trying to created a list of year-week (equivalent to mySQL's YEARWEEK(date,1)) falling between two date values in Rails. List is generating perfectly if start-date and end-date are falling in same year. Here is my code:
campaign_start_date = "2013-08-02 06:59:00"
campaing_end_date = "2013-09-01 06:59:00"
start_year = DateTime.parse(campaign_start_date).cwyear
start_week = "%04d%02d" % [start_year, DateTime.parse(campaign_start_date).cweek]
end_year = DateTime.parse(campaing_end_date).cwyear
end_week = "%04d%02d" % [end_year, DateTime.parse(campaing_end_date).cweek]
if start_year == end_year
(start_week..end_week).each{ |i| result << i }
else
# need to build a suitable logic here. to handle the case when duration spans over multiple years. for example started in 01-Nov-14 and ended in 01-May-15
end
return result
there will be no problem with above date values, it will fall to if case and the result I will get is:
[
"201331",
"201332",
"201332",
"201333",
"201334",
"201335"
]
which is also what I exactly want. BUT if my start-date and end-date values are these for example:
campaign_start_date = "2014-07-23 06:59:00"
campaing_end_date = "2015-03-01 06:59:00"
means falling in different years, then it need different logic that the one I have in if condition because for these date values (start_week=201430 and end_week=201509) the if condition is not suitable here because it would generate 80 values, which is wrong because number of weeks between these dates are not 80. Need help to develop the logic for else case. May be its easy but right now I am just tired to dig it any deeper.
Special attention: the solutions should care about commercial year and commercial week (refer .cwyear and .cweek functions of rails) For example yearweek of 2016-01-01 would be 201553 not 201601
any help in this regard would be much appreciated.
Thank you to those who replied t. I've finally solved the problem like this:
campaign_weeks = []
campaign_start_date = "2014-07-23 06:59:00" # or any date
campaing_end_date = "2015-03-01 06:59:00" # or any date
start_year = DateTime.parse(campaign_start_date).cwyear
start_cweek_of_the_campaign = "%04d%02d" % [start_year, DateTime.parse(campaign_start_date).cweek]
end_year = DateTime.parse(campaing_end_date).cwyear
end_cweek_of_the_campaign = "%04d%02d" % [end_year, DateTime.parse(campaing_end_date).cweek]
if start_year == end_year
(start_cweek_of_the_campaign..end_cweek_of_the_campaign).each do |w|
campaign_weeks << ("%04d%02d" % [start_year, w])
end
else
(start_year..end_year).each do |y|
first_cweek_number_of_the_year = (y == start_year) ? start_cweek_of_the_campaign : 1
last_cweek_number_of_the_year = (y == end_year) ? end_cweek_of_the_campaign : DateTime.new(y, 12, 28).cweek
(first_cweek_number_of_the_year .. last_cweek_number_of_the_year).each do |w|
campaign_weeks << ("%04d%02d" % [y, w])
end
end
end
return campaign_weeks
Notes: 28th Dec always fall in the last cweek/iso-week of the year. last ISO week of the year is either 52 or 53.
Reference: http://en.wikipedia.org/wiki/ISO_week_date#Last_week
Got some hint from this answer: Calculating the number of weeks in a year with Ruby
The bottleneck is (start_week..end_week) range. It apparently goes through hundred (since we are on decimals):
2014xx ⇒ 201452 ⇒ 201453 ⇒ ... ⇒ 201499 ⇒ 201500 ⇒ ...
You should probably filter your range, like:
r = (start_week..end_week)
r.to_a.reject { |e| e[-2..-1].to_i > 52 }
Depending on how you count weeks (=-based, or 1-based,) the 201500 should be likely filtered as well:/
r.to_a.select { |e| e[-2..-1].to_i.between? 1, 52 }
Try this out; it will work for any set of dates irrespective of whether the years are the same or not:
campaign_start_date = "2014-07-23 06:59:00"
campaign_end_date = "2015-03-01 06:59:00"
start_date = DateTime.parse(campaign_start_date)
end_date = DateTime.parse(campaign_end_date)
while start_date < end_date
puts "%04d%02d" % [start_date.cw_year, start_date.cweek]
start_date = start_date + 7.days
end
A bit late in the discussion but here is what I used to get the number of commercial weeks between two dates:
def cweek_diff(start_date, end_date)
return if end_date < start_date
cweek_diff = (end_date.cweek - start_date.cweek) + 1
cwyear_diff = end_date.cwyear - start_date.cwyear
cyear_diff * 53 + cweek_diff - cwyear_diff
end
It worked perfectly in my case. Hope it helps ;)

Rails Iteration by month given range of dates

I'm trying to follow this Railscast and create a morris.js line chart for my Enquiry model.
I've grouped the counts using date_trunc into months, but now I'm not quite sure at how to get the X-axis to iterate over months (e.g Jun 2012, Jul 2013) as opposed to by date as in the railscasts notes.
I've tried the range#step method here, but now the graph displays only one date (2012-07-01) without a count and nothing else. Commenting out the .step(1.month) method from the range variable and the graph works fine but the x-axis iterates by date.
class Enquiry < ActiveRecord::Base
def self.chart_data(start = 1.year.ago)
total_count = total_count_by_month(start)
start = start.to_date.beginning_of_month
today = Date.today.beginning_of_month
range = (start..today).step(1.month)
range.map do |month|
{
created_at: month,
total_enquiries: total_count[] || 0
}
end
end
def self.total_count_by_month(start)
enquiries = unscoped.where(created_at: start.beginning_of_month..Time.now)
enquiries = enquiries.group("date_trunc('month', created_at)")
enquiries = enquiries.select("date_trunc('month', created_at) as created_at, count(*) as count")
enquiries.each_with_object({}) do |enquiry, counts|
counts[enquiry.created_at.to_date] = enquiry.count
end
end
end
How do I get the chart's x-axis to iterate by months (Jun 2013, Jul 2013 ....) instead of by dates?
For anyone else facing the same problem, the solution is outlined below:
def self.chart_data(start = 1.year.ago)
total_count = total_count_by_month(start)
##############################################
start = start.to_date.beginning_of_month
today = Date.today.beginning_of_month
range = (start..today).select {|d| d.day == 1}
##############################################
range.map do |month|
{
created_at: month,
total_enquiries: total_count[] || 0
}
end
end
The chart's x-axis now iterates by month.
The solution is found here.
I'm still looking for solutions on how the chart dates might display (%b %Y) as opposed to the current format of (yyyy-mm-dd).

Resources