Skip to content

Commit

Permalink
feat(feed): make trip histogram for pattern list
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Jun 21, 2018
1 parent f11c12a commit dd858a2
Show file tree
Hide file tree
Showing 12 changed files with 507 additions and 267 deletions.
49 changes: 29 additions & 20 deletions lib/gtfs/actions/patterns.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
// @flow

import {createAction} from 'redux-actions'

import {secureFetch} from '../../common/actions'
import {compose, patterns} from '../../gtfs/util/graphql'
import {updateRouteFilter, updatePatternFilter} from './filter'

import type {Route} from '../reducers/routes'
import type {dispatchFn, getStateFn} from '../../types'

export const fetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS')
export const clearPatterns = createAction('CLEAR_GRAPHQL_PATTERNS')
export const errorFetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS_REJECTED')
export const errorFetchingPatterns = createAction(
'FETCH_GRAPHQL_PATTERNS_REJECTED'
)
export const receivePatterns = createAction('FETCH_GRAPHQL_PATTERNS_FULFILLED')

export function patternDateTimeFilterChange (namespace, props) {
return function (dispatch, getState) {
const routeId = getState().gtfs.filter.routeFilter
const { date, from, to } = getState().gtfs.filter.dateTimeFilter
dispatch(fetchPatterns(namespace, routeId, date, from, to))
export function patternDateTimeFilterChange (namespace: string) {
return function (dispatch: dispatchFn, getState: getStateFn) {
const state = getState()
const routeId = state.gtfs.filter.routeFilter
dispatch(fetchPatterns(namespace, routeId))
}
}

export function fetchPatterns (namespace, routeId, date, from, to) {
return function (dispatch, getState) {
dispatch(fetchingPatterns({namespace, routeId, date, from, to}))
// FIXME: if routeId is null, clear current routes so we can fetch all routes
export function fetchPatterns (namespace: string, routeId: ?string) {
return function (dispatch: dispatchFn, getState: getStateFn) {
dispatch(fetchingPatterns())
if (!routeId) {
const routes = []
return dispatch(receivePatterns({namespace, routes}))
return dispatch(receivePatterns({routes}))
}
return dispatch(secureFetch(compose(patterns, {namespace, routeId, date, from, to})))
.then((response) => {
const {date, from, to} = getState().gtfs.filter.dateTimeFilter
return dispatch(
secureFetch(compose(patterns, {namespace, routeId, date, from, to}))
)
.then(response => {
if (response.status >= 300) {
return dispatch(errorFetchingPatterns(namespace, routeId))
return dispatch(errorFetchingPatterns())
}
return response.json()
})
.then(json => {
if (json.data) {
const {routes} = json.data.feed
dispatch(receivePatterns({namespace, routes}))
dispatch(receivePatterns({routes}))
} else {
console.log('Error fetching patterns')
}
})
}
}

export function patternRouteFilterChange (namespace, routeData) {
return function (dispatch, getState) {
const newRouteId = (routeData && routeData.route_id) ? routeData.route_id : null
const {date, from, to} = getState().gtfs.filter.dateTimeFilter
export function patternRouteFilterChange (namespace: string, routeData: Route) {
return function (dispatch: dispatchFn, getState: getStateFn) {
const newRouteId =
routeData && routeData.route_id ? routeData.route_id : null
dispatch(updateRouteFilter(newRouteId))
// If there's an active pattern, clear it
dispatch(updatePatternFilter(null))
dispatch(fetchPatterns(namespace, newRouteId, date, from, to))
dispatch(fetchPatterns(namespace, newRouteId))
}
}
2 changes: 1 addition & 1 deletion lib/gtfs/actions/timetables.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function timetableRouteFilterChange (feedId: string, routeData: any) {
const routeId = routeData && routeData.route_id ? routeData.route_id : null
dispatch(updateRouteFilter(routeId))
dispatch(updatePatternFilter(null))
dispatch(fetchPatterns(feedId, routeId, null))
dispatch(fetchPatterns(feedId, routeId))
dispatch(fetchTimetablesWithFilters(feedId))
}
}
Expand Down
26 changes: 25 additions & 1 deletion lib/gtfs/reducers/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,37 @@ import update from 'react-addons-update'

import {getRouteName, mapPatternShape} from '../../editor/util/gtfs'

export type Pattern = {
geometry: {
coordinates: Array<Array<number>>,
type: string,
},
name: string,
route_id: string,
route_name: string,
pattern_id: string,
shape: Array<{
lat: number,
lon: number
}>,
stops: Array<{
stop_id: string
}>,
trips: Array<{
stop_times: Array<{
arrival_time: number,
departure_time: number
}>
}>
}

export type PatternsState = {
fetchStatus: {
fetched: boolean,
fetching: boolean,
error: boolean
},
data: Array<any>
data: Array<Pattern>
}

export const defaultState = {
Expand Down
6 changes: 4 additions & 2 deletions lib/gtfs/reducers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export type RouteDetailsData = {
}>
}

export type AllRoutes = Array<{
export type Route = {
route_desc: ?string,
route_id: string,
route_long_name: ?string,
route_name: string,
route_short_name: ?string,
route_url: ?string
}>
}

export type AllRoutes = Array<Route>

export type RoutesState = {
allRoutes: {
Expand Down
29 changes: 20 additions & 9 deletions lib/gtfs/util/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ query routeDetailsQuery(
}
`

// FIXME: Pattern stats are not supported yet in gtfs-api, so this is broken.
export const patterns = `
query patternsQuery($namespace: String, $routeId: [String]
#, $date: String, $from: Long, $to: Long
query patternsQuery(
$date: String,
$from: Int,
$namespace: String,
$routeId: [String],
$to: Int
) {
feed (namespace: $namespace) {
routes (route_id: $routeId) {
Expand All @@ -86,12 +89,20 @@ query patternsQuery($namespace: String, $routeId: [String]
lat: shape_pt_lat
lon: shape_pt_lon
},
# stop_count,
# trip_count,
# stats(date: $date, from: $from, to: $to){
# headway,
# avgSpeed
# },
stops (limit: -1) {
stop_id
}
trips (
date: $date,
from: $from,
limit: -1,
to: $to
) {
stop_times (limit: 1) {
arrival_time
departure_time
}
}
}
}
}
Expand Down
Loading

0 comments on commit dd858a2

Please sign in to comment.