Import TIPLOCs

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2023-02-11 14:57:43 +00:00
parent 7144f30ca8
commit e3c919947f
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
5 changed files with 208 additions and 0 deletions

View file

@ -0,0 +1,14 @@
package config
import (
"github.com/rs/zerolog/log"
"os"
)
var RedisHostname = os.Getenv("TRAINS_REDIS_HOST")
func init() {
if len(RedisHostname) == 0 {
log.Warn().Msg("empty TRAINS_REDIS_HOST")
}
}

25
feedprocessor/main.go Normal file
View file

@ -0,0 +1,25 @@
package main
import (
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/schedule"
"github.com/rs/zerolog/log"
"os"
)
func main() {
log.Info().Msg("starting")
if len(os.Args) < 2 {
log.Fatal().Msg("missing start parameter - use feed or schedule-ingest")
}
do := os.Args[1]
if do == "feed" {
// TODO
} else if do == "schedule-ingest" {
if err := schedule.Run(); err != nil {
log.Fatal().Err(err).Str("location", "schedule-ingest").Send()
}
}
}

View file

@ -0,0 +1,33 @@
package schedule
import (
"context"
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/config"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"os"
)
func Run() error {
log.Info().Msg("starting schedule ingest")
if len(os.Args) < 3 {
return errors.New("missing schedule file path")
}
fname := os.Args[2]
client := redis.NewClient(&redis.Options{
Addr: config.RedisHostname,
Password: "",
DB: 0,
})
_, err := client.Ping(context.Background()).Result()
if err != nil {
return errors.Wrap(err, "failed to PING Redis")
}
return LoadFile(client, fname)
}

View file

@ -0,0 +1,122 @@
package schedule
import (
"bufio"
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"io"
"os"
)
func LoadFile(client *redis.Client, filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
n := 0
_, _ = fmt.Fprintf(os.Stderr, "%d\r", 0)
rd := bufio.NewReader(f)
for {
line, err := rd.ReadBytes('\n')
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
record := new(Record)
if err := json.Unmarshal(line, record); err != nil {
log.Debug().Msg(err.Error())
log.Debug().Bytes("record", line).Send()
return err
}
if record.TIPLOC != nil {
r := client.HMSet(context.Background(), fmt.Sprintf("tiploc:%s", record.TIPLOC.Code), record.TIPLOC.ToMap())
if err := r.Err(); err != nil {
return err
}
}
if record.TIPLOC != nil || record.Schedule != nil {
}
n += 1
if n%1000 == 0 {
_, _ = fmt.Fprintf(os.Stderr, "%d\r", n)
}
}
_, _ = fmt.Fprintf(os.Stderr, "%d records processed\n", n)
return nil
}
type Record struct {
//TimetableDecl any `json:"JsonTimetableV1"`
TIPLOC *TIPLOC `json:"TiplocV1"`
// TODO: Associations?
Schedule *Schedule `json:"JsonScheduleV1"`
}
type TIPLOC struct {
Code string `json:"tiploc_code"`
Description string `json:"tps_description"`
HumanDescription string `json:"description"`
}
func (t *TIPLOC) ToMap() map[string]interface{} {
mp := map[string]interface{}{
"description": t.Description,
}
if t.HumanDescription != "" {
mp["humanDescription"] = t.HumanDescription
}
return mp
}
type Schedule struct {
CIFStpIndicator string `json:"CIF_stp_indicator"`
CIFTrainUid string `json:"CIF_train_uid"`
ScheduleDaysRuns string `json:"schedule_days_runs"`
ScheduleEndDate string `json:"schedule_end_date"`
ScheduleSegment *ScheduleSegment `json:"schedule_segment"`
ScheduleStartDate string `json:"schedule_start_date"`
TrainStatus string `json:"train_status"`
TransactionType string `json:"transaction_type"`
}
type ScheduleSegment struct {
SignallingId string `json:"signalling_id"`
CIFTrainCategory string `json:"CIF_train_category"`
CIFHeadcode string `json:"CIF_headcode"`
CIFSpeed string `json:"CIF_speed"`
CIFTrainClass string `json:"CIF_train_class"`
ScheduleLocation []*ScheduleLocation `json:"schedule_location"`
}
type ScheduleLocation struct {
LocationType string `json:"location_type"`
RecordIdentity string `json:"record_identity"`
TiplocCode string `json:"tiploc_code"`
TiplocInstance *string `json:"tiploc_instance"`
Departure *string `json:"departure,omitempty"`
PublicDeparture *string `json:"public_departure,omitempty"`
Platform *string `json:"platform"`
EngineeringAllowance *string `json:"engineering_allowance,omitempty"`
PathingAllowance *string `json:"pathing_allowance"`
PerformanceAllowance *string `json:"performance_allowance"`
Arrival *string `json:"arrival,omitempty"`
Pass *string `json:"pass,omitempty"`
PublicArrival *string `json:"public_arrival,omitempty"`
}

14
go.mod
View file

@ -1,3 +1,17 @@
module git.tdpain.net/codemicro/hacknotts23
go 1.19
require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.0
)
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/redis/go-redis/v9 v9.0.2 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)