Skip to content

Commit 2dcdfe0

Browse files
committed
fix(api): implement dynamic downsampling for telemetry history ranges
1 parent 1441623 commit 2dcdfe0

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

internal/api/data/handlers.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ func (h *Handler) GetData(c *gin.Context) {
254254
if dur := parseDuration(rangeStr); dur > 0 {
255255
end = time.Now()
256256
start = end.Add(-dur)
257+
258+
// Calculate dynamic interval for downsampling if not explicitly requested
259+
if c.Query("int") == "" && limit > 0 {
260+
interval = dur / time.Duration(limit)
261+
if interval < 10*time.Second {
262+
interval = 10 * time.Second
263+
}
264+
}
257265
}
258266
}
259267

@@ -272,7 +280,12 @@ func (h *Handler) GetData(c *gin.Context) {
272280
interval = parseDuration(intStr)
273281
}
274282

275-
points, err := h.Store.GetDataHistoryWithRange(deviceID, start, end, limit)
283+
dbLimit := limit
284+
if interval > 0 {
285+
dbLimit = 100000 // Fetch a large number of raw points for aggregation
286+
}
287+
288+
points, err := h.Store.GetDataHistoryWithRange(deviceID, start, end, dbLimit)
276289
if err != nil {
277290
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
278291
return

0 commit comments

Comments
 (0)