Increase use of pointers to minimise memory usage
Some checks failed
Go Test / test (push) Failing after 1m26s
Some checks failed
Go Test / test (push) Failing after 1m26s
This commit is contained in:
parent
3481c4e314
commit
4a7bcd7f80
55
cif/convert.go
Normal file
55
cif/convert.go
Normal file
@ -0,0 +1,55 @@
|
||||
package cif
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.fjla.uk/owlboard/go-types/pkg/database"
|
||||
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||
)
|
||||
|
||||
func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database.Service, error) {
|
||||
output := database.Service{
|
||||
TransactionType: input.TransactionType,
|
||||
StpIndicator: input.CifStpIndicator,
|
||||
Vstp: vstp, // Simply uses the value passed into the function
|
||||
Operator: input.AtocCode,
|
||||
TrainUid: input.CifTrainUid,
|
||||
Headcode: input.ScheduleSegment.SignallingId,
|
||||
PowerType: input.ScheduleSegment.CifPowerType,
|
||||
PlanSpeed: parseSpeed(&input.ScheduleSegment.CifSpeed),
|
||||
ScheduleStartDate: ParseCifDate(&input.ScheduleStartDate, "start"),
|
||||
ScheduleEndDate: ParseCifDate(&input.ScheduleEndDate, "end"),
|
||||
DaysRun: parseDaysRun(&input.ScheduleDaysRun),
|
||||
Stops: parseStops(&input.ScheduleSegment.ScheduleLocation),
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
}
|
||||
|
||||
// Converts CifSpeed input string to an int32, automatically corrects VSTP speeds which are not actual speed values
|
||||
func parseSpeed(CIFSpeed *string) int32 {
|
||||
log.Msg.Debug("CIFSpeed Input: '" + *CIFSpeed + "'")
|
||||
if *CIFSpeed == "" || CIFSpeed == nil {
|
||||
log.Msg.Debug("Speed data not provided")
|
||||
return int32(0)
|
||||
}
|
||||
actualSpeed, exists := helpers.SpeedMap[*CIFSpeed]
|
||||
if !exists {
|
||||
actualSpeed = *CIFSpeed
|
||||
}
|
||||
log.Msg.Debug("Corrected Speed: " + actualSpeed)
|
||||
|
||||
speed, err := strconv.ParseInt(actualSpeed, 10, 32)
|
||||
if err != nil {
|
||||
log.Msg.Warn("Unable to parse speed: " + *CIFSpeed + ", returning 0")
|
||||
return int32(0)
|
||||
}
|
||||
return int32(speed)
|
||||
}
|
||||
|
||||
func parseStops(input *[]upstreamApi.CifScheduleLocation) []database.Stop {
|
||||
output := make([]database.Stop, 0, len(*input))
|
||||
return output
|
||||
}
|
31
cif/convert_test.go
Normal file
31
cif/convert_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package cif
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseSpeed(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input *string
|
||||
expected int32
|
||||
}{
|
||||
{strPtr("075"), 75},
|
||||
{strPtr("125"), 125},
|
||||
{strPtr("40"), 40},
|
||||
{strPtr("040"), 40},
|
||||
{strPtr("134"), 60},
|
||||
{strPtr("179"), 80},
|
||||
{strPtr("186"), 186},
|
||||
{strPtr("417"), 186},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := parseSpeed(tc.input)
|
||||
|
||||
if result != tc.expected {
|
||||
t.Errorf("For speed: %s, expected: %d, but got: %d", tc.input, tc.expected, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
@ -70,11 +70,11 @@ func generateUpdateDays(days int) []time.Time {
|
||||
}
|
||||
|
||||
// Parses CIF Schedule Start/End Dates (YYYY-MM-DD) into time.Time types (00:00:00 for start, 23:59:59 for end)
|
||||
func ParseCifDate(input, startOrEnd string) time.Time {
|
||||
func ParseCifDate(input *string, startOrEnd string) time.Time {
|
||||
layout := "2006-01-02" // Layout of input
|
||||
t, err := time.ParseInLocation(layout, input, londonTimezone)
|
||||
t, err := time.ParseInLocation(layout, *input, londonTimezone)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error parsing date string", zap.String("date string", input), zap.Error(err))
|
||||
log.Msg.Error("Error parsing date string", zap.String("date string", *input), zap.Error(err))
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ func ParseCifDate(input, startOrEnd string) time.Time {
|
||||
} else if startOrEnd == "end" {
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, londonTimezone)
|
||||
} else {
|
||||
log.Msg.Error("Error parsing date string", zap.String("date string", input), zap.Error(err))
|
||||
log.Msg.Error("Error parsing date string", zap.String("date string", *input), zap.Error(err))
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
@ -91,10 +91,10 @@ func ParseCifDate(input, startOrEnd string) time.Time {
|
||||
}
|
||||
|
||||
// Parses CIF days_run field and converts to array of day strings
|
||||
func parseDaysRun(daysBinary string) []string {
|
||||
func parseDaysRun(daysBinary *string) []string {
|
||||
shortDays := []string{"m", "t", "w", "th", "f", "s", "su"}
|
||||
var result []string
|
||||
for i, digit := range daysBinary {
|
||||
for i, digit := range *daysBinary {
|
||||
if digit == '1' {
|
||||
result = append(result, shortDays[i])
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ func TestParseCifDate(t *testing.T) {
|
||||
layout := "2006-01-02 15:04:05" // Layout for printing times in error cases.
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := ParseCifDate(tc.dateString, tc.startOrEnd)
|
||||
result := ParseCifDate(&tc.dateString, tc.startOrEnd)
|
||||
if result != tc.expect {
|
||||
t.Errorf("For datestring %s, startOrEnd %s, expected %s, but got %s", tc.dateString, tc.startOrEnd, tc.expect.Format(layout), result.Format(layout))
|
||||
}
|
||||
@ -122,7 +122,7 @@ func TestParseDaysRun(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := parseDaysRun(tc.input)
|
||||
result := parseDaysRun(&tc.input)
|
||||
if !reflect.DeepEqual(result, tc.expect) {
|
||||
t.Errorf("For input %s, expected %v, but got %v", tc.input, tc.expect, result)
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func doDeletions(deletions []*upstreamApi.JsonScheduleV1) error {
|
||||
deleteQueries := make([]database.DeleteQuery, 0)
|
||||
for _, item := range deletions {
|
||||
query := database.DeleteQuery{
|
||||
ScheduleStartDate: ParseCifDate(item.ScheduleStartDate, "start"),
|
||||
ScheduleStartDate: ParseCifDate(&item.ScheduleStartDate, "start"),
|
||||
StpIndicator: item.CifStpIndicator,
|
||||
TrainUid: item.CifTrainUid,
|
||||
}
|
||||
@ -70,10 +70,12 @@ func doCreations(creations []*upstreamApi.JsonScheduleV1) error {
|
||||
|
||||
createDocuments := make([]database.Service, 0)
|
||||
for _, item := range creations {
|
||||
document := database.Service{}
|
||||
// Do type conversion here - REMOVE THIS LOG LINE, IT WILL CAUSE 10000s of log entries
|
||||
log.Msg.Debug("item", zap.Any("item", item))
|
||||
createDocuments = append(createDocuments, document)
|
||||
document, err := ConvertServiceType(item, false)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error converting JsonSchedule to Service type", zap.Error(err))
|
||||
}
|
||||
|
||||
createDocuments = append(createDocuments, *document)
|
||||
}
|
||||
|
||||
err := dbAccess.CreateCifEntries(createDocuments)
|
||||
|
Loading…
Reference in New Issue
Block a user