Skip to content

All Presidents API Endpoint#99

Open
sehansi-9 wants to merge 6 commits intoLDFLK:mainfrom
sehansi-9:presidents-api
Open

All Presidents API Endpoint#99
sehansi-9 wants to merge 6 commits intoLDFLK:mainfrom
sehansi-9:presidents-api

Conversation

@sehansi-9
Copy link
Member

This PR introduces a new endpoint for fetching all presidents with their terms and the gazettes published during their respective tenures

  • Added DOCUMENT major kind and gazette-specific minor kinds - extgztorg and extgztperson

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new API endpoint that provides a consolidated view of all presidents, detailing their terms in office and the gazettes published during those periods. This enhancement allows for a more comprehensive retrieval of historical presidential data, improving data accessibility and integration capabilities by structuring the data with new OpenAPI schemas and robust service logic.

Highlights

  • New API Endpoint: Introduced a new /v1/person/all-presidents API endpoint to retrieve comprehensive data for all presidents, including their terms and associated gazettes.
  • OpenAPI Specification Updates: Defined new OpenAPI schemas for AllPresidentsResponse, PresidentInfo, PresidentTerm, and GazettePublished to accurately describe the data structure returned by the new endpoint.
  • Enum Additions for Document Kinds: Added DOCUMENT as a major kind and EXTRA_ORDINARY_GAZETTE_ORGANISATION, EXTRA_ORDINARY_GAZETTE_PERSON as minor kinds to kindEnum.py to support categorization of gazette-related entities.
  • Service Logic Implementation: Implemented the fetch_all_presidents method in person_service.py to aggregate president terms, fetch their names, and associate gazettes published during their tenure, including sorting by the latest term.
  • Comprehensive Unit Testing: Included extensive unit tests for the new fetch_all_presidents service method, covering success scenarios, cases with no data or no gazettes, and correct sorting behavior.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new endpoint to fetch all presidents. The implementation is mostly sound, with comprehensive tests. However, I've identified a potential performance bottleneck in person_service.py due to nested loops, and a minor issue with error handling that could be improved. There's also a small inconsistency in an endpoint description in the router file. Please see my detailed comments.

Comment on lines +348 to +364
# Finding the relevant president for the gazette
for president_id, president_info in presidents_map.items():
for term in president_info["terms"]:
start = term["start"]
end = term["end"]

# Find if the gazette created date falls between the president's term (excluding end date)
if start <= gazette_date and (end is None or gazette_date < end):
# O(1) lookup using the temporary dictionary
date_dict = president_info["_gazettes_dict"]
if gazette_date not in date_dict:
date_dict[gazette_date] = []

if gazette_id not in date_dict[gazette_date]:
date_dict[gazette_date].append(gazette_id)

break
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current approach to associate gazettes with presidents involves nested loops: iterating through all gazettes, then all presidents, then all terms for each president. This results in a time complexity of roughly O(Gazettes * Presidents * Terms), which could lead to performance issues if the number of gazettes or presidents is large.

Additionally, the logic allows a single gazette to be associated with multiple presidents if their terms overlap. If this is not the intended behavior, the logic needs adjustment.

Consider refactoring this section for better performance. One possible approach is to process and sort gazettes and presidential terms by date, allowing you to associate them in a single pass, which would be much more efficient.

return service_response No newline at end of file
return service_response

@router.get('/all-presidents', summary="Get all presidents.", description="Returns a list of all presidents.")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The description for this endpoint states that it "Returns a list of all presidents." However, the implementation and the OpenAPI contract specify that it returns a dictionary (or map) where keys are president IDs. To avoid confusion and ensure documentation accuracy, please update the description to reflect the actual return type.

Suggested change
@router.get('/all-presidents', summary="Get all presidents.", description="Returns a list of all presidents.")
@router.get('/all-presidents', summary="Get all presidents.", description="Returns a dictionary of all presidents.")

Comment on lines +343 to +346
try:
gazette_id = Util.decode_protobuf_attribute_name(gazette.name)
except Exception:
gazette_id = str(gazette.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This try...except block catches all exceptions silently and falls back to using the raw gazette name. This can hide underlying issues in Util.decode_protobuf_attribute_name or other unexpected errors. It's better to catch a more specific exception or at least log the exception that occurred to aid in debugging.

Suggested change
try:
gazette_id = Util.decode_protobuf_attribute_name(gazette.name)
except Exception:
gazette_id = str(gazette.name)
try:
gazette_id = Util.decode_protobuf_attribute_name(gazette.name)
except Exception as e:
logger.warning(f"Could not decode gazette name, falling back to raw string: {e}")
gazette_id = str(gazette.name)

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