ruby - mongodb charts for date ranges and increasing numbers -


i want save watt-hour of electricity meter in mongodb. each minute current value stored in database. https://gist.github.com/ffaerber/9473682 (1440 minutes == 1 day)

database this: https://gist.github.com/ffaerber/9505235

what need increased value per hour.

my first idea was

hour0 = measurement.where(meter_id: @meter.id, timestamp: datetime.now.utc.beginning_of_day).first.wh  hour1 = measurement.where(meter_id: @meter.id, timestamp: datetime.now.utc.beginning_of_day + 1.hour).first.wh  puts hour0 => 1234136  puts hour1 => 1234838  

hour1 - hour0 = 702 watthour have been consumed between 0 , 1 clock @ night.

it works, not seem right way me. if want refine resolution. not every hour every 15 minutes?

it best if specify daterange , resolution(minutes, hours, dayes). , mongodb gives me array values.

i believe mongodb aggregation framework suited for?!

clearing question, dates bson dates deal differently. if you have time-stamp value date math below in order select last value each hour:

db.collection.aggregate([     { "$match" : { "timestamp": { "$gte": daystart, "$lt": dayend } } },      { "$project": {         "wh": 1,         "hourly": { "$hour": "$timestamp" }     }},     { "$sort": { "hourly": 1 } },     { "$group": {        "_id": "$hourly",        "wh": { "$last": "$wh" }     }} ]) 

to return total consumption within hour, math:

db.collection.aggregate([     { "$match" : { "timestamp": { "$gte": daystart, "$lt": dayend } } },      { "$project": {         "wh": 1,         "hourly": { "$hour": "$timestamp" }     }},     { "$group": {        "_id": "$hourly",        "firstreading": { "$last": "$wh" },                 "lastreading": { "$first": "$wh" }     }},     { "$project": {        "hourreading": { "$subtract": [ "$lastreading", "$firstreading" ] }     }},     { "$sort": { "_id": 1 } },  ]) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -