commit 7de0142e927605acb0227590f756efa0ecd285d7
parent cbfa2fefb64ba825ff6abc72b364b7126f708f8a
Author: Chris Bracken <chris@bracken.jp>
Date: Fri, 12 Nov 2021 09:52:55 -0800
Limit reading queries by type and sensor ID
Previously queries for readings returned all readings of any type for
all sensors over the specified time period -- though that time period is
currently still hard-coded to the last 24 hours.
This changes the query API to return readings at the following URL path:
/sensor/reading/:sensorId/:readingType
and restricts readings to the specified sensor and reading type.
Further, this fixes a bug wherein the order of returned data was
previously not guaranteed to be in chronological order.
Diffstat:
2 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/http/http.go b/http/http.go
@@ -25,7 +25,7 @@ func CreateServer(ds storage.DataStore) *HttpServer {
func (s HttpServer) ListenAndServe(addr string) error {
mux := goji.NewMux()
mux.HandleFunc(pat.Post("/sensor/airrohr/"), s.PostAirrohr)
- mux.HandleFunc(pat.Get("/sensor/reading/"), s.QueryReadings)
+ mux.HandleFunc(pat.Get("/sensor/reading/:sensorId/:readingType"), s.QueryReadings)
return http.ListenAndServe(addr, mux)
}
@@ -56,8 +56,12 @@ func (s HttpServer) PostAirrohr(w http.ResponseWriter, r *http.Request) {
func (s HttpServer) QueryReadings(w http.ResponseWriter, r *http.Request) {
start := time.Now().Add(time.Hour * -24)
end := time.Now()
+
+ sensorId := pat.Param(r, "sensorId")
+ readingType := pat.Param(r, "readingType")
+
// TODO: extract start/end from r.URL.Query() with a default if not present.
- readings, err := s.ds.QueryReadings(start, end)
+ readings, err := s.ds.QueryReadings(sensorId, readingType, start, end)
if err != nil {
fmt.Println("Error: failed to query sensor data: " + err.Error())
errorResponse(w, "Bad Request. "+err.Error(), http.StatusBadRequest)
diff --git a/storage/storage.go b/storage/storage.go
@@ -11,8 +11,14 @@ const (
INSERT_READING = "INSERT INTO " +
"sensor_data(sensor_id, sw_version, reading_time, reading_type, reading_value) " +
"VALUES($1, $2, $3, $4, $5)"
- QUERY_READINGS = "SELECT sensor_id, sw_version, reading_time, reading_type, reading_value " +
- "FROM sensor_data WHERE reading_time >= $1 AND reading_time < $2"
+ QUERY_READINGS = "SELECT " +
+ " sensor_id, sw_version, reading_time, reading_type, reading_value " +
+ "FROM " +
+ " sensor_data " +
+ "WHERE " +
+ " sensor_id = $1 AND reading_type = $2 AND reading_time >= $3 AND reading_time < $4 " +
+ "ORDER BY " +
+ " reading_time"
)
type Reading struct {
@@ -26,7 +32,7 @@ type Reading struct {
type DataStore interface {
Close()
StoreReading(r *Reading) error
- QueryReadings(start time.Time, end time.Time) ([]Reading, error)
+ QueryReadings(sensorId string, readingType string, start time.Time, end time.Time) ([]Reading, error)
}
type PostgresDataStore struct {
@@ -62,8 +68,12 @@ func (s *PostgresDataStore) StoreReading(r *Reading) error {
return err
}
-func (s *PostgresDataStore) QueryReadings(start time.Time, end time.Time) ([]Reading, error) {
- rows, err := s.db.Query(QUERY_READINGS, start, end)
+func (s *PostgresDataStore) QueryReadings(sensorId string,
+ readingType string,
+ start time.Time,
+ end time.Time) ([]Reading, error) {
+
+ rows, err := s.db.Query(QUERY_READINGS, sensorId, readingType, start, end)
if err != nil {
return nil, err
}
@@ -97,7 +107,12 @@ func (s *NullDataStore) StoreReading(r *Reading) error {
return nil
}
-func (s *NullDataStore) QueryReadings(start time.Time, end time.Time) ([]Reading, error) {
+func (s *NullDataStore) QueryReadings(
+ sensorId string,
+ readingType string,
+ start time.Time,
+ end time.Time) ([]Reading, error) {
+
var readings []Reading
return readings, nil
}