PIS module ready for testing
This commit is contained in:
45
pis/data.go
45
pis/data.go
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user