-
Notifications
You must be signed in to change notification settings - Fork 1
/
elasticsearch.go
51 lines (42 loc) · 1.37 KB
/
elasticsearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package twitchchatscraper
import (
"fmt"
"time"
"github.com/sorcix/irc"
"gopkg.in/olivere/elastic.v3"
log "github.com/cihub/seelog"
)
type ElasticBroker struct {
inputChannel <-chan *irc.Message
elastiClient *elastic.Client
}
type TwitchMessage struct {
Channel string
Message string
From string
Timestamp time.Time `json:"@timestamp"`
}
func (e *ElasticBroker) Connect(givenUrl string) chan<- *irc.Message {
inputChannel := make(chan *irc.Message, 10000)
e.inputChannel = inputChannel
var clientError error
e.elastiClient, clientError = elastic.NewClient(elastic.SetURL(givenUrl), elastic.SetSniff(false))
if clientError != nil {
log.Errorf("Error connecting to elasticsearch: %s", clientError.Error())
}
go e.listenForMessages()
return inputChannel
}
func (e *ElasticBroker) listenForMessages() {
bulkRequest := e.elastiClient.Bulk()
for {
message := <-e.inputChannel
twitchMessage := TwitchMessage{Channel: message.Params[0], Message: message.Trailing, From: message.User, Timestamp: time.Now()}
indexToInsertInto := fmt.Sprintf("twitch-%s", twitchMessage.Timestamp.Format("2006.01.02"))
bulkRequest.Add(elastic.NewBulkIndexRequest().Index(indexToInsertInto).Type("chatmessage").Doc(twitchMessage))
if bulkRequest.NumberOfActions() > 999 {
log.Debugf("Applying %d bulk operations", bulkRequest.NumberOfActions())
bulkRequest.Do()
}
}
}