Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce DB updates by traits update requests without real changes in the data #4451

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 56 additions & 23 deletions api/environments/identities/traits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,47 @@ def increment_value(self, request):

return Response(serializer.data, status=200)

@swagger_auto_schema(request_body=SDKCreateUpdateTraitSerializer(many=True))
@action(detail=False, methods=["PUT"], url_path="bulk")
def bulk_create(self, request):
try:
if not request.environment.trait_persistence_allowed(request):
raise BadRequest("Unable to set traits with client key.")
def _update_traits(self, request):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _update_traits(self, request):
def _update_traits(self, request: Request) -> None:

Copy link
Author

Choose a reason for hiding this comment

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

✔️


identities = {trait["identity"]["identifier"] for trait in request.data}
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved

existing_traits = Trait.objects.filter(
identity__identifier__in=identities,
identity__environment=request.environment,
)

# Map to easily access existing traits
existing_traits_map = {
(trait.identity.identifier, trait.trait_key): trait
for trait in existing_traits
}

updated_traits = []
delete_filter_query = Q()

for trait in request.data:
trait_key = trait.get("trait_key")
identifier = trait["identity"]["identifier"]

# endpoint allows users to delete existing traits by sending null values
# for the trait value so we need to filter those out here
traits = []
delete_filter_query = Q()

for trait in request.data:
if trait.get("trait_value") is None:
delete_filter_query = delete_filter_query | Q(
trait_key=trait.get("trait_key"),
identity__identifier=trait["identity"]["identifier"],
identity__environment=request.environment,
)
else:
traits.append(trait)
if trait.get("trait_value") is None:
delete_filter_query = delete_filter_query | Q(
trait_key=trait_key,
identity__identifier=identifier,
identity__environment=request.environment,
)
continue

if delete_filter_query:
Trait.objects.filter(delete_filter_query).delete()
existing_trait = existing_traits_map.get((identifier, trait_key))
if not existing_trait or existing_trait.trait_value != trait["trait_value"]:
updated_traits.append(trait)

serializer = self.get_serializer(data=traits, many=True)
if delete_filter_query:
Trait.objects.filter(delete_filter_query).delete()

if len(updated_traits) > 0:
serializer = self.get_serializer(data=updated_traits, many=True)
serializer.is_valid(raise_exception=True)
serializer.save()

Expand All @@ -273,11 +288,29 @@ def bulk_create(self, request):
request.method,
dict(request.headers),
request.environment.project.id,
request.data,
updated_traits,
)
)

return Response(serializer.data, status=200)
@swagger_auto_schema(request_body=SDKCreateUpdateTraitSerializer(many=True))
@action(detail=False, methods=["PUT"], url_path="bulk")
def bulk_create(self, request):
try:
if not request.environment.trait_persistence_allowed(request):
raise BadRequest("Unable to set traits with client key.")

self._update_traits(request)

identities = {trait["identity"]["identifier"] for trait in request.data}

all_traits = Trait.objects.filter(
identity__identifier__in=identities,
identity__environment=request.environment,
)

return Response(
SDKCreateUpdateTraitSerializer(instance=all_traits, many=True).data,
)

except (TypeError, AttributeError) as excinfo:
logger.error("Invalid request data: %s" % str(excinfo))
Expand Down
Loading