Rails, postgres, timezones, and ActiveRecord order method - ruby-on-rails

I have a query that orders a group of tasks by their start_date(UTC datetime).
This works perfectly.
The problem comes when displaying the ordered records for the end user in their timezone.
lets say I have 5 tasks (ordered by their utc times)
1 2:00
2 3:00
3 12:00
4 16:00
5 22:00
This is the correct order.
But when I convert to EDT:
1 22:00
2 23:00
4 12:00
5 18:00
Is there a way to specify the timezone you're querying in postgres using activerecord?
Thank you

Related

Neo4j creating nodes and relationships from Bus route CSV

I have a CSV file with bus route information that looks like this. I am having trouble creating nodes and path relationships in Neo4j with it in this format.
I would like to have nodes for the stops and routes, and routes between them using the sequence and route detail id to show the direction of the routes.
RouteName
route_detail_id
Stop
Sequence
Arrives
Departs
Bus1
50701
Cherry
1
9:00
Bus1
50802
Market
2
9:30
10:00
Bus1
59003
Raleigh
3
10:30
10:50
Bus1
59004
Stuart
4
11:05
11:30
Bus1
58006
Possum
5
12:30
Bus2
67003
Cherry
1
11:00
Bus2
67004
Market
2
11:30
12:00
Bus2
67009
Raleigh
3
12:30
12:50
Bus2
67010
Stuart
4
13:05
13:30
Bus2
67011
Possum
5
14:30
Bus3
89004
Highland
1
9:00
Bus3
88005
McKinley
2
9:30
10:00
Bus3
67098
Jersey
3
10:30
10:50
Bus3
4500
Ridgewood
4
11:05
11:30
Bus3
67890
Osprey
5
12:30
route_detail_id is the unique identifier for that particular stop on that particular route.
I would like to be able to use the times for shortest path queries in the future, but right now would just like to be able to create a structure and visualize in neo4j.
Eventually it will be used to create connecting routes, and shortest path searching, but right now I am just stumbling over even converting information in this format to Neo4j.
I would start by converting the format into a list of nodes connected by arcs, such as:
Cherry -- Bus1, 50701, n/a, 9:00 --> Market
Market -- Bus1, 50802, 9:30, 10:00 --> Raleigh
...
Cherry -- Bus2, 67003, n/a, 11:00 --> Market
..
This seems to me to be a more natural way of representing the data, as you have stops (nodes), which are connected by bus routes (directed arcs, with route details).
You can then query the database by looking for links between the nodes. You can convert also convert the arrival/departure times into the duration of the journey between two nodes if you want to find a shortest path.

Efficient Postgresql Query for Sorting Appointments on Simple Calendar with Rails

TL;DR:
I have a database with thousands of appointments that have start_time and end_time attributes. Given a date range (May 26-June 31), how do I find
every appointment that happens throughout this range?
Appointment from May 15 to May 25 NOT included
Appointment from May 15 to May 29 needs to be included
Appointment from June 1 to June 3 needs to be included
Appointment from June 20 to July 5 needs to be included
Appointment from May 15 to July 15 needs to be included (most difficult part of the problem)
Appointment from July 1 to July 4 NOT included
We have an appointment model that has a start_time and an end_time. If an appointment occurs on any days during the monthly view, it needs to be loaded into the instance variable (#monthly_appointments) so that the simple_calendar gem can display it on the calendar.
Example: An appointment from June 1 to June 3 needs to show up when the user views the "June 2020" calendar. An appointment from May 15 to July 15 also needs to show up every single day during June.
There's 6-day padding on each side of the calendar dates, since if the week starts on a Saturday (June 1), you'll have May 26 - May 31 showing on the June calendar (see picture).
You'd think it was just as easy as saying "If an appointment starts or ends during the given month, add that appointment to the instance variable. However, there are cases when an appointment starts on May 15 and goes for 60 days, until July 15. The appointment neither starts nor ends during June, but it needs to still show up on the calendar.
Originally, we told users, "appointments cannot last longer than 6 months" and then we used this lookup where we assumed, "if the appointment started during the last 6 months, include it in the variable and then we'll let the calendar gem work out the rest."
#monthly_appointments = current_user.appointments.includes(:pet).where(start_time: (Time.zone.now).beginning_of_month - 6.months..(Time.zone.now).end_of_month + 6.days))
However, this query can sometimes save 3,000+ appointments in the variable, when, in reality, there are only 50-70 appointments that NEED to be shown for that month.
So, I wrote up the following code, and it succeeds in finding appointments that occur on at least one day during the calendar's timeframe. It compares the monthly calendar's range(May 26..June 31) and an appointment's range(June 1..June 3), and then looks for any dates that occur in both arrays (&). It works well, but it takes a bit of time because it needs to load ALL the appointments for a user (thousands) and then goes through each one to see if it occurs during that month.
Does anyone have any other clever solutions to this query issue? I'm sure something exists out there, but I haven't found it yet. Thanks!
month_dates = ((Time.zone.now.beginning_of_month - 6.days)..(Time.zone.now.end_of_month + 6.days)).to_a
#monthly_appointments = current_user.appointments.includes(:pet).select do |appt|
#create array of appointment dates and see if it intersects any of the monthly date array
appt_dates = (appt.start_time.to_date..appt.end_time.to_date).to_a
(month_dates & appt_dates).present?
end
Models:
Appointment(start_time, end_time, note, user_id, pet_id)
Pet
has_many appointments
User
has_many appointments
Here's an example.
carley = Pet.find(12)
Appointment.create(pet_id: carley.id, start_time: "May 15 2020 06:00:00", end_time: "July 15 2020 06:00:00"...)
When I'm looking at the June 2020 calendar, this appointment needs to show up on every single day.
from_time = '2019-05-26'.to_date
to_time = '2919-06-30'.to_date
#appointments = Appointment.where('start_time <= ? AND end_time >= ?', to_time, from_time)
The above will select all appointments that are included in or overlap the from_time and to_time range, and also appointments that start before the range and end after the range.

Informix today operator

I have a condition qa.actual_date >= today - 1 in a informix query.
Does it fetch the records exactly from the past 24 hours?
Eg:
Curent date and time is 13 Jun 2019 12:45 PM
does qa.actual_date >= today - 1 will fetch the records from 12 Jun 2019 12:45 PM or 12 Jun 2019 12:00 AM
Under Informix, a DATE value refers to a day, and has no explicit time component. Given a current date of 2019-06-13 and assuming the type of qa.actual_date is DATE (and not a DATETIME type), the condition:
qa.actual_date >= TODAY - 1
selects all records where the qa.actual_date value is (any time on) 2019-06-12 or later.
If qa.actual_date is of type DATETIME YEAR TO SECOND or any other type that has an hour, minute, or second component (as well as day, month, year components), then the value of TODAY - 1 will be converted (extended) to that type, and the missing time components will be treated as zeroes.
SELECT EXTEND(TODAY - 1, YEAR TO SECOND) FROM sysmaster:sysdual;
That will return 2019-06-12 00:00:00.

get time until hour in user timezone

I'm trying to get the time until 12:00 AM in the users timezone. How can I do this in rails?
I want to get the time until 12:00 AM and then add that time to the current time in rails to store it in the database because I want to have a field with the GMT time at is the equivalent to 12:00 AM in the users timezone
I'm using this gem:
gem 'time_difference', '~> 0.5.0'
In order to get the time difference between two timestamps
TimeDifference.between(DateTime.now, created_at)
But I'm not sure how to get the time until 12:00 AM in the users timezone.
Thanks
Given a particular timezone, you can use Rails' tools for dealing with timezones to just directly find out when midnight is for a particular timezone. This example supposes you have a time_zone column on your User model with an appropriate value (e.g., America/Chicago, or anything else Rails supports):
midnight_for_user = ActiveSupport::TimeZone[user.time_zone].now.midnight.tomorrow
For example, I can use the same logic to find when midnight is for a person in New York:
pry(main)> Time.current
=> Thu, 05 Jan 2017 10:34:02 CST -06:00
pry(main)> ActiveSupport::TimeZone['America/New_York'].now.midnight.tomorrow
=> Fri, 06 Jan 2017 00:00:00 EST -05:00
Note that I'm looking for midnight tomorrow; remember that "midnight" for a given day is actually the very first minute of the day, not the last.

How to display a record set grouped by month?

I'm interested in display a record set of timeline type events in the format:
2011
APR
- Record Out
- Record Out
AUG
- Record Out
- Record Out
SEP
- Record Out
- Record Out
OCT
- Record Out
- Record Out
2009
OCT
- Record Out
- Record Out
The trick here being it only display the year if there are items in that year to show.. Same for a month, the month grouping only renders if there are records for that month to display. Does rails have any built in grouping for this type of output? Or would this be 100% custom to implement? Thanks for any pointers.
The grouping can be done by Ruby's built-in Enumerable#group_by method:
events.group_by(&:year).each do |year, year_events|
puts year, year_events
end
I assume you have event.year method. The same thing with grouping by months.

Resources