From ef1b4d351c9acb013cbb625a616ef1f8825e4b6f Mon Sep 17 00:00:00 2001 From: Mehdi Hasan Date: Sat, 10 Aug 2019 18:50:10 +0600 Subject: [PATCH] Truncate query before showing --- README.md | 10 +++++++++- kill-mysql-query.go | 11 +++++++---- mysql/query.go | 33 ++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index dd1f22f..25ed561 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # kill-mysql-query -`kill-mysql-query` is a cli that interactively shows long running queries in MySQL database and provide option kill them one by one. +``` + _____ ____ +/ \ | o | +| |/ ___\| +|_________/ +|_|_| |_|_| +``` + +`kill-mysql-query` interactively shows long running queries in MySQL database and provide option kill them one by one. 👉 Great for firefighting 🔥🚨🚒 diff --git a/kill-mysql-query.go b/kill-mysql-query.go index f4c4d21..89fa076 100644 --- a/kill-mysql-query.go +++ b/kill-mysql-query.go @@ -72,6 +72,9 @@ func main() { } showKillPrompt(longQueries, dbConn, config) + fmt.Println() + fmt.Println() + fmt.Println() fmt.Println("-----------------------------------") fmt.Println("💫 Rechecking...") fmt.Println("-----------------------------------") @@ -154,9 +157,9 @@ func showKillPrompt(longQueries []mysql.MysqlProcess, dbConn *sqlx.DB, config co if len(longQueries) > 1 { templates := &promptui.SelectTemplates{ Label: "{{ . }}?", - Active: "👉 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .Info.String | cyan }}", - Inactive: " DB `{{ .DB }}`, Running Time: {{ .Time }}s, Query: {{ .Info.String }}", - Selected: "💥 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .Info.String | cyan }}", + Active: "👉 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .TruncatedQuery | cyan }}", + Inactive: " DB `{{ .DB }}`, Running Time: {{ .Time }}s, Query: {{ .TruncatedQuery }}", + Selected: "💥 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .TruncatedQuery | cyan }}", Details: ` --------- QUERY ---------- {{ "ID:" | faint }} {{ .ID }} @@ -164,7 +167,7 @@ func showKillPrompt(longQueries []mysql.MysqlProcess, dbConn *sqlx.DB, config co {{ "State:" | faint }} {{ .State.String }} {{ "Command:" | faint }} {{ .Command }} {{ "Running Time:" | faint }} {{ .Time }} second(s) -{{ "Query:" | faint }} {{ .Info.String }}`, +{{ "Query:" | faint }} {{ .TruncatedQuery }}`, } prompt := promptui.Select{ diff --git a/mysql/query.go b/mysql/query.go index 797328b..f5d8016 100644 --- a/mysql/query.go +++ b/mysql/query.go @@ -4,6 +4,7 @@ import ( "bytes" "database/sql" "fmt" + "regexp" "text/template" "github.com/mugli/go-kill-mysql-query/configuration" @@ -46,13 +47,14 @@ type queryParams struct { } type MysqlProcess struct { - ID int `db:"ID"` - KillCommand string `db:"KILL_COMMAND"` - DB string `db:"DB"` - State sql.NullString `db:"STATE"` - Command string `db:"COMMAND"` - Time int `db:"TIME"` - Info sql.NullString `db:"INFO"` + ID int `db:"ID"` + KillCommand string `db:"KILL_COMMAND"` + DB string `db:"DB"` + State sql.NullString `db:"STATE"` + Command string `db:"COMMAND"` + Time int `db:"TIME"` + Info sql.NullString `db:"INFO"` + TruncatedQuery string } func generateQuery(config configuration.Config) (string, error) { @@ -87,6 +89,20 @@ func generateQuery(config configuration.Config) (string, error) { return queryBytes.String(), nil } +func truncateString(str string, num int) string { + // Remove newlines + re := regexp.MustCompile(`\r?\n`) + retval := re.ReplaceAllString(str, " ") + + if len(retval) > num { + if num > 3 { + num -= 3 + } + retval = retval[0:num] + "..." + } + return retval +} + func GetLongRunningQueries(dbConn *sqlx.DB, config configuration.Config) ([]MysqlProcess, error) { fmt.Println("🕴 Looking for slow queries...") @@ -100,6 +116,9 @@ func GetLongRunningQueries(dbConn *sqlx.DB, config configuration.Config) ([]Mysq for rows.Next() { longQ := MysqlProcess{} rows.StructScan(&longQ) + + longQ.TruncatedQuery = truncateString(longQ.Info.String, 50) + longQueries = append(longQueries, longQ) } rows.Close()