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
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)
}

View File

@ -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"`

View File

@ -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.

View File

@ -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
}

View File

@ -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")