Get objects size between relative dates - ruby-on-rails

I have many User objects with created_at attribute e.g.
I get objects with #users = User.all
I want get the count of User objects with various ages from creation with
#users.size
for these date ranges:
yesterday
last week
last month
last year.
How can I do it?
I use mongoid.

You can write scopes for this:
class User
include Mongoid::Document
...
## Scopes for calculating relative users
scope :created_yesterday, lambda { where(:created_at.gte => (Time.now - 1.day)) }
scope :created_last_week, lambda { where(:created_at.gte => (Time.now - 1.week)) }
scope :created_last_month, lambda { where(:created_at.gte => (Time.now - 1.month)) }
scope :created_last_year, lambda { where(:created_at.gte => (Time.now - 1.year)) }
...
end
The reason we need to use a lambda here is that it delays the evaluation of the Time.now argument to when the scope is actually invoked. Without the lambda the time that would be used in the query logic would be the time that the class was first evaluated, not the scope itself.
Now we can get the counts, by simply calling:
User.created_yesterday.count
User.created_last_week.count
...
If you want the objects:
#users_created_yesterday = User.created_yesterday
You can use #users_created_yesterday in the views.
Update
Well with yesterday, if you mean time between, yesterday beginning of day 0:00 and yesterday end of day 23:59, you will have to take Time zones into consideration.
Fo example, in your application.rb, if you have written:
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Central Time (US & Canada)'
If you have use this, all the times fetched by activerecord queries will be converted to this time zone, Central Time (US & Canada). In the database, all the times will be stored in UTC and will be converted to the time zone on fetching from the database. If you have commented out this line, default will be UTC. You can get all the time zones using the rake task rake time:zones:all or only the US timezones using rake time:zones:us.
If you want the time zone specified in the application.rb, you need to use Time.zone.now, in the following code. If you use Time.now in the following code, you will get the time zone according to the time zone of your server machine.
class User
include Mongoid::Document
...
scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }
class << self
## Class methods for calculating relative users
def created_yesterday
yesterday = Time.zone.now - 1.day
created_between(yesterday.beginning_of_day, yesterday.end_of_day)
end
def created_last_week
start_time = (Time.zone.now - 1.week).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_month
start_time = (Time.zone.now - 1.month).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_year
start_time = (Time.zone.now - 1.year).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
end
..
..
end
So, you can write class methods and calculate start time and end time, supply it to the scope created_between, and you will be able to call them like User.created_yesterday, like we called before.
Credits: EdgeRails. Since it is Mongoid, I had to look up at Mongoid docs

Related

How to manage different time_zones in ruby on rails

Attributes of users are start_time,end_time and time_zone.
I am saving only time not date and user time_zone
e.g
start_time => "09:00"
end_time => "02:00"
time_zone => "Samoa"
User is only called b/w given start_time and end_time according to given time_zone. How I check this?
My research , I found this
in_time_zone('Eastern Time (US & Canada)')
I want to check that user is available or not according to its start_time and end_time based on time_zone
server_tz = Time.zone # zone on server
server_now = server_tz.now # time on server right now
Time.use_zone(time_zone) do # ⇐ user’s time zone, 'Samoa'
server_start, server_end = %i|start_time end_time|.map do |t|
# ⇓ parse in user time zone and convert to server tz
Time.zone.parse("#{Date.today} #{t}").in_time_zone(server_tz)
end
# ⇓ use case-triple-equal to check if time is in between
(server_start..server_end) === server_now
end

(Rails) Test fails during late night time

My rails application is covered by many tests. All the tests pass in regular situations. That is, excluding when it's late at night.
There's actually a few tests that end up failing when it's night. All these tests involve modifying a model's time attribute and seeing if related models are affected.
test "changing time should affect hours" do
// ..User is loaded.
user.attend(event)
assert_equal 1, user.hours // User attends a 1 hour event that has passed.
// Move event to the future.
event.update_attributes(date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour)
assert_equal 0, attendance_of(user).hours // Passes in day, fails during night
end
test "valid event creation" do
// Count does NOT change by 1 at night.
assert_difference '#group.events.count', 1 do
post group_events_path(#group), event: { ...
date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour,
... }
end
end
What is going on here? For reference, here's what I currently use to determine when to update an attendance (which is something that an event has). This comes from the event controller:
def not_ended?
date.future? || (date.today? &&
(Time.now.seconds_since_midnight < end_time.seconds_since_midnight))
end
def update_attendances
// ... Determine the new date, start, and end time values through ActiveRecord::Dirty
if not_ended?
remove_checks = true
end
attendances.each do |attendance|
new_checked = remove_checks ? false : attendance.checked
attendance.update_attributes(went: new_start, left: new_end,
checked: new_checked)
end
end
end
Validating an event to make sure its times aren't weird:
def valid_time
if start_time == end_time
// Error...
end
if start_time > end_time
// Error...
end
end
Time zone in application.rb:
config.time_zone = 'Pacific Time (US & Canada)'
Use Time.zone.now or timecop.freeze to freeze the timezone as your test is probably using system time and not rails app config time
Your not_ended? method is broken. It doesn't work when the event starts before midnight, but ends after. In that case the date is today (assuming the data is based on start time) but the number of seconds since midnight of the end of the event is less that the current time.
In these situations you shouldn't be trying to deal with dates and times separately. You should have a way to retrieve the datetime of the end of the event and compare that with the current datetime.

Rails/Postgres query rows grouped by day with time zone

I'm trying to display a count of impressions per day for the last 30 days in the specific users time zone. The trouble is that depending on the time zone, the counts are not always the same, and I'm having trouble reflecting that in a query.
For example, take two impressions that happen at 11:00pm in CDT (-5) on day one, and one impression that happens at 1:00am CDT. If you query using UTC (+0) you'll get all 3 impressions occurring on day two, instead of two the first day and one the second. Both CDT times land on the day two in UTC.
This is what I'm doing now, I know I must be missing something simple here:
start = 30.days.ago
finish = Time.now
# if the users time zone offset is less than 0 we need to make sure
# that we make it all the way to the newest data
if Time.now.in_time_zone(current_user.timezone) < 0
start += 1.day
finish += 1.day
end
(start.to_date...finish.to_date).map do |date|
# get the start of the day in the user's timezone in utc so we can properly
# query the database
day = date.to_time.in_time_zone(current_user.timezone).beginning_of_day.utc
[ (day.to_i * 1000), Impression.total_on(day) ]
end
Impressions model:
class Impression < ActiveRecord::Base
def self.total_on(day)
count(conditions: [ "created_at >= ? AND created_at < ?", day, day + 24.hours ])
end
end
I've been looking at other posts and it seems like I can let the database handle a lot of the heavy lifting for me, but I wasn't successful with using anything like AT TIME ZONE or INTERVAL.
What I have no seems really dirty, I know I must missing something obvious. Any help is appreciated.
Ok, with a little help from this awesome article I think I've figured it out. My problem stemmed from not knowing the difference between the system Ruby time methods and the time zone aware Rails methods. Once I set the correct time zone for the user using an around_filter like this I was able to use the built in Rails methods to simplify the code quite a bit:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
around_filter :set_time_zone
def set_time_zone
if logged_in?
Time.use_zone(current_user.time_zone) { yield }
else
yield
end
end
end
# app/controllers/charts_controller.rb
start = 30.days.ago
finish = Time.current
(start.to_date...finish.to_date).map do |date|
# Rails method that uses Time.zone set in application_controller.rb
# It's then converted to the proper time in utc
time = date.beginning_of_day.utc
[ (time.to_i * 1000), Impression.total_on(time) ]
end
# app/models/impression.rb
class Impression < ActiveRecord::Base
def self.total_on(time)
# time.tomorrow returns the time 24 hours after the instance time. so it stays UTC
count(conditions: [ "created_at >= ? AND created_at < ?", time, time.tomorrow ])
end
end
There might be some more that I can do, but I'm feeling much better about this now.
Presuming the around_filter correctly works and sets the Time.zone in the block, you should be able to refactor your query into this:
class Impression < ActiveRecord::Base
def self.days_ago(n, zone = Time.zone)
Impression.where("created_at >= ?", n.days.ago.in_time_zone(zone))
end
end

Time.zone.now locked in production

In prodction, method Time.zone.now always return the same value while Time.now returns
the correct system time.
Here one example:
banner.rb
scope :active, where("'#{Time.zone.now}' between start_date and end_date")
log of this scope:
SELECT `banners`.* FROM `banners` WHERE ('2013-03-06 08:06:46 -0300' between start_date and end_date) AND ((`banners`.`store_id` = 1 AND `banners`.`spot_id` = 3 AND `banners`.`at_home` = 1))
Time.zone.now is always 2013-03-06 08:06:46 -0300
Anyone know why?
This is because the scope is evaluated once, when the class is loaded.
You should define it like this:
def self.active
where("'#{Time.zone.now}' between start_date and end_date")
end
Every time this method is called, it will be re-evaluated and so the time will change.

Rails time zone not overrides as well

I have a noisy problems with UTC on my Rails project.
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
Time.zone = current_user.time_zone if current_user
end
Cool. I overrided the time zone.
And now, server' time zone is +3. User's time zone is +5. I hope that any requests to Time should get the User's time zone, but this code returns not expected values:
render :text => Time.zone.to_s + "<br/>" +
Time.now.to_s + "<br/>" +
Time.now.in_time_zone.to_s
RESULT:
(GMT+05:00) Tashkent
Thu Oct 20 19:41:11 +0300 2011
2011-10-20 21:41:11 +0500
Where does from +0300 offset comes??
To get the current time in the currently set timezone you can use
Time.zone.now
Your server' time zone is +3 and
Time.now.to_s is returning this
saha! Sorry, but I have not a 15 points of reputation to give you the level-up)).
Anyway thanks for your help.
I wrote a TimeUtil helper, an uses it for time correction. This is my current pseudo-code:
class RacesController < ApplicationController
def create
#race = Race.new(params[:race])
#race.correct_time_before_save #can be before_save
#race.save
end
class Race < ActiveRecord::Base
def correct_time_before_save
date = self.attributes["race_date"]
time = self.attributes["race_time"]
datetime = Time.local(date.year, date.month, date.day, time.hour, time.min, time.sec)
datetime_corrected = TimeUtil::override_offset(datetime)
self.race_date = datetime_corrected.to_date
self.race_time = datetime_corrected.to_time
end
# TimeUtil is uses for time correction. It should be very clear, please read description before using.
# It's for time correction, when server's and user's time zones are different.
# Example: User lives in Madrid where UTC +1 hour, Server had deployed in New York, UTC -5 hours.
# When user say: I want this race to be started in 10:00.
# Server received this request, and say: Okay man, I can do it!
# User expects to race that should be started in 10:00 (UTC +1hour) = 09:00 UTC+0
# Server will start the race in 10:00 (UTC -5 hour) = 15:00 UTC+0
#
# This module brings the workaround. All that you need is to make a preprocess for all incoming Time data from users.
# Check the methods descriptions for specific info.
#
# The Time formula is:
# UTC + offset = local_time
# or
# UTC = local_time - offset
#
module TimeUtil
# It returns the UTC+0 DateTime object, that computed from incoming parameter "datetime_arg".
# The offset data in "datetime_arg" is ignored - it replaces with Time.zone offset.
# Time.zone offset initialized in ApplicationController::set_timezone before-filter method.
#
def self.override_offset datetime_arg
Time.zone.parse(datetime_arg.strftime("%D %T")).utc
end
ActiveRecord getters adapted to user's time zones too. Time is stored in database (mysql) in "utc+0" format, and we want to get this time in current user's timezone format:
class Race < ActiveRecord::Base
def race_date
date = self.attributes["race_date"]
time = self.attributes["race_time"]
datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
datetime.to_date
end
def race_time
date = self.attributes["race_date"]
time = self.attributes["race_time"]
datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
datetime.to_time
end

Resources