package query import ( "math" "slices" "testing" ) func TestDownsampleLTTB_Basic(t *testing.T) { // Generate 10,000 synthetic points: sine wave with an isolated spike n := 10000 ts := make([]int64, n) vals := make([]float64, n) for i := range n { ts[i] = int64(i * 1000) vals[i] = math.Sin(float64(i) / 100.0) } // Insert an extreme spike at index 4500 vals[4500] = 999.0 // Downsample to 500 points target := 500 outTS, outVals := DownsampleLTTB(ts, vals, target) if len(outTS) != target || len(outVals) != target { t.Fatalf("expected %d points, got %d ts, %d vals", target, len(outTS), len(outVals)) } // First and last points must match exactly if outTS[0] != ts[0] || outVals[0] != vals[0] { t.Errorf("first point mismatch: (%d, %f) vs (%d, %f)", outTS[0], outVals[0], ts[0], vals[0]) } if outTS[target-1] != ts[n-1] || outVals[target-1] != vals[n-1] { t.Errorf("last point mismatch: (%d, %f) vs (%d, %f)", outTS[target-1], outVals[target-1], ts[n-1], vals[n-1]) } // Verify timestamps are strictly monotonically increasing for i := 1; i < len(outTS); i++ { if outTS[i] <= outTS[i-1] { t.Fatalf("timestamps not strictly increasing at index %d: %d <= %d", i, outTS[i], outTS[i-1]) } } // Verify the extreme spike was preserved by LTTB spikePreserved := slices.Contains(outVals, 999.0) if !spikePreserved { t.Errorf("expected extreme spike (999.0) to be preserved by LTTB downsampling") } } func TestDownsampleLTTB_EdgeCases(t *testing.T) { ts := []int64{100, 200, 300, 400} vals := []float64{1.0, 2.0, 3.0, 4.0} var outVals []float64 // 1. threshold >= len(ts) outTS, _ := DownsampleLTTB(ts, vals, 10) if len(outTS) != 4 { t.Errorf("expected 4 points when threshold > len, got %d", len(outTS)) } // 2. threshold <= 0 outTS, _ = DownsampleLTTB(ts, vals, 0) if len(outTS) != 4 { t.Errorf("expected 4 points when threshold <= 0, got %d", len(outTS)) } // 3. threshold == 1 outTS, outVals = DownsampleLTTB(ts, vals, 1) if len(outTS) != 1 || len(outVals) != 1 || outTS[0] != 100 || outVals[0] != 1.0 { t.Errorf("expected 1 point [100], got %v, %v", outTS, outVals) } // 4. threshold == 2 outTS, outVals = DownsampleLTTB(ts, vals, 2) if len(outTS) != 2 || len(outVals) != 2 || outTS[0] != 100 || outTS[1] != 400 || outVals[0] != 1.0 || outVals[1] != 4.0 { t.Errorf("expected 2 endpoints [100, 400], got %v, %v", outTS, outVals) } // 5. len <= 2 shortTS := []int64{100, 200} shortVals := []float64{1.0, 2.0} outTS, outVals = Downsample(shortTS, shortVals, DownsampleOptions{MaxPoints: 100}) if len(outTS) != 2 || len(outVals) != 2 { t.Errorf("expected 2 points for short series, got %d, %d", len(outTS), len(outVals)) } } func TestAggregateBuckets(t *testing.T) { // Points: // Bucket 0 [0..999]: (0, 10), (500, 20) -> avg: 15, min: 10, max: 20, sum: 30, count: 2 // Bucket 1 [1000..1999]: (1000, 40), (1500, 60) -> avg: 50, min: 40, max: 60, sum: 100, count: 2 ts := []int64{0, 500, 1000, 1500} vals := []float64{10.0, 20.0, 40.0, 60.0} // 1. Avg outTS, outVals := AggregateBuckets(ts, vals, 1000, "avg") if len(outTS) != 2 || outVals[0] != 15.0 || outVals[1] != 50.0 { t.Errorf("unexpected avg results: %v, %v", outTS, outVals) } // 2. Min _, outVals = AggregateBuckets(ts, vals, 1000, "min") if len(outVals) != 2 || outVals[0] != 10.0 || outVals[1] != 40.0 { t.Errorf("unexpected min results: %v", outVals) } // 3. Max _, outVals = AggregateBuckets(ts, vals, 1000, "max") if len(outVals) != 2 || outVals[0] != 20.0 || outVals[1] != 60.0 { t.Errorf("unexpected max results: %v", outVals) } // 4. Sum _, outVals = AggregateBuckets(ts, vals, 1000, "sum") if len(outVals) != 2 || outVals[0] != 30.0 || outVals[1] != 100.0 { t.Errorf("unexpected sum results: %v", outVals) } // 5. Count _, outVals = AggregateBuckets(ts, vals, 1000, "count") if len(outVals) != 2 || outVals[0] != 2.0 || outVals[1] != 2.0 { t.Errorf("unexpected count results: %v", outVals) } } func TestDownsampleOptions_Integration(t *testing.T) { ts := make([]int64, 1000) vals := make([]float64, 1000) for i := range 1000 { ts[i] = int64(i * 1000) vals[i] = float64(i) } // Method "raw" / "none" outTS, _ := Downsample(ts, vals, DownsampleOptions{MaxPoints: 100, Method: "raw"}) if len(outTS) != 1000 { t.Errorf("expected raw bypass to return 1000 points, got %d", len(outTS)) } // MaxPoints with step calculation outTS, outVals := Downsample(ts, vals, DownsampleOptions{MaxPoints: 50, Method: "avg"}) if len(outTS) > 60 || len(outTS) < 40 { t.Errorf("expected ~50 points from step-based avg downsampling, got %d", len(outTS)) } if len(outVals) != len(outTS) { t.Errorf("values count mismatch with timestamps") } }