Skip to content

Commit

Permalink
Truncate query before showing
Browse files Browse the repository at this point in the history
  • Loading branch information
mugli committed Aug 10, 2019
1 parent 0882d11 commit ef1b4d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
10 changes: 9 additions & 1 deletion 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 🔥🚨🚒

Expand Down
11 changes: 7 additions & 4 deletions kill-mysql-query.go
Expand Up @@ -72,6 +72,9 @@ func main() {
}

showKillPrompt(longQueries, dbConn, config)
fmt.Println()
fmt.Println()
fmt.Println()
fmt.Println("-----------------------------------")
fmt.Println("💫 Rechecking...")
fmt.Println("-----------------------------------")
Expand Down Expand Up @@ -154,17 +157,17 @@ 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 }}
{{ "DB:" | faint }} {{ .DB }}
{{ "State:" | faint }} {{ .State.String }}
{{ "Command:" | faint }} {{ .Command }}
{{ "Running Time:" | faint }} {{ .Time }} second(s)
{{ "Query:" | faint }} {{ .Info.String }}`,
{{ "Query:" | faint }} {{ .TruncatedQuery }}`,
}

prompt := promptui.Select{
Expand Down
33 changes: 26 additions & 7 deletions mysql/query.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql"
"fmt"
"regexp"
"text/template"

"github.com/mugli/go-kill-mysql-query/configuration"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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...")

Expand All @@ -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()
Expand Down

0 comments on commit ef1b4d3

Please sign in to comment.