Skip to content
Permalink
Browse files

De-duplicate some common code in backendA

  • Loading branch information...
pboyd committed Oct 21, 2019
1 parent 8791dce commit 5831e2d862b23fc80f2b39935574605111b3b2ad
Showing with 58 additions and 53 deletions.
  1. +20 −11 backendA/airport.go
  2. +6 −21 backendA/daily_stats.go
  3. +4 −0 backendA/main.go
  4. +6 −21 backendA/monthly_stats.go
  5. +22 −0 backendA/types.go
@@ -12,21 +12,20 @@ import (
func resolveAirportQuery(db *sql.DB) graphql.FieldResolveFn {
return graphQLMetrics("airport",
func(p graphql.ResolveParams) (interface{}, error) {
code, _ := p.Args["code"].(string)
code = strings.ToUpper(code)
if !isAirportCode(code) {
code := getAirportCodeParam(p, "code")
if code == "" {
return nil, nil
}

row := db.QueryRow(`
SELECT
code, name, city, state, lat, lng
FROM
airports
WHERE
is_active=1 AND
code=?
`, code)
SELECT
code, name, city, state, lat, lng
FROM
airports
WHERE
is_active=1 AND
code=?
`, code)

var a airport
err := row.Scan(&a.Code, &a.Name, &a.City, &a.State, &a.Latitude, &a.Longitude)
@@ -42,6 +41,16 @@ func resolveAirportQuery(db *sql.DB) graphql.FieldResolveFn {
)
}

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

return code
}

func isAirportCode(code string) bool {
if len(code) != 3 {
return false
@@ -2,22 +2,16 @@ package main

import (
"database/sql"
"sort"
"strings"

"github.com/graphql-go/graphql"
)

func resolveDailyFlightStats(db *sql.DB) graphql.FieldResolveFn {
return graphQLMetrics("daily_flight_stats",
func(p graphql.ResolveParams) (interface{}, error) {
origin, _ := p.Args["origin"].(string)
origin = strings.ToUpper(origin)

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

if !isAirportCode(origin) || !isAirportCode(dest) {
origin := getAirportCodeParam(p, "origin")
dest := getAirportCodeParam(p, "destination")
if origin == "" || dest == "" {
return nil, nil
}

@@ -51,7 +45,7 @@ func resolveDailyFlightStats(db *sql.DB) graphql.FieldResolveFn {
return nil, err
}

row.OnTimePercentage = (1.0 - float64(row.Delays)/float64(row.Flights)) * 100
row.OnTimePercentage = calculateOnTimePercentage(row.Delays, row.Flights)

if statsMap[airline] == nil {
statsMap[airline] = []*flightStatsByDateRow{}
@@ -60,17 +54,8 @@ func resolveDailyFlightStats(db *sql.DB) graphql.FieldResolveFn {
statsMap[airline] = append(statsMap[airline], &row)
}

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
})
stats := newFlightStatsByDateSlice(statsMap)
stats.Sort()

return stats, nil
},
@@ -79,3 +79,7 @@ func graphqlHandler(db *sql.DB) http.HandlerFunc {
enc.Encode(result.Data)
}
}

func calculateOnTimePercentage(delays, flights int) float64 {
return (1.0 - float64(delays)/float64(flights)) * 100
}
@@ -2,8 +2,6 @@ package main

import (
"database/sql"
"sort"
"strings"
"time"

"github.com/graphql-go/graphql"
@@ -12,13 +10,9 @@ import (
func resolveMonthlyFlightStats(db *sql.DB) graphql.FieldResolveFn {
return graphQLMetrics("monthly_flight_stats",
func(p graphql.ResolveParams) (interface{}, error) {
origin, _ := p.Args["origin"].(string)
origin = strings.ToUpper(origin)

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

if !isAirportCode(origin) || !isAirportCode(dest) {
origin := getAirportCodeParam(p, "origin")
dest := getAirportCodeParam(p, "destination")
if origin == "" || dest == "" {
return nil, nil
}

@@ -54,7 +48,7 @@ func resolveMonthlyFlightStats(db *sql.DB) graphql.FieldResolveFn {
}

row.Date = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
row.OnTimePercentage = (1.0 - float64(row.Delays)/float64(row.Flights)) * 100
row.OnTimePercentage = calculateOnTimePercentage(row.Delays, row.Flights)

if statsMap[airline] == nil {
statsMap[airline] = []*flightStatsByDateRow{}
@@ -63,17 +57,8 @@ func resolveMonthlyFlightStats(db *sql.DB) graphql.FieldResolveFn {
statsMap[airline] = append(statsMap[airline], &row)
}

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
})
stats := newFlightStatsByDateSlice(statsMap)
stats.Sort()

return stats, nil
},
@@ -1,6 +1,7 @@
package main

import (
"sort"
"time"
)

@@ -20,6 +21,27 @@ type airlineStats struct {
LastFlight time.Time
}

type flightStatsByDateSlice []flightStatsByDate

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

// newFlightStatsByDateSlice converts a map of airline names and flight stats to a flightStatsByDateSlice
func newFlightStatsByDateSlice(m map[string][]*flightStatsByDateRow) flightStatsByDateSlice {
stats := make(flightStatsByDateSlice, 0, len(m))
for airline, rows := range m {
stats = append(stats, flightStatsByDate{
Airline: airline,
Rows: rows,
})
}

return stats
}

type flightStatsByDate struct {
Airline string
Rows []*flightStatsByDateRow

0 comments on commit 5831e2d

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