PIS module ready for testing

This commit is contained in:
Fred Boniface
2024-10-23 11:42:47 +01:00
parent 0320197147
commit e8e715aa37
7 changed files with 155 additions and 32 deletions

View File

@@ -5,11 +5,15 @@ import (
"sort"
"strings"
"git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
// Process the YAML data to a struct
func processYaml(yamlStr string) (*[]PisData, error) {
func processYaml(yamlStr string) (*[]database.PIS, error) {
var pis []PisData
// Unmarshal the YAML data into a slice of PisData
@@ -24,7 +28,9 @@ func processYaml(yamlStr string) (*[]PisData, error) {
return nil, err
}
return &pis, nil
documents, err := convertPisForDatabase(&pis)
return documents, nil
}
// Deduplicate data in place and return error if failed
@@ -46,7 +52,42 @@ func deduplicateCodes(pis *[]PisData) error {
return nil
}
// Join slice elements to a string for comparison
func stopsToString(stops []string) string {
sort.Strings(stops)
return strings.Join(stops, ",")
}
// Convert PisData to database.PIS
func convertPisForDatabase(in *[]PisData) (*[]database.PIS, error) {
var out []database.PIS
for _, code := range *in {
var document database.PIS
document.Code = code.Code
document.Operator = code.Operator
document.Stops = code.Stops
document.Tiplocs = GetTiplocsFromCrs(code.Stops)
out = append(out, document)
}
return &out, nil
}
// Return a list of TIPLOCS from a list of CRS using Database Lookups
func GetTiplocsFromCrs(crsList []string) (TiplocList []string) {
for _, crs := range crsList {
alpha := strings.ToUpper(crs)
tiploc, err := dbAccess.GetTiplocFromCrs(alpha)
if err != nil {
log.Warn("Unable to find matching TIPLOC for 3ALPHA", zap.String("3ALPHA", alpha), zap.Error(err))
return
}
TiplocList = append(TiplocList, strings.ToUpper(tiploc))
}
return
}

View File

@@ -9,6 +9,10 @@ import (
"os"
"path/filepath"
"strings"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
const (
@@ -26,13 +30,11 @@ func runUpdate(tarballUrl string) error {
// Extract to disk
file, err := os.Open(destPath)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return err
}
defer file.Close()
if err := extractFiles(file, extractPath); err != nil {
fmt.Printf("Error extracting file: %v\n", err)
return err
}
@@ -48,8 +50,23 @@ func runUpdate(tarballUrl string) error {
}
fmt.Println(&pisSlice)
// Replace db contents with new pis data
// Ensure indeces are present
err = dbAccess.DropCollection(dbAccess.PisCollection)
if err != nil {
return err
}
count, err := dbAccess.PutPisData(pisSlice)
if err != nil {
return err
}
log.Info("Insterted new PIS Data", zap.Int64("PIS Codes", count))
err = dbAccess.CreatePisIndeces()
if err != nil {
log.Error("Failed to create PIS Indeces, poor performance expected", zap.Error(err))
}
// Cleanup files
cleanupFiles(destPath, extractPath)