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 ...
func init() {
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(&esMessagesType, "estype", "post", "ES index type")
flag.Parse()

View File

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

View File

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