Skip to content
Permalink
Browse files

Refactor duplicated code in backendB

Also moving a few files around.
  • Loading branch information...
pboyd committed Oct 22, 2019
1 parent 5831e2d commit 85abc9f074ce3b398dbb130cdf8cfc5db181ab49
@@ -1,10 +1,7 @@
package graphql

import (
"strings"

"github.com/graphql-go/graphql"
"github.com/pboyd/flightranker-backend/backendb/app"
)

var airlineFlightStatsType = graphql.NewObject(
@@ -37,13 +34,9 @@ func (p *Processor) flightStatsByAirlineQuery() *graphql.Field {
}

func (p *Processor) resolveFlightStatsByAirlineQuery(params graphql.ResolveParams) (interface{}, error) {
origin, _ := params.Args["origin"].(string)
origin = strings.ToUpper(origin)

dest, _ := params.Args["destination"].(string)
dest = strings.ToUpper(dest)

if !app.IsAirportCode(origin) || !app.IsAirportCode(dest) {
origin := p.getAirportCodeParam(params, "origin")
dest := p.getAirportCodeParam(params, "destination")
if origin == "" || dest == "" {
return nil, nil
}

@@ -36,9 +36,8 @@ func (p *Processor) airportQuery() *graphql.Field {
}

func (p *Processor) resolveAirportQuery(params graphql.ResolveParams) (interface{}, error) {
code, _ := params.Args["code"].(string)
code = strings.ToUpper(code)
if !app.IsAirportCode(code) {
code := p.getAirportCodeParam(params, "code")
if code == "" {
return nil, nil
}

@@ -67,3 +66,13 @@ func (p *Processor) resolveAirportList(params graphql.ResolveParams) (interface{

return p.config.AirportStore.AirportSearch(params.Context, term)
}

func (p *Processor) getAirportCodeParam(params graphql.ResolveParams, key string) string {
code, _ := params.Args[key].(string)
code = strings.ToUpper(code)
if !app.IsAirportCode(code) {
return ""
}

return code
}
@@ -2,7 +2,6 @@ package graphql

import (
"sort"
"strings"

"github.com/graphql-go/graphql"
"github.com/pboyd/flightranker-backend/backendb/app"
@@ -54,14 +53,25 @@ type flightStatsByDate struct {
Rows []*app.FlightStatsByDateRow
}

func (p *Processor) resolveDailyFlightStats(params graphql.ResolveParams) (interface{}, error) {
origin, _ := params.Args["origin"].(string)
origin = strings.ToUpper(origin)
type flightStatsByDateSlice []flightStatsByDate

func newFlightStatsByDateSlice(statsMap map[string][]*app.FlightStatsByDateRow) flightStatsByDateSlice {
stats := make(flightStatsByDateSlice, 0, len(statsMap))
for airline, rows := range statsMap {
stats = append(stats, flightStatsByDate{Airline: airline, Rows: rows})
}

dest, _ := params.Args["destination"].(string)
dest = strings.ToUpper(dest)
sort.Slice(stats, func(i, j int) bool {
return stats[i].Airline < stats[j].Airline
})

if !app.IsAirportCode(origin) || !app.IsAirportCode(dest) {
return stats
}

func (p *Processor) resolveDailyFlightStats(params graphql.ResolveParams) (interface{}, error) {
origin := p.getAirportCodeParam(params, "origin")
dest := p.getAirportCodeParam(params, "destination")
if origin == "" || dest == "" {
return nil, nil
}

@@ -70,16 +80,7 @@ func (p *Processor) resolveDailyFlightStats(params graphql.ResolveParams) (inter
return nil, err
}

stats := make([]flightStatsByDate, 0, len(statsMap))
for airline, rows := range statsMap {
stats = append(stats, flightStatsByDate{Airline: airline, Rows: rows})
}

sort.Slice(stats, func(i, j int) bool {
return stats[i].Airline < stats[j].Airline
})

return stats, nil
return newFlightStatsByDateSlice(statsMap), nil
}

func (p *Processor) monthlyFlightStatsQuery() *graphql.Field {
@@ -100,13 +101,9 @@ func (p *Processor) monthlyFlightStatsQuery() *graphql.Field {
}

func (p *Processor) resolveMonthlyFlightStats(params graphql.ResolveParams) (interface{}, error) {
origin, _ := params.Args["origin"].(string)
origin = strings.ToUpper(origin)

dest, _ := params.Args["destination"].(string)
dest = strings.ToUpper(dest)

if !app.IsAirportCode(origin) || !app.IsAirportCode(dest) {
origin := p.getAirportCodeParam(params, "origin")
dest := p.getAirportCodeParam(params, "destination")
if origin == "" || dest == "" {
return nil, nil
}

@@ -115,14 +112,5 @@ func (p *Processor) resolveMonthlyFlightStats(params graphql.ResolveParams) (int
return nil, err
}

stats := make([]flightStatsByDate, 0, len(statsMap))
for airline, rows := range statsMap {
stats = append(stats, flightStatsByDate{Airline: airline, Rows: rows})
}

sort.Slice(stats, func(i, j int) bool {
return stats[i].Airline < stats[j].Airline
})

return stats, nil
return newFlightStatsByDateSlice(statsMap), nil
}
@@ -0,0 +1,52 @@
package mysql

import (
"context"
"sort"

"github.com/pboyd/flightranker-backend/backendb/app"
)

func (s *Store) FlightStatsByAirline(ctx context.Context, origin, dest string) ([]*app.FlightStats, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT
carriers.name AS carrier_name, total_flights, delays_flights, last_flight
FROM
(
SELECT
carrier AS carrier_code,
SUM(total_flights) AS total_flights,
SUM(delayed_flights) AS delays_flights,
MAX(date) AS last_flight
FROM
flights_day
WHERE origin=? AND destination=?
GROUP BY carrier_code
) AS stats
INNER JOIN carriers ON carrier_code=carriers.code
`,
origin, dest)
if err != nil {
return nil, err
}
defer rows.Close()

stats := []*app.FlightStats{}

for rows.Next() {
var row app.FlightStats

err := rows.Scan(&row.Airline, &row.TotalFlights, &row.TotalDelays, &row.LastFlight)
if err != nil {
return nil, err
}

stats = append(stats, &row)
}

sort.Slice(stats, func(i, j int) bool {
return stats[j].OnTimePercentage() < stats[i].OnTimePercentage()
})

return stats, nil
}
@@ -0,0 +1,48 @@
package mysql

import (
"context"

"github.com/pboyd/flightranker-backend/backendb/app"
)

func (s *Store) DailyFlightStats(ctx context.Context, origin, destination string) (map[string][]*app.FlightStatsByDateRow, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT
date,
carriers.name,
total_flights,
IF(delayed_flights IS NULL, 0, delayed_flights) AS delay_flights_not_null
FROM
flights_day
INNER JOIN carriers ON carrier=carriers.code
WHERE origin=? AND destination=?
ORDER BY date`,
origin, destination)
if err != nil {
return nil, err
}
defer rows.Close()

stats := map[string][]*app.FlightStatsByDateRow{}

for rows.Next() {
var (
airline string
row app.FlightStatsByDateRow
)

err := rows.Scan(&row.Date, &airline, &row.Flights, &row.Delays)
if err != nil {
return nil, err
}

if stats[airline] == nil {
stats[airline] = []*app.FlightStatsByDateRow{}
}

stats[airline] = append(stats[airline], &row)
}

return stats, nil
}

This file was deleted.

0 comments on commit 85abc9f

Please sign in to comment.
You can’t perform that action at this time.