Skip to content

Conversation

@momo3404
Copy link
Collaborator

Changes proposed in this PR:

  • Add the complete flag to the V2 API to enable accessing all questions and answers belonging to a plan in the API response.
  • Per the V2 API Documentation: "The idea is to have a flag which defaults to false and thereby is compliant with the RDA metadata standard, but can be passed in with a value of true to include the questions and answers of the plan."
  • The flag is optional and works with the api/v2/plans and api/v2/plans/[plan_id] endpoints. For example: api/v2/plans/12345?complete=true.
  • The schema the flag returns is as follows:
  •       "complete_plan": [
              {
                "title": "Question 1",
                "question": "<p>What considerations will you take into account with respect to ethical, legal, or commercial issues?</p>\r\n<p><br>Describe any applicable ethical, legal, or commercial considerations related to your project and data. This includes research involving Indigenous communities and knowledges, human subjects, legal and commercial considerations/agreements, partnerships or data with a high level of risk associated with it.</p>",
                "answer": "<p>test</p>"
              },
              {
                "title": "Question 2",
                "question": "<p>What data will you collect or otherwise bring into your project under this plan?</p>\r\n<p><br>Describe the data that will be collected, generated, and/or acquired.</p>",
                "answer": "<p>test</p>"
              },
            ]
    
  • Questions with no answers are not included in the response.

- This optional parameter will return all questions and answers in a plan when set to true
- Add the fetch_all_q_and_a function to return all questions and answers belonging to a plan to allow flag to work
- Add complete_plan_data to call fetch function inside initialize
- Add call for complete flag
- Add questions and answers of a plan to the json if complete is set to true
@github-actions
Copy link

github-actions bot commented Jan 29, 2026

</tr>
1 Error
🚫

Please include a CHANGELOG entry.

You can find it at [CHANGELOG.md](https://github.com/DMPRoadmap/roadmap/blob/main/CHANGELOG.md).
1 Warning
⚠️ There are code changes, but no corresponding tests. Please include tests if this PR introduces any modifications in behavior. \n
Ignore this warning if the PR ONLY contains translation.io synced updates.

Generated by 🚫 Danger

aaronskiba and others added 9 commits January 29, 2026 15:53
Replaced raw SQL with ActiveRecord query methods + enum scopes
- Improves readability and eliminates SQL injection risk
Move theme filtering from Ruby to SQL using joins for better performance and scalability.
Previously, `fetch_q_and_a` iterated over all plan questions and searched for matching answers in Ruby, performing theme filtering and Q&A formatting in memory. This caused unnecessary loops, potential N+1 lookups, and inefficient handling of multi-theme questions. Accessing questions directly via `Plan.questions` also triggered joins through sections, phases, and  templates, along with ordering (due to default scope), which added query overhead.

The refactored version:

- Queries answers directly via a join to question themes, filtering at the DB level.
- Accesses questions through the answers (`answer.question`) instead of `plan.questions`, avoiding heavy joins and unnecessary default-scoped sorting.
Previously, the /api/v2/plans endpoint triggered many N+1 queries when rendering plan associations, causing slow response times. This change adds .includes() for all associations that were previously triggering Bullet N+1 warnings.

Benchmark comparison (local dev, 10 sequential requests):

| Metric            | Before  | After  |
| ----------------- | ------- | ------ |
| Mean request time | 1909 ms | 492 ms |
| Requests/sec      | 0.52    | 2.03   |
| Max request time  | 2628 ms | 641 ms |

- This simple comparison shows that these changes make the  `GET api/v2/plans` ~4x faster.
- This optional parameter will return all questions and answers in a plan when set to true
- Add the fetch_all_q_and_a function to return all questions and answers belonging to a plan to allow flag to work
- Add complete_plan_data to call fetch function inside initialize
- Add call for complete flag
- Add questions and answers of a plan to the json if complete is set to true
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.

3 participants