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 1 commit
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
62 changes: 44 additions & 18 deletions api/environments/identities/traits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,39 +245,65 @@ def bulk_create(self, request):
if not request.environment.trait_persistence_allowed(request):
raise BadRequest("Unable to set traits with client key.")

# endpoint allows users to delete existing traits by sending null values
# for the trait value so we need to filter those out here
identities = {trait["identity"]["identifier"] for trait in request.data}

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
}

traits = []
delete_filter_query = Q()

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

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"],
trait_key=trait_key,
identity__identifier=identifier,
identity__environment=request.environment,
)
else:
continue

existing_trait = existing_traits_map.get((identifier, trait_key))
if (
not existing_trait
or existing_trait.trait_value != trait["trait_value"]
):
traits.append(trait)

if delete_filter_query:
Trait.objects.filter(delete_filter_query).delete()

serializer = self.get_serializer(data=traits, many=True)
serializer.is_valid(raise_exception=True)
serializer.save()

if settings.EDGE_API_URL and request.environment.project.enable_dynamo_db:
forward_trait_requests.delay(
args=(
request.method,
dict(request.headers),
request.environment.project.id,
request.data,
if len(traits) > 0:
serializer = self.get_serializer(data=traits, many=True)
serializer.is_valid(raise_exception=True)
serializer.save()

if (
settings.EDGE_API_URL
and request.environment.project.enable_dynamo_db
):
forward_trait_requests.delay(
args=(
request.method,
dict(request.headers),
request.environment.project.id,
traits,
)
)
)

return Response(serializer.data, status=200)
return Response(serializer.data if traits else [], status=200)

return Response(traits, status=200)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... this actually looks like it changes the behaviour of the endpoint which could have an effect on clients. Previously, we are always returning the traits that were sent (assuming they were valid). Now we're sending an empty list if none of the traits are actually updated / created.

Perhaps we could do something like the following:

all_traits = [*traits, *SDKCreateUpdateTraitSerializer(instance=existing_traits, many=True).data]
return Response(all_traits)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for review.
I made changes, now traits always added to the response.
Moved all logic of updating traits to new method, I think it will be a bit easy to read an understand in this case.


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