50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
// telemetry.go — REST-чтение телеметрии из per-tenant PostgreSQL.
|
|
//
|
|
// Endpoint: GET /v1/namespaces/{ns}/iot/telemetry?device_id={id}&limit={n}
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"gitea.services.ngcloud.ru/Nail/IoT/internal/storage/iotpg"
|
|
)
|
|
|
|
// ListIoTTelemetry — GET /v1/namespaces/{ns}/iot/telemetry.
|
|
func (h *Handler) ListIoTTelemetry(w http.ResponseWriter, r *http.Request) {
|
|
if h.IoTPG == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, errResp("IoT telemetry storage not configured"))
|
|
return
|
|
}
|
|
|
|
ns := pathVar(r, "namespace")
|
|
deviceID := r.URL.Query().Get("device_id")
|
|
limit := 50
|
|
if ls := r.URL.Query().Get("limit"); ls != "" {
|
|
if n, err := strconv.Atoi(ls); err == nil && n > 0 {
|
|
limit = n
|
|
}
|
|
}
|
|
offset := 0
|
|
if os := r.URL.Query().Get("offset"); os != "" {
|
|
if n, err := strconv.Atoi(os); err == nil && n >= 0 {
|
|
offset = n
|
|
}
|
|
}
|
|
|
|
rows, err := h.IoTPG.QueryTelemetry(r.Context(), ns, deviceID, limit, offset)
|
|
if err != nil {
|
|
h.Log.Error("query IoT telemetry", "namespace", ns, "device", deviceID, "err", err)
|
|
writeJSON(w, http.StatusInternalServerError, errResp("failed to query telemetry"))
|
|
return
|
|
}
|
|
if rows == nil {
|
|
rows = []iotpg.TelemetryRow{}
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"items": rows,
|
|
"count": len(rows),
|
|
})
|
|
}
|