1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 18:02:51 +00:00

adjusted end points again

This commit is contained in:
2016-09-14 20:12:41 +00:00
parent 4354dfe458
commit 2f45864fcc
3 changed files with 40 additions and 20 deletions

View File

@@ -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)
}

View File

@@ -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")
}
}
}

View File

@@ -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