<commit_before>package db

import (
	"errors"
	"github.com/garyburd/redigo/redis"
	"github.com/sad0vnikov/radish/config"
	"github.com/sad0vnikov/radish/logger"
	"github.com/sad0vnikov/radish/helpers"
	rd "github.com/sad0vnikov/radish/redis"
	"regexp"
	"strconv"
	"strings"
)

//Connections is an interface for objects storing Redis connections
type Connections interface {
	GetByName(serverName string, dbNum uint8) (redis.Conn, error)
	GetMaxDbNumsForServer(serverName string) (uint8, error)
	GetServerKeyspaceStat(serverName string) (map[string]rd.ServerKeyspaceStat, error)
	GetServerStat(serverName string) (rd.ServerStat, error)
}

//RedisConnections is a struct storing redis connection
type RedisConnections struct {
	pool *redis.Pool
}

//GetByName function returns redigo Redis connection instance by server name
func (connections RedisConnections) GetByName(serverName string, dbNum uint8) (redis.Conn, error) {
	server, prs := config.Get().Servers[serverName]
	if !prs {
		return nil, errors.New("no server with name " + serverName + " found")
	}

	if connections.pool == nil {
		connections.pool = &redis.Pool{
			MaxIdle: 3,
			Dial: func() (redis.Conn, error) {
				serverAddr := server.Host + ":" + strconv.Itoa(server.Port)
				logger.Info("connecting to Redis server " + serverAddr)
				conn, err := redis.Dial("tcp", serverAddr)
				logger.Info("connected to Redis server " + serverAddr)
				return conn, err
			},
		}
	}

	var c redis.Conn
	c = connections.pool.Get()
	c.Do("SELECT", dbNum)

	return c, nil
}

//GetMaxDbNumsForServer returns a maxium db number for given Redis server
func (connections RedisConnections) GetMaxDbNumsForServer(serverName string) (uint8, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return 0, err
	}

	r, err := conn.Do("CONFIG", "GET", "databases")
	cfgValues, err := redis.Strings(r, err)
	if err != nil {
		return 0, err
	}

	cnt, err := strconv.ParseUint(cfgValues[1], 10, 8)

	if err != nil {
		return 0, err
	}

	return uint8(cnt), nil
}

func (connections RedisConnections) GetServerStat(serverName string) (rd.ServerStat, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return rd.ServerStat{}, err
	}

	r, err := conn.Do("INFO")
	info, err := redis.String(r, err)
	if err != nil {
		return rd.ServerStat{}, err
	}

	c := parseInfoStrings(strings.Split(info, "\r\n"))

	maxMemory, err := connections.getServerConfigParam(serverName, "maxmemory")
	if err != nil {
		return rd.ServerStat{}, err
	}
	maxMemoryBytes, err := strconv.Atoi(maxMemory)
	c.MaxMemoryBytes =  int64(maxMemoryBytes) * 1024
	c.MaxMemoryHuman = helpers.SizeInBytesToHumanReadable(c.MaxMemoryBytes)
	return c, err
}

func (connections RedisConnections) GetServerKeyspaceStat(serverName string) (map[string]rd.ServerKeyspaceStat, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return nil, err
	}

	r, err := conn.Do("INFO", "keyspace")
	rs, err := redis.String(r, err)
	info := strings.Split(rs, "\r\n")

	databaseStatRowsCount := len(info) - 1
	stat := make(map[string]rd.ServerKeyspaceStat, databaseStatRowsCount)
	dbNum := 0
	for i := 1; i < len(info); i++ {<commit_msg>* maxmemory value<commit_after>package db

import (
	"errors"
	"github.com/garyburd/redigo/redis"
	"github.com/sad0vnikov/radish/config"
	"github.com/sad0vnikov/radish/logger"
	"github.com/sad0vnikov/radish/helpers"
	rd "github.com/sad0vnikov/radish/redis"
	"regexp"
	"strconv"
	"strings"
)

//Connections is an interface for objects storing Redis connections
type Connections interface {
	GetByName(serverName string, dbNum uint8) (redis.Conn, error)
	GetMaxDbNumsForServer(serverName string) (uint8, error)
	GetServerKeyspaceStat(serverName string) (map[string]rd.ServerKeyspaceStat, error)
	GetServerStat(serverName string) (rd.ServerStat, error)
}

//RedisConnections is a struct storing redis connection
type RedisConnections struct {
	pool *redis.Pool
}

//GetByName function returns redigo Redis connection instance by server name
func (connections RedisConnections) GetByName(serverName string, dbNum uint8) (redis.Conn, error) {
	server, prs := config.Get().Servers[serverName]
	if !prs {
		return nil, errors.New("no server with name " + serverName + " found")
	}

	if connections.pool == nil {
		connections.pool = &redis.Pool{
			MaxIdle: 3,
			Dial: func() (redis.Conn, error) {
				serverAddr := server.Host + ":" + strconv.Itoa(server.Port)
				logger.Info("connecting to Redis server " + serverAddr)
				conn, err := redis.Dial("tcp", serverAddr)
				logger.Info("connected to Redis server " + serverAddr)
				return conn, err
			},
		}
	}

	var c redis.Conn
	c = connections.pool.Get()
	c.Do("SELECT", dbNum)

	return c, nil
}

//GetMaxDbNumsForServer returns a maxium db number for given Redis server
func (connections RedisConnections) GetMaxDbNumsForServer(serverName string) (uint8, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return 0, err
	}

	r, err := conn.Do("CONFIG", "GET", "databases")
	cfgValues, err := redis.Strings(r, err)
	if err != nil {
		return 0, err
	}

	cnt, err := strconv.ParseUint(cfgValues[1], 10, 8)

	if err != nil {
		return 0, err
	}

	return uint8(cnt), nil
}

func (connections RedisConnections) GetServerStat(serverName string) (rd.ServerStat, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return rd.ServerStat{}, err
	}

	r, err := conn.Do("INFO")
	info, err := redis.String(r, err)
	if err != nil {
		return rd.ServerStat{}, err
	}

	c := parseInfoStrings(strings.Split(info, "\r\n"))

	maxMemory, err := connections.getServerConfigParam(serverName, "maxmemory")
	if err != nil {
		return rd.ServerStat{}, err
	}
	maxMemoryBytes, err := strconv.Atoi(maxMemory)
	c.MaxMemoryBytes =  int64(maxMemoryBytes)
	c.MaxMemoryHuman = helpers.SizeInBytesToHumanReadable(c.MaxMemoryBytes)
	return c, err
}

func (connections RedisConnections) GetServerKeyspaceStat(serverName string) (map[string]rd.ServerKeyspaceStat, error) {
	conn, err := connections.GetByName(serverName, 0)
	if err != nil {
		return nil, err
	}

	r, err := conn.Do("INFO", "keyspace")
	rs, err := redis.String(r, err)
	info := strings.Split(rs, "\r\n")

	databaseStatRowsCount := len(info) - 1
	stat := make(map[string]rd.ServerKeyspaceStat, databaseStatRowsCount)
	dbNum := 0
	for i := 1; i < len(info); i++ {
{'fraction_non_alphanumeric': 0.1321848081440877, 'fraction_numerical': 0.00845732184808144, 'mean_word_length': 4.616534740545295, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1424813', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2253, 'n_words': 561}