Skip to content

Commit

Permalink
started the creation of slowql-digest
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Mar 9, 2021
1 parent f6399a4 commit 2280ea2
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cmd/slowql-digest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"bytes"
"errors"
"flag"
"io"
"os"

"github.com/sirupsen/logrus"
)

type options struct {
filepath string
file *os.File
debug bool
quiet bool
}

func main() {
var opt options
flag.StringVar(&opt.filepath, "file", "", "Slow query log file")
flag.BoolVar(&opt.debug, "debug", false, "Show debug logs")
flag.BoolVar(&opt.quiet, "quiet", false, "Quiet mode: show only errors")
flag.Parse()

if err := opt.parse(); err != nil {
logrus.Fatal(err)
}

lines, err := lineCounter(opt.file)
if err != nil {
logrus.Fatal(err)
}

logrus.Infof("%d lines found", lines)
}

// parse parses the different flags
func (o *options) parse() error {
var err error
if o.filepath == "" {
return errors.New("no file provided")
}

// Open file to obtain io.Reader
o.file, err = os.Open(o.filepath)
if err != nil {
return err
}
logrus.Infof("using %s as input file", o.file.Name())

// Set global log level depending on the different options that have been
// provided
logrus.SetLevel(logrus.InfoLevel)
if o.debug {
logrus.SetLevel(logrus.DebugLevel)
}
if o.quiet {
logrus.SetLevel(logrus.ErrorLevel)
}

return nil
}

// lineCounter returns the number of new line caracters that have been found in
// the io.Reader content
func lineCounter(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}

for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)

switch {
case err == io.EOF:
return count, nil

case err != nil:
return count, err
}
}
}

0 comments on commit 2280ea2

Please sign in to comment.