This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicola Strappazzon C
committed
Nov 5, 2023
1 parent
9fdc6ea
commit a48153a
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package digest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/debeando/go-common/file" | ||
"github.com/debeando/go-common/mysql/sql/digest" | ||
"github.com/debeando/go-common/mysql/sql/parser/slow" | ||
"github.com/debeando/go-common/table" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "digest [FILENAME]", | ||
Short: "Analyze MySQL queries from slow query log.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
cmd.Help() | ||
return | ||
} | ||
|
||
if !file.Exist(args[0]) { | ||
fmt.Println(fmt.Sprintf("File not exist: %s", args[0])) | ||
return | ||
} | ||
|
||
queries := &digest.List{} | ||
channelIn := make(chan string) | ||
channelOut := make(chan string) | ||
|
||
defer close(channelIn) | ||
|
||
go slow.LogsParser(channelIn, channelOut) | ||
|
||
go func() { | ||
defer close(channelOut) | ||
for log := range channelOut { | ||
queries.Add(slow.QueryParser(log)) | ||
} | ||
}() | ||
|
||
file.ReadLineByLine(args[0], func(line string){ | ||
channelIn <- line | ||
}) | ||
|
||
tbl := table.New("DIGEST_ID", "SCORE", "COUNT", "Q. TIME", "L. TIME", "R. SENT", "R. EXAMINED") | ||
tbl.SetFirstColumnAlignment(table.Right) | ||
|
||
fmt.Println(fmt.Sprintf("Summary:\n- Analizded: %d\n- Unique: %d\n- Total: %d", 0, queries.Unique(), queries.Count())) | ||
|
||
queries.SortByScore() | ||
|
||
for index := range *queries { | ||
tbl.AddRow( | ||
(*queries)[index].ID, | ||
formatScore((*queries)[index].Score), | ||
(*queries)[index].Count, | ||
formatTime((*queries)[index].Time.Query), | ||
formatTime((*queries)[index].Time.Lock), | ||
(*queries)[index].Rows.Sent, | ||
(*queries)[index].Rows.Examined, | ||
) | ||
} | ||
tbl.Print() | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func formatTime(seconds float64) string { | ||
if seconds < 1e-6 { | ||
return fmt.Sprintf("%06.3f", seconds*1e6) | ||
} else if seconds < 1e-3 { | ||
return fmt.Sprintf("%06.3f", seconds*1e3) | ||
} | ||
return fmt.Sprintf("%06.3f", seconds) | ||
} | ||
|
||
func formatScore(score float64) string { | ||
return fmt.Sprintf("%03d", int(((score - 0.5) / (517550 - 0.5)) * 99 + 1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package mysql | ||
|
||
import ( | ||
"zenit/mysql/digest" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "mysql", | ||
Short: "MySQL commands to facilitate administration.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand(digest.NewCommand()) | ||
|
||
return cmd | ||
} |