-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add random identifiers, service layer, refactor `IdentifyWithTraitsSe…
…rializer`
- Loading branch information
Showing
6 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import uuid | ||
from itertools import chain | ||
from typing import TypeAlias | ||
|
||
from django.utils import timezone | ||
|
||
from environments.identities.models import Identity | ||
from environments.identities.traits.models import Trait | ||
from environments.models import Environment | ||
from environments.sdk.types import SDKTraitData | ||
|
||
IdentityAndTraits: TypeAlias = tuple[Identity, list[Trait]] | ||
|
||
|
||
def _get_transient_identity( | ||
environment: Environment, | ||
identifier: str, | ||
) -> Identity: | ||
return Identity( | ||
created_date=timezone.now(), | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
|
||
|
||
def get_transient_identity_and_traits( | ||
environment: Environment, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
return ( | ||
( | ||
identity := _get_transient_identity( | ||
environment=environment, | ||
identifier=str(uuid.uuid4()), | ||
) | ||
), | ||
identity.generate_traits(sdk_trait_data, persist=False), | ||
) | ||
|
||
|
||
def get_identified_transient_identity_and_traits( | ||
environment: Environment, | ||
identifier: str, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
if identity := Identity.objects.filter( | ||
environment=environment, | ||
identifier=identifier, | ||
).first(): | ||
for sdk_trait_data_item in sdk_trait_data: | ||
sdk_trait_data_item["transient"] = True | ||
return identity, identity.update_traits(sdk_trait_data) | ||
return ( | ||
identity := _get_transient_identity( | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
), identity.generate_traits(sdk_trait_data, persist=False) | ||
|
||
|
||
def get_persisted_identity_and_traits( | ||
environment: Environment, | ||
identifier: str, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
identity, created = Identity.objects.get_or_create( | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
persist_trait_data = environment.project.organisation.persist_trait_data | ||
if created: | ||
return identity, identity.generate_traits( | ||
sdk_trait_data, | ||
persist=persist_trait_data, | ||
) | ||
if persist_trait_data: | ||
return identity, identity.update_traits(sdk_trait_data) | ||
return identity, list( | ||
{ | ||
trait.trait_key: trait | ||
for trait in chain( | ||
identity.identity_traits.all(), | ||
identity.generate_traits(sdk_trait_data, persist=False), | ||
) | ||
}.values() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import typing | ||
|
||
from typing_extensions import NotRequired | ||
|
||
|
||
class SDKTraitData(typing.TypedDict): | ||
trait_key: str | ||
trait_value: typing.Any | ||
transient: NotRequired[bool] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters