Modify vaa count endpoint to use capped collection (#35)

This commit is contained in:
walker-16 2022-11-23 10:15:16 -03:00 committed by GitHub
parent 8ed4804d63
commit bf8319bf10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 24 deletions

View File

@ -2,8 +2,6 @@
package vaa package vaa
import ( import (
"fmt"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/wormhole-foundation/wormhole-explorer/api/middleware" "github.com/wormhole-foundation/wormhole-explorer/api/middleware"
"go.uber.org/zap" "go.uber.org/zap"
@ -32,8 +30,6 @@ func (c *Controller) FindAll(ctx *fiber.Ctx) error {
// FindByChain handler for the endpoint /vaas/:chain. // FindByChain handler for the endpoint /vaas/:chain.
func (c *Controller) FindByChain(ctx *fiber.Ctx) error { func (c *Controller) FindByChain(ctx *fiber.Ctx) error {
fmt.Println(ctx.Locals("requestid"))
p := middleware.GetPaginationFromContext(ctx) p := middleware.GetPaginationFromContext(ctx)
chainID, err := middleware.ExtractChainID(ctx, c.logger) chainID, err := middleware.ExtractChainID(ctx, c.logger)
if err != nil { if err != nil {
@ -95,10 +91,12 @@ func (c *Controller) FindForPythnet(ctx *fiber.Ctx) error {
return nil return nil
} }
func (c *Controller) GetStats(ctx *fiber.Ctx) error { // GetVaaCount handler for the endpoint /vaas/vaa-counts.
stats, err := c.srv.GetVAAStats(ctx.Context()) func (c *Controller) GetVaaCount(ctx *fiber.Ctx) error {
p := middleware.GetPaginationFromContext(ctx)
vaas, err := c.srv.GetVaaCount(ctx.Context(), p)
if err != nil { if err != nil {
return err return err
} }
return ctx.JSON(stats) return ctx.JSON(vaas)
} }

View File

@ -21,6 +21,7 @@ type VaaDoc struct {
IndexedAt *time.Time `bson:"indexedAt" json:"indexedAt"` IndexedAt *time.Time `bson:"indexedAt" json:"indexedAt"`
} }
// VaaStats definition.
type VaaStats struct { type VaaStats struct {
ChainID vaa.ChainID `bson:"_id" json:"chainId"` ChainID vaa.ChainID `bson:"_id" json:"chainId"`
Count uint `bson:"count" json:"count"` Count uint `bson:"count" json:"count"`

View File

@ -21,6 +21,7 @@ type Repository struct {
collections struct { collections struct {
vaas *mongo.Collection vaas *mongo.Collection
invalidVaas *mongo.Collection invalidVaas *mongo.Collection
vaaCount *mongo.Collection
} }
} }
@ -31,7 +32,9 @@ func NewRepository(db *mongo.Database, logger *zap.Logger) *Repository {
collections: struct { collections: struct {
vaas *mongo.Collection vaas *mongo.Collection
invalidVaas *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. // Find get a list of *VaaDoc.
@ -77,23 +80,28 @@ func (r *Repository) FindOne(ctx context.Context, q *VaaQuery) (*VaaDoc, error)
return &vaaDoc, err return &vaaDoc, err
} }
func (r *Repository) FindStats(ctx context.Context) ([]*VaaStats, error) { // GetVaaCount get a count of vaa by chainID.
group := bson.D{ func (r *Repository) GetVaaCount(ctx context.Context, q *VaaQuery) ([]*VaaStats, error) {
{"$group", bson.D{ if q == nil {
{"_id", "$emitterChain"}, q = Query()
{"Count", bson.D{{"$sum", 1}}},
}},
} }
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 { if err != nil {
requestID := fmt.Sprintf("%v", ctx.Value("requestid")) 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)) zap.Error(err), zap.String("requestID", requestID))
return nil, err return nil, errors.WithStack(err)
} }
var stats []*VaaStats var varCounts []*VaaStats
err = c.All(ctx, &stats) err = cur.All(ctx, &varCounts)
return stats, err 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. // VaaQuery respresent a query for the vaa mongodb document.

View File

@ -56,8 +56,13 @@ func (s *Service) FindById(ctx context.Context, chain vaa.ChainID, emitter vaa.A
return &res, err return &res, err
} }
func (s *Service) GetVAAStats(ctx context.Context) (*response.Response[[]*VaaStats], error) { // GetVaaCount get a list a list of vaa count grouped by chainID.
stats, err := s.repo.FindStats(ctx) 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} res := response.Response[[]*VaaStats]{Data: stats}
return &res, err return &res, err
} }

View File

@ -103,12 +103,12 @@ func main() {
// vaas resource // vaas resource
vaas := api.Group("/vaas") vaas := api.Group("/vaas")
vaas.Use(cache.New(cacheConfig)) vaas.Use(cache.New(cacheConfig))
vaas.Get("/vaa-counts", vaaCtrl.GetVaaCount)
vaas.Get("/", vaaCtrl.FindAll) vaas.Get("/", vaaCtrl.FindAll)
vaas.Get("/:chain", vaaCtrl.FindByChain) vaas.Get("/:chain", vaaCtrl.FindByChain)
vaas.Get("/:chain/:emitter", vaaCtrl.FindByEmitter) vaas.Get("/:chain/:emitter", vaaCtrl.FindByEmitter)
vaas.Get("/:chain/:emitter/:sequence", vaaCtrl.FindById) vaas.Get("/:chain/:emitter/:sequence", vaaCtrl.FindById)
api.Get("vaa-counts", vaaCtrl.GetStats) vaas.Get("vaas-sans-pythnet", vaaCtrl.FindForPythnet)
api.Get("vaas-sans-pythnet", vaaCtrl.FindForPythnet)
// oservations resource // oservations resource
observations := api.Group("/observations") observations := api.Group("/observations")