Skip to content

Commit

Permalink
avoid n+1 queries on bulk GET /system endpoint (#4120)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsachs authored Sep 19, 2023
1 parent acf9fc0 commit b9efa45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The types of changes are:

### Fixed
- Allows CDN to cache empty experience responses from fides.js API [#4113](https://github.com/ethyca/fides/pull/4113)
- Avoid un-optimized query pattern in bulk `GET /system` endpoint [#4120](https://github.com/ethyca/fides/pull/4120)

## [2.20.0](https://github.com/ethyca/fides/compare/2.19.1...2.20.0)

Expand Down
4 changes: 2 additions & 2 deletions src/fides/api/api/v1/endpoints/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from fides.api.schemas.connection_configuration.saas_config_template_values import (
SaasConnectionTemplateValues,
)
from fides.api.schemas.system import SystemResponse
from fides.api.schemas.system import BasicSystemResponse, SystemResponse
from fides.api.util.api_router import APIRouter
from fides.api.util.connection_util import (
connection_status,
Expand Down Expand Up @@ -361,7 +361,7 @@ async def create(
scopes=[SYSTEM_READ],
)
],
response_model=List[SystemResponse],
response_model=List[BasicSystemResponse],
name="List",
)
async def ls( # pylint: disable=invalid-name
Expand Down
24 changes: 19 additions & 5 deletions src/fides/api/schemas/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ class PrivacyDeclarationResponse(PrivacyDeclaration):
cookies: Optional[List[Cookies]] = []


class SystemResponse(System):
"""Extension of base pydantic model to include additional fields like `privacy_declarations`, connection_config fields
and cookies in responses"""
class BasicSystemResponse(System):
"""
Extension of base pydantic model to include additional fields on the DB model that
are relevant in API responses.
This is still meant to be a "lightweight" model that does not reference relationships
that may require additional querying beyond the `System` db table.
"""

created_at: datetime


class SystemResponse(BasicSystemResponse):
"""Extension of base pydantic response model to include additional relationship fields that
may require extra DB queries, like `privacy_declarations`, connection_config fields and cookies.
This response model is generally useful for an API that returns a detailed view of _single_
System record. Attempting to return bulk results with this model can lead to n+1 query issues.
"""

privacy_declarations: List[PrivacyDeclarationResponse] = Field(
description=PrivacyDeclarationResponse.__doc__,
Expand All @@ -38,8 +54,6 @@ class SystemResponse(System):

cookies: Optional[List[Cookies]] = []

created_at: datetime


class SystemHistoryResponse(BaseModel):
"""Response schema for a single system history entry"""
Expand Down

0 comments on commit b9efa45

Please sign in to comment.