@@ -60,17 +60,30 @@ const socrataServiceRequestSchema = object({
6060
6161const srArraySchema = array ( ) . of ( socrataServiceRequestSchema ) ;
6262
63- export async function getServiceRequestSocrata ( ) {
63+ export async function getServiceRequestSocrata ( startDate , endDate ) {
6464 const dataLoadStartTime = performance . now ( ) ;
6565
6666 try {
67- // Fetch current year SR data through Socrata API
68- const currentYear = String ( new Date ( ) . getFullYear ( ) ) ;
69- const currentYearFilename = `https://data.lacity.org/resource/${ dataResources [ currentYear ] } .json`
70- const response = await fetch (
71- currentYearFilename
67+ // Build list of years covered by the date range
68+ const startYear = moment ( startDate ) . year ( ) ;
69+ const endYear = moment ( endDate ) . year ( ) ;
70+ const years = [ ] ;
71+ for ( let year = startYear ; year <= endYear ; year ++ ) {
72+ years . push ( String ( year ) ) ;
73+ }
74+
75+ // Fetch data for each year filtered by the requested date range.
76+ // Without a $where clause, Socrata returns only 1000 records in internal-ID
77+ // order (i.e. the oldest records first), which would all fail the client-side
78+ // Mapbox date filter. We also raise $limit well above the default 1000 so that
79+ // the full date range is covered.
80+ const unvalidatedByYear = await Promise . all (
81+ years . map ( ( year ) => {
82+ const where = `createddate >= '${ startDate } T00:00:00.000' AND createddate <= '${ endDate } T23:59:59.999'` ;
83+ const url = `https://data.lacity.org/resource/${ dataResources [ year ] } .json?$where=${ encodeURIComponent ( where ) } &$limit=1000` ;
84+ return fetch ( url ) . then ( ( res ) => res . json ( ) ) ;
85+ } )
7286 ) ;
73- const unvalidatedSrs = await response . json ( ) ;
7487
7588 const dataLoadEndTime = performance . now ( ) ;
7689 console . log (
@@ -80,7 +93,12 @@ export async function getServiceRequestSocrata() {
8093 ) ;
8194
8295 const mapLoadStartTime = performance . now ( ) ;
83- const validatedSrs = await srArraySchema . validate ( unvalidatedSrs ) ;
96+ const validatedByYear = await Promise . all (
97+ unvalidatedByYear . map ( ( unvalidatedSrs ) =>
98+ srArraySchema . validate ( unvalidatedSrs )
99+ )
100+ ) ;
101+ const validatedSrs = validatedByYear . flat ( ) ;
84102 const mapLoadEndTime = performance . now ( ) ;
85103 console . log (
86104 `Socrata map preparation time: ${ Math . floor (
0 commit comments