timetable-mgr/cif/metadata.go

70 lines
2.6 KiB
Go
Raw Normal View History

2024-04-12 20:43:03 +01:00
package cif
import (
"fmt"
"time"
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
2024-04-20 08:44:14 +01:00
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
2024-04-12 20:43:03 +01:00
)
// Evaluates last and new metadata and determines whether an update is required.
// // If last update == "full" && lastupdate after yesterday at 0600, run update.
// // Beyond that condition, comparisons are made between old and new metadata.
func checkMetadata(oldMeta *dbAccess.CifMetadata, newMeta *upstreamApi.JsonTimetableV1) (reason string, updateRequired bool) {
// Handle nil pointer - although this should be resolved in the calling function in ideal situations
if oldMeta == nil || newMeta == nil {
2024-04-20 08:44:14 +01:00
log.Debug("oldMeta or newMeta is a nil pointer.")
2024-04-12 20:43:03 +01:00
return "nil-pointer", false
}
if !isTimestampWithinLastFiveDays(newMeta.Timestamp) {
2024-04-20 08:44:14 +01:00
log.Debug("Downloaded CIF File not produced in last five days", zap.Time("file_timestamp", time.Unix(newMeta.Timestamp, 0)))
return "downloaded-data-is-too-old", false
2024-04-12 20:43:03 +01:00
}
// Handle non-matching sequence numbers between full and daily download types
// if isAfterYesterdayAt0600(oldMeta.LastUpdate) {
// return "last-update-full", true
// }
2024-04-12 20:43:03 +01:00
// Check that the new metadata sequence is as expected.
if newMeta.Metadata.Sequence == oldMeta.LastSequence+1 {
2024-04-20 08:44:14 +01:00
log.Debug("Sequence numbers", zap.Int64("New", newMeta.Metadata.Sequence), zap.Int64("Old", oldMeta.LastSequence))
2024-04-12 20:43:03 +01:00
return "correct-sequence", true
} else {
s := fmt.Sprintf("incorrect sequence, Old: %d, New: %d, Expected New: %d", oldMeta.LastSequence, newMeta.Metadata.Sequence, oldMeta.LastSequence+1)
return s, false
}
}
// Evaluates whether the given time is after yesterday at 0600
func isAfterYesterdayAt0600(t time.Time) bool {
yesterday0600 := time.Now().In(londonTimezone).AddDate(0, 0, -1)
2024-04-12 20:43:03 +01:00
yesterday0600 = time.Date(yesterday0600.Year(), yesterday0600.Month(), yesterday0600.Day(), 6, 0, 0, 0, time.UTC)
return t.After(yesterday0600)
}
// Accepts the JsonTimetableV1 struct which contains CIF File metadata and produces a DB Ready struct.
func generateMetadata(header *upstreamApi.JsonTimetableV1) *dbAccess.CifMetadata {
newMetadata := dbAccess.CifMetadata{
Doctype: dbAccess.Doctype,
LastTimestamp: header.Timestamp,
LastUpdate: time.Now().In(londonTimezone),
LastSequence: header.Metadata.Sequence,
}
return &newMetadata
}
// Accepts a unix timestamp and returns true if the timestamp is within the last five days, else false
func isTimestampWithinLastFiveDays(ts int64) bool {
timestamp := time.Unix(ts, 0)
timeNow := time.Now()
fiveDaysAgo := timeNow.AddDate(0, 0, -5)
return !timestamp.Before(fiveDaysAgo)
}