/
store2es_web.go
122 lines (90 loc) · 2.54 KB
/
store2es_web.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"os"
//"bufio"
"fmt"
"time"
//"encoding/base64"
//"net/http"
//"github.com/gorilla/mux"
"strings"
"strconv"
"github.com/kellydunn/golang-geo"
"github.com/xuyu/goredis"
"gopkg.in/olivere/elastic.v2"
)
func FetchFromRedis(messages chan string, RedisClient *goredis.Redis) {
data, _ := RedisClient.RPop("Tqueue")
if data != nil {
fmt.Println("Data: ", string(data))
messages <- string(data)
}
}
func Store2ES(ElasticClient *elastic.Client, file *os.File, val string) {
//index := time.Now().Format("2006.01.02")
res, err := ElasticClient.Index().
Index("brighten").
Type("gps").
Id(time.Now().Format("20060102150405")).
BodyString( val ).
Do()
if err != nil {
fmt.Println("After ES insert:", err)
} else {
file.WriteString((val + "\n"))
}
fmt.Println("ES response - status:", res.Created, ", id:", res.Id, ", index:", res.Index,", type:", res.Type)
}
func StoreDistance(val string) string {
fmt.Println("Current val: ", val)
dataPoints := strings.Split(val, "},")
coordinates := []*geo.Point{}
total := 0.000
for i := range dataPoints {
values := strings.Split(dataPoints[i], ",")
lat,_ := strconv.ParseFloat(values[3], 64)
lng,_ := strconv.ParseFloat(values[5], 64)
//fmt.Println(lat, " ", lng)
pt := geo.NewPoint(lat, lng)
coordinates = append(coordinates, pt)
if i > 0 {
total += coordinates[i-1].GreatCircleDistance(coordinates[i])
}
//fmt.Println("\n\nResult[", i, "]: \n", dataPoints[i])
}
dist := ", \"distance\":" + strconv.FormatFloat(total, 'f', 15, 64)
result_final := strings.Join(dataPoints, "},")
result_final = result_final[0:len(result_final)-1] + dist + "}"
// fmt.Println("great circle distance:", total)
fmt.Println("\n\nResult_final: ", result_final)
return result_final
}
func main() {
ElasticClient, err := elastic.NewClient()
if err != nil {
fmt.Println( "ES start error: ", err)
} else {
fmt.Println( "Connection to ES server established successfully...")
}
RedisClient, err2 := goredis.Dial(&goredis.DialConfig{Address: "0.0.0.0:6379"})
if err2 != nil {
fmt.Println( "Redis start error: ", err)
} else {
fmt.Println( "Connection to redis-server established successfully...")
}
file, _ := os.OpenFile("index.txt", os.O_APPEND|os.O_WRONLY, 0666)
messages := make(chan string)
go func() {
for {
FetchFromRedis(messages, RedisClient)
}
}()
for {
select {
case val := <- messages:
file.WriteString(string(val) + '\n')
val = StoreDistance(val)
Store2ES(ElasticClient, file, val)
}
}
}