Skip to content
Snippets Groups Projects
Commit cac2ca1b authored by Miguel Montes's avatar Miguel Montes
Browse files

Agregado formateo de salida de sealers para facilitar integración con telegraf

parent 9863e814
No related branches found
No related tags found
No related merge requests found
......@@ -153,16 +153,18 @@ func sealers() {
balance bool
header bool
format string
formatStr string
)
description = "Presenta la lista de selladores. Opcionalmente indica el último bloque sellado por cada uno."
description = "Presenta la lista de selladores. Opcionalmente presenta información sobre los selladores."
setFlags()
flags.Int64Var(&blockNumber, "block-number", latest, "Número del bloque en el cual se quiere conocer la lista de selladores (-1 para el último)")
flags.BoolVar(&lastBlock, "last-block", false, "Muestra el último bloque sellado por cada sellador, o 0 si un nodo no ha sellado en las últimas 5 rondas.")
flags.BoolVar(&timestamp, "timestamp", false, "Muestra el timestamp del sellado.")
flags.BoolVar(&difficulty, "difficulty", false, "Muestra la dificultad del sellado (1: fuera de turno, 2: en turno).")
flags.BoolVar(&balance, "balance", false, "Muestra el saldo del sellador (en Wei).")
flags.BoolVar(&header, "header", true, "Muestra un encabezado en cada columna.")
flags.BoolVar(&header, "header", false, "Muestra un encabezado en cada columna.")
flags.StringVar(&format, "format", "", "Formato del timestamp. Ignorado en formato json. Opciones: 'unix', 'rfc3339', 'long' ('YYYY-MM-DD hh:mm:ss'), 'short' ('hh:mm:ss') o un formato específico. Ejemplo 'DD/MM/YY hh.mm.ss'. También se admite el formato del paquete 'time' de go.")
flags.StringVar(&formatStr, "output-format", "", "Formato de la salida. Ignorado en formato json. Es una string en la que se reemplazan las ocurrencias de {sealer}, {last-block}, {timestamp}, {difficulty} y {balance} por sus respectivos valores.")
parseFlags(false)
if blockNumber == 0 {
util.Error("El bloque génesis no tiene firmantes")
......@@ -173,21 +175,41 @@ func sealers() {
defer node.Close()
blockNumber = node.BlockNumberInRange(blockNumber)
extended := lastBlock || timestamp || difficulty || balance
if extended {
sealers := node.SealersStatus(blockNumber)
if !extended && formatStr == "" {
sealers := node.SealersAtBlock(blockNumber)
sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] })
if json {
util.PrintJson(sealers)
return
}
var (
list []string
lastBlockLen int64
timestampLen int64
balanceLen int64
)
for sealer := range sealers {
list = append(list, sealer)
} else {
for _, sealer := range sealers {
fmt.Println(sealer)
}
}
return
}
sealers := node.SealersStatus(blockNumber)
if json {
util.PrintJson(sealers)
return
}
var (
list []string
lastBlockLen int64
timestampLen int64
balanceLen int64
)
for sealer := range sealers {
list = append(list, sealer)
}
if formatStr != "" {
sort.Strings(list)
formatStr = strings.Replace(formatStr, "{sealer}", "%[1]v", 1)
formatStr = strings.Replace(formatStr, "{last-block}", "%[3]v", 1)
formatStr = strings.Replace(formatStr, "{timestamp}", "%[5]v", 1)
formatStr = strings.Replace(formatStr, "{difficulty}", "%[6]v", 1)
formatStr = strings.Replace(formatStr, "{balance}", "%[8]v", 1)
timestamp = false
} else {
switch {
case lastBlock:
sort.Slice(list, func(i, j int) bool { return sealers[list[i]].LastBlock > sealers[list[j]].LastBlock })
......@@ -198,7 +220,7 @@ func sealers() {
case balance:
sort.Slice(list, func(i, j int) bool { return sealers[list[i]].Balance.Cmp(sealers[list[j]].Balance) > 0 })
}
formatStr := "%42v"
formatStr = "%42v"
if lastBlock {
formatStr += " %[2]*[3]v"
lastBlockLen = util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]].LastBlock, 10))))
......@@ -222,7 +244,6 @@ func sealers() {
balanceLen = util.Max(balanceLen, int64(len(s.Balance.String())))
}
}
formatStr += "\n"
if header {
fmt.Printf(formatStr,
fmt.Sprintf("%-24v", "Sealer"),
......@@ -231,29 +252,17 @@ func sealers() {
"Dif",
balanceLen, fmt.Sprintf("%*v", -balanceLen/2-4, "Balance"))
}
for _, sealer := range list {
var formatedTimestamp interface{}
s := sealers[sealer]
if timestamp {
t := int64(s.Time)
if len(format) > 0 && t > 0 {
formatedTimestamp = time.Unix(t, 0).Format(format)
} else {
formatedTimestamp = t
}
}
fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, s.Balance)
}
} else {
sealers := node.SealersAtBlock(blockNumber)
sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] })
if json {
util.PrintJson(sealers)
} else {
for _, sealer := range sealers {
fmt.Println(sealer)
}
}
formatStr += "\n"
for _, sealer := range list {
var formatedTimestamp interface{}
s := sealers[sealer]
t := int64(s.Time)
formatedTimestamp = t
if timestamp && len(format) > 0 && t > 0 {
formatedTimestamp = time.Unix(t, 0).Format(format)
}
fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, s.Balance)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment