Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Blanc <eze-kiel@users.noreply.github.com>
  • Loading branch information
leucos and eze-kiel committed Mar 10, 2021
1 parent 9744807 commit d0c8620
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 116 deletions.
8 changes: 5 additions & 3 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/devops-works/slowql"
"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
)

func main() {
Expand All @@ -27,7 +29,7 @@ func main() {
start := time.Now()
for {
q := p.GetNext()
if q == (slowql.Query{}) {
if q == (query.Query{}) {
break
}

Expand All @@ -39,7 +41,7 @@ func main() {
fmt.Printf("\nparsed %d queries in %s\n", count, elapsed)
}

func showQueries(q slowql.Query) {
func showQueries(q query.Query) {
fmt.Printf("Time: %s\nUser: %s\nHost: %s\nID: %d\nSchema: %s\nLast_errno: %d\nKilled: %d\nQuery_time: %f\nLock_time: %f\nRows_sent: %d\nRows_examined: %d\nRows_affected: %d\nBytes_sent: %d\nQuery: %s\n",
q.Time,
q.User,
Expand All @@ -58,7 +60,7 @@ func showQueries(q slowql.Query) {
)
}

func showServer(srv slowql.Server) {
func showServer(srv server.Server) {
fmt.Printf("Binary: %s\nVersion short: %s\nVersion: %s\nVersion description: %s\nSocket: %s\nPort: %d\n",
srv.Binary,
srv.VersionShort,
Expand Down
23 changes: 12 additions & 11 deletions mariadb.go → database/mariadb/mariadb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slowql
package mariadb

import (
"regexp"
Expand All @@ -7,17 +7,18 @@ import (
"time"

"github.com/sirupsen/logrus"
)

// MariaDB is the MariaDB kind
const MariaDB Kind = 1
"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
)

type mariadbParser struct {
wl chan Query
sm chan Server
// Database holds parser structure
type Database struct {
WaitingList chan query.Query
ServerMeta chan server.Server
}

func (p *mariadbParser) parseBlocs(rawBlocs chan []string) {
func (p *Parser) parseBlocs(rawBlocs chan []string) {
for {
select {
case bloc := <-rawBlocs:
Expand All @@ -35,7 +36,7 @@ func (p *mariadbParser) parseBlocs(rawBlocs chan []string) {
}
}

func (p *mariadbParser) GetNext() Query {
func (p *Parser) GetNext() Query {
var q Query
select {
case q = <-p.wl:
Expand Down Expand Up @@ -138,7 +139,7 @@ func (q *Query) parseMariaDBHeader(line string) {
}
}

func (p *mariadbParser) parseServerMeta(lines chan []string) {
func (p *Parser) parseServerMeta(lines chan []string) {
for {
select {
case header := <-lines:
Expand Down Expand Up @@ -169,6 +170,6 @@ func (p *mariadbParser) parseServerMeta(lines chan []string) {
}
}

func (p *mariadbParser) GetServerMeta() Server {
func (p *Parser) GetServerMeta() Server {
return srv
}
84 changes: 43 additions & 41 deletions mysql.go → database/mysql/mysql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slowql
package mysql

import (
"regexp"
Expand All @@ -7,49 +7,49 @@ import (
"time"

"github.com/sirupsen/logrus"
)

// MySQL is the MySQL kind
const MySQL Kind = 0
"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
)

type mysqlParser struct {
wl chan Query
sm chan Server
// Database holds parser structure
type Database struct {
WaitingList chan query.Query
ServerMeta chan server.Server
stringInBrackets *regexp.Regexp
srv server.Server
}

// srv holds the server configuration so we can access it multiple times
var srv Server
// New instance of parser
func New(qc chan query.Query) *Database {
p := Database{
WaitingList: qc,
stringInBrackets: regexp.MustCompile(`\[(.*?)\]`),
}

return &p
}

func (p *mysqlParser) parseBlocs(rawBlocs chan []string) {
// ParseBlocs parses query blocks
func (p *Database) ParseBlocs(rawBlocs chan []string) {
for {
select {
case bloc := <-rawBlocs:
var q Query
var q query.Query

for _, line := range bloc {
if line[0] == '#' {
q.parseMySQLHeader(line)
p.parseMySQLHeader(line, &q)
} else {
q.Query = q.Query + line
}
}
p.wl <- q
p.WaitingList <- q
}
}
}

func (p *mysqlParser) GetNext() Query {
var q Query
select {
case q = <-p.wl:
return q
case <-time.After(2 * time.Second):
close(p.wl)
}
return q
}

func (q *Query) parseMySQLHeader(line string) {
func (p *Database) parseMySQLHeader(line string, q *query.Query) {
var err error
parts := strings.Split(line, " ")

Expand Down Expand Up @@ -111,7 +111,7 @@ func (q *Query) parseMySQLHeader(line string) {
}

} else if strings.Contains(part, "user@host:") {
items := stringInBrackets.FindAllString(line, -1)
items := p.stringInBrackets.FindAllString(line, -1)
// We remove first and last bytes of the strings because they are
// square brackets
q.User = items[0][1 : len(items[0])-1]
Expand Down Expand Up @@ -141,7 +141,8 @@ func (q *Query) parseMySQLHeader(line string) {
}
}

func (p *mysqlParser) parseServerMeta(lines chan []string) {
// ParseServerMeta parses server meta information
func (p *Database) ParseServerMeta(lines chan []string) {
for {
select {
case header := <-lines:
Expand All @@ -153,25 +154,26 @@ func (p *mysqlParser) parseServerMeta(lines chan []string) {
matches := versionre.FindStringSubmatch(versions)

if len(matches) != 5 {
srv.Binary = "unable to parse line"
srv.VersionShort = srv.Binary
srv.Version = srv.Binary
srv.VersionDescription = srv.Binary
srv.Port = 0
srv.Socket = srv.Binary
p.srv.Binary = "unable to parse line"
p.srv.VersionShort = p.srv.Binary
p.srv.Version = p.srv.Binary
p.srv.VersionDescription = p.srv.Binary
p.srv.Port = 0
p.srv.Socket = p.srv.Binary
}

srv.Binary = matches[1]
srv.VersionShort = matches[2]
srv.Version = srv.VersionShort + matches[3]
srv.VersionDescription = matches[4]
srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
p.srv.Binary = matches[1]
p.srv.VersionShort = matches[2]
p.srv.Version = p.srv.VersionShort + matches[3]
p.srv.VersionDescription = matches[4]
p.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
p.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
return
}
}
}

func (p *mysqlParser) GetServerMeta() Server {
return srv
// GetServerMeta returns server meta information
func (p *Database) GetServerMeta() server.Server {
return p.srv
}
3 changes: 0 additions & 3 deletions pxc.go

This file was deleted.

21 changes: 21 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package query

import "time"

// Query is a single SQL query and the data associated
type Query struct {
Time time.Time
QueryTime float64
LockTime float64
ID int
RowsSent int
RowsExamined int
RowsAffected int
LastErrNo int
Killed int
BytesSent int
User string
Host string
Schema string
Query string
}
11 changes: 11 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package server

// Server holds the SQL server informations that are parsed from the header
type Server struct {
Binary string
Port int
Socket string
Version string
VersionShort string
VersionDescription string
}
Loading

0 comments on commit d0c8620

Please sign in to comment.