Parser now almost complete

This commit is contained in:
Fred Boniface 2023-07-20 21:48:43 +01:00
parent 362779f7de
commit 9526f7da9d
3 changed files with 48 additions and 23 deletions

View File

@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/mq-client
go 1.19 go 1.19
require ( require (
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2 git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a
github.com/go-stomp/stomp/v3 v3.0.5 github.com/go-stomp/stomp/v3 v3.0.5
go.mongodb.org/mongo-driver v1.12.0 go.mongodb.org/mongo-driver v1.12.0
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0

View File

@ -14,6 +14,20 @@ git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc h1:7s+svu/FOU/q
git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2 h1:FOLnHCAL/2fnjC/TLpQkt4WSMzijcDrtyf0YdiyZIbo= git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2 h1:FOLnHCAL/2fnjC/TLpQkt4WSMzijcDrtyf0YdiyZIbo=
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5 h1:oNEoHfifZc/F/z+4T8wJ8Ky68vzt/OxL7E0iFhLoh9s=
git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d h1:NIVo66/uvUXV5lvfdLHLyFRXMGXk/iG+IemDU1Iu5JM=
git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e h1:KhnFsS2lumpPwhgVQKTGWXiIOd2KCh0phVSH4/JATAE=
git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220 h1:XsxDYTm5s2O9WQPDtl7LvvtlrKnmaKCZCvZpXl6xaDs=
git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07 h1:jGnHCl5UZi9y65Uqy2nW65UD4zJEBTWB9+10qHlbCTg=
git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4 h1:HeHukiRnDzXNDp/NaqI2n9WWChoglx1yRS620rgvj6o=
git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a h1:JvDUM2sIawyyLKhIOgciAj0PpVWziYlXb+RA2IgphMg=
git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/database"
@ -14,25 +15,29 @@ import (
// Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct // Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct
func unmarshalData(jsonData string) database.Service { func unmarshalData(jsonData string) database.Service {
log.Msg.Debug("Unmarshalling message body") var schedule upstreamApi.MsgData
fmt.Println(jsonData) err := json.Unmarshal([]byte(jsonData), &schedule)
log.Msg.Debug("Converting to byte array")
jsonDataBytes := []byte(jsonData)
var schedule upstreamApi.VstpMsg
err := json.Unmarshal(jsonDataBytes, &schedule)
if err != nil { if err != nil {
log.Msg.Error("Unable to unmarshal message body: " + err.Error()) log.Msg.Error("Unable to unmarshal message body: " + err.Error())
//return err //return err
} }
log.Msg.Debug("Unmarshalling Complete") log.Msg.Debug("Unmarshalling Complete")
fmt.Println(schedule)
saveToFile(schedule, "unmarshalled") if schedule.Data.CIFMsg.ScheduleSegment == nil {
return formatData(schedule.CIFMsg) log.Msg.Warn("ScheduleSegment is nil")
return database.Service{}
} else if len(schedule.Data.CIFMsg.ScheduleSegment) == 0 {
log.Msg.Warn("ScheduleSegment is empty")
return database.Service{}
}
saveToFile(schedule, "unmarshalled") // For Debugging Only
return formatData(&schedule.Data.CIFMsg)
} }
// Transforms the upstreamApi.Schedule type into a database.Service type // Transforms the upstreamApi.Schedule type into a database.Service type
func formatData(dataInput *upstreamApi.Schedule) database.Service { func formatData(dataInput *upstreamApi.Schedule) database.Service {
log.Msg.Debug("ScheduleSegment length: " + fmt.Sprint(len(dataInput.ScheduleSegment))) log.Msg.Debug("ScheduleSegment length: " + fmt.Sprint(len(dataInput.ScheduleSegment)))
log.Msg.Debug("Printing dataInput to console:")
service := database.Service{ service := database.Service{
TransactionType: dataInput.TransactionType, TransactionType: dataInput.TransactionType,
StpIndicator: dataInput.CIFSTPIndicator, StpIndicator: dataInput.CIFSTPIndicator,
@ -60,8 +65,8 @@ func parseSpeed(CIFSpeed string) int32 {
speed, err := strconv.ParseInt(actualSpeed, 10, 32) speed, err := strconv.ParseInt(actualSpeed, 10, 32)
if err != nil { if err != nil {
log.Msg.Warn("Unable to parse speed: " + CIFSpeed + " " + actualSpeed) log.Msg.Warn("Unable to parse speed: " + CIFSpeed + ", returning nil")
return 0 return int32(0)
} }
return int32(speed) return int32(speed)
} }
@ -74,7 +79,6 @@ func parseDate(dateString string, end bool) time.Time {
log.Msg.Error("Unable to parse date: " + dateString) log.Msg.Error("Unable to parse date: " + dateString)
return time.Time{} return time.Time{}
} }
log.Msg.Debug("Parsed date: " + date.String())
var hour, minute, second, nanosecond int var hour, minute, second, nanosecond int
location := time.UTC location := time.UTC
@ -85,6 +89,7 @@ func parseDate(dateString string, end bool) time.Time {
} }
dateWithTime := time.Date(date.Year(), date.Month(), date.Day(), hour, minute, second, nanosecond, location) dateWithTime := time.Date(date.Year(), date.Month(), date.Day(), hour, minute, second, nanosecond, location)
log.Msg.Debug("Parsed date: " + dateWithTime.String())
return dateWithTime return dateWithTime
} }
@ -107,12 +112,12 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop {
for _, loc := range inputStops { for _, loc := range inputStops {
stop := database.Stop{ stop := database.Stop{
PublicDeparture: getStringPointer(loc.PublicDepartureTime), PublicDeparture: parseTimeStrings(loc.PublicDepartureTime),
WttDeparture: getStringPointer(loc.ScheduledDepartureTime), WttDeparture: parseTimeStrings(loc.ScheduledDepartureTime),
PublicArrival: getStringPointer(loc.PublicArrivalTime), PublicArrival: parseTimeStrings(loc.PublicArrivalTime),
WttArrival: getStringPointer(loc.ScheduledArrivalTime), WttArrival: parseTimeStrings(loc.ScheduledArrivalTime),
IsPublic: loc.PublicDepartureTime != "" || loc.PublicArrivalTime != "", IsPublic: strings.TrimSpace(loc.PublicDepartureTime) != "" || strings.TrimSpace(loc.PublicArrivalTime) != "",
Tiploc: loc.Location.Tiploc.TiplocID, Tiploc: loc.Tiploc.Tiploc.TiplocId,
} }
stops = append(stops, stop) stops = append(stops, stop)
@ -121,9 +126,15 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop {
return stops return stops
} }
func getStringPointer(s string) *string { func parseTimeStrings(t string) string {
if s == "" { if t == "" {
return nil return t
}
strippedT := strings.TrimSpace(t)
if strippedT == "" {
return ""
} else {
return strippedT[:4]
} }
return &s
} }