Iterating the dates in ruby on rails - ruby-on-rails

I am developing an application for reporting and analytic,where I need to generate data on a daily basis and put it in a CSV.To be more clear, if I take the report today like day one any how I can get the report thats already done, if I get the report tomorrow I should be able to get the data of today,yesterday. If i take after 4 days I should get the report starting from day 1 to day 4.
How it can be done. Looking for heads up

How about this:
today = Date.today
tomorrow = today.next
four_days_ago = 4.days.ago(today)
four_days_later = 4.days.since(today)
(today..(4.days.since(today)).each {|d| puts d}

Related

Set start and end date to current month Google Anaytics API

I'm setting up Google Sheet report using the Google Analytics app to generate a custom report, I've spent days searching for info on the subject all over the web for an answer to set current month for the report.
I can set start and end date with no problem, but I want the automated reports to be able to reset to the current month without me having to update the start and end date every month.
To achieve this, use the below:
For End Date, use
today
or, to make your report upto the previous day:
yesterday
For the start date, use the formula below:
=CONCATENATE(YEAR(today()),"-",
IF(LEN(MONTH(TODAY()))=1,CONCATENATE(0,MONTH(TODAY())),MONTH(TODAY())),
"-01" )
The formula will concatenate the current year, current month, and 01.
Another way to approach this problem is through using EOMONTH, for example to get the first day of this month:
=EOMONTH(today(), -1)+1

Rails 3 Impressionist indexing by day

I am using the Ruby gem Impressionist. It gives you the ability to look at a count of page hits from a specific date to today date. I'm trying to get the hits from the past 7 days but spit out the number for each day.
#widget.impressionist_count(:start_date=>"2011-01-01") #specify start date only, end date = now
How would I do that? I want 7 days ago but for each day to give me the exact count for that day.
Try this:
#widget.impressionist_count(:start_date => 1.week.ago)
You can get 1 week ago date like this
t = Time.now
lastweek = t - 1.week
and then you can do like this
#widget.impressionist_count(:start_date=>lastweek.strftime('%Y-%m-%d'))
I guess you shold do this in controller, or you can pass just the last week date to the view.
This code works in Rails app cause it uses active_support.
I tried impressionist with mongoid but the filter function didn't work at all in my enviroment.
i hope it does in your app.

Find all records that were created on a specific day of the week

I need to find all records that were created on a specific day of week.
I only have available to me the standard model datetime timestamps.
How would I go about doing this in activerecord?
To follow up on Justin's answer
where("extract(dow from created_at) = ?", Date.today.wday)
This is what I'm using in my application for postgres. This will find all records that were created on the same day-of-week as today. For example, if today was tuesday it would find all records created on tuesdays.
You can use the DAYOFWEEK function in MySQL and pass it to the :conditions option. Supposing you have a model called Item, this would return all of the items created on Sunday:
Item.all(:conditions => ['dayofweek(created_at) = ?', 1])
Using Postgres you could do something similar with to_char.
Note that using a function like this will probably make the database do a full table scan, since at least MySQL doesn't support adding an index to a function. You may want to consider extracting the day of week out to another column if this is something that you anticipate doing frequently.
Obtain the seconds since Unix Epoch. Time.to_i does this in Ruby.
Use modulus of 7 to obtain the day of the week (0 to 6).
dayOfWeek = (epochseconds / 86400 ) % 7;
If you're not opposed to using ruby you could try this.
array.select { |arr| ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].include?(arr.created_at.strftime('%A'))
I originally tried using dayofweek that was suggested in another answer.
The issue I ran in to was that it seems like my sql server was using UTC time and my rails server was using Eastern US. Records created after 8pm would be picked up while those that happened before would be considered the previous day.
Here is another related question:
How to filter by day of week in Rails 4.2 and sqlite?

Rails: find by day of week with timestamp

I need to grab the records for same day of the week for the preceeding X days of the week. There must be a better way to do it than this:
Transaction.find_by_sql "select * from transactions where EXTRACT(DOW from date) = 1 and organisation_id = 4 order by date desc limit 7"
It gets me what I need but is Postgres specific and not very "Rails-y". Date is a timestamp.
Anyone got suggestions?
How many days do you want to go back?
I have written a gem called by_star that has a dynamic finder suited for finding up to a certain number of days in the past. If the number of days was always a number you could use this finder:
Transaction.as_of_3_days_ago
If it was dynamic then I would recommend using something such as future or between, depending on if you have transactions in the future (i.e. time travel):
Transaction.future(params[:start_date].to_time)
Transaction.between(params[:start_date].to_time, Time.now)
AFAIK Rails has no any methods to do this by other way. So best, and faster, solution - build DOW index on date column and use your query.

Finding schedule overlaps with Ruby & Runt?

I'm using Ruby on Rails to build a simple application to keep track of a shop's opening and closing times and its appointments, and I'm having some trouble validating the appointments against closings in the shop's schedule.
I've been using Runt to compose the schedule. For example, if the shop is open Monday morning from 9am-12pm closed for an hour for lunch and then open in the afternoon until 5pm, it would look like:
require 'runt'
include Runt
monday = DIWeek.new(Mon)
morning = REDay.new(9,00,12,00)
afternoon = REDAy.new(13,00,17,00)
expr = monday & morning & afternoon
For a given appointment (also a Runt Temporal Expression), how can I make sure that the appointment does overlap with the opening times and does not overlap the lunch hour (or other times before or after the opening times)?
I gather that Runt has an overlaps? method, but if I do something like:
expr.overlaps?(DIWeek.new(Mon) & REDay.new(10,00,11,00)) # the appointment is from 10-11am on Monday and should overlap the morning opening time
I get this error:
NoMethodError: undefined method `overlaps?' for #<Runt::Intersect:0x10483cc>
Can anyone please advises me on how to correct this error or else another way to solve this problem?
The overlaps? method is defined only on Runt::Collection and Runt::DateRange. Perhaps you can create a DateRange object from your expr and then run overlap? on it.

Resources