-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed ws. Added /v1/head endpoint for stats
- Loading branch information
1 parent
8a59335
commit 65c657a
Showing
14 changed files
with
91 additions
and
1,024 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.