简体   繁体   English

在红宝石中将时间迭代24小时30天

[英]Iterating the Time for 24 hours for 30 days in ruby

I am working on google analytics API using the ruby gem. 我正在使用ruby gem开发Google Analytics(分析)API。 It accepts the parameters and generates the results, I am fetching the data from the seven metrics. 它接受参数并生成结果,我正在从七个指标中获取数据。 It accepts the start time and end time and generates the data according to the Time we pass. 它接受开始时间和结束时间,并根据我们经过的时间生成数据。

Here is the sample of that 这是该示例

Visits.results(pro1, {:start_date => Time.now - 86400, :end_date => Time.now})

Which gives me total number of visits for the last 24 hours. 这给了我最近24小时的总访问次数。

My requirement is little complicated. 我的要求有点复杂。 There are several other metrics also. 还有其他几个指标。 I need to get the data from the google analytics of previous 30 days. 我需要从前30天的Google Analytics(分析)中获取数据。 Suppose if I take the dump today it should get me previous 30 days of data. 假设如果我今天进行转储,则应该可以获取之前30天的数据。 like 28th 27th 26th 25th.... which gets the data of 28th march 2012 00:00:00 to 28th March 2012 59:59:59 , 27th march 2012 00:00:00 to 27th March 2012 59:59:59 and ......I have to trying to achieve this since 2-3 days inruby. 例如28th 27th 26th 25th .......,它获取2012年3月28日00:00:00至2012年3月28日59:59:59,2012年3月27日00:00:00至2012年3月27日59:59:59和......自2-3天inruby以来,我必须努力实现这一目标。 I am newbie to ruby. 我是红宝石的新手。 Kindly help me out. 请帮我。 TO make the things I need to take data from google analytic s iterating for 24 hours for the past 30 days. 为了使事情变得更糟,我需要从过去30天的24小时内Google Analytics(分析)中获取数据。

If you want results for individual days 如果您希望获得每一天的结果

results = []
30.downto(1) do |n|
  results << Visits.results(pro1, {:start_date => Date.today - n.days, :end_date => Date.today - (n-1).days})
end

or if you want them all at once 或者您一次要全部

Visits.results(pro1, {:start_date => Date.today - 30.days, :end_date => Date.today})

If you want to include today as well then set the end date to Date.tomorrow 如果您还想包括今天,则将结束日期设置为Date.tomorrow

尝试

Visits.results(pro1, {:start_date => Date.today.last_month , :end_date => Date.today})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM