1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-08-31 21:38:12 +02:00

internal/api: rename eosio_*.go to antelope_*.go

This commit is contained in:
Henrik Hautakoski 2022-12-22 14:17:22 +01:00
parent 1fc8c927aa
commit 86e87d8245
No known key found for this signature in database
GPG key ID: 217490840C18A5D9
4 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,84 @@
package api
import (
"fmt"
"github.com/eosswedenorg-go/haproxy/agentcheck"
"github.com/eosswedenorg-go/leapapi"
"github.com/eosswedenorg/antelope-api-healthcheck/internal/utils"
)
type AntelopeV2 struct {
client leapapi.Client
offset int64
}
func AntelopeV2Factory(args ApiArguments) ApiInterface {
return NewAntelopeV2(args.Url, args.Host, int64(args.NumBlocks))
}
func NewAntelopeV2(url string, host string, offset int64) AntelopeV2 {
api := AntelopeV2{
client: *leapapi.New(url),
offset: offset,
}
api.client.Host = host
return api
}
func (e AntelopeV2) LogInfo() LogParams {
p := LogParams{
"type", "antelope-v2",
"url", e.client.Url,
}
if len(e.client.Host) > 0 {
p.Add("host", e.client.Host)
}
p.Add("offset", e.offset)
return p
}
func (e AntelopeV2) Call() (agentcheck.Response, string) {
health, err := e.client.GetHealth()
if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
return resp, err.Error()
}
// Fetch elasticsearch and nodeos block numbers from json.
var es_block int64 = 0
var node_block int64 = 0
for _, v := range health.Health {
if v.Name == "Elasticsearch" {
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
} else if v.Name == "NodeosRPC" {
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
}
}
// Error out if ether or both are zero.
if es_block == 0 || node_block == 0 {
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos "+
"block numbers (es: %d, eos: %d)", es_block, node_block)
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
return resp, msg
}
// Check if ES is behind or in the future.
diff := node_block - es_block
if diff > e.offset {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
} else if diff < -e.offset {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1*diff)
}
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
}