1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-26 10:43:44 +02:00

src/api/v2.go: rename file to eosio_v2.go

This commit is contained in:
Henrik Hautakoski 2022-08-18 23:19:10 +02:00
parent c8361f6a44
commit 3fce1f558b
No known key found for this signature in database
GPG key ID: 608414D93E862CCD
2 changed files with 10 additions and 10 deletions

View file

@ -1,86 +0,0 @@
package api
import (
"fmt"
"github.com/eosswedenorg/eosio-api-healthcheck/src/utils"
"github.com/eosswedenorg-go/haproxy/agentcheck"
"github.com/eosswedenorg-go/eosapi"
)
type EosioV2 struct {
params eosapi.ReqParams
offset int64
}
func NewEosioV2(url string, host string, offset int64) EosioV2 {
return EosioV2{
params: eosapi.ReqParams{
Url: url,
Host: host,
},
offset: offset,
}
}
func (e EosioV2) LogInfo() LogParams {
p := LogParams{
"type", "eosio-v2",
"url", e.params.Url,
}
if len(e.params.Host) > 0 {
p.Add("host", e.params.Host)
}
p.Add("offset", e.offset)
return p
}
func (e EosioV2) Call() (agentcheck.Response, string) {
health, err := eosapi.GetHealth(e.params)
if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
return resp, err.Error()
}
// Check HTTP Status Code
if health.HTTPStatusCode > 299 {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
return resp, fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
}
// 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.Failed, "")
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"
}