Friday 15 February 2013

postgresql - Rails/Postgres query rows grouped by day with time zone -


I am trying to display the number of impressions per day for the past 30 days in specific users' time zones. The problem is that based on the time zone, the calculation is not always the same, and I'm having trouble showing it in the query. For example, two impressions that occur at 11:00 a day CDT (-5), and an impression that happens at 1:00 PM on CDT if you ask UTC (+0) All 3 impressions occurring on two days will be available, the first two days and the second second both CTT times UTC has land on day two.

This is what I am doing, I know that I should be missing something simple here:

  start = 30 .days.ago finish = time.No # If the user's time zone offset is less than 0, then we need to make sure that we create it all the time for the timeline if time.now.in_time_zone (current_user.timezone) & lt; 0 start + = 1 day ends + = 1. End of the day (start.to_date ... finish.to_date). MAP | Date | # You start the day in the user's timings in UCT, so that we can query just # on the day of the database. Date.to_time.in_time_zone (current_user.timezone) .beginning_of_day.utc [(day.to_i * 1000), impressions. Tot_on (day)] end   

impression model:

  class impressions & lt; ActiveRecord :: Base def self.total_on (day) count (Status: ["created_at> =? And created_it", "day", day + 24.hours]) end end   

I am looking at other posts and it seems that I can allow the database to take heavy load for me, but I AT time zone or INTERVAL .

I do not feel dirty, I know I should have some clarity. Any help is appreciated.

OK, with a little help, I think I have understood that my problem is Ruby's system. Knowing about time and time zone does not happen, rail system Once I set the right time zone for the user, I was able to use rail methods to simplify the sales around:

  # app / controller / application_controller Rb class ApplicationController & lt; ActionController :: Base around_filter: set_time_zone def set_time_zone if logged in? Time.use_zone (current_user.time_zone) {yield} and overhead end and end and # app / controller / charts_controller.rb start = 30.days.ago finish = time serient (start.to_date ... finish.to_date). | Date | #Rail method that sets time.zone to application_controller.rb # this UTC time = date Bijeneing_of_de.utc [(Time to toii * 1000), Impressional_on (time)] End # app / model / impression RB class impressions & lt; ActiveRecord :: Base def self.total_on (time) # time. Return time returns 24 hours after the frequency time. Therefore, this UTC calculation (status: ["created_at" = = and built_it "lt ;?", time, time.com]) stays at the end of the end   

I can do whatever I can, but now I am feeling better about this.

No comments:

Post a Comment