Skip to content

Commit 24ce1f9

Browse files
author
Sara Siegal
committed
A few odds and ends...some Socrata related fixes, and a unit test
1 parent 3ffa1f3 commit 24ce1f9

4 files changed

Lines changed: 638 additions & 11 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ venv/
4343

4444
.DS_Store
4545

46+
# claude
47+
CLAUDE.md
48+
/.claude

src/features/Map/index.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class MapContainer extends React.Component {
329329
endDate
330330
);
331331
} else {
332-
requests = await getServiceRequestSocrata();
332+
requests = await getServiceRequestSocrata(startDate, endDate);
333333
}
334334
return requests;
335335
}
@@ -373,8 +373,12 @@ class MapContainer extends React.Component {
373373
const typeId = getTypeIdFromTypeName(
374374
request.RequestType ?? request.requesttype
375375
);
376-
const closedDate =
377-
request.ClosedDate ?? moment(request.closeddate).valueOf();
376+
377+
// request.ClosedDate is undefined for Socrata data source (Socrata uses lowercase, PascalCase field doesn't exist)
378+
// For open requests: request.closeddate is also undefined (absent from JSON when no close date)
379+
// For closed requests: request.closeddate is a Date object (yup coerced it), so closedDate = moment(Date).valueOf() — also not null
380+
const rawClosedDate = request.ClosedDate ?? request.closeddate;
381+
const closedDate = rawClosedDate != null ? moment(rawClosedDate).valueOf() : null;
378382
const createdDateMs = moment(
379383
request.CreatedDate ?? request.createddate
380384
).valueOf();

src/utils/DataService.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,30 @@ const socrataServiceRequestSchema = object({
6060

6161
const 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

Comments
 (0)