From 9e2d3b445db14f426a1bf35b036ab6c4c44dbe0a Mon Sep 17 00:00:00 2001 From: agodnic Date: Tue, 30 May 2023 11:35:30 -0300 Subject: [PATCH] [API] Fix sorting order in observation endpoints (#357) ### Summary This pull request fixes an issue in which all observation-related endpoints were returning results sorted by `id` (https://github.com/wormhole-foundation/wormhole-explorer/issues/358). The expected behavior is to sort results based on descending timestamp order instead. Also, swagger docs were updated to document this behavior. --- api/docs/docs.go | 6 +++--- api/docs/swagger.json | 6 +++--- api/docs/swagger.yaml | 8 +++++--- api/handlers/observations/repository.go | 4 ++-- api/routes/wormscan/observations/controller.go | 6 +++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/api/docs/docs.go b/api/docs/docs.go index 246141ca..4d4248a4 100644 --- a/api/docs/docs.go +++ b/api/docs/docs.go @@ -640,7 +640,7 @@ const docTemplate = `{ }, "/api/v1/observations": { "get": { - "description": "Returns all observations.", + "description": "Returns all observations, sorted in descending timestamp order.", "tags": [ "Wormscan" ], @@ -690,7 +690,7 @@ const docTemplate = `{ }, "/api/v1/observations/:chain": { "get": { - "description": "Returns all observations for a given blockchain.", + "description": "Returns all observations for a given blockchain, sorted in descending timestamp order.", "tags": [ "Wormscan" ], @@ -740,7 +740,7 @@ const docTemplate = `{ }, "/api/v1/observations/:chain/:emitter": { "get": { - "description": "Returns all observations for a specific emitter address.", + "description": "Returns all observations for a specific emitter address, sorted in descending timestamp order.", "tags": [ "Wormscan" ], diff --git a/api/docs/swagger.json b/api/docs/swagger.json index 9cfc95a5..92ea557b 100644 --- a/api/docs/swagger.json +++ b/api/docs/swagger.json @@ -633,7 +633,7 @@ }, "/api/v1/observations": { "get": { - "description": "Returns all observations.", + "description": "Returns all observations, sorted in descending timestamp order.", "tags": [ "Wormscan" ], @@ -683,7 +683,7 @@ }, "/api/v1/observations/:chain": { "get": { - "description": "Returns all observations for a given blockchain.", + "description": "Returns all observations for a given blockchain, sorted in descending timestamp order.", "tags": [ "Wormscan" ], @@ -733,7 +733,7 @@ }, "/api/v1/observations/:chain/:emitter": { "get": { - "description": "Returns all observations for a specific emitter address.", + "description": "Returns all observations for a specific emitter address, sorted in descending timestamp order.", "tags": [ "Wormscan" ], diff --git a/api/docs/swagger.yaml b/api/docs/swagger.yaml index e46dcd63..a42e1618 100644 --- a/api/docs/swagger.yaml +++ b/api/docs/swagger.yaml @@ -1093,7 +1093,7 @@ paths: - Wormscan /api/v1/observations: get: - description: Returns all observations. + description: Returns all observations, sorted in descending timestamp order. operationId: find-observations parameters: - description: Page number. @@ -1126,7 +1126,8 @@ paths: - Wormscan /api/v1/observations/:chain: get: - description: Returns all observations for a given blockchain. + description: Returns all observations for a given blockchain, sorted in descending + timestamp order. operationId: find-observations-by-chain parameters: - description: Page number. @@ -1159,7 +1160,8 @@ paths: - Wormscan /api/v1/observations/:chain/:emitter: get: - description: Returns all observations for a specific emitter address. + description: Returns all observations for a specific emitter address, sorted + in descending timestamp order. operationId: find-observations-by-emitter parameters: - description: Page number. diff --git a/api/handlers/observations/repository.go b/api/handlers/observations/repository.go index db69b4d7..f78c1605 100644 --- a/api/handlers/observations/repository.go +++ b/api/handlers/observations/repository.go @@ -36,8 +36,8 @@ func NewRepository(db *mongo.Database, logger *zap.Logger) *Repository { // The input parameter [q *ObservationQuery] define the filters to apply in the query. func (r *Repository) Find(ctx context.Context, q *ObservationQuery) ([]*ObservationDoc, error) { - // Sort observations by ascending ID to provide deterministic output. - sort := bson.D{{"_id", 1}} + // Sort observations in descending timestamp order + sort := bson.D{{"indexedAt", -1}} cur, err := r.collections.observations.Find(ctx, q.toBSON(), options.Find().SetLimit(q.Limit).SetSkip(q.Skip).SetSort(sort)) if err != nil { diff --git a/api/routes/wormscan/observations/controller.go b/api/routes/wormscan/observations/controller.go index 586b6133..d4b11bfb 100644 --- a/api/routes/wormscan/observations/controller.go +++ b/api/routes/wormscan/observations/controller.go @@ -25,7 +25,7 @@ func NewController(srv *observations.Service, logger *zap.Logger) *Controller { } // FindAll godoc -// @Description Returns all observations. +// @Description Returns all observations, sorted in descending timestamp order. // @Tags Wormscan // @ID find-observations // @Param page query integer false "Page number." @@ -51,7 +51,7 @@ func (c *Controller) FindAll(ctx *fiber.Ctx) error { } // FindAllByChain godoc -// @Description Returns all observations for a given blockchain. +// @Description Returns all observations for a given blockchain, sorted in descending timestamp order. // @Tags Wormscan // @ID find-observations-by-chain // @Param page query integer false "Page number." @@ -82,7 +82,7 @@ func (c *Controller) FindAllByChain(ctx *fiber.Ctx) error { } // FindAllByEmitter godoc -// @Description Returns all observations for a specific emitter address. +// @Description Returns all observations for a specific emitter address, sorted in descending timestamp order. // @Tags Wormscan // @ID find-observations-by-emitter // @Param page query integer false "Page number."