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

@@ -18,10 +18,10 @@ const Doctype = "CifMetadata"
// The type describing the CifMetadata 'type' in the database.
// This type will be moved to owlboard/go-types
type CifMetadata struct {
Doctype string `json:"type"`
LastUpdate time.Time `json:"lastUpdate"`
LastTimestamp int64 `json:"lastTimestamp"`
LastSequence int64 `json:"lastSequence"`
Doctype string `bson:"type"`
LastUpdate time.Time `bson:"lastUpdate"`
LastTimestamp int64 `bson:"lastTimestamp"`
LastSequence int64 `bson:"lastSequence"`
}
// Fetches the CifMetadata from the database, returns nil if no metadata exists - before first initialisation for example.
@@ -39,20 +39,24 @@ func GetCifMetadata() (*CifMetadata, error) {
return nil, err
}
log.Msg.Debug("Fetched CIF Metadata from database", zap.Any("Metadata", result))
return &result, nil
}
// Uses upsert to Insert/Update the CifMetadata in the database
func PutCifMetadata(metadata CifMetadata) bool {
func PutCifMetadata(metadata *CifMetadata) bool {
database := MongoClient.Database(databaseName)
collection := database.Collection(metaCollection)
options := options.Update().SetUpsert(true)
filter := bson.M{"type": Doctype}
update := bson.M{
"type": Doctype,
"LastUpdate": metadata.LastUpdate,
"LastTimestamp": metadata.LastTimestamp,
"LastSequence": metadata.LastSequence,
"$set": bson.M{
"type": Doctype,
"lastUpdate": metadata.LastUpdate,
"lastTimestamp": metadata.LastTimestamp,
"lastSequence": metadata.LastSequence,
},
}
_, err := collection.UpdateOne(context.Background(), filter, update, options)
@@ -61,14 +65,21 @@ func PutCifMetadata(metadata CifMetadata) bool {
log.Msg.Error("Error updating CIF Metadata", zap.Error(err))
return false
}
log.Msg.Info("New CIF Metadata written", zap.Time("Update time", metadata.LastUpdate))
return true
}
// Handles 'Delete' tasks from CIF Schedule updates, accepts DeleteQuery types and batches deletions.
func DeleteCifEntries(deletions []database.DeleteQuery) error {
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
// Skip if deletions is empty
if len(deletions) == 0 {
log.Msg.Info("No deletions required")
return nil
}
// Prepare deletion tasks
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
bulkDeletions := make([]mongo.WriteModel, 0, len(deletions))
for _, deleteQuery := range deletions {
@@ -95,6 +106,12 @@ func DeleteCifEntries(deletions []database.DeleteQuery) error {
// Handles 'Create' tasks for CIF Schedule updates, accepts Service structs and batches their creation.
func CreateCifEntries(schedules []database.Service) error {
// Skip if deletions is empty
if len(schedules) == 0 {
log.Msg.Info("No creations required")
return nil
}
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
models := make([]mongo.WriteModel, 0, len(schedules))