-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
246 lines (198 loc) · 5.97 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"syscall"
hsl "github.com/dmw2151/hsldatabridge"
redis "github.com/go-redis/redis/v8"
"github.com/mmcloughlin/geohash"
log "github.com/sirupsen/logrus"
)
var (
msgBroker = hsl.NewMsgBroker(1024)
ctx, cancel = context.WithCancel(context.Background())
_ = hsl.InitMQTTClient(msgBroker)
redisClient = hsl.InitRedisClient(ctx)
nWorkers = 10 // Set Variable for System CPU cap...
)
// statJourneyID checks if a journeyID already exists in the set of previously
// seen JourneyID; attempts to SADD. Returns True if journey exists....
func statJourneyID(client *redis.Client, key string, journeyID string) bool {
resp, err := client.Do(
ctx, "SADD", key, journeyID,
).Result()
if err != nil {
return false
}
// If resp == 0; then already exists...
return resp.(int64) == 0
}
// createTimeSeriesPair - create a timeseries of events and maps it to
// auto-update a secondary time series with a compaction rule...
//
// WARNING: by default this setup ONLY allows for mapping 1:1 src to target
// event timeseries, should consider using something better to customize rules
func createTimeSeriesPair(client *redis.Client, journeyID string, label string) {
// Initialize Creation Pipeline For a Statistic
pipe := client.TxPipeline()
// Create Parent && Child Series
pipe.Do(
ctx, "TS.CREATE", fmt.Sprintf("positions:%s:%s", journeyID, label),
)
pipe.Do(
ctx, "TS.CREATE", fmt.Sprintf("positions:%s:%s:agg", journeyID, label),
"RETENTION", 120*60*1000, "LABELS", label, 1, "journey", journeyID,
)
_, err := pipe.Exec(ctx)
if err != nil {
log.WithFields(
log.Fields{
"JourneyID": journeyID,
"Series": fmt.Sprintf("positions:%s:%s", journeyID, label),
"ChildSeries": fmt.Sprintf("positions:%s:%s:agg", journeyID, label),
},
).Warn("Create TimeSeries Root Series Failed: ", err)
}
// Using a second pipe, create a rule, split into 2 stages to ensure parent && child
// series exist first....
pipe.Do(
ctx, "TS.CREATERULE",
fmt.Sprintf("positions:%s:%s", journeyID, label),
fmt.Sprintf("positions:%s:%s:agg", journeyID, label),
"AGGREGATION", "LAST", 15000,
)
_, err = pipe.Exec(ctx)
if err != nil {
log.WithFields(
log.Fields{
"JourneyID": journeyID,
"Series": fmt.Sprintf("positions:%s:%s", journeyID, label),
"ChildSeries": fmt.Sprintf("positions:%s:%s:agg", journeyID, label),
},
).Warn("Create TimeSeries Pair Failed: ", err)
}
}
// Launch some workers here...
func writeRedis(ctx context.Context, C <-chan []byte, client *redis.Client) {
for msg := range C {
// Receive the content of the MQTT message and de-serialize bytes into
// struct
e := &hsl.EventHolder{}
err := hsl.DeserializeMQTTBody(msg, e)
if err != nil {
switch err := err.(type) {
case *hsl.MQTTValidationError:
// Most common error is Missing or Bad Coords; See defn for
// `hsl.MQTTValidationError` for more...
log.WithFields(log.Fields{"Body": e}).Debug("%+v", err)
default:
// The entry was not deserializable into a known msg types
// Most often an error from the source feed, e.g the feed published
// a route as 123 instead of "123", fail to unmarshal string into Go
log.WithFields(log.Fields{"Body": e}).Debug("%+v", err)
}
continue
}
// Main procedure for adding a series keys, values to the redis
// instance
// MEMOIZE!!
journeyID := e.VP.GetEventHash()
// Check if JourneyID is known...
journeyExists := statJourneyID(client, "journeyID", journeyID)
// if not...then create the timeseries pair for the journey...
if !(journeyExists) {
log.WithFields(
log.Fields{
"JourneyID": journeyID,
},
).Info("New Journey Registered")
createTimeSeriesPair(client, journeyID, "speed")
createTimeSeriesPair(client, journeyID, "gh")
}
// Write The incoming event to multiple locations using
// a single client Tx pipeline, cuts back on some network
// round-trip
pipe := client.TxPipeline()
// 1. Publish full body...
pipe.Publish(
ctx, "currentLocationsPS", msg,
)
// 2. XADD the full event body to a stream of events, these
// are swept up by a gears function and written behind to a DB
// every XXXXms
pipe.XAdd(
ctx, &redis.XAddArgs{
Stream: "events",
Values: []interface{}{
"rt", e.VP.RouteID,
"jid", journeyID,
"lat", e.VP.Lat,
"lng", e.VP.Lng,
"time", e.VP.Timestamp,
"spd", e.VP.Spd,
"acc", e.VP.Acc,
"dl", e.VP.DeltaToSchedule,
},
},
)
// 3. TS.ADD a series of statistics to the timeseries created
// by `createTimeSeriesPair`
pipe.Do(
ctx,
"TS.ADD", fmt.Sprintf("positions:%s:speed", journeyID),
"*",
e.VP.Spd,
"RETENTION", 60*1000,
"CHUNK_SIZE", 16,
"ON_DUPLICATE", "LAST",
)
pipe.Do(
ctx,
"TS.ADD", fmt.Sprintf("positions:%s:gh", journeyID),
"*",
geohash.EncodeIntWithPrecision(e.VP.Lat, e.VP.Lng, 64),
"RETENTION", 60*1000,
"ON_DUPLICATE", "LAST",
)
// Execute Pipe!
_, err = pipe.Exec(ctx)
// Failed to Write an Event
if err != nil {
if err, ok := err.(net.Error); ok {
log.Errorf("Redis Down: %+v", err)
}
log.WithFields(
log.Fields{
"Body": fmt.Sprintf("positions:%s:*", journeyID),
},
).Errorf("Failed to Write Event: %+v", err)
} else {
log.WithFields(
log.Fields{"Journey": journeyID},
).Debug("Wrote Event")
}
}
}
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
}
func main() {
quitChannel := make(chan os.Signal, 1)
// Start Staging Channel -> Redis Workers
for i := 0; i < nWorkers; i++ {
go writeRedis(ctx, msgBroker.StagingC, redisClient)
}
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
}