Skip to content

Commit

Permalink
feat: fix json indentation
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Dec 10, 2024
1 parent 3ad20d6 commit 15046b0
Show file tree
Hide file tree
Showing 4 changed files with 524 additions and 516 deletions.
4 changes: 4 additions & 0 deletions go/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (m *MarkDown) PrintTable(headers []string, rows [][]string) {
m.NewLine()
}

func (m *MarkDown) Write(p []byte) (n int, err error) {
return m.buffer.Write(p)
}

func (m *MarkDown) WriteTo(w io.Writer) (n int64, err error) {
return m.buffer.WriteTo(w)
}
38 changes: 26 additions & 12 deletions go/summarize/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package summarize

import (
"bytes"
"encoding/json"
"fmt"
"slices"
"sort"
Expand Down Expand Up @@ -227,10 +229,10 @@ func renderTransactions(md *markdown.MarkDown, transactions []TransactionSummary
}
}

func renderPlans(md *markdown.MarkDown, analysis PlanAnalysis) {
func renderPlansSection(md *markdown.MarkDown, analysis PlanAnalysis) error {
sum := analysis.PassThrough + analysis.SimpleRouted + analysis.Complex + analysis.Unplannable
if sum == 0 {
return
return nil
}

md.PrintHeader("Query Planning Report", 2)
Expand All @@ -246,23 +248,35 @@ func renderPlans(md *markdown.MarkDown, analysis PlanAnalysis) {
md.PrintTable(headers, rows)
md.NewLine()

for i, query := range analysis.simpleRouted {
if i == 0 {
md.Printf("# %s Queries\n\n", planalyze.SimpleRouted.String())
}
md.Printf("## Query\n\n```sql\n%s\n```\n\n", query.QueryStructure)
md.Printf("## Plan\n\n```json\n%s\n```\n\n", query.PlanOutput)
md.NewLine()
err := renderQueryPlans(md, analysis.simpleRouted, planalyze.SimpleRouted.String())
if err != nil {
return err
}
return renderQueryPlans(md, analysis.complex, planalyze.Complex.String())
}

for i, query := range analysis.complex {
func renderQueryPlans(md *markdown.MarkDown, queries []planalyze.AnalyzedQuery, title string) error {
for i, query := range queries {
if i == 0 {
md.Printf("# %s Queries\n\n", planalyze.Complex.String())
md.Printf("# %s Queries\n\n", title)
}
md.Printf("## Query\n\n```sql\n%s\n```\n\n", query.QueryStructure)
md.Printf("## Plan\n\n```json\n%s\n```\n\n", query.PlanOutput)
md.Println("## Plan\n\n```json")

// Indent the JSON output. If we don't do this, the json will be indented all wrong
var buf bytes.Buffer
if err := json.Indent(&buf, query.PlanOutput, "", " "); err != nil {
return err
}

if _, err := md.Write(buf.Bytes()); err != nil {
return err
}
md.NewLine()
md.Println("```")
md.NewLine()
}
return nil
}

func uniquefy(s []string) []string {
Expand Down
7 changes: 5 additions & 2 deletions go/summarize/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ func (s *Summary) PrintMarkdown(out io.Writer, now time.Time) error {
s.analyzedFiles[i] = "`" + file + "`"
}
md.Printf(msg, now.Format(time.DateTime), filePlural, strings.Join(s.analyzedFiles, ", "))
renderPlans(md, s.planAnalysis)
err := renderPlansSection(md, s.planAnalysis)
if err != nil {
return err
}
renderHotQueries(md, s.hotQueries, s.hotQueryFn)
renderTableUsage(md, s.tables, s.hasRowCount)
renderTablesJoined(md, s)
renderTransactions(md, s.transactions)
renderFailures(md, s.failures)

_, err := md.WriteTo(out)
_, err = md.WriteTo(out)
if err != nil {
return fmt.Errorf("error writing markdown: %w", err)
}
Expand Down
Loading

0 comments on commit 15046b0

Please sign in to comment.