Skip to content

Commit

Permalink
started implementing digest basics
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Apr 9, 2021
1 parent c25ff83 commit 0ab4467
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 55 deletions.
47 changes: 47 additions & 0 deletions cmd/slowql-digest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

GOCMD=go
GOTEST=$(GOCMD) test
GOVET=$(GOCMD) vet
BINARY_NAME=slowql-digest
BUILD_DIR=./out/bin/
BUILD_ROOT=./out

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)

.PHONY: all build clean

all: help

## Build:
build: ## Build the Go project
mkdir -p ./out/bin
GO111MODULE=on $(GOCMD) build -o $(BUILD_DIR)$(BINARY_NAME) .
@echo "${GREEN}[*]${RESET} ${BINARY_NAME} successfully built in ${YELLOW}${BUILD_DIR}${BINARY_NAME}${RESET}"

clean: ## Clean all the files and binaries generated by the Makefile
rm -rf $(BUILD_ROOT)
rm -f ./profile.cov

## Test:
test: ## Run the tests of the project
$(GOTEST) -v -race ./...

coverage: ## Run the tests of the project and export the coverage
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
$(GOCMD) tool cover -func profile.cov

## Help:
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)

89 changes: 89 additions & 0 deletions cmd/slowql-digest/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"errors"
"sync"
"time"

"github.com/devops-works/slowql"
"github.com/devops-works/slowql/query"
"github.com/sirupsen/logrus"
)

func newApp(loglevel, kind string) (*app, error) {
var a app

// init res map
a.res = make(map[string]statistics)

// create application logger
a.logger = logrus.New()
switch loglevel {
case "trace":
a.logger.SetLevel(logrus.TraceLevel)
case "debug":
a.logger.SetLevel(logrus.DebugLevel)
case "info":
a.logger.SetLevel(logrus.InfoLevel)
case "warn":
a.logger.SetLevel(logrus.WarnLevel)
case "error", "err":
a.logger.SetLevel(logrus.ErrorLevel)
case "fatal":
a.logger.SetLevel(logrus.FatalLevel)
default:
return nil, errors.New("log level not recognised: " + loglevel)
}

// convert kind from string to slowql.Kind
switch kind {
case "mysql":
a.kind = slowql.MySQL
case "mariadb":
a.kind = slowql.MariaDB
case "pxc":
a.kind = slowql.PXC
default:
return nil, errors.New("kind not recognised: " + kind)
}

return &a, nil
}

func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
defer wg.Done()
var s statistics
s.fingerprint = fingerprint(q.Query)
s.hash = hash(s.fingerprint)

a.mu.Lock()
if cur, ok := a.res[s.hash]; ok {
// there is already results
cur.calls++
cur.cumBytesSent += q.BytesSent
cur.cumKilled += q.Killed
cur.cumLockTime += time.Duration(q.LockTime)
cur.cumRowsExamined += q.RowsExamined
cur.cumRowsSent += q.RowsSent

// update the entry in the map
a.res[s.hash] = cur
} else {
// it is the first time this hash appears
s.calls++
s.cumBytesSent = q.BytesSent
s.cumKilled = q.Killed
s.cumLockTime = time.Duration(q.LockTime)
s.cumRowsExamined = q.RowsExamined
s.cumRowsSent = q.RowsSent

// getting those values is done only once: same hash == same fingerprint & schema
s.schema = q.Schema

// add the entry to the map
a.res[s.hash] = s
}
a.mu.Unlock()

return nil
}
Loading

0 comments on commit 0ab4467

Please sign in to comment.