Skip to content

Commit 2e590ce

Browse files
authored
diagnostics: move E3 changes to E2 (#10806)
Merged all the work done from main branch to keep diagnostics up to date.
1 parent 79d0182 commit 2e590ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1940
-369
lines changed
129 KB
Loading
154 KB
Loading
116 KB
Loading
49.2 KB
Loading
221 KB
Loading
288 KB
Loading
180 KB
Loading
17.6 KB
Loading
50.9 KB
Loading

cmd/diag/db/db.go

Lines changed: 94 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,122 @@
11
package db
22

33
import (
4-
"encoding/json"
54
"fmt"
5+
"os"
66

7+
"github.com/jedib0t/go-pretty/v6/table"
8+
"github.com/jedib0t/go-pretty/v6/text"
79
"github.com/ledgerwatch/erigon-lib/common"
810
"github.com/ledgerwatch/erigon/cmd/diag/flags"
911
"github.com/ledgerwatch/erigon/cmd/diag/util"
1012
"github.com/urfave/cli/v2"
1113
)
1214

1315
type DBInfo struct {
14-
name string
15-
tables []BDTableInfo
16-
count int
17-
size string
16+
name string `header:"DB Name"`
17+
tables []BDTableInfo `header:"Tables"`
18+
count int `header:"Keys Count"`
19+
size string `header:"Size"`
1820
}
1921

2022
type BDTableInfo struct {
21-
Name string
22-
Count int
23-
Size uint64
23+
Name string `header:"Table Name"`
24+
Count int `header:"Keys Count"`
25+
Size uint64 `header:"Size"`
2426
}
2527

28+
var (
29+
DBPopulatedFlag = cli.BoolFlag{
30+
Name: "db.appearance.populated",
31+
Aliases: []string{"dbap"},
32+
Usage: "Print populated table content only",
33+
Required: false,
34+
Value: false,
35+
}
36+
37+
DBNameFlag = cli.StringFlag{
38+
Name: "db.name",
39+
Aliases: []string{"dbn"},
40+
Usage: "DB name to print info about. If not set, all dbs will be printed.",
41+
Required: false,
42+
Value: "",
43+
}
44+
)
45+
2646
var Command = cli.Command{
47+
Action: startPrintDBsInfo,
2748
Name: "databases",
2849
Aliases: []string{"dbs"},
50+
Usage: "Print database tables info.",
2951
ArgsUsage: "",
30-
Subcommands: []*cli.Command{
31-
{
32-
Name: "all",
33-
Aliases: []string{"a"},
34-
Action: printAllDBsInfo,
35-
Usage: "Print database tables info.",
36-
ArgsUsage: "",
37-
Flags: []cli.Flag{
38-
&flags.DebugURLFlag,
39-
&flags.OutputFlag,
40-
},
41-
},
42-
{
43-
Name: "populated",
44-
Aliases: []string{"pop"},
45-
Action: printPopuplatedDBsInfo,
46-
Usage: "Print database tables info which is not empty.",
47-
ArgsUsage: "",
48-
Flags: []cli.Flag{
49-
&flags.DebugURLFlag,
50-
&flags.OutputFlag,
51-
},
52-
},
52+
Flags: []cli.Flag{
53+
&flags.DebugURLFlag,
54+
&flags.OutputFlag,
55+
&DBPopulatedFlag,
56+
&DBNameFlag,
5357
},
5458
Description: ``,
5559
}
5660

57-
func printAllDBsInfo(cliCtx *cli.Context) error {
58-
data, err := AllDBsInfo(cliCtx)
61+
func startPrintDBsInfo(cliCtx *cli.Context) error {
62+
data, err := DBsInfo(cliCtx)
5963
if err != nil {
60-
return err
61-
}
62-
63-
switch cliCtx.String(flags.OutputFlag.Name) {
64-
case "json":
65-
bytes, err := json.Marshal(data)
66-
if err != nil {
67-
return err
68-
}
69-
70-
fmt.Println(string(bytes))
71-
72-
case "text":
73-
printDBsInfo(data)
64+
util.RenderError(err)
65+
return nil
7466
}
7567

76-
return nil
77-
}
68+
dbToPrint := cliCtx.String(DBNameFlag.Name)
7869

79-
func printPopuplatedDBsInfo(cliCtx *cli.Context) error {
80-
data, err := AllDBsInfo(cliCtx)
81-
if err != nil {
82-
return err
83-
}
84-
85-
// filter out empty tables
86-
for i := 0; i < len(data); i++ {
87-
tables := data[i].tables
88-
for j := 0; j < len(tables); j++ {
89-
if tables[j].Count == 0 {
90-
tables = append(tables[:j], tables[j+1:]...)
91-
j--
70+
if dbToPrint != "" {
71+
for _, db := range data {
72+
if db.name == dbToPrint {
73+
printDBsInfo([]DBInfo{db})
74+
return nil
9275
}
9376
}
94-
data[i].tables = tables
95-
}
9677

97-
//filter out empty dbs
98-
for i := 0; i < len(data); i++ {
99-
if len(data[i].tables) == 0 {
100-
data = append(data[:i], data[i+1:]...)
101-
i--
102-
}
78+
fmt.Printf("DB %s not found\n", dbToPrint)
79+
return nil
10380
}
10481

105-
switch cliCtx.String(flags.OutputFlag.Name) {
106-
case "json":
107-
bytes, err := json.Marshal(data)
108-
if err != nil {
109-
return err
110-
}
111-
112-
fmt.Println(string(bytes))
113-
114-
case "text":
115-
printDBsInfo(data)
116-
}
82+
printDBsInfo(data)
11783

84+
txt := text.Colors{text.BgGreen, text.Bold}
85+
fmt.Println(txt.Sprint("To get detailed info about Erigon node state use 'diag ui' command."))
11886
return nil
11987
}
12088

12189
func printDBsInfo(data []DBInfo) {
122-
fmt.Print("\n")
123-
fmt.Println("------------------------DBs-------------------")
124-
fmt.Println("DB Name Keys Count Size")
125-
fmt.Println("----------------------------------------------")
126-
for _, db := range data {
127-
nLen := len(db.name)
128-
fmt.Print(db.name)
129-
for i := 0; i < 20-nLen; i++ {
130-
fmt.Print(" ")
131-
}
90+
txt := text.Colors{text.FgBlue, text.Bold}
91+
fmt.Println(txt.Sprint("Databases Info:"))
92+
t := table.NewWriter()
93+
t.SetOutputMirror(os.Stdout)
94+
t.AppendHeader(table.Row{"DB Name", "Keys Count", "Size"})
13295

133-
fmt.Printf("%d", db.count)
134-
for i := 0; i < 22-len(fmt.Sprint(db.count)); i++ {
135-
fmt.Print(" ")
136-
}
137-
138-
fmt.Printf("%s\n", db.size)
139-
}
140-
141-
fmt.Print("\n")
142-
fmt.Print("\n")
143-
144-
//db := data[0]
14596
for _, db := range data {
97+
t.AppendRow(table.Row{db.name, db.count, db.size})
98+
}
14699

147-
nl := len(db.name)
148-
149-
dashCount := (60 - nl) / 2
150-
151-
for i := 0; i < dashCount; i++ {
152-
fmt.Print("-")
153-
}
154-
155-
//fmt.Printf(" %s ", db.name)
156-
fmt.Print("\033[1m " + db.name + " \033[0m")
100+
t.AppendSeparator()
101+
t.Render()
157102

158-
for i := 0; i < dashCount; i++ {
159-
fmt.Print("-")
160-
}
161-
fmt.Print("\n")
103+
t.ResetHeaders()
104+
t.AppendHeader(table.Row{"Table Name", "Keys Count", "Size"})
162105

163-
//fmt.Println("------------------------------------------------------------")
164-
fmt.Println("Table Name Keys Count Size")
165-
for i := 0; i < 60; i++ {
166-
fmt.Print("-")
106+
for _, db := range data {
107+
t.ResetRows()
108+
fmt.Println(txt.Sprint("DB " + db.name + " tables:"))
109+
for _, tbl := range db.tables {
110+
t.AppendRow(table.Row{tbl.Name, tbl.Count, common.ByteCount(tbl.Size)})
167111
}
168-
fmt.Print("\n")
169112

170-
for _, table := range db.tables {
171-
nLen := len(table.Name)
172-
fmt.Printf("%s", table.Name)
173-
for i := 0; i < 34-nLen; i++ {
174-
fmt.Print(" ")
175-
}
176-
177-
fmt.Printf("%d", table.Count)
178-
for i := 0; i < 22-len(fmt.Sprint(table.Count)); i++ {
179-
fmt.Print(" ")
180-
}
181-
182-
fmt.Printf("%s\n", common.ByteCount(table.Size))
183-
}
113+
t.AppendSeparator()
114+
t.Render()
184115
fmt.Print("\n")
185116
}
186117
}
187118

188-
func AllDBsInfo(cliCtx *cli.Context) ([]DBInfo, error) {
119+
func DBsInfo(cliCtx *cli.Context) ([]DBInfo, error) {
189120
data := make([]DBInfo, 0)
190121

191122
dbsNames, err := getAllDbsNames(cliCtx)
@@ -215,12 +146,35 @@ func AllDBsInfo(cliCtx *cli.Context) ([]DBInfo, error) {
215146
data = append(data, dbInfo)
216147
}
217148

149+
// filter out empty tables
150+
if cliCtx.Bool(DBPopulatedFlag.Name) {
151+
// filter out empty tables
152+
for i := 0; i < len(data); i++ {
153+
tables := data[i].tables
154+
for j := 0; j < len(tables); j++ {
155+
if tables[j].Count == 0 {
156+
tables = append(tables[:j], tables[j+1:]...)
157+
j--
158+
}
159+
}
160+
data[i].tables = tables
161+
}
162+
163+
//filter out empty dbs
164+
for i := 0; i < len(data); i++ {
165+
if len(data[i].tables) == 0 {
166+
data = append(data[:i], data[i+1:]...)
167+
i--
168+
}
169+
}
170+
}
171+
218172
return data, nil
219173
}
220174

221175
func getAllDbsNames(cliCtx *cli.Context) ([]string, error) {
222176
var data []string
223-
url := "http://" + cliCtx.String(flags.DebugURLFlag.Name) + "/debug/dbs"
177+
url := "http://" + cliCtx.String(flags.DebugURLFlag.Name) + flags.ApiPath + "/dbs"
224178

225179
err := util.MakeHttpGetCall(cliCtx.Context, url, &data)
226180
if err != nil {
@@ -232,7 +186,7 @@ func getAllDbsNames(cliCtx *cli.Context) ([]string, error) {
232186

233187
func getDb(cliCtx *cli.Context, dbName string) ([]BDTableInfo, error) {
234188
var data []BDTableInfo
235-
url := "http://" + cliCtx.String(flags.DebugURLFlag.Name) + "/debug/dbs/" + dbName + "/tables"
189+
url := "http://" + cliCtx.String(flags.DebugURLFlag.Name) + flags.ApiPath + "/dbs/" + dbName + "/tables"
236190

237191
err := util.MakeHttpGetCall(cliCtx.Context, url, &data)
238192
if err != nil {

0 commit comments

Comments
 (0)