timetable-mgr/cif/metadata.go

52 lines
1.9 KiB
Go

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
}
// Handle non-matching sequence numbers between full and daily download types
if isAfterYesterdayAt0600(oldMeta.LastUpdate) {
return "last-update-full", true
}
// 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 {
yesterday0600 := time.Now().In(londonTimezone).AddDate(0, 0, -1)
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
}