This commit is contained in:
Denis Zheleztsov 2017-02-12 13:19:02 +03:00
commit 84e2cc0ee5
3 changed files with 126 additions and 0 deletions

31
main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"flag"
"gitea.difrex.ru/Umbrella/lessmore/node"
)
var (
listen string
es string
esMessagesIndex string
esMessagesType string
)
// 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(&esMessagesIndex, "esindex", "idec3", "ES index")
flag.StringVar(&esMessagesType, "estype", "post", "ES index type")
flag.Parse()
}
// main ...
func main() {
esconf := node.ESConf{}
esconf.Host = es
esconf.Index = esMessagesIndex
esconf.Type = esMessagesType
node.Serve(listen, esconf)
}

37
node/api.go Normal file
View File

@ -0,0 +1,37 @@
package node
import (
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
// ListTXTHandler ...
func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) {
// Get echolist
echoes, err := es.GetListTXT()
if err != nil {
w.WriteHeader(500)
}
w.WriteHeader(200)
w.Write(echoes)
}
// Serve ...
func Serve(listen string, es ESConf) {
r := mux.NewRouter()
r.HandleFunc("/list.txt", es.ListTXTHandler)
http.Handle("/list.txt", r)
srv := http.Server{
Handler: r,
Addr: listen,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}

58
node/elastic.go Normal file
View File

@ -0,0 +1,58 @@
package node
import (
"bytes"
// "encoding/json"
"gitea.difrex.ru/Umbrella/fetcher/i2es"
"github.com/Jeffail/gabs"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
const (
echoAgg = "echo_uniq"
)
// ESConf ...
type ESConf i2es.ESConf
// Bucket ...
type Bucket struct {
Key string `json:"key"`
DocCount int `json:"doc_count"`
}
// GetListTXT ...
func (es ESConf) GetListTXT() ([]byte, error) {
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
searchQ := []byte(`{"aggs": {"echo_uniq": { "terms": { "field": "echo","size": 1000}}}}`)
req, err := http.NewRequest("POST", searchURI, bytes.NewBuffer(searchQ))
client := &http.Client{}
resp, err := client.Do(req)
// defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte(""), err
}
esresp, err := gabs.ParseJSON(body)
if err != nil {
panic(err)
}
var buckets []Bucket
buckets, _ = esresp.Path("aggregations.echo_uniq.buckets").Data().([]Bucket)
var echoes []string
for _, bucket := range buckets {
c := strconv.Itoa(bucket.DocCount)
echostr := strings.Join([]string{bucket.Key, ":", c, ":"}, "")
echoes = append(echoes, echostr)
}
return []byte(strings.Join(echoes, "\n")), nil
}