Skip to content

Commit

Permalink
improve handling of allegris page
Browse files Browse the repository at this point in the history
- error state
- link to flight page using allegris filter
- improve filters on flight page to allow filtering for allegris
  • Loading branch information
its-felix committed Nov 4, 2024
1 parent d6bad42 commit 69ded24
Show file tree
Hide file tree
Showing 11 changed files with 641 additions and 267 deletions.
47 changes: 28 additions & 19 deletions go/api/data/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ type Aircraft struct {
Name string `json:"name"`
}

type RouteAndRanges struct {
DepartureAirport string `json:"departureAirport"`
ArrivalAirport string `json:"arrivalAirport"`
Ranges xtime.LocalDateRanges `json:"ranges"`
type RouteAndRange struct {
DepartureAirport string `json:"departureAirport"`
ArrivalAirport string `json:"arrivalAirport"`
Range xtime.LocalDateRange `json:"range"`
}

type MinimalS3Client interface {
Expand Down Expand Up @@ -484,8 +484,8 @@ func (h *Handler) seatMapS3Key(fn common.FlightNumber, departureAirport, arrival
return fmt.Sprintf("tmp/seatmap/%s/%s/%s/%s/%s.json", fn.String(), departureAirport, arrivalAirport, departureDate.String(), cabinClass)
}

func (h *Handler) QuerySchedules(ctx context.Context, airline common.AirlineIdentifier, aircraftType, aircraftConfigurationVersion string) (map[common.FlightNumber][]RouteAndRanges, error) {
result := make(map[common.FlightNumber][]RouteAndRanges)
func (h *Handler) QuerySchedules(ctx context.Context, airline common.AirlineIdentifier, aircraftType, aircraftConfigurationVersion string) (map[common.FlightNumber][]RouteAndRange, error) {
result := make(map[common.FlightNumber][]RouteAndRange)
return result, h.flightSchedules(ctx, airline, func(seq iter.Seq[jstream.KV]) error {
for kv := range seq {
b, err := json.Marshal(kv.Value)
Expand All @@ -499,20 +499,29 @@ func (h *Handler) QuerySchedules(ctx context.Context, airline common.AirlineIden
}

for _, variant := range fs.Variants {
if variant.Data.ServiceType == "J" && variant.Data.AircraftType == aircraftType && variant.Data.AircraftConfigurationVersion == aircraftConfigurationVersion {
fn := fs.Number()
idx := slices.IndexFunc(result[fn], func(rr RouteAndRanges) bool {
return rr.DepartureAirport == variant.Data.DepartureAirport && rr.ArrivalAirport == variant.Data.ArrivalAirport
})

if idx == -1 {
result[fn] = append(result[fn], RouteAndRanges{
DepartureAirport: variant.Data.DepartureAirport,
ArrivalAirport: variant.Data.ArrivalAirport,
Ranges: variant.Ranges,
fn := fs.Number()

if variant.Data.ServiceType == "J" && variant.Data.AircraftType == aircraftType && variant.Data.AircraftConfigurationVersion == aircraftConfigurationVersion && variant.Data.OperatedAs == fn {
if span, ok := variant.Ranges.Span(); ok {
idx := slices.IndexFunc(result[fn], func(rr RouteAndRange) bool {
return rr.DepartureAirport == variant.Data.DepartureAirport && rr.ArrivalAirport == variant.Data.ArrivalAirport
})
} else {
result[fn][idx].Ranges = result[fn][idx].Ranges.ExpandAll(variant.Ranges)

if idx == -1 {
result[fn] = append(result[fn], RouteAndRange{
DepartureAirport: variant.Data.DepartureAirport,
ArrivalAirport: variant.Data.ArrivalAirport,
Range: span,
})
} else {
if result[fn][idx].Range[0].Compare(span[0]) > 0 {
result[fn][idx].Range[0] = span[0]
}

if result[fn][idx].Range[1].Compare(span[1]) < 0 {
result[fn][idx].Range[1] = span[1]
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
e.GET("/data/aircraft.json", web.NewAircraftEndpoint(dataHandler))
e.GET("/data/flight/:fn", web.NewFlightNumberEndpoint(dataHandler))
e.GET("/data/flight/:fn/seatmap/:departure/:arrival/:date/:aircraft", web.NewSeatMapEndpoint(dataHandler))
e.GET("/data/:airline/schedule/:aircraftType/:aircraftConfigurationVersion", web.NewQueryFlightSchedulesEndpoint(dataHandler))
e.GET("/data/:airline/schedule/:aircraftType/:aircraftConfigurationVersion/v2", web.NewQueryFlightSchedulesEndpoint(dataHandler))

if err := run(ctx, e); err != nil {
panic(err)
Expand Down
9 changes: 9 additions & 0 deletions go/common/xtime/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (ldrs LocalDateRanges) Iter() iter.Seq[LocalDate] {
}
}

func (ldrs LocalDateRanges) Span() (LocalDateRange, bool) {
sorted := slices.SortedFunc(ldrs.Iter(), LocalDate.Compare)
if len(sorted) < 1 {
return LocalDateRange{}, false
}

return LocalDateRange{sorted[0], sorted[len(sorted)-1]}, true
}

func (ldrs LocalDateRanges) ExpandAll(other LocalDateRanges) LocalDateRanges {
return NewLocalDateRanges(xiter.Combine(ldrs.Iter(), other.Iter()))
}
Expand Down
Loading

0 comments on commit 69ded24

Please sign in to comment.