Skip to content

Commit

Permalink
refactor: remove types we can do without
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Nov 27, 2024
1 parent 63d3714 commit 2b86f61
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 77 deletions.
25 changes: 2 additions & 23 deletions go/summarize/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package summarize

import (
"encoding/json"
"errors"
"os"
"sort"
"strconv"
Expand All @@ -27,27 +26,7 @@ import (
"github.com/vitessio/vt/go/schema"
)

func readTraceFile(fi fileInfo) traceSummary {
switch fi.fileType {
case traceFile:
return readTracedQueryFile(fi.filename)
default:
panic("Unsupported file type")
}
}

func readFile(fi fileInfo) (func(s *Summary) error, error) {
switch fi.fileType {
case keysFile:
return readAnalysedQueryFile(fi.filename), nil
case dbInfoFile:
return readDBInfoFile(fi.filename), nil
default:
return nil, errors.New("unknown file format")
}
}

func readTracedQueryFile(fileName string) traceSummary {
func readTracedFile(fileName string) traceSummary {
c, err := os.ReadFile(fileName)
if err != nil {
exit("Error opening file: " + err.Error())
Expand Down Expand Up @@ -81,7 +60,7 @@ func readTracedQueryFile(fileName string) traceSummary {
}
}

func readAnalysedQueryFile(fileName string) func(s *Summary) error {
func readKeysFile(fileName string) func(s *Summary) error {
c, err := os.ReadFile(fileName)
if err != nil {
exit("Error opening file: " + err.Error())
Expand Down
20 changes: 5 additions & 15 deletions go/summarize/summarize-keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,12 @@ func TestSummarizeKeysFile(t *testing.T) {
sb := &strings.Builder{}
now := time.Date(2024, time.January, 1, 1, 2, 3, 0, time.UTC)

fnKeys, err := readFile(fileInfo{
filename: "../testdata/keys-log.json",
fileType: keysFile,
})
require.NoError(t, err)

fnSchemaInfo, err := readFile(fileInfo{
filename: "../testdata/keys-schema-info.json",
fileType: dbInfoFile,
})
require.NoError(t, err)
fnKeys := readKeysFile("../testdata/keys-log.json")
fnSchemaInfo := readDBInfoFile("../testdata/keys-schema-info.json")

s := NewSummary("")

err = fnKeys(s)
err := fnKeys(s)
require.NoError(t, err)

err = fnSchemaInfo(s)
Expand All @@ -111,14 +102,13 @@ func TestSummarizeKeysWithHotnessFile(t *testing.T) {

for _, metric := range tests {
t.Run(metric, func(t *testing.T) {
fn, err := readFile(fileInfo{filename: "../testdata/bigger_slow_query_log.json", fileType: keysFile})
require.NoError(t, err)
fn := readKeysFile("../testdata/bigger_slow_query_log.json")
sb := &strings.Builder{}
now := time.Date(2024, time.January, 1, 1, 2, 3, 0, time.UTC)

s := NewSummary(metric)

err = fn(s)
err := fn(s)
require.NoError(t, err)

s.PrintMarkdown(sb, now)
Expand Down
59 changes: 21 additions & 38 deletions go/summarize/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ type (
Name string
TracedQueries []TracedQuery
}

fileInfo struct {
filename string
fileType
}
)

func NewSummary(hotMetric string) *Summary {
Expand All @@ -59,55 +54,45 @@ func NewSummary(hotMetric string) *Summary {
}
}

type summaryWorker = func(s *Summary) error

func Run(files []string, hotMetric string) {
var filesToRead []fileInfo
var hasTrace bool
var traces []traceSummary
var workers []summaryWorker

for _, file := range files {
typ, _ := getFileType(file)
switch typ {
case dbInfoFile:
fmt.Printf("dbinfo file: %s\n", file)
filesToRead = append(filesToRead, fileInfo{filename: file, fileType: dbInfoFile})
workers = append(workers, readDBInfoFile(file))
case transactionFile:
fmt.Printf("transaction file: %s\n", file)
case traceFile:
filesToRead = append(filesToRead, fileInfo{filename: file, fileType: traceFile})
hasTrace = true
traces = append(traces, readTracedFile(file))
case keysFile:
filesToRead = append(filesToRead, fileInfo{filename: file, fileType: keysFile})
workers = append(workers, readKeysFile(file))
default:
panic("Unknown file type")
}
}
checkTraceConditions(hasTrace, filesToRead, hotMetric)

if hasTrace {
if len(filesToRead) == 2 {
compareTraces(os.Stdout, terminalWidth(), highlightQuery, readTraceFile(filesToRead[0]), readTraceFile(filesToRead[1]))
} else {
printTraceSummary(os.Stdout, terminalWidth(), highlightQuery, readTraceFile(filesToRead[0]))
}
checkTraceConditions(traces, workers, hotMetric)

if len(traces) == 2 {
compareTraces(os.Stdout, terminalWidth(), highlightQuery, traces[0], traces[1])
return
} else if len(traces) == 1 {
printTraceSummary(os.Stdout, terminalWidth(), highlightQuery, traces[0])
return
}

s := NewSummary(hotMetric)

rFuncs := make([]func(s *Summary) error, len(filesToRead))
var err error
for i, f := range filesToRead {
rFuncs[i], err = readFile(f)
for _, worker := range workers {
err := worker(s)
if err != nil {
exit(err.Error())
}
}

for _, f := range rFuncs {
err = f(s)
if err != nil {
panic(err)
}
}
s.PrintMarkdown(os.Stdout, time.Now())
}

Expand Down Expand Up @@ -148,16 +133,14 @@ func (s *Summary) AddTable(table *TableSummary) {
s.tables = append(s.tables, table)
}

func checkTraceConditions(hasTrace bool, filesToRead []fileInfo, hotMetric string) {
if !hasTrace {
func checkTraceConditions(traces []traceSummary, workers []summaryWorker, hotMetric string) {
if len(traces) == 0 {
return
}
for _, f := range filesToRead {
if f.fileType != traceFile {
panic("Trace files cannot be mixed with other file types")
}
if len(workers) > 0 {
panic("Trace files cannot be mixed with other file types")
}
if len(filesToRead) > 2 {
if len(traces) > 2 {
panic("Can only summarize up to two trace files at once")
}
if hotMetric != "" {
Expand Down
2 changes: 1 addition & 1 deletion go/summarize/summarize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Summary:
}

func TestSummarizeTraceFile(t *testing.T) {
tq := readTraceFile(fileInfo{filename: "../testdata/trace-log.json", fileType: traceFile})
tq := readTracedFile("../testdata/trace-log.json")
sb := &strings.Builder{}
printTraceSummary(sb, 80, noHighlight, tq)
expected := `Query: INSERT INTO region (R_REGIONKEY, R_NAME, R_COMMENT) VALUES (1, 'ASIA',...
Expand Down

0 comments on commit 2b86f61

Please sign in to comment.