mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-11 02:12:53 +00:00
adjusted end points again
This commit is contained in:
@@ -177,7 +177,21 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par
|
|||||||
|
|
||||||
s, err := daily_sensor.GetUniqueSensorDates(location)
|
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)
|
fmt.Fprint(w, response)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,13 +204,24 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string)
|
|||||||
//MongoDB $addToSet creates an array of objecta
|
//MongoDB $addToSet creates an array of objecta
|
||||||
//this ends up being a different type than the Data struct above
|
//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{
|
type UniqueDates struct{
|
||||||
Dates []Data `json:"dates" bson:"dates"`
|
Dates []Data `json:"dates" bson:"dates"`
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
type Years struct {
|
||||||
d := []Data{}
|
Year int `json:"year", bson:"year"`
|
||||||
//temp := UniqueDates{};
|
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 {
|
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)
|
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||||
|
|
||||||
err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}},
|
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"}}}},
|
bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname"}}}},
|
||||||
{"$unwind": "$dates"},
|
bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}},
|
||||||
{"$project": bson.M{"_id": 0, "month": "$dates.month", "monthname": "$dates.monthname", "year": "$dates.year"}},
|
|
||||||
{"$sort": bson.M{"year": 1, "month": 1}},
|
|
||||||
|
|
||||||
}).All(&d)
|
}).All(&d)
|
||||||
|
|
||||||
//d = temp.Dates;
|
|
||||||
|
|
||||||
test12, _ := json.MarshalIndent(d, "", " ")
|
|
||||||
|
|
||||||
log.Println(string(test12))
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return d, nil
|
return d, nil
|
||||||
|
|||||||
@@ -59,7 +59,8 @@ func (s *Data) StoreData() error {
|
|||||||
|
|
||||||
//handle queries for all sensors page
|
//handle queries for all sensors page
|
||||||
type DataStore_AllSensors struct {
|
type DataStore_AllSensors struct {
|
||||||
ID string `json:"location" bson:"_id"`
|
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||||
|
Location string `json:"location", bson:"location"`
|
||||||
Temperature float64 `json:"temperature" bson:"temperature"`
|
Temperature float64 `json:"temperature" bson:"temperature"`
|
||||||
Updated time.Time `json:"updated" bson:"updated"`
|
Updated time.Time `json:"updated" bson:"updated"`
|
||||||
}
|
}
|
||||||
@@ -78,7 +79,9 @@ func GetAllSensors() ([]DataStore_AllSensors, error) {
|
|||||||
|
|
||||||
err := c.Pipe([]bson.M{{"$group": bson.M{"_id": "$location", "temperature": bson.M{"$last": "$temperature"},
|
err := c.Pipe([]bson.M{{"$group": bson.M{"_id": "$location", "temperature": bson.M{"$last": "$temperature"},
|
||||||
"updated": bson.M{"$last": "$updated"}}},
|
"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 {
|
if err != nil {
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user