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:
@@ -149,7 +149,21 @@ func HandleSensorByLocationYear(w http.ResponseWriter, r *http.Request, ps httpr
|
|||||||
|
|
||||||
s, err := daily_sensor.GetAllSensorInfoByYear(location, year)
|
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)
|
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)
|
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)
|
fmt.Fprint(w, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
|
||||||
location := ps.ByName("location")
|
location := ps.ByName("location")
|
||||||
@@ -182,7 +209,7 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par
|
|||||||
|
|
||||||
fmt.Fprint(w, response)
|
fmt.Fprint(w, response)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
func createResponse(s []daily_sensor.Data, err error) string{
|
func createResponse(s []daily_sensor.Data, err error) string{
|
||||||
var response string
|
var response string
|
||||||
|
|
||||||
@@ -190,10 +217,10 @@ func createResponse(s []daily_sensor.Data, err error) string{
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
response = "{message : \"Error loading data from database\""
|
response = "{message : \"Error loading data from database\""
|
||||||
} else {
|
} else {
|
||||||
js, err1 := json.MarshalIndent(s, "", " ")
|
js, err := json.MarshalIndent(s, "", " ")
|
||||||
|
|
||||||
if err1 != nil {
|
if err != nil {
|
||||||
log.Println(err1)
|
log.Println(err)
|
||||||
response = "{message : \"Error loading data from database\""
|
response = "{message : \"Error loading data from database\""
|
||||||
} else {
|
} else {
|
||||||
response = string(js)
|
response = string(js)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type Data struct {
|
|||||||
MonthName string `json:"monthname,omitempty" bson:"monthname"`
|
MonthName string `json:"monthname,omitempty" bson:"monthname"`
|
||||||
Day int `json:"day,omitempty" bson:"day"`
|
Day int `json:"day,omitempty" bson:"day"`
|
||||||
Year int `json:"year,omitempty" bson:"year"`
|
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
|
//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)
|
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}},
|
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
|
||||||
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
|
{"$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){
|
func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
||||||
d := []Data{}
|
d := []Data{}
|
||||||
|
temp := UniqueDates{};
|
||||||
|
|
||||||
if db.Mongo.Connected() == true {
|
if db.Mongo.Connected() == true {
|
||||||
|
|
||||||
@@ -213,9 +216,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{{"$match": bson.M{"location": sensor_location}},
|
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
|
||||||
{"$project": bson.M{""}},
|
{"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}},
|
||||||
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
|
}).One(&temp)
|
||||||
|
|
||||||
|
d = temp.Dates;
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return d, nil
|
return d, nil
|
||||||
@@ -227,4 +232,3 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
|||||||
return d, errors.New("Query failed")
|
return d, errors.New("Query failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
@@ -20,7 +20,7 @@ func Routes() *httprouter.Router {
|
|||||||
r.GET("/api/sensor/:location", api.HandleSensorByLocation)
|
r.GET("/api/sensor/:location", api.HandleSensorByLocation)
|
||||||
r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear)
|
r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear)
|
||||||
r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth)
|
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("/discord", controller.DiscordRedirect)
|
||||||
r.GET("/vpn", controller.VPNRedirect)
|
r.GET("/vpn", controller.VPNRedirect)
|
||||||
|
|||||||
Reference in New Issue
Block a user