Skip to content

Commit cc004c4

Browse files
committed
code refacto
changed interface construction and how databses are declared
1 parent 9dd21a8 commit cc004c4

5 files changed

Lines changed: 50 additions & 48 deletions

File tree

cmd/slowql-digest/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/devops-works/slowql"
12+
"github.com/devops-works/slowql/query"
1213
"github.com/sirupsen/logrus"
1314
)
1415

@@ -66,7 +67,7 @@ func main() {
6667
q := p.GetNext()
6768

6869
// If the query is empty, there is no more queries to get
69-
if q == (slowql.Query{}) {
70+
if q == (query.Query{}) {
7071
logrus.Debug("no more queries to get from the slow query log file")
7172
break
7273
}

cmd/slowql-replayer/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/briandowns/spinner"
1717
"github.com/devops-works/slowql"
1818
"github.com/devops-works/slowql/cmd/slowql-replayer/pprof"
19+
"github.com/devops-works/slowql/query"
1920
"github.com/olekukonko/tablewriter"
2021
"github.com/sirupsen/logrus"
2122
"golang.org/x/crypto/ssh/terminal"
@@ -223,7 +224,7 @@ func (db *database) replay(f io.Reader) (results, error) {
223224
var previousDate time.Time
224225
for {
225226
q := p.GetNext()
226-
if q == (slowql.Query{}) {
227+
if q == (query.Query{}) {
227228
s.Stop()
228229
break
229230
}

database/mariadb/mariadb.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,41 @@ import (
1414

1515
// Database holds parser structure
1616
type Database struct {
17-
WaitingList chan query.Query
18-
ServerMeta chan server.Server
17+
WaitingList chan query.Query
18+
ServerMeta chan server.Server
19+
stringInBrackets *regexp.Regexp
20+
srv server.Server
1921
}
2022

21-
func (p *Parser) parseBlocs(rawBlocs chan []string) {
23+
// New instance of parser
24+
func New(qc chan query.Query) *Database {
25+
p := Database{
26+
WaitingList: qc,
27+
stringInBrackets: regexp.MustCompile(`\[(.*?)\]`),
28+
}
29+
30+
return &p
31+
}
32+
33+
func (db *Database) ParseBlocs(rawBlocs chan []string) {
2234
for {
2335
select {
2436
case bloc := <-rawBlocs:
25-
var q Query
37+
var q query.Query
2638

2739
for _, line := range bloc {
28-
if strings.HasPrefix(line, "#") {
29-
q.parseMariaDBHeader(line)
40+
if line[0] == '#' {
41+
db.parseMariaDBHeader(line, &q)
3042
} else {
3143
q.Query = q.Query + line
3244
}
3345
}
34-
p.wl <- q
46+
db.WaitingList <- q
3547
}
3648
}
3749
}
3850

39-
func (p *Parser) GetNext() Query {
40-
var q Query
41-
select {
42-
case q = <-p.wl:
43-
return q
44-
case <-time.After(2 * time.Second):
45-
close(p.wl)
46-
}
47-
return q
48-
}
49-
50-
func (q *Query) parseMariaDBHeader(line string) {
51+
func (db *Database) parseMariaDBHeader(line string, q *query.Query) {
5152
var err error
5253
parts := strings.Split(line, " ")
5354

@@ -109,7 +110,7 @@ func (q *Query) parseMariaDBHeader(line string) {
109110
}
110111

111112
} else if strings.Contains(part, "user@host:") {
112-
items := stringInBrackets.FindAllString(line, -1)
113+
items := db.stringInBrackets.FindAllString(line, -1)
113114
// We remove first and last bytes of the strings because they are
114115
// square brackets
115116
q.User = items[0][1 : len(items[0])-1]
@@ -139,7 +140,7 @@ func (q *Query) parseMariaDBHeader(line string) {
139140
}
140141
}
141142

142-
func (p *Parser) parseServerMeta(lines chan []string) {
143+
func (p *Database) ParseServerMeta(lines chan []string) {
143144
for {
144145
select {
145146
case header := <-lines:
@@ -151,25 +152,25 @@ func (p *Parser) parseServerMeta(lines chan []string) {
151152
matches := versionre.FindStringSubmatch(versions)
152153

153154
if len(matches) != 5 {
154-
srv.Binary = "unable to parse line"
155-
srv.VersionShort = srv.Binary
156-
srv.Version = srv.Binary
157-
srv.VersionDescription = srv.Binary
158-
srv.Port = 0
159-
srv.Socket = srv.Binary
155+
p.srv.Binary = "unable to parse line"
156+
p.srv.VersionShort = p.srv.Binary
157+
p.srv.Version = p.srv.Binary
158+
p.srv.VersionDescription = p.srv.Binary
159+
p.srv.Port = 0
160+
p.srv.Socket = p.srv.Binary
160161
}
161162

162-
srv.Binary = matches[1]
163-
srv.VersionShort = matches[2]
164-
srv.Version = srv.VersionShort + matches[3]
165-
srv.VersionDescription = matches[4]
166-
srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
167-
srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
163+
p.srv.Binary = matches[1]
164+
p.srv.VersionShort = matches[2]
165+
p.srv.Version = p.srv.VersionShort + matches[3]
166+
p.srv.VersionDescription = matches[4]
167+
p.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
168+
p.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
168169
return
169170
}
170171
}
171172
}
172173

173-
func (p *Parser) GetServerMeta() Server {
174-
return srv
174+
func (d *Database) GetServerMeta() server.Server {
175+
return d.srv
175176
}

database/mysql/mysql.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/sirupsen/logrus"
10-
119
"github.com/devops-works/slowql/query"
1210
"github.com/devops-works/slowql/server"
11+
"github.com/sirupsen/logrus"
1312
)
1413

1514
// Database holds parser structure
@@ -31,25 +30,25 @@ func New(qc chan query.Query) *Database {
3130
}
3231

3332
// ParseBlocs parses query blocks
34-
func (p *Database) ParseBlocs(rawBlocs chan []string) {
33+
func (db *Database) ParseBlocs(rawBlocs chan []string) {
3534
for {
3635
select {
3736
case bloc := <-rawBlocs:
3837
var q query.Query
3938

4039
for _, line := range bloc {
4140
if line[0] == '#' {
42-
p.parseMySQLHeader(line, &q)
41+
db.parseMySQLHeader(line, &q)
4342
} else {
4443
q.Query = q.Query + line
4544
}
4645
}
47-
p.WaitingList <- q
46+
db.WaitingList <- q
4847
}
4948
}
5049
}
5150

52-
func (p *Database) parseMySQLHeader(line string, q *query.Query) {
51+
func (db *Database) parseMySQLHeader(line string, q *query.Query) {
5352
var err error
5453
parts := strings.Split(line, " ")
5554

@@ -111,7 +110,7 @@ func (p *Database) parseMySQLHeader(line string, q *query.Query) {
111110
}
112111

113112
} else if strings.Contains(part, "user@host:") {
114-
items := p.stringInBrackets.FindAllString(line, -1)
113+
items := db.stringInBrackets.FindAllString(line, -1)
115114
// We remove first and last bytes of the strings because they are
116115
// square brackets
117116
q.User = items[0][1 : len(items[0])-1]

slowql.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/sirupsen/logrus"
14-
13+
"github.com/devops-works/slowql/database/mariadb"
1514
"github.com/devops-works/slowql/database/mysql"
16-
// "github.com/devops-works/slowql/parser/pxc"
17-
1815
"github.com/devops-works/slowql/query"
1916
"github.com/devops-works/slowql/server"
17+
"github.com/sirupsen/logrus"
2018
)
2119

2220
// Kind is a database kind
@@ -65,6 +63,8 @@ func NewParser(k Kind, r io.Reader) Parser {
6563
switch k {
6664
case MySQL, PXC:
6765
p.db = mysql.New(p.waitingList)
66+
case MariaDB:
67+
p.db = mariadb.New(p.waitingList)
6868
}
6969

7070
p.db.ParseServerMeta(p.servermeta)

0 commit comments

Comments
 (0)