Reputation: 18028
I have a sensor that sends in a value say ever 5 mins. This is being stored in a table along with the time stamp. I am using the code below to retrieve all values of that sensor in last 24 hours.
series = sensor.raindata.all(:order => "time_stamp", :conditions => {:time_stamp => Date.today...Date.today+1} )
But as the sensor us updating every 5 mins this is huge data. How do I sample the just one data point every hour? That is I need one value of that sensor per every hour. (I don't need the average for every hour. Just one sample per hour)
What is the most elegant way of achieving this? Is it possible to modify the above query to fetch this kind of data? or only way to do it is by post processing 'series' array?
Thanks Shaunak
Upvotes: 0
Views: 854
Reputation: 9700
The way to do this is probably with SQL. Here's an example, though I had to make a lot of assumptions about your setup to give it. I'm assuming:
So, with those disclaimers out of the way, here's the example:
Sensor.select('avg(reading) as avg_reading, extract(hour from time_stamp) as hour').where(:time_stamp => Date.today...Date.today+1).group('extract(hour from time_stamp)').order('hour')
This will give you a collection of Sensor objects (sort of), each of which responds to .hour
and .avg_reading
.
Alternatively (if, for instance, you're not on Rails 3), you should be attempting to execute a SQL query that looks something like this (inserting dates where indicated):
SELECT AVG(reading) AS avg_reading, EXTRACT(HOUR FROM time_stamp) AS hour FROM sensors WHERE time_stamp > {start date} AND time_stamp < {end date} GROUP BY EXTRACT(HOUR FROM time_stamp) ORDER BY hour
I hope this is helpful.
Upvotes: 1