|
| 1 | +package slowql |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/eze-kiel/dbg" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +type Parser struct { |
| 14 | + Source io.Reader |
| 15 | + StackSize int |
| 16 | + stack chan Query |
| 17 | + rawBlocs chan []string |
| 18 | + scanner bufio.Scanner |
| 19 | +} |
| 20 | + |
| 21 | +// Query contains query informations |
| 22 | +type Query struct { |
| 23 | + Time time.Time |
| 24 | + User string |
| 25 | + Host string |
| 26 | + IP string |
| 27 | + ID int |
| 28 | + QueryTime time.Time |
| 29 | + LockTime time.Time |
| 30 | + RowsSent int |
| 31 | + RowsExamined int |
| 32 | + Timestamp time.Time |
| 33 | + Query string |
| 34 | +} |
| 35 | + |
| 36 | +// GetNext returns the next query |
| 37 | +func (p Parser) GetNext() (Query, error) { |
| 38 | + var q Query |
| 39 | + |
| 40 | + select { |
| 41 | + case q := <-p.stack: |
| 42 | + return q, nil |
| 43 | + default: |
| 44 | + return q, io.EOF |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// NewParser creates the stack channel and launches background goroutines |
| 49 | +func NewParser(r io.Reader) *Parser { |
| 50 | + var p Parser |
| 51 | + |
| 52 | + p.StackSize = 1024 |
| 53 | + p.Source = r |
| 54 | + p.scanner = *bufio.NewScanner(r) |
| 55 | + |
| 56 | + p.stack = make(chan Query, p.StackSize) |
| 57 | + p.rawBlocs = make(chan []string, p.StackSize) |
| 58 | + |
| 59 | + go p.consume() |
| 60 | + go p.scan() |
| 61 | + |
| 62 | + return &p |
| 63 | +} |
| 64 | + |
| 65 | +// scan reads the source line by line, and send those lines to the parser one |
| 66 | +// after each other |
| 67 | +func (p *Parser) scan() { |
| 68 | + var bloc []string |
| 69 | + inHeader, inQuery := false, false |
| 70 | + |
| 71 | + for p.scanner.Scan() { |
| 72 | + line := p.scanner.Text() |
| 73 | + |
| 74 | + // Drop useless lines |
| 75 | + if p.scanner.Text()[0] == '#' && strings.Contains(p.scanner.Text(), "Quit;") { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + This big if/else statement detects if the curernt line in a header |
| 81 | + or a request, and if it belongs to the same bloc or not |
| 82 | + */ |
| 83 | + // In header |
| 84 | + if strings.HasPrefix(line, "#") { |
| 85 | + inHeader = true |
| 86 | + if inQuery { |
| 87 | + // A new bloc is starting, we send the previous one if it is not |
| 88 | + // the first one |
| 89 | + inQuery = false |
| 90 | + dbg.Point(len(bloc)) |
| 91 | + if len(bloc) != 0 { |
| 92 | + p.rawBlocs <- bloc |
| 93 | + } |
| 94 | + } else { |
| 95 | + // We are in the same header as before |
| 96 | + bloc = append(bloc, line) |
| 97 | + } |
| 98 | + } else { // In request |
| 99 | + inQuery = true |
| 100 | + if inHeader { |
| 101 | + // We were in an header, and now are in a request, but in the |
| 102 | + // same bloc |
| 103 | + inHeader = false |
| 104 | + } |
| 105 | + bloc = append(bloc, line) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // In case of error, log it |
| 110 | + if err := p.scanner.Err(); err != nil { |
| 111 | + logrus.Error(err) |
| 112 | + } |
| 113 | + |
| 114 | + logrus.Infof("all the file has been parsed") |
| 115 | + |
| 116 | + // Send the last bloc |
| 117 | + p.rawBlocs <- bloc |
| 118 | + |
| 119 | + close(p.rawBlocs) |
| 120 | +} |
| 121 | + |
| 122 | +// consume consumes the received line to extract the informations, and send the |
| 123 | +// Query object to the stack |
| 124 | + |
| 125 | +// TODO(ezekiel): here do excatly the same thing as before, so instead of sending |
| 126 | +// bloc, we could conusme the lines and return the query instead |
| 127 | +func (p *Parser) consume() { |
| 128 | + select { |
| 129 | + case bloc := <-p.rawBlocs: |
| 130 | + dbg.Printf("received new bloc\n") |
| 131 | + var q Query |
| 132 | + |
| 133 | + // consume each line of the bloc |
| 134 | + for _, line := range bloc { |
| 135 | + if strings.HasPrefix(line, "#") { |
| 136 | + q.parseHeader(line) |
| 137 | + } else { |
| 138 | + q.parseRequest(line) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func (q *Query) parseHeader(line string) { |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +func (q *Query) parseRequest(line string) { |
| 149 | + |
| 150 | +} |
0 commit comments