Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions documentation/api/metricsAPI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@
"string",
"null"
]
},
"value": {
"type": "number"
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions metrics-service/handlers/sensor_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"metrics-service/models"
"metrics-service/services"
"strconv"

"github.com/gin-gonic/gin"
)
Expand All @@ -21,7 +22,7 @@ func NewSensorHandler(victoriaMetricsService *services.VictoriaMetricsService) *
func (handler *SensorHandler) GetSensorStatus(context *gin.Context) {
sensorID := context.Param("sensorId")

query := fmt.Sprintf(`timestamp(temperature_celsius{device_id="%s"}[31d])`, sensorID)
query := fmt.Sprintf(`last_over_time(temperature_celsius{device_id="%s"}[31d])`, sensorID)

var response models.VictoriaMetricsResponse
if queryError := handler.victoriaMetricsService.QueryStruct(query, &response); queryError != nil {
Expand All @@ -32,11 +33,20 @@ func (handler *SensorHandler) GetSensorStatus(context *gin.Context) {
formattedResponse := models.SensorStatusResponse{
DeviceID: sensorID,
LastSeen: nil,
Value: 0,
}

if len(response.Data.Result) > 0 && len(response.Data.Result[0].Value) > 1 {
if lastSeenUnixStr, ok := response.Data.Result[0].Value[1].(string); ok {
formattedResponse.LastSeen = lastSeenUnixStr
if valueString, ok := response.Data.Result[0].Value[1].(string); ok {
if floatValue, err := strconv.ParseFloat(valueString, 64); err == nil {
formattedResponse.Value = floatValue
}
} else if floatValue, ok := response.Data.Result[0].Value[1].(float64); ok {
formattedResponse.Value = floatValue
}

if timestampFloat, ok := response.Data.Result[0].Value[0].(float64); ok {
formattedResponse.LastSeen = fmt.Sprintf("%d", int64(timestampFloat))
}
}

Expand Down
1 change: 1 addition & 0 deletions metrics-service/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type SensorStatusResponse struct {
Status string `json:"status"`
DeviceID string `json:"device_id"`
LastSeen interface{} `json:"last_seen"`
Value float64 `json:"value"`
}

type VictoriaMetricsResponse struct {
Expand Down
19 changes: 11 additions & 8 deletions metrics-service/tests/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ func setupMockVMServer(status int, body string) *httptest.Server {
}

func vmJSON(val string) string {
if val == "" {
return `{"status":"success","data":{"resultType":"vector","result":[]}}`
}
return `{"data":{"result":[{"value":[1704067200,"` + val + `"]}]}}`
}

func setup(testingState *testing.T, mockStatus int, mockBody string) (*testHelper, func()) {
mockVM := setupMockVMServer(mockStatus, mockBody)
helper := &testHelper{
router: setupTestRouter(mockVM.URL),
t: testingState,
}
return helper, func() { mockVM.Close() }
}
mockVM := setupMockVMServer(mockStatus, mockBody)
helper := &testHelper{
router: setupTestRouter(mockVM.URL),
t: testingState,
}

return helper, func() { mockVM.Close() }
}
Loading