Skip to content

Fix start_date and end_date never being set in calculated totals - #1092

Open
arpitjain099 wants to merge 1 commit into
18F:developfrom
arpitjain099:fix/totals-start-end-date
Open

Fix start_date and end_date never being set in calculated totals#1092
arpitjain099 wants to merge 1 commit into
18F:developfrom
arpitjain099:fix/totals-start-end-date

Conversation

@arpitjain099

Copy link
Copy Markdown

There are two bugs in the start/end date block in src/process_results/result_totals_calculator.js, and they hide each other, so they have to be fixed together.

The guard is if (result.data[0].data). Result rows don't have a data key, they have date plus the metric keys, so the guard is always false and start_date / end_date are never set. Right inside that branch, result.date[0].date should be result.data[0].date. result.date is undefined, so if the guard ever passed, this would throw.

That's why fixing only the guard is worse than leaving it alone: it turns a silent no-op into a TypeError. I checked this rather than assuming it. Correcting the guard on its own takes the suite from 1 pre-existing failure to 19, all TypeError: Cannot read properties of undefined (reading '0').

On intent, I didn't want to guess, so I traced it. Both typos came in together in 55a6f79, when this moved out of process-ga-data.js. The code before that move was:

if ((result.data.length > 0) && (result.data[0].date)) {
    if (result.data[0].date == "(other)")
        result.totals.start_date = result.data[1].date;
    ...

So result.data[0].date in both spots is the original behavior, and the result.data.length > 0 part is already covered by the early return at the top of calculateTotals. This restores that and nothing more.

One knock-on: test/actions/query_google_analytics.test.js asserted a totals object with no dates in it. That fixture is 24 hourly rows all on 20170130, so it now gets start_date and end_date of 2017-01-30. I updated the expectation. Happy to hear if any consumer would rather not see these fields appear, since they've effectively been absent for years.

Tests added in test/process_results/result_totals_calculator.test.js: dates set from first and last row, a (other) first row skipped in favor of row 1, and rows with no date dimension still not getting the fields.

Verified on Node 22 (per .nvmrc) with NODE_ENV=test npx mocha:

  • before: 212 passing, 1 failing
  • both bugs reverted, new tests in place: 213 passing, 3 failing (the 2 new date tests plus the pre-existing one)
  • guard fixed, typo left in: 174 passing, 19 failing, all TypeError
  • with this change: 215 passing, 1 failing

The one constant failure is PostgresPublisher, which is ECONNREFUSED against a database I don't have locally. It fails identically on an unmodified checkout. npx eslint . and prettier --check are both clean.

The guard tested result.data[0].data, but result rows carry a date key,
not a data key, so the branch never ran and start_date/end_date were
never emitted. Inside the branch, result.date[0].date is a typo for
result.data[0].date, so the branch would throw a TypeError if it ever
did run.

Both were introduced together in 55a6f79 when this logic moved out of
process-ga-data.js. The pre-move code read result.data[0].date in both
places, so this restores the original behavior.

Also updates the totals expectation in the query_google_analytics test,
which now sees the dates it should always have had.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant