Skip to content

Commit

Permalink
Merge pull request #1 from flant/codereview
Browse files Browse the repository at this point in the history
Codereview
  • Loading branch information
uzhinskiy authored Dec 7, 2020
2 parents c2c0d11 + 0f80e52 commit 54acbc9
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 145 deletions.
16 changes: 2 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@ module github.com/flant/elasticsearch-extractor

go 1.14

replace github.com/flant/elasticsearch-extractor/modules/front => ./modules/front

replace github.com/flant/elasticsearch-extractor/modules/router => ./modules/router

replace github.com/flant/elasticsearch-extractor/modules/config => ./modules/config

replace github.com/flant/elasticsearch-extractor/modules/version => ./modules/version

require (
github.com/flant/elasticsearch-extractor/modules/config v0.0.0
github.com/flant/elasticsearch-extractor/modules/front v0.0.0 // indirect
github.com/flant/elasticsearch-extractor/modules/router v0.0.0
github.com/flant/elasticsearch-extractor/modules/version v0.0.0
github.com/uzhinskiy/lib.go v0.1.3 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
github.com/uzhinskiy/lib.go v0.1.3
gopkg.in/yaml.v2 v2.3.0
)
5 changes: 3 additions & 2 deletions main.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
app:
port: 9400
bind: 0.0.0.0
timeout: 60
elastic:
host: http://elasticsearch:9200/
# use this fields if elastic requires BA
# username: elastic
# password: elastic
username: elastic
password: elastic
ssl: false
# Видим-ли в списке снапшотов системые (.kibana* / .opendistro* )
# значение по умолчанию = false, то-есть не видим
Expand Down
24 changes: 16 additions & 8 deletions modules/config/c.go → modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ package config

import (
"io/ioutil"
"log"

"gopkg.in/yaml.v2"
)

type Config struct {
App struct {
Port string `yaml:"port"`
TimeOut int `yaml:"timeout"`
Port string `yaml:"port"`
Bind string `yaml:"bind"`
TimeOut int `yaml:"-"`
TimeOutRaw *int `yaml:"timeout"`
} `yaml:"app"`
Elastic struct {
Host string `yaml:"host`
Expand All @@ -36,22 +39,27 @@ type Config struct {

func Parse(f string) Config {
var c Config
yamlFile, err := ioutil.ReadFile(f)
yamlBytes, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
log.Panic(err)
}

err = yaml.Unmarshal(yamlFile, &c)
err = yaml.Unmarshal(yamlBytes, &c)
if err != nil {
panic(err)
log.Panic(err)
}

if c.App.Port == "" {
c.App.Port = "9400"
}

if c.App.TimeOut == 0 {
c.App.TimeOut = 30
if c.App.Bind == "" {
c.App.Bind = "0.0.0.0"
}

c.App.TimeOut = 30
if c.App.TimeOutRaw != nil {
c.App.TimeOut = *c.App.TimeOutRaw
}

if c.Elastic.Host == "" {
Expand Down
1 change: 0 additions & 1 deletion modules/config/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion modules/front/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions modules/router/go.mod

This file was deleted.

58 changes: 58 additions & 0 deletions modules/router/f.go → modules/router/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"

"bytes"
"net"

"time"

"github.com/uzhinskiy/lib.go/helpers"
)

type esError struct {
Expand Down Expand Up @@ -144,3 +147,58 @@ func (rt *Router) doPost(url string, request map[string]interface{}) ([]byte, er

return body, nil
}

func (rt *Router) getNodes() ([]singleNode, error) {

var nresp []singleNode
var na nodesArray

// rt.nodes.RLock()
// defer rt.nodes.RUnlock()

response, err := rt.doGet(rt.conf.Elastic.Host + "_cat/nodes?format=json&bytes=b&h=ip,name,dt,du,dup,d&s=name")
if err != nil {
return nil, err
}

err = json.Unmarshal(response, &nresp)
if err != nil {
return nil, err
}
s := 0
for i, n := range nresp {
nresp[i].Dt = fmt.Sprintf("%dGb", helpers.Atoi(n.Dt)/(1024*1024*1024))
na.list = append(na.list, helpers.Atoi(n.D))
s += helpers.Atoi(n.D)
}
na.sum = s
na.max = helpers.GetMaxValueInArray(na.list)
rt.nodes = na
return nresp, nil

}

func (rt *Router) Barrel(array IndicesInSnap) ([]string, []string) {
var (
k int
Sk int
a []string
b []string
)

for name, ind := range array {
for n := range rt.nodes.list {
for m := range ind.Shards {
k = rt.nodes.list[n] / ind.Shards[m]
Sk = Sk + k
}
}

if Sk > len(ind.Shards) {
a = append(a, name)
} else {
b = append(b, name)
}
}
return a, b
}
Loading

0 comments on commit 54acbc9

Please sign in to comment.