// Get counter events in a optionally given time range // Query parameters: start,end func (c *Context) CounterByCodeEventsHandler(rw web.ResponseWriter, req *web.Request) { code := parseStringPathParameter(req, "code") var counter orm.Counter db.Where(&orm.Counter{Code: code}).First(&counter) if counter.ID == 0 { rw.WriteHeader(http.StatusNotFound) rw.Write([]byte("Counter not found")) return } start, err := c.parseUintQueryParameter(rw, "start") if err != nil { return } end, err := c.parseUintQueryParameter(rw, "end") if err != nil { return } var counterEvents []orm.CounterEvent orm.GetOrderedWindowedQuery(db, "counter_id", counter.ID, start, end).Find(&counterEvents) lastEvent := orm.NewLastCounterEvent(counter) if lastEvent.Timestamp > end { counterEvents = append(counterEvents, lastEvent) } marshal(rw, counterEvents) }
// Get thermometer readings in a optionally given time range // Query parameters: start,end func (c *Context) ThermometerByCodeGetReadingsHandler(rw web.ResponseWriter, req *web.Request) { code := parseStringPathParameter(req, "code") var thermometer orm.Thermometer db.Where(&orm.Thermometer{Code: code}).First(&thermometer) if thermometer.ID == 0 { rw.WriteHeader(http.StatusNotFound) rw.Write([]byte("Thermometer not found")) return } start, err := c.parseUintQueryParameter(rw, "start") if err != nil { return } end, err := c.parseUintQueryParameter(rw, "end") if err != nil { return } var thermometerReadings []orm.ThermometerReading orm.GetOrderedWindowedQuery(db, "thermometer_id", thermometer.ID, start, end).Find(&thermometerReadings) startReading := orm.ThermometerReading{ Reading: thermometerReadings[0].Reading, Timestamp: start, ThermometerID: thermometer.ID, } endReading := orm.ThermometerReading{ Reading: thermometerReadings[len(thermometerReadings)-1].Reading, Timestamp: end, ThermometerID: thermometer.ID, } thermometerReadings = append([]orm.ThermometerReading{startReading}, thermometerReadings...) thermometerReadings = append(thermometerReadings, endReading) marshal(rw, thermometerReadings) }