|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/devops-works/slowql" |
| 9 | + "github.com/devops-works/slowql/query" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +func newApp(loglevel, kind string) (*app, error) { |
| 14 | + var a app |
| 15 | + |
| 16 | + // init res map |
| 17 | + a.res = make(map[string]statistics) |
| 18 | + |
| 19 | + // create application logger |
| 20 | + a.logger = logrus.New() |
| 21 | + switch loglevel { |
| 22 | + case "trace": |
| 23 | + a.logger.SetLevel(logrus.TraceLevel) |
| 24 | + case "debug": |
| 25 | + a.logger.SetLevel(logrus.DebugLevel) |
| 26 | + case "info": |
| 27 | + a.logger.SetLevel(logrus.InfoLevel) |
| 28 | + case "warn": |
| 29 | + a.logger.SetLevel(logrus.WarnLevel) |
| 30 | + case "error", "err": |
| 31 | + a.logger.SetLevel(logrus.ErrorLevel) |
| 32 | + case "fatal": |
| 33 | + a.logger.SetLevel(logrus.FatalLevel) |
| 34 | + default: |
| 35 | + return nil, errors.New("log level not recognised: " + loglevel) |
| 36 | + } |
| 37 | + |
| 38 | + // convert kind from string to slowql.Kind |
| 39 | + switch kind { |
| 40 | + case "mysql": |
| 41 | + a.kind = slowql.MySQL |
| 42 | + case "mariadb": |
| 43 | + a.kind = slowql.MariaDB |
| 44 | + case "pxc": |
| 45 | + a.kind = slowql.PXC |
| 46 | + default: |
| 47 | + return nil, errors.New("kind not recognised: " + kind) |
| 48 | + } |
| 49 | + |
| 50 | + return &a, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (a *app) digest(q query.Query, wg *sync.WaitGroup) error { |
| 54 | + defer wg.Done() |
| 55 | + var s statistics |
| 56 | + s.fingerprint = fingerprint(q.Query) |
| 57 | + s.hash = hash(s.fingerprint) |
| 58 | + |
| 59 | + a.mu.Lock() |
| 60 | + if cur, ok := a.res[s.hash]; ok { |
| 61 | + // there is already results |
| 62 | + cur.calls++ |
| 63 | + cur.cumBytesSent += q.BytesSent |
| 64 | + cur.cumKilled += q.Killed |
| 65 | + cur.cumLockTime += time.Duration(q.LockTime) |
| 66 | + cur.cumRowsExamined += q.RowsExamined |
| 67 | + cur.cumRowsSent += q.RowsSent |
| 68 | + |
| 69 | + // update the entry in the map |
| 70 | + a.res[s.hash] = cur |
| 71 | + } else { |
| 72 | + // it is the first time this hash appears |
| 73 | + s.calls++ |
| 74 | + s.cumBytesSent = q.BytesSent |
| 75 | + s.cumKilled = q.Killed |
| 76 | + s.cumLockTime = time.Duration(q.LockTime) |
| 77 | + s.cumRowsExamined = q.RowsExamined |
| 78 | + s.cumRowsSent = q.RowsSent |
| 79 | + |
| 80 | + // getting those values is done only once: same hash == same fingerprint & schema |
| 81 | + s.schema = q.Schema |
| 82 | + |
| 83 | + // add the entry to the map |
| 84 | + a.res[s.hash] = s |
| 85 | + } |
| 86 | + a.mu.Unlock() |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
0 commit comments