-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.go
55 lines (43 loc) · 1.46 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"os"
"time"
"github.com/chainbound/cbctl/api"
"github.com/jedib0t/go-pretty/v6/table"
)
func printMessageTrace(trace []*api.TraceEntry, showSource bool) {
if len(trace) == 0 {
fmt.Println("No trace entries found")
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight)
if showSource {
t.AppendHeader(table.Row{"Timestamp", "Node ID", "Region", "Observation Type", "Source"})
} else {
t.AppendHeader(table.Row{"Timestamp", "Node ID", "Region", "Observation Type"})
}
firstTimestamp := trace[0].Timestamp / 1000
fiberPropagationTime := 0
p2pPropagationTime := 0
for _, entry := range trace {
id := entry.NodeID
if entry.ObservationType == "fiber" {
fiberPropagationTime = int(entry.Timestamp/1000) - int(firstTimestamp)
} else if entry.ObservationType == "p2p" {
p2pPropagationTime = int(entry.Timestamp/1000) - int(firstTimestamp)
}
timestamp := time.Unix(0, int64(entry.Timestamp/1000)*int64(time.Millisecond))
timestampFmt := timestamp.Format("2006-01-02 15:04:05.000")
if showSource {
t.AppendRow(table.Row{timestampFmt, id, entry.Region, entry.ObservationType, entry.Source})
} else {
t.AppendRow(table.Row{timestampFmt, id, entry.Region, entry.ObservationType})
}
}
t.AppendFooter(table.Row{"fiber propagation time", fmt.Sprint(fiberPropagationTime) + "ms"})
t.AppendFooter(table.Row{"p2p propagation time", fmt.Sprint(p2pPropagationTime) + "ms"})
t.Render()
}