From b041fcd80b1c1fbd162c371c3e2a3d3157ee562a Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Thu, 4 Jan 2018 22:59:09 +0300 Subject: [PATCH] Topicid field initial --- i2es/elastic.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/i2es/elastic.go b/i2es/elastic.go index ceb0d6d..30daa7e 100644 --- a/i2es/elastic.go +++ b/i2es/elastic.go @@ -5,9 +5,11 @@ import ( "encoding/json" "errors" "io/ioutil" - "log" "net/http" "strings" + + log "github.com/Sirupsen/logrus" + "github.com/google/uuid" ) // ESDoc Elasticsearch document structure @@ -22,6 +24,7 @@ type ESDoc struct { Tags string `json:"tags"` Repto string `json:"repto"` Address string `json:"address"` + TopicID string `json:"topicid"` } // ESConf ES connection settings @@ -31,6 +34,46 @@ type ESConf struct { Type string } +type topicid struct{} + +// findTopicID for msgid +func (t topicid) findTopicID(msgid string) (string, error) { + var topicid string + return topicid, nil +} + +// check if +func (t topicid) checkID(msg *ESDoc) (bool, func()) { + if msg.TopicID != "" { + return false, func() {} + } + + if msg.Repto == "" { + var genTopicID = func() { + u := uuid.New() + log.Debug("New UUID " + u.String() + " for message " + msg.MsgID) + msg.TopicID = u.String() + } + return true, genTopicID + } + + var findAndSetTopicID = func() { + topicid, err := t.findTopicID(msg.MsgID) + if err != nil { + log.Error(err.Error()) + } + log.Debug("Topic ID " + topicid + " found for message " + msg.MsgID) + } + return true, findAndSetTopicID +} + +// getorcreate topicID. Generate new unique topicID if message is start message +// Find top message and get it topicid if ESDoc.Repto is not empty +func (t topicid) getOrCreate(msg *ESDoc) error { + // TODO: write the code + return nil +} + // PutToIndex ... func (es ESConf) PutToIndex(msg ESDoc) error { putURI := strings.Join([]string{es.Host, es.Index, es.Type, msg.MsgID}, "/")