/
realtimejourneys.go
196 lines (172 loc) · 5.74 KB
/
realtimejourneys.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
package dbwatch
import (
"context"
"encoding/json"
"time"
"github.com/adjust/rmq/v5"
"github.com/rs/zerolog/log"
"github.com/travigo/travigo/pkg/ctdf"
"github.com/travigo/travigo/pkg/database"
"github.com/travigo/travigo/pkg/redis_client"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type RealtimeJourneysWatch struct {
EventQueue rmq.Queue
}
type realtimeJourneyUpdate struct {
OperationType string `bson:"operationType"`
UpdateDescription struct {
UpdatedFields ctdf.RealtimeJourney `bson:"updatedFields"`
} `bson:"updateDescription"`
FullDocument ctdf.RealtimeJourney `bson:"fullDocument"`
FullDocumentBeforeChange ctdf.RealtimeJourney `bson:"fullDocumentBeforeChange"`
}
func NewRealtimeJourneysWatch() *RealtimeJourneysWatch {
eventQueue, err := redis_client.QueueConnection.OpenQueue("events-queue")
if err != nil {
log.Fatal().Err(err).Msg("Failed to start event queue")
}
return &RealtimeJourneysWatch{
EventQueue: eventQueue,
}
}
func (w *RealtimeJourneysWatch) Run() {
log.Info().Msg("Starting dbwatch on collection realtime_journeys")
collection := database.GetCollection("realtime_journeys")
matchPipeline := bson.D{
{
Key: "$match", Value: bson.D{
{
Key: "$and", Value: bson.A{
bson.D{{Key: "operationType", Value: "update"}}, //ignore inserts for now
bson.D{
{
Key: "$or", Value: bson.A{
bson.D{
{
Key: "updateDescription.updatedFields.cancelled",
Value: bson.D{{Key: "$exists", Value: true}},
},
},
// This is prob a bit hacky but it does work so who really cares?
bson.D{
{
Key: "fullDocument.datasource.provider",
Value: "National-Rail",
},
},
},
},
},
},
},
},
},
}
projectPipeline := bson.D{
{
Key: "$project",
Value: bson.D{
bson.E{Key: "activelytracked", Value: 0},
bson.E{Key: "datasource", Value: 0},
bson.E{Key: "timeoutdurationminutes", Value: 0},
bson.E{Key: "creationdatetime", Value: 0},
// bson.E{Key: "modificationdatetime", Value: 0},
bson.E{Key: "reliability", Value: 0},
bson.E{Key: "offset", Value: 0},
bson.E{Key: "vehiclelocation", Value: 0},
bson.E{Key: "vehiclebearing", Value: 0},
bson.E{Key: "vehicleref", Value: 0},
},
},
}
opts := options.ChangeStream().SetFullDocumentBeforeChange(options.WhenAvailable).SetFullDocument(options.WhenAvailable)
stream, err := collection.Watch(context.Background(), mongo.Pipeline{matchPipeline, projectPipeline}, opts)
if err != nil {
log.Fatal().Err(err).Msg("Failed to watch collection")
}
defer stream.Close(context.Background())
for stream.Next(context.Background()) {
var data realtimeJourneyUpdate
if err := stream.Decode(&data); err != nil {
log.Error().Err(err).Msg("Failed to decode event")
continue
}
go func(data *realtimeJourneyUpdate) {
if data.OperationType == "insert" {
log.Info().Str("id", data.FullDocument.PrimaryIdentifier).Msg("New RealtimeJourney inserted")
eventBytes, _ := json.Marshal(ctdf.Event{
Type: ctdf.EventTypeRealtimeJourneyCreated,
Timestamp: time.Now(),
Body: data.FullDocument,
})
w.EventQueue.PublishBytes(eventBytes)
} else if data.OperationType == "update" {
if data.FullDocument.PrimaryIdentifier == "" {
return
}
// Detect newly cancelled journeys
if data.UpdateDescription.UpdatedFields.Cancelled == true && !data.FullDocumentBeforeChange.Cancelled {
log.Info().Str("id", data.FullDocument.PrimaryIdentifier).Msg("RealtimeJourney has been cancelled")
eventBytes, _ := json.Marshal(ctdf.Event{
Type: ctdf.EventTypeRealtimeJourneyCancelled,
Timestamp: time.Now(),
Body: data.FullDocument,
})
w.EventQueue.PublishBytes(eventBytes)
return
}
// Checks for set or changed platforms
for id, journeyStop := range data.FullDocument.Stops {
// This shouldnt happen as why would a historical stop change platforms
if journeyStop.TimeType == ctdf.RealtimeJourneyStopTimeHistorical {
continue
}
newPlatform := journeyStop.Platform
oldJourneyPlatform := data.FullDocumentBeforeChange.Stops[id]
if oldJourneyPlatform == nil {
continue
}
oldPlatform := oldJourneyPlatform.Platform
if oldPlatform == "" && newPlatform != oldPlatform {
log.Info().
Str("id", data.FullDocument.PrimaryIdentifier).
Str("platform", newPlatform).
Msg("RealtimeJourney stop platform set")
eventBytes, _ := json.Marshal(ctdf.Event{
Type: ctdf.EventTypeRealtimeJourneyPlatformSet,
Timestamp: time.Now(),
Body: map[string]interface{}{
"RealtimeJourney": data.FullDocument,
"Stop": id,
"NewPlatform": newPlatform,
},
})
w.EventQueue.PublishBytes(eventBytes)
} else if oldPlatform != "" && newPlatform != oldPlatform {
log.Info().
Str("id", data.FullDocument.PrimaryIdentifier).
Str("oldplatform", oldPlatform).
Str("newplatform", newPlatform).
Msg("RealtimeJourney stop platform changed")
eventBytes, _ := json.Marshal(ctdf.Event{
Type: ctdf.EventTypeRealtimeJourneyPlatformChanged,
Timestamp: time.Now(),
Body: map[string]interface{}{
"RealtimeJourney": data.FullDocument,
"Stop": id,
"OldPlatform": oldPlatform,
"NewPlatform": newPlatform,
},
})
w.EventQueue.PublishBytes(eventBytes)
}
}
}
}(&data)
}
log.Error().Err(stream.Err()).Msg("realtime journey watch fell over")
w.Run() // this is a hack and a half
}