[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.
This commit is contained in:
agodnic 2023-05-30 11:35:30 -03:00 committed by GitHub
parent 37d6d42c19
commit 9e2d3b445d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 14 deletions

View File

@ -640,7 +640,7 @@ const docTemplate = `{
}, },
"/api/v1/observations": { "/api/v1/observations": {
"get": { "get": {
"description": "Returns all observations.", "description": "Returns all observations, sorted in descending timestamp order.",
"tags": [ "tags": [
"Wormscan" "Wormscan"
], ],
@ -690,7 +690,7 @@ const docTemplate = `{
}, },
"/api/v1/observations/:chain": { "/api/v1/observations/:chain": {
"get": { "get": {
"description": "Returns all observations for a given blockchain.", "description": "Returns all observations for a given blockchain, sorted in descending timestamp order.",
"tags": [ "tags": [
"Wormscan" "Wormscan"
], ],
@ -740,7 +740,7 @@ const docTemplate = `{
}, },
"/api/v1/observations/:chain/:emitter": { "/api/v1/observations/:chain/:emitter": {
"get": { "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": [ "tags": [
"Wormscan" "Wormscan"
], ],

View File

@ -633,7 +633,7 @@
}, },
"/api/v1/observations": { "/api/v1/observations": {
"get": { "get": {
"description": "Returns all observations.", "description": "Returns all observations, sorted in descending timestamp order.",
"tags": [ "tags": [
"Wormscan" "Wormscan"
], ],
@ -683,7 +683,7 @@
}, },
"/api/v1/observations/:chain": { "/api/v1/observations/:chain": {
"get": { "get": {
"description": "Returns all observations for a given blockchain.", "description": "Returns all observations for a given blockchain, sorted in descending timestamp order.",
"tags": [ "tags": [
"Wormscan" "Wormscan"
], ],
@ -733,7 +733,7 @@
}, },
"/api/v1/observations/:chain/:emitter": { "/api/v1/observations/:chain/:emitter": {
"get": { "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": [ "tags": [
"Wormscan" "Wormscan"
], ],

View File

@ -1093,7 +1093,7 @@ paths:
- Wormscan - Wormscan
/api/v1/observations: /api/v1/observations:
get: get:
description: Returns all observations. description: Returns all observations, sorted in descending timestamp order.
operationId: find-observations operationId: find-observations
parameters: parameters:
- description: Page number. - description: Page number.
@ -1126,7 +1126,8 @@ paths:
- Wormscan - Wormscan
/api/v1/observations/:chain: /api/v1/observations/:chain:
get: 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 operationId: find-observations-by-chain
parameters: parameters:
- description: Page number. - description: Page number.
@ -1159,7 +1160,8 @@ paths:
- Wormscan - Wormscan
/api/v1/observations/:chain/:emitter: /api/v1/observations/:chain/:emitter:
get: 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 operationId: find-observations-by-emitter
parameters: parameters:
- description: Page number. - description: Page number.

View File

@ -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. // The input parameter [q *ObservationQuery] define the filters to apply in the query.
func (r *Repository) Find(ctx context.Context, q *ObservationQuery) ([]*ObservationDoc, error) { func (r *Repository) Find(ctx context.Context, q *ObservationQuery) ([]*ObservationDoc, error) {
// Sort observations by ascending ID to provide deterministic output. // Sort observations in descending timestamp order
sort := bson.D{{"_id", 1}} sort := bson.D{{"indexedAt", -1}}
cur, err := r.collections.observations.Find(ctx, q.toBSON(), options.Find().SetLimit(q.Limit).SetSkip(q.Skip).SetSort(sort)) cur, err := r.collections.observations.Find(ctx, q.toBSON(), options.Find().SetLimit(q.Limit).SetSkip(q.Skip).SetSort(sort))
if err != nil { if err != nil {

View File

@ -25,7 +25,7 @@ func NewController(srv *observations.Service, logger *zap.Logger) *Controller {
} }
// FindAll godoc // FindAll godoc
// @Description Returns all observations. // @Description Returns all observations, sorted in descending timestamp order.
// @Tags Wormscan // @Tags Wormscan
// @ID find-observations // @ID find-observations
// @Param page query integer false "Page number." // @Param page query integer false "Page number."
@ -51,7 +51,7 @@ func (c *Controller) FindAll(ctx *fiber.Ctx) error {
} }
// FindAllByChain godoc // 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 // @Tags Wormscan
// @ID find-observations-by-chain // @ID find-observations-by-chain
// @Param page query integer false "Page number." // @Param page query integer false "Page number."
@ -82,7 +82,7 @@ func (c *Controller) FindAllByChain(ctx *fiber.Ctx) error {
} }
// FindAllByEmitter godoc // 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 // @Tags Wormscan
// @ID find-observations-by-emitter // @ID find-observations-by-emitter
// @Param page query integer false "Page number." // @Param page query integer false "Page number."