Repository navigation
fix(query_helper): patch date filter & sends 422 invalid date ops (BDMS-1523) - #1064
TylerAdamMartinez wants to merge 1 commit into
Conversation
Coverage✅ 82.15% total — gate is 75%. Coverage for the Python files changed in this PR
|
ksmuczynski
left a comment
There was a problem hiding this comment.
Looking good! Few comments for consideration.
Also,docs/refine-json-filters-and-virtual-fields.md suggests operator changes should be documented there. Could you add a brief section covering the accepted date formats and the calendar-day behavior for timestamp columns?
| if is_date and operator not in {"null", "nnull"}: | ||
| return _apply_date_filter_clause(sql, column, operator, value) |
There was a problem hiding this comment.
Because this branch runs on every Date and DateTime column for routes using order_sort_filter (including Observation.observation_datetime, Sample.sample_date, and created_at across models), it also handles the ISO timestamp filters sent by the UI, and currently rejects them.
In OcotilloUI, observation/list.tsx defines observation_datetime and created_at as type: 'dateTime', and thing-id-link/list.tsx does the same for created_at. Since the data provider serializes filters with JSON.stringify, the API receives ISO 8601 strings like "2026-08-10T06:00:00.000Z". Previously, these requests failed in Postgres (operator does not exist: timestamp with time zone >= character varying), and now _parse_json_filter_date rejects them with a 422. While this isn't a regression, those filters still don't work, and this is the exact code path that could fix them.
Two ways we could handle this:
- Update
_parse_json_filter_dateto accept ISO 8601 datetimes (preserving calendar-day comparisons for date-only values and using exact comparisons for full datetimes), which would also fix the date filters in the observation and thing ID link grids. - Leave this PR as is and open a follow-up ticket for ISO datetime support.
Either way, adding a test that filters /observation/groundwater-level by observation_datetime with an ISO timestamp would cover this case.
| start = datetime.combine(filter_date, time.min, timezone.utc) | ||
| end = start + timedelta(days=1) |
There was a problem hiding this comment.
There is a timezone mismatch between how the wells grid displays created_at and how this filter evaluates it:
- Grid display: Uses
formatAppDatewithAPP_TIMEZONE(which defaults toAmerica/Denver). - Filter: Uses UTC day bounds.
Because of this, any record created after 6:00 PM MDT (or 5:00 PM MST) has a UTC date one day later than the date the grid shows. For example, a well created at 7:00 PM MDT on Oct 5 displays as "Oct 5, 2026" in the grid, yet filtering for 10/05/2026 doesn't return it and filtering for 10/06/2026 does.
Although the Notes call out UTC days, this mismatch between visible column values and filter results is likely to cause user confusion and bug reports about "missing" records. Could we use America/Denver day bounds here to match the grid, or link a follow-up ticket if UTC is intentional for now?
| if not isinstance(value, str): | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail="Date filter values must use MM/DD/YYYY or YYYY-MM-DD", | ||
| ) |
There was a problem hiding this comment.
Minor comment, but neither 422 message (here or at line 214) includes the field name, so a caller sending multiple filter params can't tell which one failed. Could _apply_json_filter_clause pass field through _apply_date_filter_clause so we can include it in the error?
Also minor, but worth noting, is null handling. Rejecting all non-string values means eq with null now returns a 422 rather than IS NULL. The UI sends null/nnull for empty checks, so current callers shouldn't be affected; just flagging the behavior change.
| .values( | ||
| first_visit_date=date(2026, 8, 10), | ||
| well_completion_date=date(2023, 6, 6), | ||
| created_at=datetime(2026, 10, 5, 14, 30, tzinfo=timezone.utc), |
There was a problem hiding this comment.
Since the fixture sets created_at to 14:30 UTC—9.5 hours away from either day boundary—these tests would still pass even if the day bounds were shifted by several hours. Off-by-a-few-hours bugs only show up near midnight, so adding a test case around 23:30 or 00:30 UTC would verify that the boundaries themselves are accurate.
I ran the new date tests against staging's query_helper.py and confirmed they all fail there except null/nnull, so the suite is definitely exercising the new behavior.
Why
This PR addresses the following problem / context:
How
Implementation summary: the following was changed / added / removed:
services/query_helper.py.MM/DD/YYYYandYYYY-MM-DDdate formats.Author Checklist
Changes are limited to the intended scope.Tests were added or updated where appropriate.No secrets, credentials, or sensitive data were publicly exposed.Documentation was updated where necessary. (N/A)Reviewer Checklist
Notes
Any special considerations, known limitations, risks, workarounds, or follow-up work to note?