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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return "nil-pointer", false
|
|
|
|
}
|
|
|
|
|
2024-04-19 20:38:11 +01:00
|
|
|
if !isTimestampWithinLastFiveDays(newMeta.Timestamp) {
|
|
|
|
return "downloaded-data-is-too-old", false
|
2024-04-12 20:43:03 +01:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:38:11 +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 {
|
|
|
|
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 {
|
2024-04-14 22:17:31 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-04-14 21:28:17 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2024-04-19 20:38:11 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|