-
Notifications
You must be signed in to change notification settings - Fork 4
/
archiver.go
165 lines (130 loc) · 4.72 KB
/
archiver.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
package archiver
import (
"encoding/json"
"time"
"github.com/go-redis/redis"
"github.com/rijdendetreinen/gotrain/models"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var redisDb *redis.Client
// Connect initializes the Redis client
func Connect() error {
redisAddress := viper.GetString("archive.address")
redisPassword := viper.GetString("archive.password")
redisDbNumber := viper.GetInt("archive.db")
log.WithField("address", redisAddress).
WithField("db", redisDbNumber).
Info("Connecting to Redis server")
redisDb = redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: redisPassword,
DB: redisDbNumber,
})
result := redisDb.Ping()
return result.Err()
}
// ProcessService adds a service object to the queue
func ProcessService(service models.Service) {
serviceJSON, _ := json.Marshal(serviceToJSON(service))
if serviceJSON != nil {
redisDb.LPush("services", string(serviceJSON))
}
}
func serviceToJSON(service models.Service) map[string]interface{} {
response := map[string]interface{}{
"id": service.ID,
"product": service.ProductID,
"timestamp": service.Timestamp,
"service_date": service.ServiceDate,
"service_number": service.ServiceNumber,
"type": service.ServiceType,
"type_code": service.ServiceTypeCode,
"company": service.Company,
"journey_planner": service.JourneyPlanner,
"reservation_required": service.ReservationRequired,
"special_ticket": service.SpecialTicket,
"with_supplement": service.WithSupplement,
"parts": []interface{}{},
"remarks_nl": models.GetRemarks(service.Modifications, "nl"),
"remarks_en": models.GetRemarks(service.Modifications, "en"),
"tips": []interface{}{},
}
responseParts := []interface{}{}
for _, part := range service.ServiceParts {
partResponse := map[string]interface{}{
"service_number": part.ServiceNumber,
"remarks_nl": models.GetRemarks(part.Modifications, "nl"),
"remarks_en": models.GetRemarks(part.Modifications, "en"),
"tips": []interface{}{},
"stops": []interface{}{},
}
stops := part.GetStoppingStations()
responseStops := []interface{}{}
for _, stop := range stops {
responseStops = append(responseStops, serviceStopToJSON(stop))
}
partResponse["stops"] = responseStops
responseParts = append(responseParts, partResponse)
}
response["parts"] = responseParts
return response
}
func serviceStopToJSON(stop models.ServiceStop) map[string]interface{} {
stopResponse := map[string]interface{}{
"station": stop.Station,
"station_accessible": stop.StationAccessible,
"assistance_available": stop.AssistanceAvailable,
"stopping_actual": stop.StoppingActual,
"stopping_planned": stop.StoppingPlanned,
"stop_type": stop.StopType,
"do_not_board": stop.DoNotBoard,
"arrival_time": localTimeString(stop.ArrivalTime),
"arrival_platform_actual": nullString(stop.ArrivalPlatformActual),
"arrival_platform_planned": nullString(stop.ArrivalPlatformPlanned),
"arrival_delay": stop.ArrivalDelay,
"arrival_cancelled": stop.ArrivalCancelled,
"departure_time": localTimeString(stop.DepartureTime),
"departure_platform_actual": nullString(stop.DeparturePlatformActual),
"departure_platform_planned": nullString(stop.DeparturePlatformPlanned),
"departure_delay": stop.DepartureDelay,
"departure_cancelled": stop.DepartureCancelled,
"remarks_nl": models.GetRemarks(stop.Modifications, "nl"),
"remarks_en": models.GetRemarks(stop.Modifications, "en"),
"tips": []interface{}{},
"material": materialsToJSON(stop.Material),
}
return stopResponse
}
func localTimeString(originalTime time.Time) *string {
if !originalTime.IsZero() {
formattedTime := originalTime.Local().Format(time.RFC3339)
return &formattedTime
}
return nil
}
func nullString(value string) *string {
if value == "" {
return nil
}
return &value
}
func materialsToJSON(materials []models.Material) []map[string]interface{} {
materialsResponse := []map[string]interface{}{}
for _, material := range materials {
materialsResponse = append(materialsResponse, materialToJSON(material))
}
return materialsResponse
}
func materialToJSON(material models.Material) map[string]interface{} {
materialResponse := map[string]interface{}{
"type": material.NaterialType,
"accessible": material.Accessible,
"number": material.NormalizedNumber(),
"position": material.Position,
"remains_behind": material.RemainsBehind,
"destination": material.DestinationActual.NameLong,
"destination_code": material.DestinationActual.Code,
}
return materialResponse
}