From c9c5f282fb8089fd3a497da9caa4a355babc6503 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Mon, 13 Feb 2017 10:24:21 +0300 Subject: [PATCH] Working /list.txt --- main.go | 2 +- node/api.go | 10 ++++++---- node/elastic.go | 26 +++++++++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index d70245d..a46d4ac 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/node/api.go b/node/api.go index ca9f3fb..81ddcf5 100644 --- a/node/api.go +++ b/node/api.go @@ -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) diff --git a/node/elastic.go b/node/elastic.go index a9b2532..c38ddcc 100644 --- a/node/elastic.go +++ b/node/elastic.go @@ -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")) }