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() { ...@@ -153,16 +153,18 @@ func sealers() {
balance bool balance bool
header bool header bool
format string 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() 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.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(&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(&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(&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(&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(&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) parseFlags(false)
if blockNumber == 0 { if blockNumber == 0 {
util.Error("El bloque génesis no tiene firmantes") util.Error("El bloque génesis no tiene firmantes")
...@@ -173,21 +175,41 @@ func sealers() { ...@@ -173,21 +175,41 @@ func sealers() {
defer node.Close() defer node.Close()
blockNumber = node.BlockNumberInRange(blockNumber) blockNumber = node.BlockNumberInRange(blockNumber)
extended := lastBlock || timestamp || difficulty || balance extended := lastBlock || timestamp || difficulty || balance
if extended { if !extended && formatStr == "" {
sealers := node.SealersStatus(blockNumber) sealers := node.SealersAtBlock(blockNumber)
sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] })
if json { if json {
util.PrintJson(sealers) util.PrintJson(sealers)
return } else {
} for _, sealer := range sealers {
var ( fmt.Println(sealer)
list []string }
lastBlockLen int64
timestampLen int64
balanceLen int64
)
for sealer := range sealers {
list = append(list, 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 { switch {
case lastBlock: case lastBlock:
sort.Slice(list, func(i, j int) bool { return sealers[list[i]].LastBlock > sealers[list[j]].LastBlock }) sort.Slice(list, func(i, j int) bool { return sealers[list[i]].LastBlock > sealers[list[j]].LastBlock })
...@@ -198,7 +220,7 @@ func sealers() { ...@@ -198,7 +220,7 @@ func sealers() {
case balance: case balance:
sort.Slice(list, func(i, j int) bool { return sealers[list[i]].Balance.Cmp(sealers[list[j]].Balance) > 0 }) 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 { if lastBlock {
formatStr += " %[2]*[3]v" formatStr += " %[2]*[3]v"
lastBlockLen = util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]].LastBlock, 10)))) lastBlockLen = util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]].LastBlock, 10))))
...@@ -222,7 +244,6 @@ func sealers() { ...@@ -222,7 +244,6 @@ func sealers() {
balanceLen = util.Max(balanceLen, int64(len(s.Balance.String()))) balanceLen = util.Max(balanceLen, int64(len(s.Balance.String())))
} }
} }
formatStr += "\n"
if header { if header {
fmt.Printf(formatStr, fmt.Printf(formatStr,
fmt.Sprintf("%-24v", "Sealer"), fmt.Sprintf("%-24v", "Sealer"),
...@@ -231,29 +252,17 @@ func sealers() { ...@@ -231,29 +252,17 @@ func sealers() {
"Dif", "Dif",
balanceLen, fmt.Sprintf("%*v", -balanceLen/2-4, "Balance")) balanceLen, fmt.Sprintf("%*v", -balanceLen/2-4, "Balance"))
} }
for _, sealer := range list { }
var formatedTimestamp interface{} formatStr += "\n"
s := sealers[sealer] for _, sealer := range list {
if timestamp { var formatedTimestamp interface{}
t := int64(s.Time) s := sealers[sealer]
if len(format) > 0 && t > 0 { t := int64(s.Time)
formatedTimestamp = time.Unix(t, 0).Format(format) formatedTimestamp = t
} else { if timestamp && len(format) > 0 && t > 0 {
formatedTimestamp = t formatedTimestamp = time.Unix(t, 0).Format(format)
}
}
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)
}
} }
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