Skip to content

Commit dd858a2

Browse files
committed
feat(feed): make trip histogram for pattern list
1 parent f11c12a commit dd858a2

File tree

12 files changed

+507
-267
lines changed

12 files changed

+507
-267
lines changed

lib/gtfs/actions/patterns.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
1+
// @flow
2+
13
import {createAction} from 'redux-actions'
24

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

9+
import type {Route} from '../reducers/routes'
10+
import type {dispatchFn, getStateFn} from '../../types'
11+
712
export const fetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS')
813
export const clearPatterns = createAction('CLEAR_GRAPHQL_PATTERNS')
9-
export const errorFetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS_REJECTED')
14+
export const errorFetchingPatterns = createAction(
15+
'FETCH_GRAPHQL_PATTERNS_REJECTED'
16+
)
1017
export const receivePatterns = createAction('FETCH_GRAPHQL_PATTERNS_FULFILLED')
1118

12-
export function patternDateTimeFilterChange (namespace, props) {
13-
return function (dispatch, getState) {
14-
const routeId = getState().gtfs.filter.routeFilter
15-
const { date, from, to } = getState().gtfs.filter.dateTimeFilter
16-
dispatch(fetchPatterns(namespace, routeId, date, from, to))
19+
export function patternDateTimeFilterChange (namespace: string) {
20+
return function (dispatch: dispatchFn, getState: getStateFn) {
21+
const state = getState()
22+
const routeId = state.gtfs.filter.routeFilter
23+
dispatch(fetchPatterns(namespace, routeId))
1724
}
1825
}
1926

20-
export function fetchPatterns (namespace, routeId, date, from, to) {
21-
return function (dispatch, getState) {
22-
dispatch(fetchingPatterns({namespace, routeId, date, from, to}))
23-
// FIXME: if routeId is null, clear current routes so we can fetch all routes
27+
export function fetchPatterns (namespace: string, routeId: ?string) {
28+
return function (dispatch: dispatchFn, getState: getStateFn) {
29+
dispatch(fetchingPatterns())
2430
if (!routeId) {
2531
const routes = []
26-
return dispatch(receivePatterns({namespace, routes}))
32+
return dispatch(receivePatterns({routes}))
2733
}
28-
return dispatch(secureFetch(compose(patterns, {namespace, routeId, date, from, to})))
29-
.then((response) => {
34+
const {date, from, to} = getState().gtfs.filter.dateTimeFilter
35+
return dispatch(
36+
secureFetch(compose(patterns, {namespace, routeId, date, from, to}))
37+
)
38+
.then(response => {
3039
if (response.status >= 300) {
31-
return dispatch(errorFetchingPatterns(namespace, routeId))
40+
return dispatch(errorFetchingPatterns())
3241
}
3342
return response.json()
3443
})
3544
.then(json => {
3645
if (json.data) {
3746
const {routes} = json.data.feed
38-
dispatch(receivePatterns({namespace, routes}))
47+
dispatch(receivePatterns({routes}))
3948
} else {
4049
console.log('Error fetching patterns')
4150
}
4251
})
4352
}
4453
}
4554

46-
export function patternRouteFilterChange (namespace, routeData) {
47-
return function (dispatch, getState) {
48-
const newRouteId = (routeData && routeData.route_id) ? routeData.route_id : null
49-
const {date, from, to} = getState().gtfs.filter.dateTimeFilter
55+
export function patternRouteFilterChange (namespace: string, routeData: Route) {
56+
return function (dispatch: dispatchFn, getState: getStateFn) {
57+
const newRouteId =
58+
routeData && routeData.route_id ? routeData.route_id : null
5059
dispatch(updateRouteFilter(newRouteId))
5160
// If there's an active pattern, clear it
5261
dispatch(updatePatternFilter(null))
53-
dispatch(fetchPatterns(namespace, newRouteId, date, from, to))
62+
dispatch(fetchPatterns(namespace, newRouteId))
5463
}
5564
}

lib/gtfs/actions/timetables.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function timetableRouteFilterChange (feedId: string, routeData: any) {
6060
const routeId = routeData && routeData.route_id ? routeData.route_id : null
6161
dispatch(updateRouteFilter(routeId))
6262
dispatch(updatePatternFilter(null))
63-
dispatch(fetchPatterns(feedId, routeId, null))
63+
dispatch(fetchPatterns(feedId, routeId))
6464
dispatch(fetchTimetablesWithFilters(feedId))
6565
}
6666
}

lib/gtfs/reducers/patterns.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,37 @@ import update from 'react-addons-update'
44

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

7+
export type Pattern = {
8+
geometry: {
9+
coordinates: Array<Array<number>>,
10+
type: string,
11+
},
12+
name: string,
13+
route_id: string,
14+
route_name: string,
15+
pattern_id: string,
16+
shape: Array<{
17+
lat: number,
18+
lon: number
19+
}>,
20+
stops: Array<{
21+
stop_id: string
22+
}>,
23+
trips: Array<{
24+
stop_times: Array<{
25+
arrival_time: number,
26+
departure_time: number
27+
}>
28+
}>
29+
}
30+
731
export type PatternsState = {
832
fetchStatus: {
933
fetched: boolean,
1034
fetching: boolean,
1135
error: boolean
1236
},
13-
data: Array<any>
37+
data: Array<Pattern>
1438
}
1539

1640
export const defaultState = {

lib/gtfs/reducers/routes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ export type RouteDetailsData = {
2626
}>
2727
}
2828

29-
export type AllRoutes = Array<{
29+
export type Route = {
3030
route_desc: ?string,
3131
route_id: string,
3232
route_long_name: ?string,
3333
route_name: string,
3434
route_short_name: ?string,
3535
route_url: ?string
36-
}>
36+
}
37+
38+
export type AllRoutes = Array<Route>
3739

3840
export type RoutesState = {
3941
allRoutes: {

lib/gtfs/util/graphql.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ query routeDetailsQuery(
6969
}
7070
`
7171

72-
// FIXME: Pattern stats are not supported yet in gtfs-api, so this is broken.
7372
export const patterns = `
74-
query patternsQuery($namespace: String, $routeId: [String]
75-
#, $date: String, $from: Long, $to: Long
73+
query patternsQuery(
74+
$date: String,
75+
$from: Int,
76+
$namespace: String,
77+
$routeId: [String],
78+
$to: Int
7679
) {
7780
feed (namespace: $namespace) {
7881
routes (route_id: $routeId) {
@@ -86,12 +89,20 @@ query patternsQuery($namespace: String, $routeId: [String]
8689
lat: shape_pt_lat
8790
lon: shape_pt_lon
8891
},
89-
# stop_count,
90-
# trip_count,
91-
# stats(date: $date, from: $from, to: $to){
92-
# headway,
93-
# avgSpeed
94-
# },
92+
stops (limit: -1) {
93+
stop_id
94+
}
95+
trips (
96+
date: $date,
97+
from: $from,
98+
limit: -1,
99+
to: $to
100+
) {
101+
stop_times (limit: 1) {
102+
arrival_time
103+
departure_time
104+
}
105+
}
95106
}
96107
}
97108
}

0 commit comments

Comments
 (0)