Skip to content

Commit

Permalink
update upsert logic
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Sep 6, 2024
1 parent 0d16c05 commit 2d93691
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 80 deletions.
6 changes: 3 additions & 3 deletions go/api/search/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (ch *ConnectionsHandler) FindConnections(ctx context.Context, origins, dest
return nil, err
}

flightsByDeparture = groupByDeparture(flightsByDate, f.all)
flightsByDeparture = groupByDepartureUTC(flightsByDate, f.all)
}

return collectCtx(ctx, findConnections(
Expand Down Expand Up @@ -187,12 +187,12 @@ func allMatch(predicates []flightPredicate, f *common.Flight) bool {
return true
}

func groupByDeparture(flightsByDate map[common.LocalDate][]*common.Flight, predicates []flightPredicate) map[common.Departure][]*common.Flight {
func groupByDepartureUTC(flightsByDate map[common.LocalDate][]*common.Flight, predicates []flightPredicate) map[common.Departure][]*common.Flight {
result := make(map[common.Departure][]*common.Flight)
for _, flights := range flightsByDate {
for _, f := range flights {
if allMatch(predicates, f) {
d := f.Departure()
d := f.DepartureUTC()
result[d] = append(result[d], f)
}
}
Expand Down
20 changes: 10 additions & 10 deletions go/api/search/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

type ConnectionsResponse struct {
Connections []ConnectionResponse `json:"connections"`
Flights map[string]FlightResponse `json:"flights"`
Connections []ConnectionResponse `json:"connections"`
Flights map[common.FlightId]FlightResponse `json:"flights"`
}

type ConnectionResponse struct {
FlightId string `json:"flightId"`
FlightId common.FlightId `json:"flightId"`
Outgoing []ConnectionResponse `json:"outgoing"`
}

Expand All @@ -38,7 +38,7 @@ type FlightNumberResponse struct {
}

func ExportConnectionsJson(conns []Connection) ConnectionsResponse {
flights := make(map[string]FlightResponse)
flights := make(map[common.FlightId]FlightResponse)
connections := buildConnectionsResponse(conns, flights)

return ConnectionsResponse{
Expand All @@ -47,13 +47,13 @@ func ExportConnectionsJson(conns []Connection) ConnectionsResponse {
}
}

func buildConnectionsResponse(conns []Connection, flights map[string]FlightResponse) []ConnectionResponse {
func buildConnectionsResponse(conns []Connection, flights map[common.FlightId]FlightResponse) []ConnectionResponse {
r := make([]ConnectionResponse, 0, len(conns))

for _, conn := range conns {
flightId := fmt.Sprintf("%v@%v@%v", conn.Flight.Number(), conn.Flight.DepartureAirport, conn.Flight.DepartureDate())
if _, ok := flights[flightId]; !ok {
flights[flightId] = FlightResponse{
fid := conn.Flight.Id()
if _, ok := flights[fid]; !ok {
flights[fid] = FlightResponse{
FlightNumber: FlightNumberResponse{
Airline: string(conn.Flight.Airline),
Number: conn.Flight.FlightNumber,
Expand All @@ -71,15 +71,15 @@ func buildConnectionsResponse(conns []Connection, flights map[string]FlightRespo
}

r = append(r, ConnectionResponse{
FlightId: flightId,
FlightId: fid,
Outgoing: buildConnectionsResponse(conn.Outgoing, flights),
})
}

return r
}

func convertCodeShares(inp map[common.FlightNumber]map[int]string) []FlightNumberResponse {
func convertCodeShares(inp map[common.FlightNumber]common.CodeShare) []FlightNumberResponse {
r := make([]FlightNumberResponse, 0, len(inp))
for fn := range inp {
r = append(r, FlightNumberResponse{
Expand Down
87 changes: 73 additions & 14 deletions go/common/flight.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -64,24 +65,75 @@ type FlightId struct {
Departure Departure `json:"departure"`
}

func (f FlightId) String() string {
return fmt.Sprintf("%v@%v@%v", f.Number, f.Departure.Airport, f.Departure.Date)
}

func (f *FlightId) UnmarshalText(text []byte) error {
values := strings.SplitN(string(text), "@", 3)
if len(values) != 3 {
return fmt.Errorf("invalid FlightId: %v", string(text))
}

fn, err := ParseFlightNumber(values[0])
if err != nil {
return err
}

d, err := ParseLocalDate(values[2])
if err != nil {
return err
}

*f = FlightId{
Number: fn,
Departure: Departure{
Airport: values[1],
Date: d,
},
}

return err
}

func (f FlightId) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}

type Flight struct {
Airline AirlineIdentifier `json:"airline"`
FlightNumber int `json:"flightNumber"`
Suffix string `json:"suffix"`
DepartureTime time.Time `json:"departureTime"`
DepartureAirport string `json:"departureAirport"`
ArrivalTime time.Time `json:"arrivalTime"`
ArrivalAirport string `json:"arrivalAirport"`
ServiceType string `json:"serviceType"`
AircraftOwner AirlineIdentifier `json:"aircraftOwner"`
AircraftType string `json:"aircraftType"`
AircraftConfigurationVersion string `json:"aircraftConfigurationVersion"`
Registration string `json:"registration"`
DataElements map[int]string `json:"dataElements"`
CodeShares map[FlightNumber]map[int]string `json:"codeShares"`
Airline AirlineIdentifier `json:"airline"`
FlightNumber int `json:"flightNumber"`
Suffix string `json:"suffix"`
DepartureTime time.Time `json:"departureTime"`
DepartureAirport string `json:"departureAirport"`
ArrivalTime time.Time `json:"arrivalTime"`
ArrivalAirport string `json:"arrivalAirport"`
ServiceType string `json:"serviceType"`
AircraftOwner AirlineIdentifier `json:"aircraftOwner"`
AircraftType string `json:"aircraftType"`
AircraftConfigurationVersion string `json:"aircraftConfigurationVersion"`
Registration string `json:"registration"`
DataElements map[int]string `json:"dataElements"`
CodeShares map[FlightNumber]CodeShare `json:"codeShares"`
Metadata FlightMetadata `json:"metadata"`
}

type CodeShare struct {
DataElements map[int]string `json:"dataElements"`
Metadata FlightMetadata `json:"metadata"`
}

type FlightMetadata struct {
QueryDate LocalDate `json:"queryDate"`
CreationTime time.Time `json:"creationTime"`
UpdateTime time.Time `json:"updateTime"`
}

func (f *Flight) DepartureDate() LocalDate {
return NewLocalDate(f.DepartureTime)
}

func (f *Flight) DepartureDateUTC() LocalDate {
return NewLocalDate(f.DepartureTime.UTC())
}

Expand All @@ -92,6 +144,13 @@ func (f *Flight) Departure() Departure {
}
}

func (f *Flight) DepartureUTC() Departure {
return Departure{
Airport: f.DepartureAirport,
Date: f.DepartureDateUTC(),
}
}

func (f *Flight) Number() FlightNumber {
return FlightNumber{
Airline: f.Airline,
Expand Down
Loading

0 comments on commit 2d93691

Please sign in to comment.