Skip to content

Commit

Permalink
Simplify details table
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Gartner committed Oct 24, 2019
1 parent 36ccb55 commit 41a07f4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
76 changes: 56 additions & 20 deletions pkg/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ type Flame struct {
Time float64 `json:"time"`
Detail string `json:"detail"`
Color string `json:"color"`
InitPlan bool
InitPlan bool `json:"init_plan"`
Children []Flame `json:"children"`
}

const tableHeader = `<table class="table table-striped table-bordered"><tbody>`
const rowTemplate = "<tr><th>%s</th><td>%v</td></tr>"
const tableFooter = `</tbody></table>`

const detailTemplate = "<span>%s</span>"

const colorPlan = "#00C05A"
const colorInit = "#C0C0C0"

Expand Down Expand Up @@ -55,7 +61,7 @@ func buildFlame(p plan.Plan) Flame {
Name: "Query Planning",
Value: p.PlanningTime,
Time: p.PlanningTime,
Detail: "Time to generate the query plan",
Detail: fmt.Sprintf(detailTemplate, "Time to generate the query plan"),
Color: colorPlan,
}

Expand All @@ -65,7 +71,7 @@ func buildFlame(p plan.Plan) Flame {
Name: "Total",
Value: planningFlame.Value + executionFlame.Value,
Time: planningFlame.Time + executionFlame.Time,
Detail: "This node includes planning and execution time",
Detail: fmt.Sprintf(detailTemplate, "Includes planning and execution time"),
Children: []Flame{planningFlame, executionFlame},
}
}
Expand Down Expand Up @@ -117,23 +123,53 @@ func name(n plan.Node) string {

func detail(n plan.Node) string {
var b strings.Builder
b.WriteString(`<table class="table table-striped table-bordered"><tbody>`)

rowTemplate := "<tr><th>%s</th><td>%v</td></tr>"

fmt.Fprintf(&b, rowTemplate, "Parent Relationship", n.ParentRelationship)
fmt.Fprintf(&b, rowTemplate, "Filter", n.Filter)
fmt.Fprintf(&b, rowTemplate, "Join Filter", n.JoinFilter)
fmt.Fprintf(&b, rowTemplate, "Hash Cond", n.HashCond)
fmt.Fprintf(&b, rowTemplate, "Index Cond", n.IndexCond)
fmt.Fprintf(&b, rowTemplate, "Recheck Cond", n.RecheckCond)
fmt.Fprintf(&b, rowTemplate, "Buffers Shared Hit", n.BuffersHit)
fmt.Fprintf(&b, rowTemplate, "Buffers Shared Read", n.BuffersRead)
fmt.Fprintf(&b, rowTemplate, "Hash Buckets", n.HashBuckets)
fmt.Fprintf(&b, rowTemplate, "Hash Batches", n.HashBatches)
fmt.Fprintf(&b, rowTemplate, "Memory Usage", fmt.Sprintf("%vkB", n.HashBatches))

b.WriteString(`</tbody></table>`)
b.WriteString(tableHeader)

if n.ParentRelationship != "" {
fmt.Fprintf(&b, rowTemplate, "Parent Relationship", n.ParentRelationship)
}

if n.Filter != "" {
fmt.Fprintf(&b, rowTemplate, "Filter", n.Filter)
}

if n.JoinFilter != "" {
fmt.Fprintf(&b, rowTemplate, "Join Filter", n.JoinFilter)
}

if n.HashCond != "" {
fmt.Fprintf(&b, rowTemplate, "Hash Cond", n.HashCond)
}

if n.IndexCond != "" {
fmt.Fprintf(&b, rowTemplate, "Index Cond", n.IndexCond)
}

if n.RecheckCond != "" {
fmt.Fprintf(&b, rowTemplate, "Recheck Cond", n.RecheckCond)
}

if n.BuffersHit != 0 {
fmt.Fprintf(&b, rowTemplate, "Buffers Shared Hit", n.BuffersHit)
}

if n.BuffersRead != 0 {
fmt.Fprintf(&b, rowTemplate, "Buffers Shared Read", n.BuffersRead)
}

if n.HashBuckets != 0 {
fmt.Fprintf(&b, rowTemplate, "Hash Buckets", n.HashBuckets)
}

if n.HashBatches != 0 {
fmt.Fprintf(&b, rowTemplate, "Hash Batches", n.HashBatches)
}

if n.MemoryUsage != 0 {
fmt.Fprintf(&b, rowTemplate, "Memory Usage", fmt.Sprintf("%vkB", n.MemoryUsage))
}

b.WriteString(tableFooter)

return b.String()
}
10 changes: 4 additions & 6 deletions pkg/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ func Test_buildFlame(t *testing.T) {
assert.Equal(t, "Total", f.Name)
assert.Equal(t, 0.133, f.Value)
assert.Equal(t, 0.133, f.Time)
assert.Equal(t, "<span>Includes planning and execution time</span>", f.Detail)

assert.Equal(t, "Query Planning", f.Children[0].Name)
assert.Equal(t, colorPlan, f.Children[0].Color)
assert.Equal(t, 0.01, f.Children[0].Value)
assert.Equal(t, 0.01, f.Children[0].Time)
assert.Equal(t, "<span>Time to generate the query plan</span>", f.Children[0].Detail)

assert.Equal(t, "Limit", f.Children[1].Name)
assert.Equal(t, 0.123, f.Children[1].Value)
Expand Down Expand Up @@ -149,24 +151,20 @@ func Test_detail(t *testing.T) {
Filter: "(id = 123)",
BuffersHit: 8,
BuffersRead: 5,
MemoryUsage: 12,
HashBuckets: 1024,
HashBatches: 1,
MemoryUsage: 12,
}

expected := strings.Join([]string{
"<table class=\"table table-striped table-bordered\"><tbody>",
"<tr><th>Parent Relationship</th><td>InitPlan</td></tr>",
"<tr><th>Filter</th><td>(id = 123)</td></tr>",
"<tr><th>Join Filter</th><td></td></tr>",
"<tr><th>Hash Cond</th><td></td></tr>",
"<tr><th>Index Cond</th><td></td></tr>",
"<tr><th>Recheck Cond</th><td></td></tr>",
"<tr><th>Buffers Shared Hit</th><td>8</td></tr>",
"<tr><th>Buffers Shared Read</th><td>5</td></tr>",
"<tr><th>Hash Buckets</th><td>1024</td></tr>",
"<tr><th>Hash Batches</th><td>1</td></tr>",
"<tr><th>Memory Usage</th><td>1kB</td></tr>",
"<tr><th>Memory Usage</th><td>12kB</td></tr>",
"</tbody></table>",
}, "")

Expand Down
5 changes: 5 additions & 0 deletions pkg/html/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const templateHTML = `
table th {
width: 250px;
}
span {
font-weight: 700;
font-family: monospace;
}
</style>
<title>pg_flame</title>
Expand Down
9 changes: 5 additions & 4 deletions pkg/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ func New(r io.Reader) (error, Plan) {
var plans []Plan

err := json.NewDecoder(r).Decode(&plans)
var e *json.UnmarshalTypeError
if errors.As(err, &e) {
return ErrInvalidPlanJSON, Plan{}
} else if err != nil {
if err != nil {
var e *json.UnmarshalTypeError
if errors.As(err, &e) {
err = ErrInvalidPlanJSON
}
return err, Plan{}
}

Expand Down

0 comments on commit 41a07f4

Please sign in to comment.