From 2f45864fcc1be1d26e9e0a30c8ad6171e6ef1f33 Mon Sep 17 00:00:00 2001 From: mgerb42 Date: Wed, 14 Sep 2016 20:12:41 +0000 Subject: [PATCH] adjusted end points again --- server/controller/api/sensor.go | 16 ++++++++++- server/model/daily_sensor/daily_sensor.go | 33 ++++++++++++----------- server/model/raw_sensor/raw_sensor.go | 11 +++++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/server/controller/api/sensor.go b/server/controller/api/sensor.go index 03ff980..5fefa2d 100644 --- a/server/controller/api/sensor.go +++ b/server/controller/api/sensor.go @@ -177,7 +177,21 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par s, err := daily_sensor.GetUniqueSensorDates(location) - response := createResponse(s, err) + var response string + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + js, err := json.MarshalIndent(s, "", " ") + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + response = string(js) + } + } fmt.Fprint(w, response) } diff --git a/server/model/daily_sensor/daily_sensor.go b/server/model/daily_sensor/daily_sensor.go index 4b45a29..8fd03a0 100644 --- a/server/model/daily_sensor/daily_sensor.go +++ b/server/model/daily_sensor/daily_sensor.go @@ -203,14 +203,25 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string) //I need to find a better implementation of this //MongoDB $addToSet creates an array of objecta //this ends up being a different type than the Data struct above -//it would be nice to make all MongoDB queries load directly into a Data struct +//it would be nice to make all MongoDB queries load directly into a Data struct +/* type UniqueDates struct{ Dates []Data `json:"dates" bson:"dates"` } +*/ -func GetUniqueSensorDates(sensor_location string) ([]Data, error){ - d := []Data{} - //temp := UniqueDates{}; +type Years struct { + Year int `json:"year", bson:"year"` + Months []Month `json:"months", bson:"months"` +} + +type Month struct { + Month int `json:"month", bson:"month"` + MonthName string `json:"monthname", bson:"monthname"` +} + +func GetUniqueSensorDates(sensor_location string) ([]Years, error){ + d := []Years{} if db.Mongo.Connected() == true { @@ -220,19 +231,11 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){ c := session.DB(db.Mongo.Info.Database).C(collection) err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}}, - {"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}}, - {"$unwind": "$dates"}, - {"$project": bson.M{"_id": 0, "month": "$dates.month", "monthname": "$dates.monthname", "year": "$dates.year"}}, - {"$sort": bson.M{"year": 1, "month": 1}}, + bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname"}}}}, + bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}}, }).All(&d) - //d = temp.Dates; - - test12, _ := json.MarshalIndent(d, "", " ") - - log.Println(string(test12)) - if err != nil { log.Println(err) return d, nil @@ -243,4 +246,4 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){ } else { return d, errors.New("Query failed") } -} +} \ No newline at end of file diff --git a/server/model/raw_sensor/raw_sensor.go b/server/model/raw_sensor/raw_sensor.go index 9b91b54..560fda8 100644 --- a/server/model/raw_sensor/raw_sensor.go +++ b/server/model/raw_sensor/raw_sensor.go @@ -59,9 +59,10 @@ func (s *Data) StoreData() error { //handle queries for all sensors page type DataStore_AllSensors struct { - ID string `json:"location" bson:"_id"` - Temperature float64 `json:"temperature" bson:"temperature"` - Updated time.Time `json:"updated" bson:"updated"` + ID bson.ObjectId `bson:"_id,omitempty"` + Location string `json:"location", bson:"location"` + Temperature float64 `json:"temperature" bson:"temperature"` + Updated time.Time `json:"updated" bson:"updated"` } //get latest update from each unique sensor @@ -78,7 +79,9 @@ func GetAllSensors() ([]DataStore_AllSensors, error) { err := c.Pipe([]bson.M{{"$group": bson.M{"_id": "$location", "temperature": bson.M{"$last": "$temperature"}, "updated": bson.M{"$last": "$updated"}}}, - bson.M{"$sort": bson.M{"_id": 1}}}).All(&s) + bson.M{"$sort": bson.M{"_id": 1}}, + bson.M{"$project": bson.M{"location": "$_id", "temperature": "$temperature", "updated": "$updated"}}, + }).All(&s) if err != nil { return s, nil