2025-09-12 17:12:02 +08:00
|
|
|
// Package handler provides HTTP handlers for various endpoints.
|
2025-09-05 17:10:34 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-09-16 15:50:22 +08:00
|
|
|
"modelRT/constants"
|
2025-09-05 17:10:34 +08:00
|
|
|
"modelRT/database"
|
2025-09-10 17:03:33 +08:00
|
|
|
"modelRT/diagram"
|
2025-09-05 17:10:34 +08:00
|
|
|
"modelRT/logger"
|
|
|
|
|
"modelRT/network"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-29 16:37:38 +08:00
|
|
|
// MeasurementGetHandler define measurement query API
|
2025-09-05 17:10:34 +08:00
|
|
|
func MeasurementGetHandler(c *gin.Context) {
|
|
|
|
|
var request network.MeasurementGetRequest
|
2025-09-16 15:50:22 +08:00
|
|
|
|
|
|
|
|
clientToken := c.GetString("client_token")
|
|
|
|
|
if clientToken == "" {
|
|
|
|
|
err := constants.ErrGetClientToken
|
|
|
|
|
|
|
|
|
|
logger.Error(c, "failed to get client token from context", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
2025-09-12 17:12:02 +08:00
|
|
|
}
|
2025-09-05 17:10:34 +08:00
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
2025-10-14 16:12:00 +08:00
|
|
|
logger.Error(c, "failed to unmarshal measurement get request", "error", err)
|
2025-09-05 17:10:34 +08:00
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-09-16 15:50:22 +08:00
|
|
|
|
2025-09-12 17:12:02 +08:00
|
|
|
zset := diagram.NewRedisZSet(c, request.MeasurementToken, clientToken, 0, false)
|
2025-09-10 17:03:33 +08:00
|
|
|
points, err := zset.ZRANGE(request.MeasurementToken, 0, -1)
|
2025-09-05 17:10:34 +08:00
|
|
|
if err != nil {
|
2025-09-10 17:03:33 +08:00
|
|
|
logger.Error(c, "failed to get measurement data from redis", "measurement_token", request.MeasurementToken, "error", err)
|
2025-09-05 17:10:34 +08:00
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
2025-09-10 17:03:33 +08:00
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"measurement_id": request.MeasurementID,
|
|
|
|
|
"measurement_token": request.MeasurementToken,
|
|
|
|
|
},
|
2025-09-05 17:10:34 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 17:03:33 +08:00
|
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
|
measurementInfo, err := database.QueryMeasurementByID(c, pgClient, request.MeasurementID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(c, "failed to query measurement by id", "measurement_id", request.MeasurementID, "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"measurement_id": request.MeasurementID,
|
|
|
|
|
"measurement_token": request.MeasurementToken,
|
|
|
|
|
"measurement_value": points,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-09-05 17:10:34 +08:00
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, network.SuccessResponse{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Msg: "success",
|
|
|
|
|
PayLoad: map[string]interface{}{
|
2025-09-10 17:03:33 +08:00
|
|
|
"measurement_id": request.MeasurementID,
|
|
|
|
|
"measurement_token": request.MeasurementToken,
|
|
|
|
|
"measurement_info": measurementInfo,
|
|
|
|
|
"measurement_value": points,
|
2025-09-05 17:10:34 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|