Просмотр исходного кода

fix(portseries): force integer division in HourlyFpsTotal (modernc driver returns float) (#650)

The previous fix (#648/#649) still returned zeros: with the modernc
sqlite driver, `ts / ?` using a bound parameter is evaluated as REAL
division, and for a large ts (~1.7e12 ms) the quotient loses integer
precision (496902.006...), so the GROUP BY bucketing is wrong and
scanning into int64 fails (all rows skipped).

Use `CAST(ts / 3600000 AS INTEGER)` with a literal divisor to force exact
integer division. Verified against a live backup: 24/25 hourly buckets
with frames/s data (max ~9955 fps) instead of all zeros.

Co-authored-by: gnacho <hnacho@proton.me>
Nacho 2 месяцев назад
Родитель
Сommit
b08ea4d447
1 измененных файлов с 5 добавлено и 2 удалено
  1. 5 2
      server-go/internal/portseries/portseries.go

+ 5 - 2
server-go/internal/portseries/portseries.go

@@ -294,10 +294,13 @@ func (s *Store) HourlyFpsTotal(routerID string, hours int) ([]float64, error) {
 	// no haya terminado) y hacia atrás `hours` buckets completos.
 	nowMs := time.Now().Truncate(time.Hour).UnixMilli()
 	startMs := nowMs - int64(hours-1)*bucketMs
-	rows, err := s.db.Query(`SELECT (ts / ?) AS h, port_id, MAX(rx_frames), MAX(tx_frames)
+	// Ojo driver (modernc): `ts / ?` con parámetro se evalúa como REAL y un
+	// ts grande (~1.7e12) pierde precisión (496902.006…). CAST a entero con
+	// literal en el divisor fuerza división entera exacta (issue #648).
+	rows, err := s.db.Query(`SELECT CAST(ts / 3600000 AS INTEGER) AS h, port_id, MAX(rx_frames), MAX(tx_frames)
 		FROM port_series_raw
 		WHERE router_id = ? AND ts >= ?
-		GROUP BY h, port_id ORDER BY h`, bucketMs, routerID, startMs)
+		GROUP BY h, port_id ORDER BY h`, routerID, startMs)
 	if err != nil {
 		return nil, err
 	}