diff --git a/api/handlers/vaa/controller.go b/api/handlers/vaa/controller.go index 0377cfb8..b34b591a 100644 --- a/api/handlers/vaa/controller.go +++ b/api/handlers/vaa/controller.go @@ -2,8 +2,6 @@ package vaa import ( - "fmt" - "github.com/gofiber/fiber/v2" "github.com/wormhole-foundation/wormhole-explorer/api/middleware" "go.uber.org/zap" @@ -32,8 +30,6 @@ func (c *Controller) FindAll(ctx *fiber.Ctx) error { // FindByChain handler for the endpoint /vaas/:chain. func (c *Controller) FindByChain(ctx *fiber.Ctx) error { - - fmt.Println(ctx.Locals("requestid")) p := middleware.GetPaginationFromContext(ctx) chainID, err := middleware.ExtractChainID(ctx, c.logger) if err != nil { @@ -95,10 +91,12 @@ func (c *Controller) FindForPythnet(ctx *fiber.Ctx) error { return nil } -func (c *Controller) GetStats(ctx *fiber.Ctx) error { - stats, err := c.srv.GetVAAStats(ctx.Context()) +// GetVaaCount handler for the endpoint /vaas/vaa-counts. +func (c *Controller) GetVaaCount(ctx *fiber.Ctx) error { + p := middleware.GetPaginationFromContext(ctx) + vaas, err := c.srv.GetVaaCount(ctx.Context(), p) if err != nil { return err } - return ctx.JSON(stats) + return ctx.JSON(vaas) } diff --git a/api/handlers/vaa/model.go b/api/handlers/vaa/model.go index 4095435b..31124cf4 100644 --- a/api/handlers/vaa/model.go +++ b/api/handlers/vaa/model.go @@ -21,6 +21,7 @@ type VaaDoc struct { IndexedAt *time.Time `bson:"indexedAt" json:"indexedAt"` } +// VaaStats definition. type VaaStats struct { ChainID vaa.ChainID `bson:"_id" json:"chainId"` Count uint `bson:"count" json:"count"` diff --git a/api/handlers/vaa/repository.go b/api/handlers/vaa/repository.go index 531dd856..13566f3c 100644 --- a/api/handlers/vaa/repository.go +++ b/api/handlers/vaa/repository.go @@ -21,6 +21,7 @@ type Repository struct { collections struct { vaas *mongo.Collection invalidVaas *mongo.Collection + vaaCount *mongo.Collection } } @@ -31,7 +32,9 @@ func NewRepository(db *mongo.Database, logger *zap.Logger) *Repository { collections: struct { vaas *mongo.Collection invalidVaas *mongo.Collection - }{vaas: db.Collection("vaas"), invalidVaas: db.Collection("invalid_vaas")}} + vaaCount *mongo.Collection + }{vaas: db.Collection("vaas"), invalidVaas: db.Collection("invalid_vaas"), + vaaCount: db.Collection("vaaCounts")}} } // Find get a list of *VaaDoc. @@ -77,23 +80,28 @@ func (r *Repository) FindOne(ctx context.Context, q *VaaQuery) (*VaaDoc, error) return &vaaDoc, err } -func (r *Repository) FindStats(ctx context.Context) ([]*VaaStats, error) { - group := bson.D{ - {"$group", bson.D{ - {"_id", "$emitterChain"}, - {"Count", bson.D{{"$sum", 1}}}, - }}, +// GetVaaCount get a count of vaa by chainID. +func (r *Repository) GetVaaCount(ctx context.Context, q *VaaQuery) ([]*VaaStats, error) { + if q == nil { + q = Query() } - c, err := r.collections.vaas.Aggregate(ctx, mongo.Pipeline{group}) + sort := bson.D{{q.SortBy, q.GetSortInt()}} + cur, err := r.collections.vaaCount.Find(ctx, q.toBSON(), options.Find().SetLimit(q.PageSize).SetSkip(q.Offset).SetSort(sort)) if err != nil { requestID := fmt.Sprintf("%v", ctx.Value("requestid")) - r.logger.Error("failed execute Aggregate command to get vaa stats", + r.logger.Error("failed execute Find command to get vaaCount", zap.Error(err), zap.String("requestID", requestID)) - return nil, err + return nil, errors.WithStack(err) } - var stats []*VaaStats - err = c.All(ctx, &stats) - return stats, err + var varCounts []*VaaStats + err = cur.All(ctx, &varCounts) + if err != nil { + requestID := fmt.Sprintf("%v", ctx.Value("requestid")) + r.logger.Error("failed decoding cursor to []*VaaStats", zap.Error(err), zap.Any("q", q), + zap.String("requestID", requestID)) + return nil, errors.WithStack(err) + } + return varCounts, nil } // VaaQuery respresent a query for the vaa mongodb document. diff --git a/api/handlers/vaa/service.go b/api/handlers/vaa/service.go index 52142b2f..68a7c704 100644 --- a/api/handlers/vaa/service.go +++ b/api/handlers/vaa/service.go @@ -56,8 +56,13 @@ func (s *Service) FindById(ctx context.Context, chain vaa.ChainID, emitter vaa.A return &res, err } -func (s *Service) GetVAAStats(ctx context.Context) (*response.Response[[]*VaaStats], error) { - stats, err := s.repo.FindStats(ctx) +// GetVaaCount get a list a list of vaa count grouped by chainID. +func (s *Service) GetVaaCount(ctx context.Context, p *pagination.Pagination) (*response.Response[[]*VaaStats], error) { + if p == nil { + p = pagination.FirstPage() + } + query := Query().SetPagination(p) + stats, err := s.repo.GetVaaCount(ctx, query) res := response.Response[[]*VaaStats]{Data: stats} return &res, err } diff --git a/api/main.go b/api/main.go index 35698c92..7a4f99a6 100644 --- a/api/main.go +++ b/api/main.go @@ -103,12 +103,12 @@ func main() { // vaas resource vaas := api.Group("/vaas") vaas.Use(cache.New(cacheConfig)) + vaas.Get("/vaa-counts", vaaCtrl.GetVaaCount) vaas.Get("/", vaaCtrl.FindAll) vaas.Get("/:chain", vaaCtrl.FindByChain) vaas.Get("/:chain/:emitter", vaaCtrl.FindByEmitter) vaas.Get("/:chain/:emitter/:sequence", vaaCtrl.FindById) - api.Get("vaa-counts", vaaCtrl.GetStats) - api.Get("vaas-sans-pythnet", vaaCtrl.FindForPythnet) + vaas.Get("vaas-sans-pythnet", vaaCtrl.FindForPythnet) // oservations resource observations := api.Group("/observations")