Working /list.txt

This commit is contained in:
Denis Zheleztsov 2017-02-13 10:24:21 +03:00
parent 84e2cc0ee5
commit c9c5f282fb
3 changed files with 24 additions and 14 deletions

View File

@ -15,7 +15,7 @@ var (
// init ... // init ...
func init() { func init() {
flag.StringVar(&listen, "listen", "127.0.0.1:15582", "Address to listen") flag.StringVar(&listen, "listen", "127.0.0.1:15582", "Address to listen")
flag.StringVar(&es, "es", "htt://127.0.0.1:9200", "ES host") flag.StringVar(&es, "es", "http://127.0.0.1:9200", "ES host")
flag.StringVar(&esMessagesIndex, "esindex", "idec3", "ES index") flag.StringVar(&esMessagesIndex, "esindex", "idec3", "ES index")
flag.StringVar(&esMessagesType, "estype", "post", "ES index type") flag.StringVar(&esMessagesType, "estype", "post", "ES index type")
flag.Parse() flag.Parse()

View File

@ -10,11 +10,13 @@ import (
// ListTXTHandler ... // ListTXTHandler ...
func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) { func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) {
ch := make(chan []byte)
// Get echolist // Get echolist
echoes, err := es.GetListTXT() go func() {
if err != nil { ch <- es.GetListTXT()
w.WriteHeader(500) }()
}
echoes := <-ch
w.WriteHeader(200) w.WriteHeader(200)
w.Write(echoes) w.Write(echoes)

View File

@ -6,6 +6,7 @@ import (
"gitea.difrex.ru/Umbrella/fetcher/i2es" "gitea.difrex.ru/Umbrella/fetcher/i2es"
"github.com/Jeffail/gabs" "github.com/Jeffail/gabs"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -25,34 +26,41 @@ type Bucket struct {
} }
// GetListTXT ... // GetListTXT ...
func (es ESConf) GetListTXT() ([]byte, error) { func (es ESConf) GetListTXT() []byte {
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/") searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
searchQ := []byte(`{"aggs": {"echo_uniq": { "terms": { "field": "echo","size": 1000}}}}`) searchQ := []byte(`{"aggs": {"echo_uniq": { "terms": { "field": "echo","size": 1000}}}}`)
log.Print("Search URI: ", searchURI)
req, err := http.NewRequest("POST", searchURI, bytes.NewBuffer(searchQ)) req, err := http.NewRequest("POST", searchURI, bytes.NewBuffer(searchQ))
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
// defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return []byte(""), err return []byte("")
} }
esresp, err := gabs.ParseJSON(body) esresp, err := gabs.ParseJSON(body)
if err != nil { if err != nil {
panic(err) panic(err)
} }
var buckets []Bucket
buckets, _ = esresp.Path("aggregations.echo_uniq.buckets").Data().([]Bucket) var uniq map[string]interface{}
uniq, _ = esresp.Path(strings.Join([]string{"aggregations", echoAgg}, ".")).Data().(map[string]interface{})
var echoes []string var echoes []string
for _, bucket := range buckets { for _, bucket := range uniq["buckets"].([]interface{}) {
c := strconv.Itoa(bucket.DocCount) b := make(map[string]interface{})
echostr := strings.Join([]string{bucket.Key, ":", c, ":"}, "") b = bucket.(map[string]interface{})
count := int(b["doc_count"].(float64))
c := strconv.Itoa(count)
echostr := strings.Join([]string{b["key"].(string), ":", c, ":"}, "")
echoes = append(echoes, echostr) echoes = append(echoes, echostr)
} }
return []byte(strings.Join(echoes, "\n")), nil log.Print("Getting ", len(echoes), " echoes")
return []byte(strings.Join(echoes, "\n"))
} }