From 9111d9c9b2cda537d260117587ed12878545234c Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Mon, 23 Sep 2024 16:36:59 +0100 Subject: [PATCH 1/2] fix: include most recent candles in the API response Signed-off-by: Elias Van Ootegem --- datanode/candlesv2/candle_updates.go | 12 +++++------- datanode/sqlstore/candles.go | 3 +++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/datanode/candlesv2/candle_updates.go b/datanode/candlesv2/candle_updates.go index 41fb0e0d36d..136450c921e 100644 --- a/datanode/candlesv2/candle_updates.go +++ b/datanode/candlesv2/candle_updates.go @@ -196,18 +196,16 @@ func (s *CandleUpdates) getCandleUpdates(ctx context.Context, lastCandle *entiti return nil, fmt.Errorf("getting candle updates:%w", err) } + // allocate slice rather than doubling cap as we go. + updates = make([]entities.Candle, 0, len(candles)) for _, candle := range candles { - if candle.LastUpdateInPeriod.After(lastCandle.LastUpdateInPeriod) || candle.PeriodStart.After(lastCandle.PeriodStart) { + // not before so either newer, or the same (last) candle should be returned. + if !candle.LastUpdateInPeriod.Before(lastCandle.LastUpdateInPeriod) || !candle.PeriodStart.Before(lastCandle.PeriodStart) { updates = append(updates, candle) } } } else { - last := int32(1) - pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, false) - if err != nil { - return nil, err - } - updates, _, err = s.candleSource.GetCandleDataForTimeSpan(ctx, s.candleID, nil, &now, pagination) + updates, _, err = s.candleSource.GetCandleDataForTimeSpan(ctx, s.candleID, nil, &now, entities.CursorPagination{}) if err != nil { return nil, fmt.Errorf("getting candle updates:%w", err) diff --git a/datanode/sqlstore/candles.go b/datanode/sqlstore/candles.go index 6791a2de557..ac33597b779 100644 --- a/datanode/sqlstore/candles.go +++ b/datanode/sqlstore/candles.go @@ -26,6 +26,7 @@ import ( "code.vegaprotocol.io/vega/datanode/entities" "code.vegaprotocol.io/vega/datanode/metrics" "code.vegaprotocol.io/vega/libs/crypto" + "code.vegaprotocol.io/vega/logging" v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" "github.com/georgysavva/scany/pgxscan" @@ -193,6 +194,8 @@ func (cs *Candles) GetCandleDataForTimeSpan(ctx context.Context, candleID string // now that we have the paged query, we can add in the subquery query = fmt.Sprintf("with gap_filled_candles as (%s) %s", subQuery, query) + cs.log.Debug(">> Candle query", logging.String("query", query)) + fmt.Printf(">>> QUERY: %s\n", query) defer metrics.StartSQLQuery("Candles", "GetCandleDataForTimeSpan")() err = pgxscan.Select(ctx, cs.ConnectionSource, &candles, query, args...) From 285b0d574223ce49e6aeaae81c2ba83f01f2413c Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Mon, 23 Sep 2024 16:52:12 +0100 Subject: [PATCH 2/2] chore: remove debug statements Signed-off-by: Elias Van Ootegem --- datanode/sqlstore/candles.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/datanode/sqlstore/candles.go b/datanode/sqlstore/candles.go index ac33597b779..6791a2de557 100644 --- a/datanode/sqlstore/candles.go +++ b/datanode/sqlstore/candles.go @@ -26,7 +26,6 @@ import ( "code.vegaprotocol.io/vega/datanode/entities" "code.vegaprotocol.io/vega/datanode/metrics" "code.vegaprotocol.io/vega/libs/crypto" - "code.vegaprotocol.io/vega/logging" v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" "github.com/georgysavva/scany/pgxscan" @@ -194,8 +193,6 @@ func (cs *Candles) GetCandleDataForTimeSpan(ctx context.Context, candleID string // now that we have the paged query, we can add in the subquery query = fmt.Sprintf("with gap_filled_candles as (%s) %s", subQuery, query) - cs.log.Debug(">> Candle query", logging.String("query", query)) - fmt.Printf(">>> QUERY: %s\n", query) defer metrics.StartSQLQuery("Candles", "GetCandleDataForTimeSpan")() err = pgxscan.Select(ctx, cs.ConnectionSource, &candles, query, args...)