Skip to content

Log search records with IDs, position, FRBR groups - #481

Open
JPrevost wants to merge 4 commits into
mainfrom
timx730
Open

JPrevost wants to merge 4 commits into
mainfrom
timx730

Conversation

@JPrevost

@JPrevost JPrevost commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

https://mitlibraries.atlassian.net/browse/TIMX-730

Example Rails log payload

For a search on the All tab, Rails logs one event for the response. Here’s an excerpt showing two returned records:

{
  "event_type": "search_results_response",
  "event_version": 1,
  "searchterm": "climate change",
  "search_tab": "all",
  "response_kind": "initial",
  "search_outcome": "success",
  "merge_algorithm": "legacy_zipper",
  "returned_results_count": 4,
  "returned_results": [
    {"api": "timdex", "record_id": "dspace:123", "position": 1},
    {
      "api": "primo",
      "record_id": "alma991234",
      "position": 2,
      "frbr_group_id": "9005185755117984338"
    },
    {"api": "timdex", "record_id": "dspace:456", "position": 3},
    {"api": "primo", "record_id": "alma995678", "position": 4}
  ]
}

position is the record’s position in the results sent to the UI. The event captures what the server returned. Visibility and clicks are separate measurements that are not in scope at this time.

Querying the exported data

The separate exporter (coming soon to a PR near you) writes one Parquet row per response. DuckDB can expand the nested list into one row per returned record. Numbering responses before expanding the list keeps repeated searches for the same term distinguishable.

WITH matching_responses AS (
  SELECT row_number() OVER () AS response_number,
         searchterm,
         returned_results
  FROM read_parquet('responses.parquet')
  WHERE searchterm = 'climate change'
)
SELECT response_number, searchterm, r.position, r.api, r.record_id,
       r.frbr_group_id
FROM matching_responses
CROSS JOIN UNNEST(returned_results) AS result(r)
ORDER BY response_number, r.position;

A zero-match search still has a response row with an empty returned_results list. Analysts can find those by querying the response rows for successful searches with both backend hit totals equal to zero.

NOTE: I have used the PR build for this to log data to CloudWatch and parse it locally into parquet and then queried it via DuckDB.

Developer

Accessibility
  • ANDI or WAVE has been run in accordance to our guide.
  • This PR contains no changes to the view layer.
  • New issues flagged by ANDI or WAVE have been resolved.
  • New issues flagged by ANDI or WAVE have been ticketed (link in the Pull Request details above).
  • No new accessibility issues have been flagged.
New ENV
  • All new ENV is documented in README.
  • All new ENV has been added to Heroku Pipeline, Staging and Prod.
  • ENV has not changed.
Approval beyond code review
  • UXWS/stakeholder approval has been confirmed.
  • UXWS/stakeholder review will be completed retroactively.
  • UXWS/stakeholder review is not needed.
Additional context needed to review

E.g., if the PR includes updated dependencies and/or data
migration, or how to confirm the feature is working.

Code Reviewer

Code
  • I have confirmed that the code works as intended.
  • Any CodeClimate issues have been fixed or confirmed as
    added technical debt.
Documentation
  • The commit message is clear and follows our guidelines
    (not just this pull request message).
  • The documentation has been updated or is unnecessary.
  • New dependencies are appropriate or there were no changes.
Testing
  • There are appropriate tests covering any new functionality.
  • No additional test coverage is required.

@qltysh

qltysh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

❌ 7 blocking issues (7 total)

Tool Category Rule Count
rubocop Lint Class has too many lines. [542/100] 3 ❌
rubocop Lint Assignment Branch Condition size for load\_all\_results is too high. [<26, 29, 3> 39.06/17] 2 ❌
rubocop Lint Method has too many lines. [27/10] 2 ❌

Comment thread app/controllers/search_controller.rb Resolved
@coveralls

coveralls commented Oct 30, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 47417029114

Coverage increased (+0.003%) to 98.427%

Details

  • Coverage increased (+0.003%) from the base build.
  • Patch coverage: 39 of 39 lines across 3 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1907
Covered Lines: 1877
Line Coverage: 98.43%
Coverage Strength: 1855.93 hits per line

💛 - Coveralls

@mitlib
mitlib temporarily deployed to timdex-ui-pi-timx730-c53vt5txf October 30, 2026 14:47 Inactive
@JPrevost
JPrevost temporarily deployed to timdex-ui-pi-timx730-c53vt5txf October 30, 2026 14:54 Inactive
@JPrevost JPrevost closed this Dec 4, 2026
@JPrevost JPrevost reopened this Dec 4, 2026
@mitlib
mitlib temporarily deployed to timdex-ui-pi-timx730-owcc5u0qj December 4, 2026 16:00 Inactive
@JPrevost
JPrevost temporarily deployed to timdex-ui-pi-timx730-owcc5u0qj December 4, 2026 19:58 Inactive
@JPrevost
JPrevost requested a balanced review from Copilot December 4, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The logging guard incorrectly records zero results when a Primo continuation notice accompanies nonempty results; it should suppress records only for errors, with tests updated accordingly.

1 open issue
What changed in this PR

Adds structured response logging to TIMDEX UI, tracking returned record IDs, positions, and Alma FRBR groups for downstream analytics.

Changes:

  • Logs response outcome, response kind, and effective merge algorithm.
  • Preserves FRBR groups on individual records and updates cache namespaces.
  • Adds logging coverage and documents the event payload.
File Description
test/​models/​normalize_primo_results_test.rb Tests record-level FRBR groups and exclusions.
test/​models/​merged_search_service_test.rb Verifies scorer names and cached FRBR preservation.
test/​controllers/​search_results_logging_test.rb Adds response logging tests.
test/​controllers/​search_controller_test.rb Updates logging assertions and mocks.
README.md Documents payload fields and analytics boundaries.
app/​models/​normalize_primo_results.rb Removes batch-level FRBR collection.
app/​models/​normalize_primo_record.rb Attaches validated FRBR groups to records.
app/​models/​merged_search_service.rb Exposes effective scorer and updates cached state.
app/​controllers/​search_controller.rb Builds versioned response events with ordered records.

Review effort: Balanced

Comment thread app/controllers/search_controller.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The logging logic can mislabel failed requests as successful and log records never returned by overlooking exception-aware instrumentation and the negotiated response media type.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Use the response format when logging returned records

app/​controllers/​search_controller.rb:293

request.format can differ from the format selected by respond_to: with Accept: application/xml, text/html, the response is HTML but the logger treats it as XML. An All-tab response with results from one backend and errors from the other then logs returned IDs even though the HTML view hides them. Use the response's media type, falling back to the request format only when no response type is available.

Medium severity Log failed requests as errors and suppress returned records

app/​controllers/​search_controller.rb:299

An unhandled TIMDEX or rendering exception can reach this logging hook while response.status is still 200; Rails supplies the failure status and exception in the process_action payload instead. This currently labels that failed request success and can log loaded records that were never returned. Pass the instrumentation status/exception into these helpers, mark the outcome as error, and suppress returned records for failed requests.

1 resolved since last review

Review effort: Balanced

@jazairi jazairi self-assigned this Dec 4, 2026

@jazairi jazairi left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think copilot is right about inaccurately labelling failed requests as having results. I replicated it locally by requesting results in an unsupported format (XML), which returns a 406 with a populated returned_results array. I suspect there's a timing issue around when the exception is set in the controller and when the payload is set in the merged search service.

This branch was successfully deployed

1 active and 1 inactive (outdated) deployments
timdex-ui-pi-timx730-owcc5u0qj — d4670781 Deployed Dec 4, 2026 by JPrevost
timdex-ui-pi-timx730-c53vt5txf — ce003777 Deployed Oct 30, 2026 by JPrevost
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.

5 participants