mirror of
https://github.com/grassrootseconomics/cic-chain-events.git
synced 2026-05-27 21:27:55 +02:00
feat: add stats api, fix pipeline exe
* added cUSD sample filters * decouple stats from metrics
This commit is contained in:
51
internal/api/stats.go
Normal file
51
internal/api/stats.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/alitto/pond"
|
||||
"github.com/grassrootseconomics/cic-chain-events/internal/syncer"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
type statsResponse struct {
|
||||
HeadCursor uint64 `json:"headCursor"`
|
||||
LowerBound uint64 `json:"lowerBound"`
|
||||
MissingBlocks uint64 `json:"missingBlocks"`
|
||||
WorkerQueueSize uint64 `json:"workerQueueSize"`
|
||||
WorkerCount int `json:"workerCount"`
|
||||
WorkerSuccessfulTasks uint64 `json:"workerSuccessfulTasks"`
|
||||
WorkerFailedTasks uint64 `json:"workerFailedTasks"`
|
||||
}
|
||||
|
||||
func StatsHandler(
|
||||
syncerStats *syncer.Stats,
|
||||
poolStats *pond.WorkerPool,
|
||||
logg logf.Logger,
|
||||
) func(echo.Context) error {
|
||||
return func(ctx echo.Context) error {
|
||||
headCursor := syncerStats.GetHeadCursor()
|
||||
lowerBound := syncerStats.GetLowerBound()
|
||||
|
||||
stats := statsResponse{
|
||||
HeadCursor: headCursor,
|
||||
LowerBound: lowerBound,
|
||||
WorkerCount: poolStats.RunningWorkers(),
|
||||
WorkerQueueSize: poolStats.WaitingTasks(),
|
||||
WorkerSuccessfulTasks: poolStats.SuccessfulTasks(),
|
||||
WorkerFailedTasks: poolStats.FailedTasks(),
|
||||
}
|
||||
|
||||
if headCursor-lowerBound < 10 {
|
||||
stats.MissingBlocks = 0
|
||||
} else {
|
||||
stats.MissingBlocks = headCursor - lowerBound
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
Data: stats,
|
||||
})
|
||||
}
|
||||
}
|
||||
15
internal/api/types.go
Normal file
15
internal/api/types.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
const (
|
||||
INTERNAL_ERROR = "ERR_INTERNAL"
|
||||
)
|
||||
|
||||
type okResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type errResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
Reference in New Issue
Block a user