1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 01:42:55 +00:00

added end point for getting unique list of dates

This commit is contained in:
2016-09-10 20:42:30 +00:00
parent 0539eb740f
commit e2cb0969d7
3 changed files with 46 additions and 15 deletions

View File

@@ -149,7 +149,21 @@ func HandleSensorByLocationYear(w http.ResponseWriter, r *http.Request, ps httpr
s, err := daily_sensor.GetAllSensorInfoByYear(location, year)
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)
}
@@ -164,12 +178,25 @@ func HandleSensorByLocationMonth(w http.ResponseWriter, r *http.Request, ps http
s, err := daily_sensor.GetAllSensorInfoByMonth(location, year, monthname)
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)
}
/*
func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
location := ps.ByName("location")
@@ -182,7 +209,7 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par
fmt.Fprint(w, response)
}
*/
func createResponse(s []daily_sensor.Data, err error) string{
var response string
@@ -190,10 +217,10 @@ func createResponse(s []daily_sensor.Data, err error) string{
log.Println(err)
response = "{message : \"Error loading data from database\""
} else {
js, err1 := json.MarshalIndent(s, "", " ")
js, err := json.MarshalIndent(s, "", " ")
if err1 != nil {
log.Println(err1)
if err != nil {
log.Println(err)
response = "{message : \"Error loading data from database\""
} else {
response = string(js)

View File

@@ -23,7 +23,7 @@ type Data struct {
MonthName string `json:"monthname,omitempty" bson:"monthname"`
Day int `json:"day,omitempty" bson:"day"`
Year int `json:"year,omitempty" bson:"year"`
Updated time.Time `json:"updated" bson:"updated"`
Updated time.Time `json:"updated,omitempty" bson:"updated"`
}
//convert struct to json string
@@ -132,7 +132,6 @@ func GetAllSensorInfo(sensor_location string) ([]Data, error) {
c := session.DB(db.Mongo.Info.Database).C(collection)
//err := c.Find(bson.M{"location": sensor_location}).Sort("-year, -month").All(&d)
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
@@ -201,9 +200,13 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string)
}
}
/*
type UniqueDates struct{
Dates []Data `json:"dates" bson:"dates"`
}
func GetUniqueSensorDates(sensor_location string) ([]Data, error){
d := []Data{}
temp := UniqueDates{};
if db.Mongo.Connected() == true {
@@ -213,8 +216,10 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
c := session.DB(db.Mongo.Info.Database).C(collection)
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
{"$project": bson.M{""}},
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
{"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}},
}).One(&temp)
d = temp.Dates;
if err != nil {
log.Println(err)
@@ -227,4 +232,3 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
return d, errors.New("Query failed")
}
}
*/

View File

@@ -20,7 +20,7 @@ func Routes() *httprouter.Router {
r.GET("/api/sensor/:location", api.HandleSensorByLocation)
r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear)
r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth)
//r.GET("/api/sensor/:uniquedates", api.HandleUniqueDates)
r.GET("/api/uniquedates/:location", api.HandleUniqueDates)
r.GET("/discord", controller.DiscordRedirect)
r.GET("/vpn", controller.VPNRedirect)