Fix /m/ID handler

This commit is contained in:
Denis Zheleztsov 2018-11-04 11:15:46 +03:00
parent f48d951722
commit b388acb8c5
2 changed files with 58 additions and 50 deletions

View File

@ -10,7 +10,7 @@ import (
"fmt" "fmt"
"github.com/Jeffail/gabs" "gitea.difrex.ru/Umbrella/fetcher/i2es"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
) )
@ -19,23 +19,34 @@ const (
) )
// MakePlainTextMessage ... // MakePlainTextMessage ...
func MakePlainTextMessage(hit interface{}) string { func MakePlainTextMessage(hit i2es.ESDoc) []byte {
tags := "ii/ok"
if hit.Repto != "" {
tags += fmt.Sprintf("/repto/%s", hit.Repto)
}
m := []string{
tags,
hit.Echo,
hit.Date,
hit.Author,
hit.Address,
hit.To,
hit.Subg,
hit.Message,
}
h := make(map[string]interface{}) return []byte(strings.Join(m, "\n"))
h = hit.(map[string]interface{})
s := make(map[string]interface{})
s = h["_source"].(map[string]interface{})
m := []string{"ii/ok", s["echo"].(string), s["date"].(string), s["author"].(string), "null", s["to"].(string), s["subg"].(string), "", s["message"].(string)}
return strings.Join(m, "\n")
} }
// GetPlainTextMessage ... // GetPlainTextMessage ...
func (es ESConf) GetPlainTextMessage(msgid string) []byte { func (es ESConf) GetPlainTextMessage(msgid string) []byte {
var message []byte var searchURI string
if es.Index != "" && es.Type != "" {
searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
} else {
searchURI = strings.Join([]string{es.Host, "search"}, "/")
}
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
searchQ := []byte(strings.Join([]string{ searchQ := []byte(strings.Join([]string{
`{"query": {"match": {"_id": "`, msgid, `"}}}`}, "")) `{"query": {"match": {"_id": "`, msgid, `"}}}`}, ""))
@ -47,27 +58,30 @@ func (es ESConf) GetPlainTextMessage(msgid string) []byte {
} }
defer resp.Body.Close() defer resp.Body.Close()
var esr ESSearchResp
body, err := ioutil.ReadAll(resp.Body) err = json.NewDecoder(resp.Body).Decode(&esr)
if err != nil { if err != nil {
return message log.Error(err.Error())
return []byte("")
} }
esresp, err := gabs.ParseJSON(body) if len(esr.Hits.Hits) > 0 {
if err != nil { return MakePlainTextMessage(esr.Hits.Hits[0].Source)
panic(err)
} }
hits, _ := esresp.Path("hits.hits").Data().([]interface{}) return []byte("")
return []byte(MakePlainTextMessage(hits[0]))
} }
// GetEchoMessageHashes ... // GetEchoMessageHashes ...
func (es ESConf) GetEchoMessageHashes(echo string) []string { func (es ESConf) GetEchoMessageHashes(echo string) []string {
var hashes []string var hashes []string
var searchURI string
if es.Index != "" && es.Type != "" {
searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
} else {
searchURI = strings.Join([]string{es.Host, "search"}, "/")
}
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
searchQ := []byte(strings.Join([]string{ searchQ := []byte(strings.Join([]string{
`{"sort": [ `{"sort": [
{"date":{ "order": "desc" }},{ "_score":{ "order": "desc" }}], {"date":{ "order": "desc" }},{ "_score":{ "order": "desc" }}],
@ -78,27 +92,23 @@ func (es ESConf) GetEchoMessageHashes(echo string) []string {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
return hashes
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) var esr ESSearchResp
err = json.NewDecoder(resp.Body).Decode(&esr)
if err != nil { if err != nil {
b, _ := ioutil.ReadAll(resp.Body)
log.Error(string(b))
log.Error(err.Error())
hashes = append(hashes, "error: Internal error")
return hashes return hashes
} }
esresp, err := gabs.ParseJSON(body) for _, hit := range esr.Hits.Hits {
if err != nil { hashes = append(hashes, hit.Source.MsgID)
panic(err)
}
hits, _ := esresp.Path("hits.hits").Data().([]interface{})
for _, hit := range hits {
h := make(map[string]interface{})
h = hit.(map[string]interface{})
source := make(map[string]interface{})
source = h["_source"].(map[string]interface{})
hashes = append(hashes, source["msgid"].(string))
} }
return hashes return hashes
@ -107,6 +117,12 @@ func (es ESConf) GetEchoMessageHashes(echo string) []string {
// GetLimitedEchoMessageHashes ... // GetLimitedEchoMessageHashes ...
func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int) []string { func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int) []string {
var hashes []string var hashes []string
var searchURI string
if es.Index != "" && es.Type != "" {
searchURI = strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
} else {
searchURI = strings.Join([]string{es.Host, "search"}, "/")
}
// Check offset // Check offset
var order string var order string
@ -118,7 +134,6 @@ func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int)
l := strconv.Itoa(limit) l := strconv.Itoa(limit)
searchURI := strings.Join([]string{es.Host, es.Index, es.Type, "_search"}, "/")
searchQ := []byte(strings.Join([]string{ searchQ := []byte(strings.Join([]string{
`{"sort": [ `{"sort": [
{"date":{ "order": "`, order, `" }},{ "_score":{ "order": "`, order, `" }}], {"date":{ "order": "`, order, `" }},{ "_score":{ "order": "`, order, `" }}],
@ -129,27 +144,19 @@ func (es ESConf) GetLimitedEchoMessageHashes(echo string, offset int, limit int)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
return hashes
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) var esr ESSearchResp
err = json.NewDecoder(resp.Body).Decode(&esr)
if err != nil { if err != nil {
log.Error(err.Error())
return hashes return hashes
} }
for _, hit := range esr.Hits.Hits {
esresp, err := gabs.ParseJSON(body) hashes = append(hashes, hit.Source.MsgID)
if err != nil {
panic(err)
}
hits, _ := esresp.Path("hits.hits").Data().([]interface{})
for _, hit := range hits {
h := make(map[string]interface{})
h = hit.(map[string]interface{})
source := make(map[string]interface{})
source = h["_source"].(map[string]interface{})
hashes = append(hashes, source["msgid"].(string))
} }
return hashes return hashes

View File

@ -16,6 +16,7 @@ type ESSearchResp struct {
Took int64 `json:"took"` Took int64 `json:"took"`
TimedOut bool `json:"timed_out"` TimedOut bool `json:"timed_out"`
ESSearchShards `json:"_shards"` ESSearchShards `json:"_shards"`
Hits `json:"hits"`
} }
type Hits struct { type Hits struct {