Skip to content

Commit

Permalink
Removed ws. Added /v1/head endpoint for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanSerikov authored and aopoltorzhicky committed Mar 15, 2021
1 parent 8a59335 commit 65c657a
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 1,024 deletions.
76 changes: 76 additions & 0 deletions cmd/api/handlers/head.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package handlers

import (
"net/http"
"time"

"github.com/gin-gonic/gin"
)

// GetHead godoc
// @Summary Show indexer head
// @Description Get indexer head for each network
// @Tags head
// @ID get-indexer-head
// @Accept json
// @Produce json
// @Success 200 {array} HeadResponse
// @Failure 500 {object} Error
// @Router /v1/head [get]
func (ctx *Context) GetHead(c *gin.Context) {
item, err := ctx.Cache.Fetch("head", time.Second*30, ctx.getHead)
if ctx.handleError(c, err, 0) {
return
}

c.JSON(http.StatusOK, item.Value())
}

func (ctx *Context) getHead() (interface{}, error) {
blocks, err := ctx.Blocks.LastByNetworks()
if err != nil {
return nil, err
}

var network string
if len(blocks) == 1 {
network = blocks[0].Network
}
callCounts, err := ctx.Storage.GetCallsCountByNetwork(network)
if err != nil {
return nil, err
}
contractStats, err := ctx.Storage.GetContractStatsByNetwork(network)
if err != nil {
return nil, err
}
faCount, err := ctx.Storage.GetFACountByNetwork(network)
if err != nil {
return nil, err
}
body := make([]HeadResponse, len(blocks))
for i := range blocks {
body[i] = HeadResponse{
Network: blocks[i].Network,
Level: blocks[i].Level,
Timestamp: blocks[i].Timestamp,
Protocol: blocks[i].Protocol,
}
calls, ok := callCounts[blocks[i].Network]
if ok {
body[i].ContractCalls = calls
}
fa, ok := faCount[blocks[i].Network]
if ok {
body[i].FACount = fa
}
stats, ok := contractStats[blocks[i].Network]
if ok {
body[i].Total = stats.Total
body[i].TotalBalance = stats.Balance
body[i].UniqueContracts = stats.SameCount
}
}

return body, nil
}
14 changes: 14 additions & 0 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,17 @@ type TZIPResponse struct {
tzip.TZIP16
tzip.TZIP20
}

// HeadResponse -
type HeadResponse struct {
Network string `json:"network"`
Level int64 `json:"level"`
Timestamp time.Time `json:"time"`
Protocol string `json:"protocol"`
Total int64 `json:"total"`
ContractCalls int64 `json:"contract_calls"`
UniqueContracts int64 `json:"unique_contracts"`
TotalBalance int64 `json:"total_balance"`
TotalWithdrawn int64 `json:"total_withdrawn"`
FACount int64 `json:"fa_count"`
}
7 changes: 1 addition & 6 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/baking-bad/bcdhub/cmd/api/handlers"
"github.com/baking-bad/bcdhub/cmd/api/seed"
"github.com/baking-bad/bcdhub/cmd/api/validations"
"github.com/baking-bad/bcdhub/cmd/api/ws"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/helpers"
"github.com/baking-bad/bcdhub/internal/logger"
Expand All @@ -21,7 +20,6 @@ import (

type app struct {
Router *gin.Engine
Hub *ws.Hub
Context *handlers.Context
}

Expand Down Expand Up @@ -53,7 +51,6 @@ func newApp() *app {
}

api := &app{
Hub: ws.DefaultHub(ctx),
Context: ctx,
}

Expand Down Expand Up @@ -92,8 +89,8 @@ func (api *app) makeRouter() {
v1 := r.Group("v1")
{
v1.GET("swagger.json", api.Context.GetSwaggerDoc)
v1.GET("ws", func(c *gin.Context) { ws.Handler(c, api.Hub) })

v1.GET("head", api.Context.GetHead)
v1.GET("opg/:hash", api.Context.GetOperation)
v1.GET("operation/:id/error_location", api.Context.GetOperationErrorLocation)
v1.GET("pick_random", api.Context.GetRandomContract)
Expand Down Expand Up @@ -258,11 +255,9 @@ func (api *app) makeRouter() {

func (api *app) Close() {
api.Context.Close()
api.Hub.Stop()
}

func (api *app) Run() {
api.Hub.Run()
if err := api.Router.Run(api.Context.Config.API.Bind); err != nil {
logger.Error(err)
helpers.CatchErrorSentry(err)
Expand Down
39 changes: 0 additions & 39 deletions cmd/api/ws/channels/channel.go

This file was deleted.

134 changes: 0 additions & 134 deletions cmd/api/ws/channels/operations.go

This file was deleted.

39 changes: 0 additions & 39 deletions cmd/api/ws/channels/option.go

This file was deleted.

Loading

0 comments on commit 65c657a

Please sign in to comment.