-
Notifications
You must be signed in to change notification settings - Fork 0
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 1, 2023
1 parent
5d046b3
commit 7a0caae
Showing
9 changed files
with
224 additions
and
123 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,4 @@ Revert replacement: | |
```bash | ||
go mod edit -dropreplace github.com/debeando/go-common | ||
go get -u | ||
go mod tidy | ||
``` |
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
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,45 @@ | ||
package slow | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func LogsParser(in <-chan string, out chan<- string) { | ||
var buffer string | ||
var isHeader bool | ||
var isQuery bool | ||
|
||
for line := range in { | ||
e := "" | ||
l := len(line) | ||
|
||
if isQuery == false && strings.HasPrefix(line, "# ") { | ||
isHeader = true | ||
} | ||
|
||
if isHeader == true && l >= 6 { | ||
buffer += line + "\n" | ||
|
||
s := string(line[0:6]) | ||
s = strings.ToUpper(s) | ||
|
||
if s == "SELECT" || s == "INSERT" || s == "UPDATE" || s == "DELETE" { | ||
isQuery = true | ||
} | ||
} | ||
|
||
if l > 1 { | ||
e = string(line[l-1:]) | ||
} else { | ||
e = string(line) | ||
} | ||
|
||
if isQuery == true && e == ";" { | ||
out <- strings.TrimRight(buffer, "\n") | ||
|
||
buffer = "" | ||
isHeader = false | ||
isQuery = false | ||
} | ||
} | ||
} |
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,59 @@ | ||
package slow_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/debeando/go-common/mysql/sql/parser/slow" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var logs = []string{ | ||
"# Time: 2023-10-27T08:51:32.366670Z", | ||
"# User@Host: test[test] @ [127.0.0.1] Id: 725", | ||
"# Query_time: 0.000135 Lock_time: 0.000002 Rows_sent: 11 Rows_examined: 11", | ||
"SET timestamp=1698396692;", | ||
"SELECT * FROM foo WHERE deleted_at IS NULL;", | ||
"# Time: 2023-10-27T08:51:34.376283Z", | ||
"# User@Host: test[test] @ [127.0.0.1] Id: 3178", | ||
"# Query_time: 0.019560 Lock_time: 0.000002 Rows_sent: 1 Rows_examined: 56914", | ||
"SET timestamp=1698397201;", | ||
"SELECT", | ||
" count(*)", | ||
"", | ||
"FROM foo", | ||
";", | ||
"# Time: 2023-10-27T08:51:34.376283Z", | ||
"# User@Host: test[test] @ [127.0.0.1] Id: 1303", | ||
"# Query_time: 0.000126 Lock_time: 0.000002 Rows_sent: 18 Rows_examined: 3583", | ||
"SET timestamp=1698396694;", | ||
"SELECT *", | ||
" FROM foo;", | ||
} | ||
|
||
func TestLogsParser(t *testing.T) { | ||
logsCount := 0 | ||
channelIn := make(chan string) | ||
channelOut := make(chan string) | ||
|
||
defer close(channelIn) | ||
|
||
go slow.LogsParser(channelIn, channelOut) | ||
|
||
go func() { | ||
defer close(channelOut) | ||
for query := range channelOut { | ||
t.Log("\n", query) | ||
logsCount++ | ||
} | ||
}() | ||
|
||
for _, line := range logs { | ||
channelIn <- line | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
assert.Equal(t, 3, logsCount) | ||
} |
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
Oops, something went wrong.