package cif import ( "fmt" "time" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" "git.fjla.uk/owlboard/timetable-mgr/dbAccess" "git.fjla.uk/owlboard/timetable-mgr/log" "go.uber.org/zap" ) // 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 { log.Debug("oldMeta or newMeta is a nil pointer.") return "nil-pointer", false } if !isTimestampWithinLastFiveDays(newMeta.Timestamp) { 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 } // 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 { log.Debug("Sequence numbers", zap.Int64("New", newMeta.Metadata.Sequence), zap.Int64("Old", oldMeta.LastSequence)) 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 } // 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) }