Writing full CIF data now works. Still need to work on updating CIF data.

This commit is contained in:
Fred Boniface
2024-04-08 21:08:07 +01:00
parent d9c249a47a
commit 3e5ed2c10a
13 changed files with 83 additions and 40 deletions

View File

@@ -46,6 +46,7 @@ func CheckCif(cfg *helpers.Configuration) {
// Check how many days since last update, if more than 5, run full update, else run update
daysSinceLastUpdate := howManyDaysAgo(metadata.LastUpdate)
if daysSinceLastUpdate > 5 {
log.Msg.Debug("Full Update Requested due to time since last update", zap.Int("daysSinceLastUpdate", daysSinceLastUpdate))
log.Msg.Info("Full CIF download required")
err := runCifFullDownload(cfg)
if err != nil {
@@ -62,6 +63,4 @@ func CheckCif(cfg *helpers.Configuration) {
if err != nil {
log.Msg.Error("Unable to run CIF update", zap.Error(err))
}
return
}

View File

@@ -12,4 +12,4 @@ const fullUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAu
const dataAvailable = 6
// An object representing the Europe/London timezone
var londonTimezone, err = time.LoadLocation("Europe/London")
var londonTimezone, _ = time.LoadLocation("Europe/London")

View File

@@ -21,7 +21,7 @@ func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database
PlanSpeed: parseSpeed(&input.ScheduleSegment.CifSpeed),
ScheduleStartDate: ParseCifDate(&input.ScheduleStartDate, "start"),
ScheduleEndDate: ParseCifDate(&input.ScheduleEndDate, "end"),
FirstClass: hasFirstClass(&input.ScheduleSegment.CifTrainClass),
FirstClass: hasFirstClass(&input.ScheduleSegment.CifTrainClass, &input.ScheduleSegment.SignallingId),
Catering: hasCatering(&input.ScheduleSegment.CifCateringCode),
Sleeper: hasSleeper(&input.ScheduleSegment.CifSleepers),
DaysRun: parseDaysRun(&input.ScheduleDaysRun),
@@ -37,14 +37,12 @@ func parseSpeed(CIFSpeed *string) int32 {
return 0
}
if *CIFSpeed == "" {
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 {
@@ -87,10 +85,17 @@ func isPublic(input *upstreamApi.CifScheduleLocation) bool {
}
// Ascertains whether the service offers first class
func hasFirstClass(input *string) bool {
if input == nil {
func hasFirstClass(input, signallingId *string) bool {
if input == nil || signallingId == nil {
return false
}
// Handle non passenger headcodes and ensure first class is not shown as available
firstChar := (*signallingId)[0]
if firstChar == '3' || firstChar == '4' || firstChar == '5' || firstChar == '6' || firstChar == '7' || firstChar == '8' || firstChar == '0' {
return false
}
return *input != "S"
}

View File

@@ -87,23 +87,34 @@ func TestIsPublic(t *testing.T) {
func TestHasFirstClass(t *testing.T) {
testCases := []struct {
input string
expect bool
input string
headcode string
expect bool
}{
{"", true},
{"B", true},
{"S", false},
{"", "1A00", true},
{"B", "2A05", true},
{"S", "1C99", false},
{"", "3C23", false},
{"", "5Q21", false},
{"", "5D32", false},
{"", "9O12", true},
{"B", "9D32", true},
{"", "7R43", false},
{"B", "6Y77", false},
{"", "8P98", false},
{"S", "4O89", false},
{"", "4E43", false},
}
for _, tc := range testCases {
result := hasFirstClass(&tc.input)
result := hasFirstClass(&tc.input, &tc.headcode)
if result != tc.expect {
t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result)
t.Errorf("For %s & headcode %s, expected %t, but got %t", tc.input, tc.headcode, tc.expect, result)
}
}
nilResult := hasFirstClass(nil)
nilResult := hasFirstClass(nil, nil)
if nilResult {
t.Errorf("hasFirstClass failed to handle nil pointer, expected %t, got %t", false, nilResult)
}

View File

@@ -44,11 +44,13 @@ func isSameToday(t time.Time) bool {
// Returns how many days ago `t` was compared to today
func howManyDaysAgo(t time.Time) int {
today := time.Now().In(time.UTC).Truncate(24 * time.Hour)
input := t.In(time.UTC).Truncate(24 * time.Hour)
log.Msg.Debug("Calculating how many days ago", zap.Time("Input time", t))
// Truncate both times to midnight in UTC timezone
today := time.Now().UTC().Truncate(24 * time.Hour)
input := t.UTC().Truncate(24 * time.Hour)
diff := today.Sub(input)
days := int(diff.Hours() / 24)
days := int(diff / (24 * time.Hour))
return days
}

View File

@@ -24,11 +24,11 @@ func TestHowManyDaysAgo(t *testing.T) {
input time.Time
expected int
}{
{time.Now(), 0}, // Today
{time.Now().Add(-24 * time.Hour), 1}, // Yesterday
{time.Now().Add(-48 * time.Hour), 2}, // Ereyesterday
{time.Now().Add(24 * time.Hour), -1}, // Tomorrow
{time.Now().Add(48 * time.Hour), -2}, // Overmorrow
{time.Now().In(time.UTC), 0}, // Today
{time.Now().In(time.UTC).Add(-24 * time.Hour), 1}, // Yesterday
{time.Now().In(time.UTC).Add(-48 * time.Hour), 2}, // Ereyesterday
{time.Now().In(time.UTC).Add(24 * time.Hour), -1}, // Tomorrow
{time.Now().In(time.UTC).Add(48 * time.Hour), -2}, // Overmorrow
}
for _, tc := range testCases {

View File

@@ -18,9 +18,11 @@ func processParsedCif(data *parsedData) error {
for _, item := range data.sched {
switch item.TransactionType {
case "Delete":
deleteTasks = append(deleteTasks, &item)
deleteItem := item // Create new variable to ensure repetition of pointers
deleteTasks = append(deleteTasks, &deleteItem)
case "Create":
createTasks = append(createTasks, &item)
createItem := item // Create new variable to ensure repetition of pointers
createTasks = append(createTasks, &createItem)
default:
log.Msg.Error("Unknown transaction type in CIF Schedule", zap.String("TransactionType", item.TransactionType))
}

View File

@@ -35,6 +35,7 @@ func TestGenerateMetadata(t *testing.T) {
if result == nil {
t.Errorf("generateMetadata returned nil pointer")
return // Static type checking likes this return to be here, even if it is redundant in reality.
}
if result.Doctype != expected.Doctype {

View File

@@ -40,7 +40,11 @@ func runCifFullDownload(cfg *helpers.Configuration) error {
log.Msg.Error("Error processing CIF data", zap.Error(err))
}
// Generate & Write metadata
newMeta := generateMetadata(&parsed.header)
ok := dbAccess.PutCifMetadata(newMeta)
if !ok {
log.Msg.Warn("CIF Data updated, but metadata write failed")
}
return nil
}