diff --git a/db/get-db.http b/db/get-db.http index e7a7d2a..681a77f 100644 --- a/db/get-db.http +++ b/db/get-db.http @@ -4,7 +4,8 @@ GET http://localhost:8080/api/v1/rooms/{{room-id}}/current ### GET history of a room -GET http://localhost:8080/api/v1/rooms/{{room-id}}/history +@window = 1 day +GET http://localhost:8080/api/v1/rooms/{{room-id}}/history?window={{window}} ### GET all rooms GET http://localhost:8080/api/v1/rooms diff --git a/db/src/docs/docs.go b/db/src/docs/docs.go index 0ca231c..cd7b455 100644 --- a/db/src/docs/docs.go +++ b/db/src/docs/docs.go @@ -112,6 +112,13 @@ const docTemplate = `{ "name": "room-id", "in": "path", "required": true + }, + { + "type": "string", + "default": "1 day", + "description": "Time window (e.g., 1 day, 1 hour, 30 min)", + "name": "window", + "in": "query" } ], "responses": { diff --git a/db/src/docs/swagger.json b/db/src/docs/swagger.json index 383510c..eb85b24 100644 --- a/db/src/docs/swagger.json +++ b/db/src/docs/swagger.json @@ -106,6 +106,13 @@ "name": "room-id", "in": "path", "required": true + }, + { + "type": "string", + "default": "1 day", + "description": "Time window (e.g., 1 day, 1 hour, 30 min)", + "name": "window", + "in": "query" } ], "responses": { diff --git a/db/src/docs/swagger.yaml b/db/src/docs/swagger.yaml index 62ff953..e79e279 100644 --- a/db/src/docs/swagger.yaml +++ b/db/src/docs/swagger.yaml @@ -68,6 +68,11 @@ paths: name: room-id required: true type: string + - default: 1 day + description: Time window (e.g., 1 day, 1 hour, 30 min) + in: query + name: window + type: string produces: - application/json responses: diff --git a/db/src/rest/rest.go b/db/src/rest/rest.go index 21e594f..02bbb04 100644 --- a/db/src/rest/rest.go +++ b/db/src/rest/rest.go @@ -125,15 +125,16 @@ func (g *RestGateway) getRoomCurrent(c *gin.Context) { // @Tags rooms // @Produce json // @Param room-id path string true "Room ID" +// @Param window query string false "Time window (e.g., 1 day, 1 hour, 30 min)" default(1 day) // @Success 200 {array} map[string]any // @Failure 500 {object} map[string]string // @Router /rooms/{room-id}/history [get] func (g *RestGateway) getRoomHistory(c *gin.Context) { roomID := c.Param("room-id") + window := c.DefaultQuery("window", "1 day") - // Default to last 24 hours if not specified - query := fmt.Sprintf(` SELECT * FROM "%s" WHERE "room" = '%s' AND time > now() - INTERVAL '1 day' ORDER BY time ASC -`, g.measurementName, roomID) + query := fmt.Sprintf(` SELECT * FROM "%s" WHERE "room" = '%s' AND time > now() - INTERVAL '%s' ORDER BY time ASC +`, g.measurementName, roomID, window) // Using context.Background() as seen in working snippet it, err := g.influxGateway.Query(context.Background(), query)